$(document).ready(function() {	
	// PNG fix
	jQuery.ifixpng(url + 'images/blank.gif');
	jQuery('img[@src$=.png], .bgPng').ifixpng();
	
	$('.bgPng').each(function() {
		element = $(this);
		
		element.focus(function() {
			jQuery(element).ifixpng();
		});
	});
	
	// Fading images
	$('#rotation').cycle('fade');

	// Search box
	var element = $('#search input.cooljs');
	var defaultSearchText = element.attr('value');

	element.focus(function() {
		if ($(this).attr('value') == defaultSearchText) {
			$(this).attr('value', '');
		}
	});

	element.blur(function() {
		if ($(this).attr('value').length == 0) {
			$(this).attr('value', defaultSearchText);
		}
	});

	// Nav classes
	var elements = $('#nav li:not(li)');

	elements.each(function() {
		var el = $(this);

		el.bind("mouseenter", (function() {
			el.addClass('active');
		}));

		el.bind("mouseleave", (function() {
			el.removeClass('active');
		}));
	});
	
	// Gallery
	if ($('.gallery')) {
		var elements = $('.gallery .thumbnails a');

		elements.each(function() {
			$(this).bind('click', function(e) {
				$('.gallery .large img').attr('src', $(this).attr('href'));
				$('.gallery .large a').attr('href', $(this).attr('rev'));
				return false;
			});
		});
	};
	
	// Send friend
	if ($('#email-friend') && $('#email-friend-toggle')) {
		$('#email-friend').hide();
		
		$('#email-friend-toggle').click(function() {
			$('#email-friend').stop();
			$('#email-friend').toggle('normal');
		});
	}
	
	if (window.location.hash == '#email-friend-toggle') {
		
		$('#email-friend-toggle').click();
		
	}
	
	//image auto title from alt tag
	$('img').each(function() {
		element = $(this);
		
		element.attr('title', element.attr('alt'));
	});
	
	if ($('.regions')) {
		var toggles = $('.regions h2:has(~ dl)');

		toggles.each(function(i) {
			$(this).css('cursor', 'pointer');
			
			var content = $('dl.suppliers:eq(' + i + ')');
			content.hide(0);
			
			$(this).click(function(e) {
				content.toggle(500);
			});
		});
	}
	
	// product out of stock alert
	if ($('#product-variants').length > 0) {
		$('#listing form.purchase').submit(function() {
			for (var i in outOfStock) {
				if ($('#product-variants').attr('value') == outOfStock[i]) {
					alert('This product is out of stock.');
					return false;
				}
			}
		});
	}
});
