/*
	Copyright: mtv.it
	Adapted from mtv.it
	Modified by ICCK NET S.A.
*/

try
{		
	if (window.external.msIsSiteMode() != true ) {
		//imposto funzioni cookies 
		function createCookie(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}
		
		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
		
		function eraseCookie(name) {
			createCookie(name,"",-1);
		}
	/* ====================================================================
					Dom Ready Function - DO NOT TOUCH THIS
	==================================================================== */ 

	(function(){
	
		var DomReady = window.DomReady = {};
		var userAgent = navigator.userAgent.toLowerCase();
		var browser = {
			version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
			msie: (/msie/.test(userAgent))
		};    
	
		var readyBound = false;	
		var isReady = false;
		var readyList = [];
	
		// Handle when the DOM is ready
		function domReady() {
			// Make sure that the DOM is not already loaded
			if(!isReady) {
				// Remember that the DOM is ready
				isReady = true;
			
				if(readyList) {
					for(var fn = 0; fn < readyList.length; fn++) {
						readyList[fn].call(window, []);
					}
				
					readyList = [];
				}
			}
		};
	
		function addLoadEvent(func) {
		  var oldonload = window.onload;
		  if (typeof window.onload != 'function') {
			window.onload = func;
		  } else {
			window.onload = function() {
			  if (oldonload) {
				oldonload();
			  }
			  func();
			}
		  }
		};

		function bindReady() {
			if(readyBound) {
				return;
			}
		
			readyBound = true;

			// Continually check to see if the document is ready
			if (browser.msie && window == top) (function(){
				if (isReady) return;
				try {
					// If IE is used, use the trick by Diego Perini
					// http://javascript.nwbox.com/IEContentLoaded/
					document.documentElement.doScroll("left");
				} catch(error) {
					setTimeout(arguments.callee, 0);
					return;
				}
				// and execute any waiting functions
				domReady();
			})();
	
			// A fallback to window.onload, that will always work
			addLoadEvent(domReady);
		};
	
		// This is the public function that people can use to hook up ready.
		DomReady.ready = function(fn, args) {
			// Attach the listeners
			bindReady();
		
			// If the DOM is already ready
			if (isReady) {
				// Execute the function immediately
				fn.call(window, []);
			} else {
				// Add the function to the wait list
				readyList.push( function() { return fn.call(window, []); } );
			}
		};
		
		bindReady();
		
	})();

	/* ====================================================================
							End Dom Ready Function
	==================================================================== */ 

	

	DomReady.ready(function() {
		var ieAlert = new IEPinningAlert();
	});

	
	//Alert "Esegui Pinning"
	function IEPinningAlert(){
		
		//funzione che crea l'html
		this.createHtml = function(){
			
			function getBrowserLink(str){
				var tmp = str.split(',');
				return tmp[0];
			}
			
			function getBrowserImg(str){
				var tmp = str.split(',');
				return tmp[1];
			}		
			
			//Div alertWrapper
			var divWrapper = document.createElement('div');
			divWrapper.setAttribute('id', 'alertWrapper');
			document.getElementsByTagName('body')[0].appendChild(divWrapper);
			
			//Icona cattura attenzione
			var attention = document.createElement('img');
			attention.setAttribute('id', 'attention');
			attention.setAttribute('src', '/sites/elespectador.com/themes/elespectador/images/ie.jpg');
			attention.setAttribute('alt', 'Nueva funcionalidad de IE 9!');
			divWrapper.appendChild(attention);
			
			//Message text
			var p = document.createElement('p');
			p.appendChild(document.createTextNode("Arrastre el icono de elespectador.com a la barra de tareas para tener acceso directo a las \xfaltimas noticias."));
			divWrapper.appendChild(p);
			
			
			//add to start menu
			var ul = document.createElement('ul');
			divWrapper.appendChild(ul);
			
			//ICONA items with links and images 
			for (var browser in browserArray) {  
				var li = document.createElement('li');
				ul.appendChild(li);	
			
				var a = document.createElement('a');
				a.setAttribute('href', getBrowserLink(browserArray[browser]));
				a.setAttribute('title', 'Agregue elespectador.com al menu inicio de Windows');
				a.setAttribute('alt', 'Agregue elespectador.com al menu inicio de Windows');
				li.appendChild(a);

				var img = document.createElement('img');
				img.setAttribute('src', getBrowserImg(browserArray[browser]));
				img.setAttribute('alt', browser);
				a.appendChild(img);
			}  
			
			var closeButton = document.createElement('button');
			closeButton.setAttribute('type','button');
			closeButton.onclick = function(){
				this.parentNode.style.display = 'none';
				createCookie('pin','ok',1);
				}
			divWrapper.appendChild(closeButton);
			
			var spanButton = document.createElement('span');
			spanButton.appendChild(document.createTextNode('No recordar'));
			closeButton.appendChild(spanButton);
		}
	
		//funzione che crea lo style
		this.createStyle = function(){
			var style = document.createElement('style');
			style.setAttribute("type", "text/css");
			document.getElementsByTagName('head')[0].appendChild(style);
			
			// css style
			var css = 'body{ margin:0; }\n';
			css += '#alertWrapper{ position:absolute; top:0; left:0; background: url(/sites/elespectador.com/themes/elespectador/images/bg.jpg) repeat-x 0 0; height:33px; width:100%; text-align:center;}\n';
			css += '#alertWrapper img#attention{ padding:0 0 0 20px; display:inline; float:left;}\n';
			css += '#alertWrapper p{color:#000; font: bold 11px Arial, Helvetica, sans-serif; line-height:33px; display:inline; margin: 0 10px; float:left;  }\n';
			css += '#alertWrapper ul{ list-style:none; margin:0; padding:0; float:left;}\n';
			css += '#alertWrapper ul li { float:left; color:#FFFFFF; margin:0; padding:0;}\n';
			css += '#alertWrapper ul li a {outline:none;}\n';
			css += '#alertWrapper ul li a img { border:0;margin-top:8px;}\n';
			css += '#alertWrapper span {width:100px!important; height:21px;position:absolute;right:10px;top:6px;font-size:10px;color:#003399; }\n';
			css += '#alertWrapper button { position:absolute; top:6px; right:5px; width:100px; height:21px; background:url(/sites/elespectador.com/themes/elespectador/images/closeie9.jpg) no-repeat top right; border:0; cursor:pointer;}\n';
			
			style.styleSheet.cssText = css;
		}
		
		//mostra l'alert con animazione
		function showAlert() {
			if(parseInt(myAlert.style.top) < 0){
				//myAlert.style.top = parseInt(myAlert.style.top) + 2 + 'px';
				myAlert.style.top = '0px';
				setTimeout(showAlert,100); // call showAlert
			}
			else{
				setTimeout(hideAlert,20000);// call hideAlert 
			}
		}
		
		//nasconde l'alert con animazione
		function hideAlert() {
			if(parseInt(myAlert.style.top) > (0 - myAlert.offsetHeight)){
				myAlert.style.top = parseInt(myAlert.style.top) - 3 + 'px';
				setTimeout(hideAlert,100); // call hideAlert 
			}
		}	
		
		//icone array -> 'nome browser' : 'url download, url immagine'
		var browserArray = { 'Windows Start Menu' : 'javascript:window.external.msAddSiteMode(), /sites/elespectador.com/themes/elespectador/images/add.gif'};
		this.createHtml();
		this.createStyle();
		var myAlert = document.getElementById('alertWrapper');
		myAlert.style.top = 0 - myAlert.offsetHeight + 'px';
		var trovoCook = readCookie('pin')
		
		//se trovo il cookie nascondo il maessaggio per tutto il giorno
		if (trovoCook){
			hideAlert();
		}
		else {
			showAlert();
		}
	}
   }
}catch(e) { }

