var initAfter = false;
var scrollUp = {
	options:{
		myBlock: ""
	},
	init: function(opts){
		for (name in opts) scrollUp.options[name] = opts[name];
		var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
		if((scrollUp.options.myBlock == 'offersLeft' && !initAfter) || (initAfter)){
			var IE_check = false;
			if (ievs) IE_check = true;
			if(!IE_check){
				$(('#'+ scrollUp.options.myBlock) + " li").each( function (){
					$(this).css("background", "none");
				});
			}
			$(('#'+ scrollUp.options.myBlock)).cycle({
				fx: 'scrollUp',
				speed: 1000,
				timeout: 6000,
				pause: 1
			});
		} else {
			initAfter = true;
			setTimeout("scrollUp.init({myBlock: 'offersRight'})", 3000);
		}
	}
}

function selectIframe(mySelect){
	if(mySelect.value != ''){
		var myIframe = '<iframe width="640" height="910" frameborder="0" src="'+ jbase64.decode(mySelect.value) +'"></iframe>';
		$('#contForm > div').html(myIframe);
	} else {
		$('#contForm > div').html('');
	}
}

$(document).ready(function(){
	newsletterBox();
	allLink();
	$('#buttonTop a').css({opacity: 0.8});
	$('#buttonTop a').hover(function(){$(this).css({opacity: 1});}, function(){$(this).css({opacity: 0.8});});
	newsScroll();
});

$(window).load(function(){
	scrollHeader.init();
	if(resetPngIe6()){
		resetPng();
		setTimeout("resetPng()", 2500);
	}
	$('a.buttVideo').click(function(){
		Shadowbox.open({player:'iframe', title:'', content:'http://www.youtube.com/v/RdM7XBE9jyA',  width:'640', height:'505'});
	});
	/*$('a.buttVideoiFrame').click(function(){
		Shadowbox.open({player:'iframe', title:'', content:'/assets/video.php',  width:'640', height:'505'});
	});*/
});

function newsScroll(){
	if($('#newsHome > ul > li'.length > 1)){
		$('#newsHome > ul').cycle({
			fx: 'scrollUp',
			speed: 600,
			timeout: 6000,
			pause: 1
		});
	}
}

function newsletterBox(){
	$('#inputSWF').val('email@email.it');
	$('#inputSWF').focus(function(){
		$(this).val('');
	});
	$('#inputSWF').blur(function(){
		if($(this).val() == '') $(this).val('email@email.it');
	});
	$('#newsletterBox').submit(function() {
		var isComplete = true;
		if($('#inputSWF').val() == 'email@email.it') isComplete = false;
		if(isComplete == false) {
			return false;
		} else {
			return true;
		}
	});
}

var scrollHeader = {
	setInit: function(){
		if($('span', '#header').length > 1){
			$('span strong', '#header').each(function(){
				$(this).clone().insertBefore($(this)).addClass('black');
			});
			$('span strong', '#header').each(function(){
				$(this).css({opacity: 0});
			});
		}
	},
	init: function(){
		if($('span', '#header').length > 1){
			$('span', '#header').each(function(){$(this).addClass('setGall');});
			$(('#header')).cycle({
				fx: 'fade',
				timeout: 100,
				before:  scrollHeader.resetClaim,
				after: scrollHeader.enterClaim
			});
		}
	},
	resetClaim: function() {
		$('strong', this).each(function(){
			$(this).css({opacity: 0});
		});
	},
	enterClaim: function() {
		$('#header').cycle('pause');
		$('strong:not(.black)', this).animate({opacity: 1}, 500, 'swing');
		$('strong.black', this).animate({opacity: 0.7}, 500, 'swing', function(){
			setTimeout("scrollHeader.playAgain()", 3000);
		});
	},
	playAgain: function() {
		$('#header span').each(function(){
			if($(this).is(':visible')){
				$(this).children('strong').animate({opacity: 0}, 500, function(){
					$('#header').cycle('resume');
				});
			}
		});
	}
}

