(function(a){var b={Timeout:1500,FadeSpeed:"slow",RevertOnOut:false,Nav:{bgColor:"white",border:{width:1,style:"solid",color:"#999999"},bottom:0,padding:2,height:5,left:0,position:"absolute",width:10},Hover:{bgColor:"gray"}};a.fn.HoverCycle=function(c){var d=a.extend({},b,c);(function(){if(isNaN(d.Timeout)){d.Timeout=b.Timeout}if(isNaN(d.FadeSpeed)){d.FadeSpeed=d.FadeSpeed.toLowerCase();if(d.FadeSpeed!="slow"||d.FadeSpeed!="fast"){d.FadeSpeed="slow"}}else if(d.FadeSpeed<=0){d.FadeSpeed="slow"}})();return this.each(function(){var b=a(this),c=a("img",b),e;var f=1,g=f+1,h=null,i=new Image,j=0,k="",l=c.filter(":first-child"),m=false,n=null;var o={"background-color":d.Nav.bgColor,"border-width":d.Nav.border.width,"border-style":d.Nav.border.style,"border-color":d.Nav.border.color,"float":"left",height:d.Nav.height,marginLeft:d.Nav.padding/2,maringRight:d.Nav.padding/2,width:d.Nav.width};var p={"background-color":d.Hover.bgColor};var q=function(){if(f!=g){_tmp_index_cur=c.filter(":nth-child("+f+")").css("z-index");_tmp_index_next=c.filter(":nth-child("+g+")").css("z-index");if(_tmp_index_next<_tmp_index_cur&&_tmp_index_cur!="auto"){c.filter(":nth-child("+g+")").css({"z-index":j+1})}c.filter(":nth-child("+g+")").fadeTo(0,1,function(){c.filter(":nth-child("+f+")").fadeTo(0,0);a(this).css({"z-index":_tmp_index_next});f=g;if(g!=j){g++}else{g=2}}).css({"z-index":_tmp_index_next})}};var r=function(){if(!b.find("[id=_nav]").length){c.filter(":last-child").after('<div id=_nav style="display: none;">'+k+"</div>");n=b.find("[id=_nav]").children("._nav-blocks");_blocksLen=n.length;_navWidth=(d.Nav.width+parseInt(d.Nav.border.width,10)+d.Nav.padding)*_blocksLen;b.find("[id=_nav]").css({overflow:"hidden",position:"absolute",top:l.position().top+l.height()-d.Nav.bottom-d.Nav.height,left:l.position().left+l.width()-l.width()/2-_navWidth/2,width:_navWidth,height:d.Nav.height+parseInt(d.Nav.border.width,10)*2,"z-index":j+2}).children().css(o).mouseover(function(){m=true;n.css(o);a(this).css(p);g=a(this).attr("index");if(g==1){g=j}q()}).mouseleave(function(){m=false;a(this).css(o);if(f==g){if(f==j){g=2}else{g=f+1}}});b.find("[id=_nav]").fadeIn("fast")}else{b.find("[id=_nav]").fadeIn("fast")}};var s=function(){if(f==1||f==j){n.css(o).filter("[index=1]").css(p)}else{n.css(o).filter("[index="+f+"]").css(p)}};var t=function(){r();clearTimeout(h);if(!m){s();h=setTimeout(function(){var b=c.filter(":nth-child("+g+")");i.src=b.attr("src")+"?rand="+(new Date).getTime();if(f==2){c.filter(":nth-child("+f+")").css({"z-index":2})}if(f==g){if(f==j){g=2}else{g=f+1}}for(_i=1;_i<=j;_i++){c.filter("nth-child("+_i+")").css({"z-index":_i})}if(b.css("z-index")<c.filter(":nth-child("+f+")").css("z-index")){b.css({"z-index":j+1})}a(i).unbind("load").load(function(){_tmp_curImg=f;_tmp_nextImg=g;f=g;if(j!=g){g++}else{g=2}b.fadeTo(d.FadeSpeed,1,function(){c.filter(":nth-child("+_tmp_curImg+")").fadeTo(0,0,function(){if(j==_tmp_nextImg){c.filter(":nth-child("+g+")").css({"z-index":j+1})}if(!h){s()}else{t()}})})})},d.Timeout)}};var u=function(){clearTimeout(h);h=null;b.find("[id=_nav]").fadeOut("fast");if(d.RevertOnOut){g=1;q()}};i.src=l.attr("src")+"?rand="+(new Date).getTime();a(i).unbind("load").load(function(){b.css({position:"relative"}).append(b.children(":first-child").clone());c=a("img",b);j=c.length;c.each(function(){var b=a(this);b.css({"z-index":f});if(f!=1){b.css({position:"absolute",top:l.position().top,left:l.position().left}).fadeTo(0,0);k+='<div class=_nav-blocks index="'+(f-1)+'"></div>'}f++});f=1;b.mouseover(t).mouseleave(u)})})}})(jQuery);

$(".cycle_images").live("hover", function(){	
	tis = $(this);
	if(tis.data("fetch_images") != true){
		tis.data("fetch_images", true);
		var image = tis.find("img:eq(0)");
		var classList = image.attr('class').split(/\s+/);
		$.each( classList, function(index, item){
			if( item.indexOf('productID_') > -1 ) {
				product_id = item.replace("productID_","");
				
				 $.ajax({
					url: 'index.php?route=product/product/images',
					type: 'post',
					data: 'product_id='+product_id + '&width='+image.width() + '&height='+image.height(),
					dataType: 'json',
					success: function(json) {
						if (json['images']) {
							for (var i = 0; i < json['images'].length; i++) { 
								image.after('<img src="'+json['images'][i]+'" class="hide" width="'+image.width()+'" height="'+image.height()+'" alt="'+$(image).attr("alt")+'" />');
							}
						
							image.parent().HoverCycle({  
								Timeout: 800,
								FadeSpeed: 'fast',
								RevertOnOut: true,
								Nav: {
									bgColor: '#DDDDDD',
									border: {
										width: 1,
										style: 'solid',
										color: '#DDDDDD'
									},
									bottom: 0,
									padding: 2,
									height: 5,
									left: 0,
									position: 'absolute',
									width: 8
								},
								Hover: {
									bgColor: '#DDDDDD'
								}
							});
						}
					}
				});
			}
		});
	}
});
