//system

var Block=0;

var leftIndex=0;

var centerIndex=2;

var rightIndex=4;

var allIndex=0;

var lindex;

var rindex;

var llindex;

var rrindex;



//settings



//var leftLeft = '5%';

var leftTop = '50px';



//var rightLeft = '5%';

var rightTop = '50px';



var leftHeight = '200';

var centerHeight = '430';

var rightHeight = '200';



var leftWidth = '180';

var centerWidth = '500';

if (screen.width<=1024) centerWidth='450';

var rightWidth = '180';



var leftFadeHeight = '50';

var rightFadeHeight = '50';



var centerLeft;

var centerTop;



var windowWidth;

var rightLeft;

var leftLeft;



function appendContent(central) {

	

			//alert(central);

			

			lindex = central-1;

			if (lindex<0) lindex = allIndex-1;

			

			llindex = lindex-1;

			if (llindex<0) llindex = allIndex-1;

			

			rindex = parseInt(central)+1;

			if (rindex>=allIndex) rindex = 0;

			

			rrindex = parseInt(rindex)+1;

			if (rrindex>=allIndex) rrindex = 0;

			

			jQuery("#main_photo").remove();

			jQuery("#foto-center .align_center_to_left .align_center_to_right").empty();

			jQuery("#cache img:eq("+central+")").clone().appendTo("#foto-center .align_center_to_left .align_center_to_right").attr('id',"main_photo");



			jQuery("#foto-left").remove();

			jQuery("#cache img:eq("+lindex+")").clone().prependTo("#gallery").attr('id',"foto-left");

			

			jQuery("#foto-right").remove();

			jQuery("#cache img:eq("+rindex+")").clone().prependTo("#gallery").attr('id',"foto-right");

			

			jQuery("#foto-left-ajax").remove();

			jQuery("#cache img:eq("+llindex+")").clone().appendTo("#gallery").attr('id',"foto-left-ajax");

			

			jQuery("#foto-right-ajax").remove();

			jQuery("#cache img:eq("+rrindex+")").clone().appendTo("#gallery").attr('id',"foto-right-ajax");

	

			jQuery("#foto-text-center .title").empty();

			jQuery("#foto-text-center .title").append(jQuery("#cache .title:eq("+central+")").text());

				

			jQuery("#foto-text-center p").empty();

			jQuery("#foto-text-center p").append(jQuery("#cache .description:eq("+central+")").text());



			jQuery("#foto-text-bottom .price").empty();

			jQuery("#foto-text-bottom .price").append(jQuery("#cache .price:eq("+central+")").text());



			jQuery("#foto-text-bottom a:last").attr('href',jQuery("#cache .link:eq("+central+")").text());

	

			centerLeft = jQuery("#main_photo").offset().left + parseInt(jQuery("#main_photo").attr('width')/2);

                                        //alert(centerLeft);

                                        //+'px';

			centerTop = jQuery("#main_photo").offset().top-155+'px';

			

			windowWidth = jQuery(window).width();

			rightLeft = parseInt(windowWidth*0.95);

			leftLeft = parseInt(windowWidth*0.05);

	

			var rightAjaxRatio = jQuery("#foto-right-ajax").attr('width') / jQuery("#foto-right-ajax").attr('height');

			if (!rightAjaxRatio) rightAjaxRatio = jQuery("#foto-right-ajax").width() / jQuery("#foto-right-ajax").height();

			

			var leftAjaxRatio = jQuery("#foto-left-ajax").attr('width') / jQuery("#foto-left-ajax").attr('height');

			if (!leftAjaxRatio) leftAjaxRatio = jQuery("#foto-left-ajax").width() / jQuery("#foto-left-ajax").height();

			

			var rightRatio = jQuery("#foto-right").attr('width') / jQuery("#foto-right").attr('height');

			if (!rightRatio) rightRatio = jQuery("#foto-right").width() / jQuery("#foto-right").height();

	

			var leftRatio = jQuery("#foto-left").attr('width') / jQuery("#foto-left").attr('height');

			if (!leftRatio) leftRatio = jQuery("#foto-left").width() / jQuery("#foto-left").height();

	

			jQuery("#foto-right-ajax").css({position:'absolute', left:(windowWidth-parseInt(rightFadeHeight*rightAjaxRatio)), top: '0px', height:rightFadeHeight+'px', width:parseInt(rightFadeHeight*rightAjaxRatio)+'px', opacity:'0'});

			jQuery("#foto-left-ajax").css({position:'absolute', left: '0px', top: '0px', height:leftFadeHeight+'px', width:parseInt(leftFadeHeight*leftAjaxRatio)+'px', opacity:'0'});

			//jQuery("#foto-right-ajax").css({position:'absolute', left:(windowWidth-parseInt(rightFadeHeight*rightAjaxRatio)), top: '0px', height:rightFadeHeight+'px', width:parseInt(rightFadeHeight*rightAjaxRatio)+'px'});

			//jQuery("#foto-left-ajax").css({position:'absolute', left: '0px', top: '0px', height:leftFadeHeight+'px', width:parseInt(leftFadeHeight*leftAjaxRatio)+'px'});

			

			if (parseInt(rightHeight*rightRatio)<rightWidth)

				jQuery("#foto-right").css({position:'absolute', left: (rightLeft-parseInt(rightHeight*rightRatio))+'px', right:'', height:rightHeight+'px', width:parseInt(rightHeight*rightRatio)+'px'});

			else 

				jQuery("#foto-right").css({position:'absolute', left: (rightLeft-rightWidth)+'px', right:'', height:parseInt(rightWidth/rightRatio)+'px', width:rightWidth+'px'});

			

			if (parseInt(leftHeight*leftRatio)<leftWidth)

				jQuery("#foto-left").css({position:'absolute', height:leftHeight+'px', width:parseInt(leftHeight*leftRatio)+'px'});

			else 

				jQuery("#foto-left").css({position:'absolute', height:parseInt(leftWidth/leftRatio)+'px', width:leftWidth+'px'});	

	



}