function allLink(){
	$("#newsHome > ul > li > p").each(function(){
		if($('a', this).length > 0){
			$(this).hover(
				function(){
					$(this).css({cursor:'pointer'});
					$(this).addClass('newsHov');
					$(this).click(function(){
						location.href=$('a', this).attr("href");
					});
				},
				function(){
					$(this).css({cursor:'auto'});
					$(this).removeClass('newsHov');
				}
			);
		}
	});
	$("#sectionList > li").each(function(){
		if($('a', this).length > 0){
			$(this).hover(
				function(){
					$(this).css({cursor:'pointer'});
					$(this).addClass('newsHov');
					$(this).click(function(){
						location.href=$('a', this).attr("href");
					});
				},
				function(){
					$(this).css({cursor:'auto'});
					$(this).removeClass('newsHov');
				}
			);
		}
	});
}

var share = {
	modal: function() {
		var myPageUrlNow = window.location.href;
		var myPageTitleNow = escape(document.title);
		var myPageTextNow = "";
		var metaArray = document.getElementsByName('description');
		if(metaArray){
			for (var i = 0; i < metaArray.length; i++) myPageTextNow = myPageTextNow + metaArray[i].content;
		}
		myPageTextNow = escape(myPageTextNow);
		
		var myUrlNow = 'http://www.facebook.com/share.php?u=myPageUrl&t=myPageTitle';
		myUrlNow = myUrlNow.replace("myPageUrl", myPageUrlNow);
		myUrlNow = myUrlNow.replace("myPageTitle", myPageTitleNow);
		//myUrlNow = myUrlNow.replace("myPageText", myPageTextNow);
		
		if (window.showModalDialog) {
			window.showModalDialog(myUrlNow, "socials", "dialogHeight:450px;dialogWidth:880px");
		} else {
			window.open(myUrlNow, 'socials', 'height=450,width=880,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
		}
	} 
}

var loadedobjects="";
var rootdomain="http://"+window.location.hostname;
function include_page(url, containerid){
	//alert(url +' - '+ rootdomain);
	var page_request = false;
	if (window.XMLHttpRequest)
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){
		try { page_request = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e){
			try{ page_request = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e){}
		}
	} else
	return false
	page_request.onreadystatechange = function(){ loadpage(page_request, containerid); }
	page_request.open('GET', url, true)
	page_request.send(null)
}

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
		if (document.getElementById(containerid)){ document.getElementById(containerid).innerHTML=page_request.responseText;
		} else { alert("Errore, l'elemento non esiste");
		}
	}
}
function loadobjs(){
	if (!document.getElementById)
		return
	for (i=0; i<arguments.length; i++){
		var file=arguments[i]
		var fileref="";
		if (loadedobjects.indexOf(file)==-1){
			if (file.indexOf(".js")!=-1){
				fileref=document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			} else if (file.indexOf(".css")!=-1){
				fileref=document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref);
			loadedobjects+=file+" ";
		}
	}
}

function resetPngIe6(){
	var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
	if (ievs){
		var iev = new Number(RegExp.$1);
		if (iev < 7) return true;
	}
	return false;
}

var clear="/images/blank.gif";
function resetPng(){
	var els = document.getElementsByTagName('*');
	var ip=/\.png/i;
	var al="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='";
	var i = els.length;
	var uels = new Array();
	var c = 0;
	while(i-- > 0){
		if(els[i].className.match(/unitPng/)){
			uels[c] = els[i];
			c++;
		}
	}
	if(uels.length == 0)
		pfx(els);
	else
		pfx(uels);
	function pfx(els){
		i = els.length;
		while(i-- > 0){
			var el = els[i];
			var es = el.style;
			var elc = el.currentStyle;
			var elb = elc.backgroundImage;
			if(el.src && el.src.match(ip) && !es.filter){
				es.height = el.height;
				es.width = el.width;
				es.filter = al + el.src+"',sizingMethod='crop')";
				el.src = clear;
			} else {
				if(elb.match(ip)){
					var path=elb.split('"');
					var rep = (elc.backgroundRepeat == 'no-repeat')?'crop':'scale';
					var elkids=el.getElementsByTagName('*');
					var j=elkids.length;
					es.filter=al+path[1]+"',sizingMethod='"+rep+"')";
					es.backgroundImage='none';
					if(j != 0){
						if(elc.position != "absolute")
							es.position='static';
						while(j-- > 0)
							if(!elkids[j].style.position) elkids[j].style.position="relative";
					} else {
						if(elc.position == "absolute") es.position = "absolute";
					}
				}
			}
		}
	}
}

