/**
 * @author itmldata@iconmedialab.it
 */
/**
 * set here the path to Tween script
 */
var jsTween = "/lavazzada/toolbar/js/tween.js";
/**
 * set here links showed on list (first one is linked to logo)
 */
var links = new Array();
if(toolbarLang.toUpperCase()=="IT"){
	links = [{"label":"storeit.lavazza.com","link":"http://storeit.lavazza.com/","fullscreen":false},
  		{"label":"www.lavazza.it","link":"http://www.lavazza.it","fullscreen":false},
		{"label":"www.lavazzamodomio.it","link":"http://www.lavazzamodomio.it","fullscreen":false},
		{"label":"20calendars.lavazza.com","link":"http://20calendars.lavazza.com/","fullscreen":false},
		{"label":"www.lavazzaespression.com","link":"http://www.lavazzaespression.com","fullscreen":false},
		{"label":"www.theitalianexperience.com","link":"http://www.theitalianexperience.com","fullscreen":false}
		];
}else{
	links = [{"label":"storeit.lavazza.com","link":"http://storeit.lavazza.com/","fullscreen":false},
	   		{"label":"www.lavazza.it","link":"http://www.lavazza.it","fullscreen":false},
			{"label":"www.lavazzamodomio.it","link":"http://www.lavazzamodomio.it","fullscreen":false},
			{"label":"20calendars.lavazza.com","link":"http://20calendars.lavazza.com/20calendars.html?lang=en","fullscreen":false},
			{"label":"www.theitalianexperience.com","link":"http://www.theitalianexperience.com","fullscreen":false}
		];
}


/**
 * set here the length of transition
 */
var toolbarDownTime = 1;
/**
 * Toolbar elements writing in page
 */
document.open();
var includes = '<link rel=\"stylesheet\" href=\"'+toolbarCssStyle+'\" type=\"text\/css\" charset=\"utf-8\" \/>\n'
				+'\n<div id=\"toolbar\">'				
				+'\n\t<div id="toolbar_btn"><a href="javascript:void(0)" id=\"toolbar_btn_link\" class=\"'+((toolbarLang.toUpperCase()=="IT")?("toolbar_IT"):("toolbar_other"))+'\" onclick="toolbarMove()">'+((toolbarLang.toUpperCase()=="IT")?("I SITI LAVAZZA"):("LAVAZZA SITES"))+'</a></div>'
				+'\n\t<div id="toolbar_list_box" onmouseover="toolbarOver=1;" onmouseout="toolbarOver=0;">'
				+'\n\t\t<ul id="toolbar_list">'
				+ getLinksCode()
				+'\n\t\t</ul>'
				+'\n\t\t<div id=\"toolbar_close\"></div>'
				+'\n\t</div>'
				+'\n</div>';
document.write(includes)
document.close();
/**
 * Window events setting
 */
var originalOnLoad = window.onload
window.onload=function(){
	toolbarInit();
	if(originalOnLoad)originalOnLoad();
}
var originalOnResize = window.onresize
window.onresize=function(){
	toolbarStatus=1;
	toolbarMove();
	if(originalOnResize)originalOnResize();
}

/**
 * @gestione pagine fullScreen
 */
function openNew(url){
	window.open(url);
	window.close();
}

/**
 * @return str:String HTML of showed list
 */
 
function getLinksCode(){
	
	var str="";
	var ctrlFull=false;
	for(var i=0;i<links.length;i++){
		if(links[i].fullscreen && location==links[i].link){
			ctrlFull=true;
		}		
	}
	
	for(var i=0;i<links.length;i++){
		if(ctrlFull){
			str+='\n\t\t\t<li><a href="javascript:;" onclick="openNew(\''+links[i].link+'\')" class=\"'+((i<links.length-1)?('normal'):('end'))+'\" >'+links[i].label+'</a></li>';
		}else{
			str+='\n\t\t\t<li><a href="'+links[i].link+'" class=\"'+((i<links.length-1)?('normal'):('end'))+'\" target="_blank">'+links[i].label+'</a></li>';
		}		
	}

	return str;
}
/**
 * Toolbar variables declaration
 */
var toolbarStatus = 0;
var toolbarOver = 0;
var toolbar,toolbarListBox,toolbarList,toolbarClose,toolbarBtn,toolbarTween,toolbarCheckInt,page;
/**
 * Toolbar initialization and first close
 */
function toolbarInit(){
	toolbar = document.getElementById("toolbar");
	toolbarListBox = document.getElementById("toolbar_list_box");
	toolbarList = document.getElementById("toolbar_list");
	toolbarClose = document.getElementById("toolbar_close");
	toolbarBtn = document.getElementById("toolbar_btn_link");
	var listSize = 0;
	for(var i=0;i<toolbarList.childNodes.length;i++){
		listSize +=(toolbarList.childNodes[i].offsetWidth)?(toolbarList.childNodes[i].offsetWidth):(0);
	}
	toolbarList.style.width  =listSize +"px";	
	toolbarListBox.style.width  = listSize + pos(toolbarClose.style.width) +"px";
	toolbarClose.style.left = listSize+"px";
	toolbarListBox.style.top = -toolbarListBox.offsetHeight+"px";
	toolbar.style.visibility = "visible";
	
}
/**
 * Open/close manager
 */
function toolbarMove(){
	if(!toolbarTween){
		toolbarTween = new Tween(toolbarListBox.style,'top',Tween.strongEaseOut,pos(toolbarListBox.style.top),0,toolbarDownTime,'px');
		toolbarTween.onMotionStarted = function(){
			if(toolbarStatus==0){
				toolbarBtn.style.backgroundPosition = "right top";
			}else if(toolbarStatus==1){
				toolbarBtn.style.backgroundPosition = "left top";
			}
		}
		toolbarTween.onMotionFinished = function(){					
			if(toolbarStatus==1)toolbarCheckInt = setInterval(toolbarCheck,3000);				
			else if(toolbarStatus==0)clearInterval(toolbarCheckInt);
		}
		toolbarTween.start();
		toolbarStatus=1;
	}else{
		if(toolbarStatus==0){
			toolbarTween.continueTo(0,toolbarDownTime);
			toolbarStatus=1;
		}else if(toolbarStatus==1){		
			toolbarTween.continueTo(-toolbarListBox.offsetHeight,toolbarDownTime);
			toolbarStatus=0;
		}
	}
}
/**
 * Timed check of mouseover.
 * If mouse is out, call toolbar close
 */
function toolbarCheck(){
	if(toolbarStatus==1 && toolbarOver==0){
		clearInterval(toolbarCheckInt);
		toolbarMove();
	}
}
/**
 * @return :Number substring of style dimension "px"
 */
function pos(strPos){
	return Number(strPos.substr(0,strPos.indexOf("px")));
}
