$(function(){

	$('ol.wp-paginate li:first').remove();
	
	$('ul#footer-links a').hover(
			function () {
				$(this).animate({
					"margin-left":"+=10px", 
				}, 200)
			},
			function () {
				$(this).animate({
					"margin-left":"-=10px"
				}, 200)
			}
		);

	
	$("input[type=text][title], input[type=password][title], textarea[title]").bind("focus blur", function(e) {
		if(e.type == "focus"){
			$(this).addClass("selected");
		}
		if(e.type == "focus" &&  $(this).val() == $(this).attr("title")) $(this).val('');

		if(e.type == "blur" && $(this).val() != '') {
				$(this).addClass("ok");
		}

		if(e.type == "blur" && $(this).is("[name=email]")) {
			var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
			if (!$(this).val().match(emailReg)) {
				$(this).removeClass("ok");
				$(this).addClass("err");
			}
		}

		if(e.type == "blur" && $(this).val() == '') {
			$(this).val($(this).attr("title"));
			if($(this).is("[name=contact-name]") || $(this).is("[name=contact-phone]") || $(this).is("[name=contact-message]")){
				$(this).addClass("err");
			}
		}
	});
	
	
	
	$('a.image-link').fancybox({
		'titleShow'		:		false
	});
	
	var $sliderC = $('#slider');
	var $sliderG = $sliderC.find('.gallery ul');
	var $sliderItems = $sliderG.find('li');
	
	var $controlers = $sliderC.find('a.controler');
	
	var $slideD = $sliderItems.width()+parseInt($sliderItems.css('margin-left'))+parseInt($sliderItems.css('margin-right'));
	
	var $offsetRight = ((($sliderItems.length*$slideD)-($slideD*2))*(-1))
	
	$controlers.click(function(){moveGallery($(this).attr('rel'))});
	
	function moveGallery(direction) {
		var marginDir;
		var margin = parseInt($sliderG.css('margin-left'));
		if 	(( (margin <0) && (direction=='left') ) || ((margin-$slideD > $offsetRight) && (direction=='right')))
		{
			(direction=='left') ? marginDir='+' : marginDir='-';
			$sliderG.animate({
				 marginLeft : marginDir+'='+$slideD+'px'
			})	
		}
	}
	
});

$(window).load(function(){
	var selector = "a.image-link, a.fp-image-link";
	$(selector).each(resizeImage);
	

	$('<a class="magnifier">&nbsp;</a>')
		.prependTo('ul.product-list a.image-link')
		.css('margin','64px 0 0 110px')
		.click(function(){$(this).parent().click()});


	$('#slideshow a').hide();
	slidePlay('#slideshow');
	
});
function resizeImage(index, container) {
	
	var containerW = $(container).width();
	var containerH = $(container).height();
	
	var $img = $(this).children('img');
	
	var imgW = $img.width();
	var imgH = $img.height();
	
	var margin
	var ratio;
	
	
	if ((imgW/imgH)>(containerW/containerH)) {
		ratio = containerH/imgH;
		margin = parseInt(((imgW*ratio)-containerW)/2)*(-1);
		$img.css({'height':containerH+'px', 'margin-left':margin+'px'});
	} else {
		ratio = containerW/imgW;
		margin = parseInt(((imgH*ratio)-containerH)/2)*(-1);
		$img.css({'width':containerW+'px', 'margin-top' : margin+'px'});
	}
};
