(function ($) {
	// Forked from https://raw.github.com/peol/jquery.imgloaded/master/ahpi.imgload.js
	// Docs on events http://brandonaaron.net/blog/2009/03/26/special-events
	$.event.special.load = {
		setup: function(data, namespaces) {
			var elem = this, $elem = jQuery(elem);
			$elem.bind('load', jQuery.event.special.load.handler);
		},
		
		teardown: function(namespaces) {
			var elem = this, $elem = jQuery(elem);
			$elem.unbind('load', jQuery.event.special.load.handler);
		},
		
		handler: function(event) {
			var elem = this, $elem = jQuery(elem);
			if ( elem.nodeType === 1 && elem.tagName.toLowerCase() === 'img' && elem.src !== '' ) {
				if ( elem.complete || elem.readyState === 4 ) {
					event.type = "load";
					$.event.handle.apply(this, arguments);
				} else if ( elem.readyState === 'uninitialized' && elem.src.indexOf('data:') === 0 ) {
					$elem.trigger('error');
				} else {
					$elem.bind('load', function (event){
						calcHeight();
					});
				}
			}
		}
	};
}(jQuery));

$(document).ready(function(){
	
	var speed = 1000;
	var text_speed = 800;
	
	var cur = 2;
	var start = 1;
	var end = $(".product").length;
		
	var pos0,pos1,pos2,pos3;
	
	var width = 300;
	var wrap_width = 980;
	var pad = 50;
	
	var width_sml = 220;
	var width_main = 310;
	
	var top_sml = 0;
	var top_main = 28;
	
	var img_width_sml = 162;
	var img_height_sml = 162;
	var img_width_main = 230;
	var img_height_main = 230;
	var img_opacity_sml = 0.5;
	var img_opacity_main = 1;
	
	var ease = "easeOutQuad";
	
	var link_cart = '/CMS/Plugins/ShoppingCart/Cart/control.php?add=';
	var link_hint = '/CMS/Plugins/ShoppingCart/Wish/control.php?add=';
	
	function calcHeight(){
		$("#p_wrapper").animate({'height':$(".product[id='"+(cur)+"']").outerHeight(false)+top_main},text_speed,ease);
	}
	
	prodid = $(".product[id='"+(cur)+"']").attr('rel');
	$("#cartlink").attr('href',link_cart+prodid);
	$("#hintlink").attr('href',link_hint+prodid);
	
	if(end > 3){
		
		$(".product[id='1']").css({display: 'block', width: width_sml, top: top_sml});
		if($(".img[id='1']").attr('type') == 'h'){
			$(".img[id='1']").css({opacity: img_opacity_sml, height: img_height_sml});
		} else {
			$(".img[id='1']").css({opacity: img_opacity_sml, width: img_width_sml});
		}
		$(".text[id='1']").css({opacity: 0 });
		
		$(".product[id='2']").css({display: 'block', left: width, width: width_main, top: top_main});
		if($(".img[id='2']").attr('type') == 'h'){
			$(".img[id='2']").css({opacity: img_opacity_main, height: img_height_main});
		} else {
			$(".img[id='2']").css({opacity: img_opacity_main, width: img_width_main});
		}
		
		$(".product[id='3']").css({display: 'block', left: (width*2.33), width: width_sml, top: top_sml});
		if($(".img[id='3']").attr('type') == 'h'){
			$(".img[id='3']").css({opacity: img_opacity_sml, height: img_height_sml});
		} else {
			$(".img[id='3']").css({opacity: img_opacity_sml, width: img_width_sml});
		}	
		$(".text[id='3']").css({opacity: 0 });
		
	} else {
	
		$("#next").fadeOut();
		$("#prev").fadeOut();
		
		$(".product[id='1']").css({display: 'block', width: width_main, top: top_main});
		if($(".img[id='1']").attr('type') == 'h'){
			$(".img[id='1']").css({opacity: img_opacity_main, height: img_height_main});
		} else {
			$(".img[id='1']").css({opacity: img_opacity_main, width: img_width_main});
		}
		
		$(".product[id='2']").css({display: 'block', left: width, width: width_main, top: top_main});
		if($(".img[id='2']").attr('type') == 'h'){
			$(".img[id='2']").css({opacity: img_opacity_main, height: img_height_main});
		} else {
			$(".img[id='2']").css({opacity: img_opacity_main, width: img_width_main});
		}
				
		$(".product[id='3']").css({display: 'block', width: width_main, top: top_main});
		if($(".img[id='3']").attr('type') == 'h'){
			$(".img[id='3']").css({opacity: img_opacity_main, height: img_height_main});
		} else {
			$(".img[id='3']").css({opacity: img_opacity_main, width: img_width_main});
		}
	}
	
	$("#next").bind('click',next);
	$("#prev").bind('click',prev);
		
	$(".img[id='2']").bind('load', function (e) {
		calcHeight();
	});
        calcHeight();

	function next (){
		
		$("#next").unbind('click');
		
		pos0 = cur-1;
		pos1 = cur;
		pos2 = cur+1;
		pos3 = cur+2;
		
		if(pos0 < start){ pos0 = end; }
		if(pos3 > end){ pos3 = start; }
		if(pos2 > end){ pos2 = start; pos3 = start+1; }
		
		prodid = $(".product[id='"+(pos2)+"']").attr('rel');
                enquirelink(prodid);
		$("#cartlink").attr('href',link_cart+prodid);
		$("#hintlink").attr('href',link_hint+prodid);
		
		$(".text[id='"+(pos1)+"']").animate({
			opacity: 0
		},text_speed);
			
		$(".product[id='"+(pos0)+"']").animate({
			left: -width,
			opacity: 0
		},speed,ease,function (){ $(this).hide(); });
		
		$(".product[id='"+(pos1)+"']").animate({
			left: 0,
			top: top_sml,
			width: width_sml
		},speed,ease);
		
		if($(".img[id='"+(pos1)+"']").attr('type') == 'h'){
			$(".img[id='"+(pos1)+"']").width('auto');
			$(".img[id='"+(pos1)+"']").animate({
				opacity: img_opacity_sml,
				height: img_height_sml
			},speed,ease);
		} else {
			$(".img[id='"+(pos1)+"']").height('auto');
			$(".img[id='"+(pos1)+"']").animate({
				opacity: img_opacity_sml,
				width: img_width_sml
			},speed,ease);
		}
			
		$(".product[id='"+(pos2)+"']").animate({
			left: width,
			top: top_main,
			width: width_main
		},speed,ease);
		
		$(".text[id='"+(pos2)+"']").animate({
			opacity: 1
		},text_speed,ease);
		
		if($(".img[id='"+(pos2)+"']").attr('type') == 'h'){
			$(".img[id='"+(pos2)+"']").width('auto');
			$(".img[id='"+(pos2)+"']").animate({
				opacity: img_opacity_main,
				height: img_height_main
			},speed,ease);
		} else {
			$(".img[id='"+(pos2)+"']").height('auto');
			$(".img[id='"+(pos2)+"']").animate({
				opacity: img_opacity_main,
				width: img_width_main
			},speed,ease);
		}
		
		if($(".img[id='"+(pos3)+"']").attr('type') == 'h'){
			$(".img[id='"+(pos3)+"']").width('auto');
			$(".img[id='"+(pos3)+"']").css({opacity: img_opacity_sml, height: img_height_sml});
		} else {
			$(".img[id='"+(pos3)+"']").height('auto');
			$(".img[id='"+(pos3)+"']").css({opacity: img_opacity_sml, width: img_width_sml});
		}
		$(".text[id='"+(pos3)+"']").css({opacity: 0 });
		$(".product[id='"+(pos3)+"']").css({ display: 'block', left: wrap_width, width: width_sml, top: top_sml });
		$(".product[id='"+(pos3)+"']").animate({
			left: (width*2.33),
			opacity: 1
		},speed,ease, function(){
			$("#next").bind('click',next);
			cur = pos2;
			calcHeight();
		});
                
		
	}
		
		
	function prev(){
		
		$("#prev").unbind('click');
		
		pos = cur-2;
		pos0 = cur-1;
		pos1 = cur;
		pos2 = cur+1;
		pos3 = cur+2;
		
		if(pos < start){ pos = end; }
		if(pos0 < start){ pos0 = end; pos = end-1; }
		if(pos3 > end){ pos3 = start; }
		if(pos2 > end){ pos2 = start; pos3 = start+1; }
		
		prodid = $(".product[id='"+(pos0)+"']").attr('rel');
		enquirelink(prodid);
		$("#cartlink").attr('href',link_cart+prodid);
		$("#hintlink").attr('href',link_hint+prodid);
		
		$(".text[id='"+(pos1)+"']").animate({
			opacity: 0
		},text_speed);
		
		$(".product[id='"+(pos)+"']").css({ opacity: 0, display: 'block', left: -width, width: width_sml, top: top_sml });
		if($(".img[id='"+(pos)+"']").attr('type') == 'h'){
			$(".img[id='"+(pos)+"']").width('auto').css({opacity: img_opacity_sml, height: img_height_sml});
		} else {
			$(".img[id='"+(pos)+"']").height('auto').css({opacity: img_opacity_sml, width: img_width_sml});
		}
		$(".text[id='"+(pos)+"']").css({opacity: 0 });
		$(".product[id='"+(pos)+"']").animate({
			left: 0,
			opacity: 1
		},speed,ease);
		
		$(".product[id='"+(pos2)+"']").animate({
			left: wrap_width,
			opacity: 0
		},speed,ease,function (){ $(this).hide(); });
		
		$(".product[id='"+(pos0)+"']").animate({
			left: width,
			top: top_main,
			width: width_main
		},speed,ease, function (){ 
			calcHeight();
		});
		if($(".img[id='"+(pos0)+"']").attr('type') == 'h'){
			$(".img[id='"+(pos0)+"']").width('auto').animate({
				opacity: img_opacity_main,
				height: img_height_main
			},speed,ease);
		} else {
			$(".img[id='"+(pos0)+"']").height('auto').animate({
				opacity: img_opacity_main,
				width: img_width_main
			},speed,ease);
		}
		$(".text[id='"+(pos0)+"']").animate({
			opacity: 1
		},text_speed,ease);
		
		$(".product[id='"+(pos1)+"']").animate({
			left: width*2.33,
			top: top_sml,
			width: width_sml
		},speed,ease);
		if($(".img[id='"+(pos1)+"']").attr('type') == 'h'){
			$(".img[id='"+(pos1)+"']").width('auto').animate({
				opacity: img_opacity_sml,
				height: img_height_sml
			},speed,ease);
		} else {
			$(".img[id='"+(pos1)+"']").height('auto').animate({
				opacity: img_opacity_sml,
				width: img_width_sml
			},speed,ease);
		}
		$(".text[id='"+(pos1)+"']").animate({
			opacity: 0
		},text_speed,ease);

		$("#prev").bind('click',prev);
		cur = pos0;
                
                
	}
        
	function enquirelink(prodid){
		$('#enquire').attr('href','mailto:info@dracakis.com.au?subject=Enquiry%20from%20Dracackis%20Online%20Boutique&body='+$('div#'+prodid+' .p_pc').html()+"\n");
	}
	
	enquirelink('2');
});
