
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"/>');

	$('#'+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 changeImage(id,d){
	$('#'+id+'_rw_'+(cIndex[id]+d)).click();
	//setImage(id,cIndex[id]+d);
}

function clickImage(id){
	var cIdx = cIndex[id];
	var iidx = getRelativeIdx(id,cIdx,0);
	if(images[id][iidx].href)
		window.location = images[id][iidx].href;
}
 
function getLeft(id,idx){
	var left = 0;
	var size = images[id].length;
	if(idx>=0){
		for(var i=0;i<idx;i++){
			left += images[id][i%size].width;
		}
	}
	else{
		for(var i=-1;i>=idx;i--){
			left -= images[id][size+(i%size)-1].width;
		}		
	}
	return left;
}

function setImage(id,idx){
	if(!idx && !cIndex[id])
		idx = 0;
	else if(!idx && idx!=0)
		idx = cIndex[id];
	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=-2;i<3;i++){
		var iidx = getRelativeIdx(id,cIdx,i);
		if(images[id][iidx]){
			var imageIdx = (cIdx+i);
			var img = $('#'+id+'_rw_'+(imageIdx));
			
			if(!img.is('*')){
				$('#'+id+'_sildeshow_images').append('<img id="'+id+'_rw_'+imageIdx+'"/>');
				img = $('#'+id+'_rw_'+(imageIdx));
				img.attr('src',images[id][iidx].src);
				img.attr('idx',iidx);
				img.attr('imageIdx',imageIdx);
				img.attr('rw',id);


				img
					.mouseenter(
						function() {
							var imageIdx = parseInt($(this).attr('imageIdx'));
							var cIdx = cIndex[id];
							if(imageIdx<cIdx)
								$('#'+id+' .prev').addClass('highlight');
							else if(imageIdx>cIdx)
								$('#'+id+' .next').addClass('highlight');
						}
					)
					.mouseleave(
						function() {
							var imageIdx = parseInt($(this).attr('imageIdx'));
							var cIdx = cIndex[id];
							if(imageIdx<cIdx)
								$('#'+id+' .prev').removeClass('highlight');
							else if(imageIdx>cIdx)
								$('#'+id+' .next').removeClass('highlight');
				 		}
					)
				;	
				

				img.click(function(){
					if(animation)
						return;
					animation = true;
					var imageIdx = parseInt($(this).attr('imageIdx'));
					var cIdx = cIndex[id];					
					var idx = parseInt($(this).attr('idx'));
					if(imageIdx==cIdx && images[id][idx].href){
						window.location = images[id][idx].href;
					}
					else{
						var cImg = $('#'+id+'_rw_'+(cIdx));
						var windowWidth = $(window).width();
						var totalWidth = parseInt($(this).attr('total_width'));
						var nLeft = Math.ceil((totalWidth) - ((windowWidth-$(this).width())/ 2)) ;
						$(this).fadeTo(200,1);
						var cPostion = cImg.position();
						var iPosition = $(this).position();
						var left = (cPostion.left-iPosition.left);
						if(imageIdx>cIdx)
							left *= -1;
						$('#'+id+'_sildeshow_images').animate({left:'+='+((cPostion.left-iPosition.left))+'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,imageIdx);
									animation = false;
									
								}
						);
						//$('#'+id+'_images_info_bg.scale').fadeTo(100,0);
						$('#'+id+'_images_info_bg').hide();
					}
				});
				
			}			
			
			
			
			var scale = windowHeight/images[id][iidx].height;
			
			/* max widow_width
			if(images[id][iidx].width*scale>windowWidth){
				scale = windowWidth/images[id][iidx].width;
			}
			else{
				img.height(windowHeight);
				img.width(images[id][iidx].width);
			}
			*/

			totalWidth += Math.ceil(images[id][iidx].width * scale);
			
			img.width(Math.ceil(images[id][iidx].width * scale));
			img.height(Math.ceil(images[id][iidx].height * scale));
			img.css({top:(-img.height()+windowHeight)+'px'});
			var left = getLeft(id,imageIdx);
			img.css({left:Math.ceil(left * scale)+'px',position:'absolute'});	
			if(i!=0){
				img.fadeTo(0,opacties[id]);
				
			}
			else{
				offset_x = left;
				var newWidth = img.width();// * 0.9;
				$('#'+id+'_images_info_bg').width(newWidth);
				if($('#about_slideshow_images_info').css('position')=='absolute'){
					$('#about_slideshow_images_info').css('margin-left',-Math.min(0,(windowWidth-img.width())/2));
				}
				//$('#'+id+'_images_info_bg.scale').fadeTo(100,1);
				if(images[id][iidx].title + images[id][iidx].description != ''){
					$('#'+id+'_images_info_bg').show();
					$('#'+id+'_image_info_title').html(images[id][iidx].title);
					$('#'+id+'_image_info_description').html(images[id][iidx].description);
				}
				else{
					$('#'+id+'_images_info_bg').hide();
				}
			}
		}
	}
	var img = $('#'+id+'_rw_' + cIdx);
	$('#'+id+'_sildeshow_images').css({left:((windowWidth-img.width())/2 - Math.ceil(offset_x * scale))+'px'});
	$('#'+id+'_sildeshow_images').parent().width(windowWidth).height(windowHeight);
	
	var newWidth = img.width();// * 0.9;
	var info_bg = $('#'+id+'_images_info_bg');
	info_bg.css({left:Math.ceil(img.offset().left)+'px'});
}

