var height;
var width;
var iPhoneShown=false;
var currentindex;
var files;

function initializeView(){
	$("div#etichettapaletteclose").hide();
	$("div#obscurer").hide();
	height=window.innerHeight;
	width=window.innerWidth;
	if(height==undefined){	//internet explorer merda
		height=document.documentElement.clientHeight;
		width=document.documentElement.clientWidth;
	}
	
	var browser=navigator.userAgent.toLowerCase();

	if (!(browser.indexOf('iphone')!=-1 || browser.indexOf("msie 6.")!=-1))
	{
		$("div#leftstripe").height(height);
		$("div#dots").height(height-90);
		$("div#page").height(height-100);
		$("div#obscurer").height(height);
	}
	
	$("div#testata").width(width-82);
	$("div#rightside").width(width-62);
	$("div#page").width(width-99-62);
	$("div#obscurer").width(width);
	
	$("div.iPhone_visualizer").hide();
	
	var lineheight=184/$("div#spaziovoci").children().length
	$("div#spaziovoci").children().css('height', lineheight+"px");
	$("div#spaziovoci").children().css('line-height', lineheight+"px");
	//$("div#spaziovoci").children().css('-top', lineheight/2-9);
	$("div#spaziovoci").children().eq(0).css('border-top', "none");
	
	$("div#menu").mouseenter(function(){
		$("div#voci").animate({
			marginLeft: "0px"
		}, 400, function(){});
		$("div#palette").animate({
			left: "282px"	 
		}, 400, function(){});
		$("img#paletteimg").fadeOut();
	});
	
	$("div#menu").mouseleave(function(){
		$("div#voci").animate({
			marginLeft: "-282px"
		}, 500, function(){});
		$("div#palette").animate({
			left: "0px"	 
		}, 500, function(){});
		$("img#paletteimg").fadeIn();
	});
	if(iPhoneShown==true){
		showiPhone();	
	}
}

function openiPhone(folder,filelist,startindex){
	files=filelist.split(",");
	for(var i=0; i<files.length; i++){
		files[i]=folder+'/'+files[i];
	}
	startindex=(startindex>0 && startindex<files.length)?startindex:0;
	currentindex=startindex;
	showiPhone();
	showImage();
}

function showiPhone(){
	var maskheight=Math.min(height-30,730);
	var maskwidth=385*maskheight/730;
	var presenter_height=Math.min(height,760);
	var screenh=480*maskheight/730;
	var screenw=320*maskwidth/385;
	var screenx=32*maskwidth/385;
	var screeny=126*maskheight/730;
	$("div#obscurer").fadeIn();
	$("div#presenterarea").css('height',presenter_height+"px");
	$("a#leftnav").css('height',presenter_height+"px");
	$("a#rightnav").css('height',presenter_height+"px");
	$("img#iphonemask").height(maskheight);
	$("div#masker").css('top',"15px");
	$("div#masker").css('left',(500-maskwidth)/2+"px");
	$("div#wait").css('top',((screenh-32)/2)+"px");
	$("div#wait").css('left',((screenw-32)/2)+"px");
	$("body").css("overflow","hidden");
	$("div#imagepositioner").css("height",screenh+"px");
	$("div#imagepositioner").css("width",screenw+"px");
	$("div#imagepositioner").css("top",screeny+1+"px");
	$("div#imagepositioner").css("left",screenx+1+"px");
	//$("img#iPhoneImage").attr("width",screenw);
	$("img#iPhoneImage").attr("height",screenh);
	iPhoneShown=true;
}

function closeiPhone(){
	iPhoneShown=false;
	$("div#obscurer").fadeOut();
	currentindex=null;
	files=null;
}

function nextPhoto(){
	$("div#wait").show();
	currentindex++
	if(currentindex>=files.length)
		currentindex=0;
	showImage();
}

function prevPhoto(){
	$("div#wait").show();
	currentindex--;
	if(currentindex<0)
		currentindex=files.length-1;
	showImage();
}

function showImage(){
	$("img#iPhoneImage").attr("src",files[currentindex]);
	$("img#iPhoneImage").load(function(){
		$("div#wait").hide();
	});
}