var maps;var infowindow=new Array();var marker=new Array();var setDirectionMultiple={options:{dirContainer:document.getElementById('dirContainer'),dirService:"",dirRenderer:"",myMarker:"",mapId:"",zoom:"",mapType:"",mapLat:"",mapLon:"",showCursor:"",point:new Array()},set:function(mapId,optiones,zoom,mapType,showCursor){setDirectionMultiple.options.mapId=mapId;setDirectionMultiple.options.zoom=zoom;setDirectionMultiple.options.mapType=mapType;setDirectionMultiple.options.showCursor=showCursor;setDirectionMultiple.options.myMarker=new Array();for(names in optiones){if(names=='point'){for(name in optiones[names]){setDirectionMultiple.options.point[name]=optiones[names][name];for(nameee in optiones[names][name]){if(nameee=='lat'){if(setDirectionMultiple.options.mapLat==""){setDirectionMultiple.options.mapLat=optiones[names][name][nameee];}else{setDirectionMultiple.options.mapLat=(setDirectionMultiple.options.mapLat*1+optiones[names][name][nameee]*1)/2;}}
if(nameee=='lon'){if(setDirectionMultiple.options.mapLon==""){setDirectionMultiple.options.mapLon=optiones[names][name][nameee];}else{setDirectionMultiple.options.mapLon=(setDirectionMultiple.options.mapLon*1+optiones[names][name][nameee]*1)/2;}}
if(nameee=='icon'){if(optiones[names][name]['iconAnchor']!=undefined&&optiones[names][name]['iconSize']!=undefined){var myPoint=optiones[names][name]['iconAnchor'].split(',');var mySize=optiones[names][name]['iconSize'].split(',');setDirectionMultiple.options.point[name]['icon']=new google.maps.MarkerImage(optiones[names][name][nameee],new google.maps.Size(mySize[0]*1,mySize[1]*1),new google.maps.Point(0,0),new google.maps.Point(myPoint[0]*1,myPoint[1]*1));}}}
setDirectionMultiple.options.point[name]['infowindow']='<div class="ballon" id="ballon_'+name+'"><h5>'+setDirectionMultiple.options.point[name]['labelTitle']+'</h5>'+setDirectionMultiple.options.point[name]['infowindow']+'<br /><br />'+setDirectionMultiple.options.myMarker['labelCalculate']+'<br /><a href="javascript:;" onclick="setDirectionMultiple.init(\'<h5>'+setDirectionMultiple.options.point[name]['labelTitle']+'</h5><br />'+setDirectionMultiple.options.myMarker['labelCalculate']+'\', \''+setDirectionMultiple.options.myMarker['labelFrom']+'\', \''+setDirectionMultiple.options.myMarker['labelTo']+'\', \'FROM\', \''+setDirectionMultiple.options.point[name]['lat']+', '+setDirectionMultiple.options.point[name]['lon']+'\','+name+');" title="'+setDirectionMultiple.options.myMarker['labelCalculate']+'"><strong>'+setDirectionMultiple.options.myMarker['labelFrom']+'</strong></a> - <a href="javascript:;" onclick="setDirectionMultiple.init(\'<h5>'+setDirectionMultiple.options.point[name]['labelTitle']+'</h5><br />'+setDirectionMultiple.options.myMarker['labelCalculate']+'\', \''+setDirectionMultiple.options.myMarker['labelFrom']+'\', \''+setDirectionMultiple.options.myMarker['labelTo']+'\', \'TO\', \''+setDirectionMultiple.options.point[name]['lat']+', '+setDirectionMultiple.options.point[name]['lon']+'\','+name+');" title="'+setDirectionMultiple.options.myMarker['labelCalculate']+'"><strong>'+setDirectionMultiple.options.myMarker['labelTo']+'</strong></a></div>';}}else{setDirectionMultiple.options.myMarker[names]=optiones[names];}}
setDirectionMultiple.openMap();},openMap:function(){var latlng=new google.maps.LatLng(setDirectionMultiple.options.mapLat,setDirectionMultiple.options.mapLon);if(setDirectionMultiple.options.mapType==undefined)setDirectionMultiple.options.mapType="ROADMAP";if(setDirectionMultiple.options.showCursor==undefined)setDirectionMultiple.options.showCursor=false;var myOptions={disableDefaultUI:setDirectionMultiple.options.showCursor,mapTypeControl:true,mapTypeControlOptions:{style:google.maps.MapTypeControlStyle.DROPDOWN_MENU},zoom:setDirectionMultiple.options.zoom,center:latlng,mapTypeId:google.maps.MapTypeId[setDirectionMultiple.options.mapType]};maps=new google.maps.Map(document.getElementById(setDirectionMultiple.options.mapId),myOptions);for(punto in setDirectionMultiple.options.point){var myLatLng=new google.maps.LatLng(setDirectionMultiple.options.point[punto]['lat'],setDirectionMultiple.options.point[punto]['lon']);marker[punto]=new google.maps.Marker({position:myLatLng,map:maps,icon:setDirectionMultiple.options.point[punto]['icon'],zIndex:10000});if(setDirectionMultiple.options.point[punto]['infowindow']!=""){infowindow[punto]=new google.maps.InfoWindow({content:setDirectionMultiple.options.point[punto]['infowindow'],maxWidth:350,position:myLatLng,zIndex:100});attachInfoBox(marker[punto],maps,punto);}}},init:function(myText,from,to,direction,latLan,id){for(punto in setDirectionMultiple.options.point){if(punto==id&&document.getElementById('ballon_'+id)){setDirectionMultiple.options.dirService=new google.maps.DirectionsService();setDirectionMultiple.options.dirRenderer=new google.maps.DirectionsRenderer();var myFormDirection,textDirection;if(direction=="FROM"){myFormDirection='<form name="direction" action="javascript:;" method="get"><input id="from-input" type="hidden" value="'+latLan+'" />';myFormDirection+='<input id="to-input" type="text" value="" class="inputE" />';myFormDirection+='<input class="inputB" onclick="setDirectionMultiple.getDirections(document.getElementById(\'from-input\').value, document.getElementById(\'to-input\').value, '+id+');" type="button" value=">" /></form>';textDirection=myText+'<br /><strong>'+from+'</strong> - <a href="javascript:;" onclick="setDirectionMultiple.init(\''+myText+'\', \''+from+'\', \''+to+'\', \'TO\', \''+latLan+'\', '+id+');"><strong>'+to+'</strong></a>'+myFormDirection;}else{myFormDirection='<form name="direction" action="javascript:;" method="get"><input id="from-input" type="text" value="" class="inputE" />';myFormDirection+='<input id="to-input" type="hidden" value="'+latLan+'" />';myFormDirection+='<input class="inputB" onclick="setDirectionMultiple.getDirections(document.getElementById(\'from-input\').value, document.getElementById(\'to-input\').value, '+id+');" type="button" value=">" /></form>';textDirection=myText+'<br /><a href="javascript:;" onclick="setDirectionMultiple.init(\''+myText+'\', \''+from+'\', \''+to+'\', \'FROM\', \''+latLan+'\', '+id+');"><strong>'+from+'</strong></a> - <strong>'+to+'</strong>'+myFormDirection;}
document.getElementById('ballon_'+id).innerHTML=textDirection;}}},getDirections:function(fromStr,toStr,id){var dirRequest={origin:fromStr,destination:toStr,travelMode:google.maps.DirectionsTravelMode.DRIVING,unitSystem:google.maps.DirectionsUnitSystem.METRIC,provideRouteAlternatives:true};setDirectionMultiple.options.dirService.route(dirRequest,setDirectionMultiple.showDirections);},showDirections:function(dirResult,dirStatus){if(dirStatus!=google.maps.DirectionsStatus.OK){if(typeof(dirStatusError)!=undefined){alert(dirStatusError+"\n(Google reports: "+dirStatus+")");}else{alert("The address entered was not found\n(Google reports: "+dirStatus+")");}
return;}
setDirectionMultiple.openMap();$('html,body').animate({scrollTop:$('#'+setDirectionMultiple.options.mapId).offset().top-20},1000);setDirectionMultiple.options.dirRenderer.setMap(null);setDirectionMultiple.options.dirRenderer.setMap(maps);$('#dirContainer').html('');setDirectionMultiple.options.dirRenderer.setPanel(document.getElementById('dirContainer'));setDirectionMultiple.options.dirRenderer.setDirections(dirResult);}};
function attachInfoBox(marker,myMap,id){google.maps.event.addListener(marker,"click",function(){for(infoBox in infowindow){infowindow[infoBox].close();}
infowindow[id].open(myMap,infowindow[id]);});};

