
var images = {};
var cIndex = {};
var opacties = {};
var animation = false;

function initSlideShow(id,ssImages,infoType,close,opacity,linkClass,linkCaption){
	images[id] = ssImages;
	cIndex[id] = 0;
	if(opacity)
		opacties[id] = opacity;
	else
		opacties[id] = 1;
	$('#'+id).append('<div id="'+id+'_sildeshow_images" class="sildeshow_images"/>');
	for(var i=0;i<5;i++){
		$('#'+id+'_sildeshow_images').append('<img id="'+id+'_rw_'+i+'"/>');

		var img = $('#'+id+'_rw_'+(i));
		if(i!=2){
			img.fadeTo(0,opacties[id]);
		
		
			if(i<2){

				img
					.mouseenter(
						function() {
							$('#'+id+' .prev').addClass('highlight');
						}
					)
					.mouseleave(
						function() {
							$('#'+id+' .prev').removeClass('highlight');
				 		}
					)
				;	
			}
			else{
				img
					.mouseenter(
						function() {
							$('#'+id+' .next').addClass('highlight');
						}
					)
					.mouseleave(
						function() {
							$('#'+id+' .next').removeClass('highlight');
				 		}
					)
				;				
			}
		}
		img.click(function(){
			if(animation)
				return;
			animation = true;
			var cIdx = cIndex[id]; 
			var idx = parseInt($(this).attr('idx'));
			if(idx==cIdx && images[id][idx].href){
				window.location = images[id][idx].href;
			}
			else{
				var windowWidth = $(window).width();
				var totalWidth = parseInt($(this).attr('total_width'));
				var nLeft = (totalWidth) - ((windowWidth-$(this).width())/ 2) ;
				$(this).fadeTo(200,1);
				
				$('#'+id+'_sildeshow_images').animate({left:(-nLeft)+'px'},500,
						function(){
							//$('#'+id+'_images_info_bg.scale').fadeTo(1,50);
							$('#'+id+'_image_info_title').html(images[id][idx].title);
							$('#'+id+'_image_info_description').html(images[id][idx].description);					
							setImage(id,idx);
							animation = false;
							
						}
				);
				//$('#'+id+'_images_info_bg.scale').fadeTo(100,0);
				$('#'+id+'_images_info_bg').hide();
				/*
				$('#'+id+'_images_info_bg.scale').animate({left:(-$(this).width()/2)+'px',width:($(this).width())+'px'},500,
						function(){
							setImage(id,idx);
							animation = false;
						}
				);
				*/				

			}
		});
		
	}
	$('#'+id).append('<a onclick="changeImage(\''+id+'\',1)" class="next" ><span /></a>');
	$('#'+id).append('<a onclick="changeImage(\''+id+'\',-1)" class="prev"  ><span /></a>');
	if(infoType){
		$('#'+id).append('<div id="'+id+'_images_info" class="images_info '+infoType+'"><div id="'+id+'_images_info_bg" class="images_info_bg "><div id="'+id+'_image_info_title" class="image_info_title"></div><div id="'+id+'_image_info_description" class="image_info_description"></div>'+(linkCaption?'<a onclick="clickImage(\''+id+'\')" class="'+linkClass+'"><span>'+linkCaption+'</span></a>':'')+'</div></div>');
		addShadow('#'+id+'_images_info .button span,#'+id+'_images_info .button_highlight span,#'+id+'_images_info .small_button span,#'+id+'_images_info .text_shadow span');
	}
	$(window).resize(
		function(){
			setImage(id);
		}
	);
	onResizeWindow();
}

function addShadow(selector){
	$(selector).each(function(){
		var shadow = $(this).clone();
		shadow.addClass('shadow');
		shadow.fadeTo(0,0.7);
		$(this).parent().append(shadow);
		var text2 = $(this).clone();
		text2.addClass('text2');
		$(this).parent().append(text2);			
	});
}

function changeImage(id,d){
	$('#'+id+'_rw_'+(2+d)).click();
	//setImage(id,cIndex[id]+d);
}

