//List of function that shall be performed onload
addOnload(initOne);
addOnload(initTwo);
addOnload(initThree);

/////////////////////////////////////////////////////////////////////////////////////
//this function verifies that all functions from the above list would be performed.
function addOnload(newFunction) {
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) {
				oldOnload();
			}
			newFunction();
		}
	}
	else {
		window.onload = newFunction;
	} 
}
/////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////
//this function is responsible for download and purchase buttons rollover
function initOne() {
	for (var i=0; i<document.images.length; i++) {
		if (document.images[i].parentNode.tagName == "A") { //if image has a link
			 //if image id fits
			if(document.images[i].id == "downloadUp" || 
				document.images[i].id == "downloadDown" ||
				document.images[i].id == "purchaseUp" ||
				document.images[i].id == "purchaseDown" ||
				document.images[i].id == "join"){
				setupRollover(document.images[i]);	
			}
		}
	}
}

function setupRollover(thisImage) {
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	thisImage.onmouseout = rollOut;

	thisImage.overImage = new Image();
	if (thisImage.id == "downloadUp" || thisImage.id == "downloadDown") {
		thisImage.overImage.src = "images/button_download1.png";	
	}
	else if (thisImage.id == "join"){
		thisImage.overImage.src = "images/button_affiliate1.png";
	}
	else {
		thisImage.overImage.src = "images/button_purchase1.png"; 
	}
	thisImage.onmouseover = rollOver;	
}

function rollOut() {
	this.src = this.outImage.src;
}

function rollOver() {
	this.src = this.overImage.src;
}

//Rollover - END
/////////////////////////////////////////////////////////////////////////////////////



function initTwo() {        //Cookies handler
    if (navigator.cookieEnabled) {
        var docTitle = document.title;  
        if (docTitle == "Downloading Acronyms Master") {
            makeCookie();
        }
        else if (docTitle == "Acronyms Master - order") {
            readCookie();
        }
    }

}




function makeCookie() {
    if (! document.cookie) {            //If cookie doesn't exist    
        var cookieName = "AMdeal"       //Indicates cookie name
        var cookieType = "1";           //Indicates cookie type
        var discountStatus = "notStartedYet";       //Indicates if discount was offered
        var discountStartTime = "0";    //Indicates when discount started
        var discountEndTime = "0";     //Indicates when discount duration
        
        var downloadTime = new Date();  //indicates download time
        
        
        
        var expireDate = new Date();    //indicates cookie expire date
        expireDate.setMonth(expireDate.getMonth() + 24);
       
    
        document.cookie = 
            cookieName + "=" + 
            cookieType + "|" + 
            discountStatus + "|" + 
            downloadTime.getTime() + "|" +
            discountStartTime + "|" + 
            discountEndTime +
            ";path=/;expires=" +
            expireDate.toGMTString();

    }
}

function readCookie() {  
    if (document.cookie !="") {                             //if cookie exists 
        var thisCookie = document.cookie.split(";");        //split cookie
        var entireCookie = thisCookie[0].split("=");        //split cookie to name and value
        
        if (entireCookie[0]=="AMdeal") {                     //if the name of the cookie matches
            var valueOfCookie = entireCookie[1].split("|");
            if (valueOfCookie.length==5) {
                var cookieType = valueOfCookie[0];
                var discountStatus = valueOfCookie[1];    
                var downloadTime = new Date();              //indicates download date
                var downloadTime = valueOfCookie[2];      
                var discountStartTime = valueOfCookie[3];
                var discountEndTime = valueOfCookie[4];
                
                            
                
                if (cookieType == 1) {                      //if type of the cookie is 1 -> continue
                    if (discountStatus == "notStartedYet") {              //if discount was timed out, then exit 
                        //Calculating number of days from the download
                        var currentTime = new Date();
                        var timeFromDownload = currentTime.getTime() - downloadTime;    //time from download in miliseconds
                        var timeFromDownload = Math.floor(timeFromDownload / 1000);     //time from download in sec
                        var sec = timeFromDownload % 60; timeFromDownload = Math.floor(timeFromDownload / 60);  //seconds
                        var min = timeFromDownload % 60; timeFromDownload = Math.floor(timeFromDownload / 60);  //minutes
                        var hr = timeFromDownload % 24; timeFromDownload = Math.floor(timeFromDownload / 24);  //hours
                        var days = timeFromDownload;
                        
                        if (days > 60) {      //if 2 month period from download is over
                            //Calculating time for discount offer
                            var currHR = currentTime.getHours();
                            var endTime = new Date();
                            endTime.setHours(currHR + 3, 0, 0, 0);        //setting offer end time
                            discountEndTime = Date.parse(endTime);      //storing offer end time in miliseconds
                            discountStatus = "offered"                  //updating offer status
                            discountStartTime = Date.parse(currentTime);  //storing offer start time in miliseconds
                            
                            
                            var expireDate = new Date();    //indicates cookie expire date
                            expireDate.setHours(expireDate.getHours() + 4); //4 hours for offer
                            var cookieName = "AMdeal"       //Indicates cookie name
                            
                            document.cookie =
                                cookieName + "=" + 
                                cookieType + "|" + 
                                discountStatus + "|" + 
                                downloadTime + "|" +
                                discountStartTime + "|" + 
                                discountEndTime +
                                ";path=/;expires=" +
                                expireDate.toGMTString();  
                                
                            
                            displayCounter(discountEndTime);    //display countdown
                            
                        }
                    }
                    else if (discountStatus == "offered") {
                        var currentTime = new Date();                      
                        var timeLeft = discountEndTime - currentTime.getTime(); //check if time left for an offer
                        if (timeLeft > 0) {
                            
                            displayCounter(discountEndTime);    //display countdown
                 
                        }                      
                        else {  //delete cookie
                            
                            var cookieName = "AMdeal"       //Indicates cookie name
                            document.cookie = cookieName + "=0;path=/;expires=;"   
                        }
                        
                        
                    }
                }
                
            }
            
        }

    }
    
}