var jbase64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(input){var output="";var chr1,chr2,chr3,enc1,enc2,enc3,enc4;var i=0;input=jbase64._utf8_encode(input);while(i<input.length){chr1=input.charCodeAt(i++);chr2=input.charCodeAt(i++);chr3=input.charCodeAt(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64;}else if(isNaN(chr3)){enc4=64;}output=output+this._keyStr.charAt(enc1)+this._keyStr.charAt(enc2)+this._keyStr.charAt(enc3)+this._keyStr.charAt(enc4);}return output;},decode:function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i<input.length){enc1=this._keyStr.indexOf(input.charAt(i++));enc2=this._keyStr.indexOf(input.charAt(i++));enc3=this._keyStr.indexOf(input.charAt(i++));enc4=this._keyStr.indexOf(input.charAt(i++));chr1=(enc1<<2)|(enc2>>4);chr2=((enc2&15)<<4)|(enc3>>2);chr3=((enc3&3)<<6)|enc4;output=output+String.fromCharCode(chr1);if(enc3!=64){output=output+String.fromCharCode(chr2);}if(enc4!=64){output=output+String.fromCharCode(chr3);}}output=jbase64._utf8_decode(output);return output;},_utf8_encode:function(string){string=string.replace(/\r\n/g,"\n");var utftext="";for(var n=0;n<string.length;n++){var c=string.charCodeAt(n);if(c<128)utftext+=String.fromCharCode(c);else if((c>127)&&(c<2048)){utftext+=String.fromCharCode((c>>6)|192);utftext+=String.fromCharCode((c&63)|128);}else{utftext+=String.fromCharCode((c>>12)|224);utftext+=String.fromCharCode(((c>>6)&63)|128);utftext+=String.fromCharCode((c&63)|128);}}return utftext;},_utf8_decode:function(utftext){var string="";var i=0;var c=c1=c2=0;while(i<utftext.length){c=utftext.charCodeAt(i);if(c<128){string+=String.fromCharCode(c);i++;}else if((c>191)&&(c<224)){c2=utftext.charCodeAt(i+1);string+=String.fromCharCode(((c&31)<<6)|(c2&63));i+=2;}else{c2=utftext.charCodeAt(i+1);c3=utftext.charCodeAt(i+2);string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));i+=3;}}return string;}};