function setImage(id,idx){
	if(idx || idx==0)
		cIndex[id] = idx;
	var cIdx = cIndex[id]; 
	windowWidth = $(window).width();
	windowHeight= $(window).height() - 50 - (cartOpen?50:0);
	var totalWidth = 0;		
	var offset_x = 0;
	for(var i=0;i<5;i++){
		var iidx = getRelativeIdx(id,cIdx,i-2);
		var img = $('#'+id+'_rw_'+(i));
		img.attr('src',images[id][iidx].src);
		img.attr('idx',iidx);
		
		var scale = windowHeight/images[id][iidx].height;

		if(images[id][iidx].width*scale>windowWidth){
			scale = windowWidth/images[id][iidx].width;
		}
		else{
			img.height(windowHeight);
			img.width(images[id][iidx].width);
		}
		img.attr('total_width',totalWidth);
		totalWidth += images[id][iidx].width * scale;
		
		img.width(images[id][iidx].width * scale);
		img.height(images[id][iidx].height * scale);
		img.css({top:(-img.height()+windowHeight)+'px'});		
		if(i!=2){
			img.fadeTo(0,opacties[id]);
			
		}
		else{
			offset_x = -((totalWidth-img.width()) - ((windowWidth-img.width())/ 2)) ;
			var newWidth = img.width();// * 0.9;
			$('#'+id+'_images_info_bg').width(newWidth);
			//$('.scale #'+id+'_images_info_bg').css({left:-(newWidth/2)+'px'});
			
			//$('#'+id+'_images_info_bg.scale').fadeTo(100,1);
			$('#'+id+'_images_info_bg').show();
			$('#'+id+'_image_info_title').html(images[id][iidx].title);
			$('#'+id+'_image_info_description').html(images[id][iidx].description);

		}
		
	}
	//offset_x = (-(totalWidth-windowWidth)/2);
	$('#'+id+'_sildeshow_images').css({left:offset_x+'px',width:totalWidth+'px'});
	$('#'+id+'_sildeshow_images').parent().width(windowWidth).height(windowHeight);
	var img = $('#'+id+'_rw_2');
	var newWidth = img.width();// * 0.9;
	var info_bg = $('#'+id+'_images_info_bg');
	info_bg.css({left:Math.round(img.offset().left)+'px'});
}

function getRelativeIdx(id,start,offset){
	offset = offset%images[id].length;
	var lastIdx = (images[id].length-1);
	var div = start+offset - images[id].length;
	if(div>=0)
		return div;
	div = start+offset;
	if(div<0)
		return  images[id].length + div;
	return start+offset;
}

function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';
	
	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');
				
				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	
	return urlVarValue;
} 

function onResizeWindow(){
	
	var windowWidth 	= $(window).width();
	var windowHeight	= $(window).height();
	
	$('img.fullscreen_preview').each(
			function(){
				var ratio = $(this).width() / $(this).height();
				var newImageWidth;
				var newImageHeight;
				var minDistTop = 110;
				var minDistBottom = 200;
				
				newImageHeight = windowHeight - (minDistTop + minDistBottom);
				if(ratio <= 1){
					newImageWidth = newImageHeight / ratio;
				}
				else{
					newImageWidth = newImageHeight * ratio;
				}
				
				
				
				$(this).height(newImageHeight);
				var pos_x	= (windowWidth-$(this).width())/2;
				
				$(this).parent().css({display:'block',position:'absolute',top: minDistTop+'px', left: pos_x+'px'});

			}
	);
	
	$('div.fullscreen_container').each(
		function(){
			$(this).css({width:windowWidth + 'px', height: (windowHeight)+'px'});
		}
	);
	
	$('.overlay')
	.height(Math.max(windowHeight,$(document).height()))
	.width(Math.max(windowWidth,$(document).width()))
	;
	 	 
	
}

function openPopup(href){
	window.open(href,'_blank','dependent=yes,location=no,resizeable=yes,width=850,status=no,toolbar=no');
}