jQuery(document).ready(function(){





        jQuery("#foto-center .align_center_to_left .align_center_to_right").append("<img src='images/preloader.gif' style='padding-top:185px;' />");



		var nextlink = 'index.php?id=375';

		

		jQuery.get(nextlink,function(data){


			jQuery("body").append("<div id='cache' class='hidden'></div>")

			jQuery("#cache").append(data);
			
			//jQuery("#gallery").append('<a href="#"><img src="" alt="" class="hidden" id="foto-right-ajax"/></a><a href="#"><img src="" alt="" class="hidden" id="foto-left-ajax"/></a>');

			jQuery("#cache .element").each(function() {

				if (jQuery("img",this).attr('src').indexOf("assets/images/no-foto.jpg")>=0) { jQuery(this).remove(); }
				if (jQuery("img",this).attr('src').indexOf("assets/images/new-foto.jpg")>=0) { jQuery(this).remove(); }

			});

			allIndex = jQuery("#cache .element").length;			

			jQuery("#foto-text-wrap").after('<div id="choose-wrap"><div class="align_center_to_left"><div class="align_center_to_right"><div id="choose"> </div></div></div></div>');

			

			centerIndex = parseInt(allIndex/2);

			leftIndex = centerIndex-2;

			if (leftIndex<0) leftIndex = allIndex-1;

			rightIndex = centerIndex+2;

			if (rightIndex>=allIndex) rightIndex = 0;

			
			appendContent(centerIndex);

			

			for (var i=0; i<allIndex; i++) {

				if (i==centerIndex) jQuery("#choose").append('<a href="#" rel="'+i+'" class="active"><img src="images/blank.gif" alt="" width="15" height="15"/></a>');

				else jQuery("#choose").append('<a href="#" rel="'+i+'"><img src="images/blank.gif" alt="" width="15" height="15"/></a>');

			}

			

			jQuery("#choose a").click(function() {

				if (jQuery(this).hasClass("active")) return false;

				appendContent(jQuery(this).attr('rel'));

				jQuery("#choose a").removeClass("active");

				jQuery("#choose a:eq("+jQuery(this).attr('rel')+")").addClass("active");

				leftIndex=parseInt(jQuery(this).attr('rel'))-2;

				centerIndex=parseInt(jQuery(this).attr('rel'));

				rightIndex=parseInt(jQuery(this).attr('rel'))+2;

				if (rightIndex>=allIndex) rightIndex = 0;

				if (leftIndex<0) leftIndex = allIndex-1;

				return false;

			});



		jQuery("#arrow-right").click(function() {

			

			if (Block==1) return false;

			

			Block=1;

			

			leftIndex++;

			centerIndex++

			rightIndex++;

			

			if (leftIndex>=allIndex) leftIndex = 0;

			if (centerIndex>=allIndex) centerIndex = 0;

			if (rightIndex>=allIndex) rightIndex = 0;

			

			jQuery("#choose a").removeClass("active");

			jQuery("#choose a:eq("+centerIndex+")").addClass("active");

			

			//if (leftIndex<0) leftIndex = allIndex;

			//if (rightIndex<0) rightIndex = allIndex;

			

			//dimensions

			

			var leftRatio = jQuery("#foto-left").attr('width') / jQuery("#foto-left").attr('height');

			if (!leftRatio) leftRatio = jQuery("#foto-left").width() / jQuery("#foto-left").height();

			

			var centerRatio = jQuery("#main_photo").attr('width') / jQuery("#main_photo").attr('height');

			if (!centerRatio) centerRatio = jQuery("#main_photo").width() / jQuery("#main_photo").height();

			if (jQuery.browser.msie && parseInt(jQuery.browser.version)<=7) centerRatio = jQuery("#main_photo").width() / (jQuery("#main_photo").height());

			

			var rightRatio = jQuery("#foto-right").attr('width') / jQuery("#foto-right").attr('height');

			if (!rightRatio) rightRatio = jQuery("#foto-right").width() / jQuery("#foto-right").height();

			

			var rightAjaxRatio = jQuery("#foto-right-ajax").attr('width') / jQuery("#foto-right-ajax").attr('height');

			if (!rightAjaxRatio) rightAjaxRatio = jQuery("#foto-right-ajax").width() / jQuery("#foto-right-ajax").height();

			

			//positioning absolute

                                        var templeft = 0;

	                           if (jQuery.browser.opera) templeft = parseInt(jQuery("#main_photo").attr('width')/2);

                                        else templeft = parseInt(jQuery("#main_photo").width()/2);

			jQuery("#main_photo").css({position:'absolute', left:(centerLeft-templeft)+'px', top: centerTop, zIndex:0}).prependTo("#gallery");

                        //alert("now2");
                        
                        

			//to left position

			if (parseInt(leftHeight*centerRatio)<leftWidth)

				jQuery("#main_photo").animate({left:leftLeft, top:leftTop, height:leftHeight+'px', width:parseInt(leftHeight*centerRatio)+'px' },1000);
                                //alert("now1");
                        
			else {
                            
                                /*alert(leftLeft);
                                alert(leftTop);
                                alert(parseInt(leftWidth/centerRatio)+'px');
                                alert(leftWidth);*/

				jQuery("#main_photo").animate({left:leftLeft+'px', top:leftTop, height:parseInt(leftWidth/centerRatio)+'px', width:leftWidth+'px'},1000);			
                                //alert("now2");
                        }
			

			//fadeOut

			jQuery("#foto-left").animate({left:'0',top:'20px',height:leftFadeHeight+'px', width:parseInt(leftFadeHeight*leftRatio)+'px', opacity:'0'},1000);

                        //alert(centerTop);
                        
                        
			//to center

			if (parseInt(centerHeight*rightRatio)<centerWidth)

				jQuery("#foto-right").animate({left:(centerLeft-parseInt(centerHeight*rightRatio/2))+'px',top:centerTop,height:centerHeight+'px', width:parseInt(centerHeight*rightRatio)+'px'},1000);

			else 

				jQuery("#foto-right").animate({left:(centerLeft-parseInt(centerWidth/2))+'px',top:centerTop,height:parseInt(centerWidth/rightRatio)+'px', width:centerWidth+'px'},1000);	

                        

			//fadeIn

			if (parseInt(rightHeight*rightAjaxRatio)<rightWidth)

				jQuery("#foto-right-ajax").animate({left:(rightLeft-parseInt(rightHeight*rightAjaxRatio)),top:rightTop, height:rightHeight+'px', width:parseInt(rightHeight*rightAjaxRatio)+'px',opacity:'1'},1000);

			else 

				jQuery("#foto-right-ajax").animate({left:(rightLeft-rightWidth),top:rightTop, height:parseInt(rightWidth/rightAjaxRatio)+'px', width:rightWidth+'px',opacity:'1'},1000);			

			//alert(rightFadeHeight);

			

			setTimeout(function() {

				jQuery("#foto-left-ajax").remove();

				jQuery("#foto-left").attr('id','foto-left-ajax');

				jQuery("#main_photo").attr('id','foto-left');

				jQuery("#foto-right").attr('id','main_photo');

				jQuery("#foto-right-ajax").attr('id','foto-right');

				//alert(rightFadeHeight);

				

				jQuery("#cache img:eq("+rightIndex+")").clone().appendTo("#gallery").attr("id","foto-right-ajax");

				//jQuery("#foto-temp").attr("id","foto-left-ajax");



				jQuery("#foto-text-center .title").empty();

				jQuery("#foto-text-center .title").append(jQuery("#cache .title:eq("+centerIndex+")").text());

				

				jQuery("#foto-text-center p").empty();

				jQuery("#foto-text-center p").append(jQuery("#cache .description:eq("+centerIndex+")").text());



				jQuery("#foto-text-bottom .price").empty();

				jQuery("#foto-text-bottom .price").append(jQuery("#cache .price:eq("+centerIndex+")").text());

				//jQuery("#foto-text-bottom .price").animateCount({newvalue:2000});



				jQuery("#foto-text-bottom a:last").attr('href',jQuery("#cache .link:eq("+centerIndex+")").text());





				var rightAjaxRatio = jQuery("#foto-right-ajax").attr('width') / jQuery("#foto-right-ajax").attr('height');

				

				jQuery("#foto-right-ajax").attr('style','').css({position:'absolute', left: (windowWidth-parseInt(rightFadeHeight*rightAjaxRatio)), top: '0px', height:rightFadeHeight+'px', width:parseInt(rightFadeHeight*rightAjaxRatio)+'px', opacity:'0'});

				

				Block = 0;

				

			}, 1000);

                        

			return false;

                       

		});

		

		

		jQuery("#arrow-left").click(function() {

			

			if (Block==1) return false;

			

			Block=1;

			

			leftIndex--;

			centerIndex--;

			rightIndex--;

			

			if (leftIndex<0) leftIndex = allIndex-1;

			if (centerIndex<0) centerIndex = allIndex-1;

			if (rightIndex<0) rightIndex = allIndex-1;

			

			jQuery("#choose a").removeClass("active");

			jQuery("#choose a:eq("+centerIndex+")").addClass("active");

			

			//dimensions

			

			var leftRatio = jQuery("#foto-left").attr('width') / jQuery("#foto-left").attr('height');

			if (!leftRatio) leftRatio = jQuery("#foto-left").width() / jQuery("#foto-left").height();

			

			var centerRatio = jQuery("#main_photo").attr('width') / jQuery("#main_photo").attr('height');

			if (jQuery.browser.msie && parseInt(jQuery.browser.version)<=7) centerRatio = jQuery("#main_photo").width() / (jQuery("#main_photo").height());

			//alert(jQuery("#main_photo").width());

			//alert(jQuery("#main_photo").height());

			//alert(centerRatio);

			

			var rightRatio = jQuery("#foto-right").attr('width') / jQuery("#foto-right").attr('height');

			if (!rightRatio) rightRatio = jQuery("#foto-right").width() / jQuery("#foto-right").height();

			

			var leftAjaxRatio = jQuery("#foto-left-ajax").attr('width') / jQuery("#foto-left-ajax").attr('height');

			if (!leftAjaxRatio) leftAjaxRatio = jQuery("#foto-left-ajax").width() / jQuery("#foto-left-ajax").height();

			

			//positioning absolute

	

                                        var templeft = 0;

	                           if (jQuery.browser.opera) templeft = parseInt(jQuery("#main_photo").attr('width')/2);

                                        else templeft = parseInt(jQuery("#main_photo").width()/2);

			jQuery("#main_photo").css({position:'absolute', left:(centerLeft-templeft)+'px', top: centerTop, zIndex:0}).appendTo("#gallery");

	

			//to right position

			//jQuery("#main_photo").css({left:'',right:});

			if (parseInt(rightHeight*centerRatio)<rightWidth)

				jQuery("#main_photo").animate({left:(rightLeft-parseInt(rightHeight*centerRatio))+'px', top:rightTop, height:rightHeight+'px', width:parseInt(rightHeight*centerRatio)+'px' },1000);

			else

				jQuery("#main_photo").animate({left:(rightLeft-rightWidth)+'px', top:rightTop, height:parseInt(rightWidth/centerRatio)+'px', width:rightWidth+'px' },1000);



			//fadeOut

			jQuery("#foto-right").animate({left:(windowWidth-parseInt(rightFadeHeight*rightRatio)),top:'20px',height:rightFadeHeight+'px', width:parseInt(rightFadeHeight*rightRatio)+'px', opacity:'0'},1000);

	

			//to center

			if (parseInt(centerHeight*leftRatio)<centerWidth)

				jQuery("#foto-left").animate({left:(centerLeft-parseInt(centerHeight*leftRatio/2))+'px',top:centerTop,height:centerHeight+'px', width:parseInt(centerHeight*leftRatio)+'px'},1000);

			else 

				jQuery("#foto-left").animate({left:(centerLeft-parseInt(centerWidth/2))+'px',top:centerTop, height:parseInt(centerWidth/leftRatio)+'px', width:centerWidth+'px'},1000);

				

			//fadeIn

			if (parseInt(leftHeight*leftAjaxRatio)<leftWidth)

				jQuery("#foto-left-ajax").animate({left:leftLeft,top:leftTop, height:leftHeight+'px', width:parseInt(leftHeight*leftAjaxRatio)+'px',opacity:'1'},1000);

			else 

				jQuery("#foto-left-ajax").animate({left:leftLeft,top:leftTop, height:parseInt(leftWidth/leftAjaxRatio)+'px', width:leftWidth+'px',opacity:'1'},1000);





			//alert(rightFadeHeight);

			

			setTimeout(function() {

				jQuery("#foto-right-ajax").remove();

				jQuery("#foto-right").attr('id','foto-right-ajax');

				jQuery("#main_photo").attr('id','foto-right');

				jQuery("#foto-left").attr('id','main_photo');

				jQuery("#foto-left-ajax").attr('id','foto-left');

				//alert(rightFadeHeight);

				jQuery("#cache img:eq("+leftIndex+")").clone().appendTo("#gallery").attr("id","foto-left-ajax");

				jQuery("#foto-text-center .title").empty();

				jQuery("#foto-text-center .title").append(jQuery("#cache .title:eq("+centerIndex+")").text());

				

				jQuery("#foto-text-center p").empty();

				jQuery("#foto-text-center p").append(jQuery("#cache .description:eq("+centerIndex+")").text());

			

				jQuery("#foto-text-bottom .price").empty();

				jQuery("#foto-text-bottom .price").append(jQuery("#cache .price:eq("+centerIndex+")").text());

			

				jQuery("#foto-text-bottom a:last").attr('href',jQuery("#cache .link:eq("+centerIndex+")").text());

	

				var leftAjaxRatio = jQuery("#foto-left-ajax").attr('width') / jQuery("#foto-left-ajax").attr('height');

				

				jQuery("#foto-left-ajax").attr('style','').css({position:'absolute', left: '0px', top: '0px', height:leftFadeHeight+'px', width:parseInt(leftFadeHeight*leftAjaxRatio)+'px', opacity:'0'});

				

				Block = 0;

				

			}, 1000);

			

			return false;

		

		});


		jQuery("#foto-right").delegate("", "click", function() { jQuery("#arrow-right").click(); });

		jQuery("#foto-left").delegate("", "click", function() { jQuery("#arrow-left").click(); });
	
		jQuery("#main_photo").delegate("", "click", function() { window.location = jQuery(".other-links a:last").attr('href'); });

		document.onkeydown = function(e){ 	

					if (e == null) { // ie

						keycode = event.keyCode;

					} else { // mozilla

						keycode = e.which;

					}

					//alert(keycode);

					if(keycode == 37) // display previous image

							jQuery("#arrow-left").click();

					else if(keycode == 39) // display next image

							jQuery("#arrow-right").click();

						

				};

	

	});



});


