﻿
//deleteCookie("pinning");

//start check for message on DOM Loaded
window.addEventListener("DOMContentLoaded", function(){ var alertP= new IEPinningAlert();alertP.init();},false);





function IEPinningAlert(){

var myAlert;
var cookieName="pinning";

this.init=function()
{
     try {
	            if (window.external.msIsSiteMode() == false) {
	              this.start();
	            }
	            else {
	               //Already Pinned
	            }
	        }
	        catch (e) {
	            // Pinning not supported
                  //alert("Pinning non supportato");
	        }


}
  
this.start=function(){
            //if cookie hide message
			var IsCookie = readCookie(cookieName);
			
			if (!IsCookie){
			
                this.createHtml();
			    this.createStyle();
			    myAlert = document.getElementById('alertWrapper');
			    myAlert.style.top = 0 - myAlert.offsetHeight + 'px';
			
				showAlert(); 
			}
};



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);
				
				//Icon
				var attention = document.createElement('img');
				attention.setAttribute('id', 'attention');
				attention.setAttribute('src', '/media/ie.jpg');
				attention.setAttribute('alt', 'IE 9 new functionality!');
				divWrapper.appendChild(attention);
				
				//Message text
                var msg="";
                
                if (navigator.userAgent.indexOf("Windows NT 6.0") !== -1){
                      msg="Aggiungi allo Start Menù il sito";
                  }
               else
                {
                 msg="Questo sito supporta IE9 Trascina la favicon sulla taskbar ! Oppure aggiungilo allo Start Menù";
                }
				
                var p = document.createElement('p');
				p.appendChild(document.createTextNode(msg));
				divWrapper.appendChild(p);
				
				
				//add to start menu
				var ul = document.createElement('ul');
				divWrapper.appendChild(ul);
				
				//ICON 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', 'Aggiungi allo Start Menù di Windows' + browser);
					a.setAttribute('alt', 'Aggiungi allo Start Menù di Windows');	
					//a.setAttribute('target', '_blank');
					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(cookieName,'ok',1);
					}
				divWrapper.appendChild(closeButton);
				
				var spanButton = document.createElement('span');
				spanButton.appendChild(document.createTextNode('Non ricordarmi'));
				closeButton.appendChild(spanButton);
			}
		
			//create style of alert
			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;padding-top:33px }\n';
				css += '#alertWrapper{ position:absolute; top:0; left:0; background: url(/media/bg.jpg) repeat-x 0 0; height:33px; width:100%; text-align:center;z-index:10}\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(/media/close.jpg) no-repeat top right; border:0; cursor:pointer;}\n';
				
				style.styleSheet.cssText = css;
			}
			
			//show alert with anim
			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 
				}
			}
			
			//Hide alert with anim
			function hideAlert() {
				if(parseInt(myAlert.style.top) > (0 - myAlert.offsetHeight)){
					document.getElementsByTagName("body")[0].style.paddingTop='0px';
					myAlert.style.top = parseInt(myAlert.style.top) - 3 + 'px';
					setTimeout(hideAlert,100); // call hideAlert 
				}
			}	
			
			//icon array -> 'name' : 'url download, url immage'
			var browserArray = { 
				'Windows Start Menu' : 
					'javascript:window.external.msAddSiteMode(), /media/add.gif', 
			};
			
			

}

  //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 deleteCookie(name) {
				createCookie(name,"",-1);
			}
	