function displayCounter(discountEndTime) { 

    
    var currentTime = new Date();                      
    var timeLeft = discountEndTime - currentTime.getTime();
    
    if (timeLeft > 0) {
        var timeLeft = Math.floor(timeLeft / 1000);     //time left in sec
        var secLeft = timeLeft % 60; timeLeft = Math.floor(timeLeft / 60);  //seconds
        var minLeft = timeLeft % 60; timeLeft = Math.floor(timeLeft / 60);  //minutes
        var hrLeft = timeLeft % 24; timeLeft = Math.floor(timeLeft / 24);  //hours
        var daysLeft = timeLeft;
        
        if (daysLeft == 0 && hrLeft < 5) {
            if (minLeft < 10) {
                var minutes = ":0" + minLeft;     
            }
            else {
                var minutes = ":" + minLeft;
            }
            if (secLeft < 10) {
                var seconds = ":0" + secLeft;     
            }
            else {
                var seconds = ":" + secLeft;
            }
            var offerExpiresAt = new Date();
            offerExpiresAt.setTime(discountEndTime);
            var offerExpireDate = offerExpiresAt.toLocaleDateString();
            var offerExpireTime = offerExpiresAt.toLocaleTimeString();
            
            var promoDIV = document.getElementById("promo");
            promoDIV.className = "promoShown";
            promoDIV.style.height = 120;
    
            promoDIV.innerHTML = 
                    /*box border*/
	                "<div id=\"lb\"><div id=\"rb\"><div id=\"bb\"><div id=\"blc\"><div id=\"brc\"><div id=\"tb\"><div id=\"tlc\"><div id=\"trc\">"  +               
	                /*----------*/
            				
	                "<div id=\"promoContent\">" +
	                	
		                "<p>For a limited time only, SBS Labs is offering a discount of 30%</p>"  +
		                
		                "<div id=\"blueText\">Offer expires <div id=\"redText\">" + offerExpireDate + ", " + offerExpireTime + "</div></div>"  +         			
            				
	                "</div>"    +
	                
	                "<div id=\"promoTimer\">" +
	                
	                    hrLeft + minutes + seconds + 
	                
	                "</div>"    +
            		
	                /*end of box border*/
	                "</div></div></div></div></div></div></div></div>"                
	                 /*----------*/ 
	                 
	              
            document.getElementById("proPrice").innerHTML = "<strong><del><font color=#333>$85</font></del><br />$59.5</strong>"
            document.getElementById("litePrice").innerHTML = "<strong><del><font color=#333>$49</font></del><br />$34.3</strong>"
            document.getElementById("Price5").innerHTML = "<strong><del><font color=#333>$79.9</font></del><br />$55.9</strong>"
            document.getElementById("Price10").innerHTML = "<strong><del><font color=#333>$74.9</font></del><br />$52.4</strong>"
            document.getElementById("Price25").innerHTML = "<strong><del><font color=#333>$69.9</font></del><br />$48.9</strong>"
            document.getElementById("Price100").innerHTML = "<strong><del><font color=#333>$64.9</font></del><br />$45.4</strong>"
            document.getElementById("Price1000").innerHTML = "<strong><del><font color=#333>$59.9</font></del><br />$41.9</strong>"
                
            
            /*Replace Links href*/
            var allLinks = document.getElementsByTagName("a");
	        for (var i=0; i<allLinks.length; i++) {
		        if (allLinks[i].className.indexOf("plimus") > -1) {
			        var oldHREF = allLinks[i].href;
			        allLinks[i].href = oldHREF + "&couponCode=SpecialOffer";
		        }
	        }
	        
	        /*Replace Buttonns href*/ 
	       
           document.getElementById("orderButton1").onclick = function(){
                window.location.href = 'https://www.plimus.com/jsp/buynow.jsp?contractId=1911706&couponCode=SpecialOffer';
           }
           document.getElementById("orderButton2").onclick = function(){
                window.location.href = 'https://www.plimus.com/jsp/buynow.jsp?contractId=2206066&couponCode=SpecialOffer';
           }
               
                        
            setTimeout("displayCounter(" + discountEndTime + ")",1000);
               
        }    
    }
    else {
        var promoDIV = document.getElementById("promo");
        promoDIV.className = "promoHidden";
        promoDIV.innerHTML = "";
         document.getElementById("proPrice").innerHTML = "<strong>$85</strong>"
            document.getElementById("litePrice").innerHTML = "<strong>$49</strong>"
            document.getElementById("Price5").innerHTML = "<strong>$79.9</strong>"
            document.getElementById("Price10").innerHTML = "<strong>$74.9</strong>"
            document.getElementById("Price25").innerHTML = "<strong>$69.9</strong>"
            document.getElementById("Price100").innerHTML = "<strong>$64.9</strong>"
            document.getElementById("Price1000").innerHTML = "<strong>$59.9</strong>"
            
            /*Replace Links href*/
            var allLinks = document.getElementsByTagName("a");
	        for (var i=0; i<allLinks.length; i++) {
		        if (allLinks[i].className.indexOf("plimus") > -1) {
			        var oldHREF = allLinks[i].href.split("&couponCode=");
			        allLinks[i].href = oldHREF[0];
		        }
	        }
    }
      
}


////////////////////////////////Cookies hanler - END////////////////////////////////////////



















function initThree() {

}