function getRelativeIdx(id,start,offset){
	start = start % images[id].length;
	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 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 onResizeWindow(){
	
	var windowWidth 	= $(window).width();
	var windowHeight	= $(window).height();

	$('.scale div').each(
		function(){
			if($(this).attr('org_font-size')==null){
				$(this).attr('org_font-size',parseInt($(this).css('font-size')));
			}
			if($(this).attr('org_line-height')==null){
				$(this).attr('org_line-height',parseInt($(this).css('line-height')));
			}			
			var fontSize = parseInt($(this).attr('org_font-size'));
			$(this).css('font-size',parseInt(fontSize/800*windowHeight)+'px');
			var lineHeight = parseInt($(this).attr('org_line-height'));
			$(this).css('line-height',parseInt(lineHeight/800*windowHeight)+'px');			
		}
	);
	$('img.fullscreen').each(
		function(idx,img){
			var ratio = $(this).width() / $(this).height();
			var newImageWidth;
			var newImageHeight;
			if(windowWidth/windowHeight >ratio){
				newImageWidth	= windowWidth;
				newImageHeight = windowWidth/ratio;	
			}
			else{
				newImageHeight	= windowHeight;
				newImageWidth 	= windowHeight * ratio;
			}
			
			var pos_x	= (windowWidth-newImageWidth)/2;
			
			if(newImageHeight>=windowHeight){
				var pos_y	= (windowHeight-newImageHeight)/2;
			}else{
				var pos_y	= 0;	
			}
			
			$(this).width(newImageWidth);
			$(this).height(newImageHeight);
			
			$(this).parent().css({display:'block',position:'absolute',top: pos_y+"px", left: pos_x+"px"});

		}
	);
	$('div.fullscreen_container').each(
		function(idx){
			$(this).css({width:windowWidth + 'px', height: (windowHeight - 50)+'px'});
		}
	);	
	


	$('.overlay')
			.height(Math.max(windowHeight,$(document).height()))
			.width(Math.max(windowWidth,$(document).width()))
	;
	
	$('.sb_icon').mouseover(function() {
		$(this).children('a').children('img').css('visibility', 'hidden');
	});
	$('.sb_icon').mouseout(function(){
		$(this).children('a').children('img').css('visibility', 'visible');
	});
	 	 
	
}

function openPopup(href){
	window.open(href,'_blank','dependent=yes,location=no,resizeable=yes,width=760,width=500,scrollbars=yes,status=no,toolbar=no');
}

function array2json(arr) {
    var parts = [];

    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(array2json(value)); /* :RECURSION: */
            else parts[key] = array2json(value); /* :RECURSION: */
        } else {
            var str = "";
            if(!is_list) str = '\'' + key + '\':';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '\'' + value + '\''; //All other things
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}



