﻿window.onload = function() {
    var pageNumber = window.location.search.substring("1");
    var pageDisplay = /^[1-5]/;
    var section = 1;
    if ((pageDisplay.test(pageNumber))) { section = pageNumber; }

    // display menus and screens
    menuDisplay(section) // creates menu state

    displayNutrition(section);
    displayNews(section);

    // init flash
    if (section == 2) { initFlash(2, 706, 318); } else { initFlash(section, 906, 318); }

    //create buttons
    coupons();
}


function initFlash(section,w,h) {
    var flashvars = {};
    var params = {
	    wmode: "transparent"
    };
    var attributes = {};

    if (!(swfobject.hasFlashPlayerVersion("9.0.0"))) { 
            document.getElementById("flashmovieError").style.display = "block"; 
    } 

    // if home page then display random after first visit
    if (section == 1) { section = convertSection()}
    
    swfobject.embedSWF("swf/flash" + section + ".swf", "flashmovie", w, h, "9.0.0","expressInstall.swf", flashvars, params, attributes);
}

function convertSection () {
    var getId = 1
    var user = ReadCookie("userSession");
    if(user.length == 0) { 
        SetCookie("userSession", "9999999", 0);
    } else {
        var randomnumber=Math.floor(Math.random()*3) + 1
        getId = "1_" + randomnumber;
    } 
    
    return getId;
}


function coupons() {
    document.getElementById("coupon1").onmouseover = function() {  
        document.getElementById("coupon1").src =  "images/coupon1-over.jpg";
        document.getElementById("coupon1").onmouseout = function() {  
            document.getElementById("coupon1").src =  "images/coupon1.jpg";
        }
    }
    
        document.getElementById("coupon2").onmouseover = function() {  
            document.getElementById("coupon2").src =  "images/coupon2-over.jpg";
            document.getElementById("coupon2").onmouseout = function() {  
                document.getElementById("coupon2").src =  "images/coupon2.jpg";
            }
        }
        
        document.getElementById("coupon3").onmouseover = function() {  
            document.getElementById("coupon3").src =  "images/coupon3-over.jpg";
            document.getElementById("coupon3").onmouseout = function() {  
                document.getElementById("coupon3").src =  "images/coupon3.jpg";
            }
        }

        //document.getElementById("couponButton").onmouseover = function() {
        //document.getElementById("couponButton").src = "images/coupon3-over.jpg";
        //document.getElementById("couponButton").onmouseout = function() {
        //document.getElementById("couponButton").src = "images/coupon3.jpg";
        //}
    //}
}

function track_spotlight(spotlightURL) {
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    var linkref = spotlightURL + a + '?';
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", linkref);
    iframe.setAttribute("width", "1");
    iframe.setAttribute("height", "1");
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("display", "none");
    document.body.appendChild(iframe);
}

function menuDisplay(section) {
    switch(section){
        case "1":
            track_spotlight('http://fls.doubleclick.net/activityi;src=1869704;type=homej575;cat=kbu_k776;ord=1;num=');
            dcsMultiTrack('DCS.dcssip', 'www.kixcereal.com', 'DCS.dcsuri', '/home', 'WT.ti', 'Home page');
            break
        case "2":
            dcsMultiTrack('DCS.dcssip', 'www.kixcereal.com', 'DCS.dcsuri', '/nutrition', 'WT.ti', 'Nutrition page');
            track_spotlight('http://fls.doubleclick.net/activityi;src=1869704;type=nutri849;cat=kbu_k044;ord=1;num=');
            break
        case "3":
            track_spotlight('http://fls.doubleclick.net/activityi;src=1869704;type=flavo327;cat=kbu_k943;ord=1;num=');
            dcsMultiTrack('DCS.dcssip','www.kixcereal.com','DCS.dcsuri','/flavors','WT.ti','Flavors page');
            break
        case "4":
            track_spotlight('http://fls.doubleclick.net/activityi;src=1869704;type=about394;cat=kbu_k248;ord=1;num=');
            dcsMultiTrack('DCS.dcssip','www.kixcereal.com','DCS.dcsuri','/aboutkix','WT.ti','About Kix page');
            break
        case "5":
            track_spotlight('http://fls.doubleclick.net/activityi;src=1869704;type=newsf541;cat=kbu_k797;ord=1;num=');
            dcsMultiTrack('DCS.dcssip','www.kixcereal.com','DCS.dcsuri','/inthenews','WT.ti','In The News page');
            break
    }

    
    document.getElementById("holder-" + section).style.backgroundImage =  "url(images/kix.jpg)";
    document.getElementById("menu-" + section).style.backgroundImage =  "url(images/menu-" + section + "_on.gif)";
    menuClear(section);
    // change button display
    if (section == 4) {
      //  switchflavors(section);
      document.getElementById("button1Link").href = "?2";
      document.getElementById("coupon1").src = "images/coupon4.jpg";
      document.getElementById("button1Link").onmouseover = function() {   
            document.getElementById("coupon1").src =  "images/coupon4-over.jpg";
            document.getElementById("button1Link").onmouseout = function() {  
                document.getElementById("coupon1").src =  "images/coupon4.jpg";
            }
        
      }
    }
    
    if (section == 3) {
      //  switchflavors(section);
      document.getElementById("button3Link").href = "?2";
      document.getElementById("coupon3").src = "images/coupon4.jpg";
      document.getElementById("button3Link").onmouseover = function() {   
            document.getElementById("coupon3").src =  "images/coupon4-over.jpg";
            document.getElementById("button3Link").onmouseout = function() {  
                document.getElementById("coupon3").src =  "images/coupon4.jpg";
            }
        
      }
    }
    
    
}

function displayNutrition(section) {
    if (section == 2) {
        document.getElementById("navigate").style.display = "inline";
        document.getElementById("original").onclick = function() {
            document.getElementById("facts").style.display = "block";
            document.getElementById("screen").innerHTML = "<IFRAME id='iframe' SRC='http://www.generalmills.com/~/media/Images/Brands/Nutritional_Images/Big_G/Kix.ashx' WIDTH=450 HEIGHT=100 />"
            document.getElementById("close").onclick = function() { document.getElementById("facts").style.display = "none"}
        }
        document.getElementById("honey").onclick = function() {
            document.getElementById("facts").style.display = "block";
            document.getElementById("screen").innerHTML = "<IFRAME id='iframe' SRC='http://www.generalmills.com/~/media/Images/Brands/Nutritional_Images/Big_G/Honey_Kix.ashx' WIDTH=450 HEIGHT=100 />"
            document.getElementById("close").onclick = function() { document.getElementById("facts").style.display = "none";}
        }
    }  else { document.getElementById("navigate").style.display = "none";  }
}

function displayNews(section){
    if (section == 5) {document.getElementById("news").style.display = "block"; } else {
    document.getElementById("news").style.display = "none";
    
    }
}

function menuClear(curr){ 
    if (curr != 1) {
        document.getElementById("menu-1").style.backgroundImage =  "url(images/menu-1.gif)";
        document.getElementById("holder-1").style.backgroundImage =  "none";
        document.getElementById("holder-1").onmouseover =  function() {
            document.getElementById("menu-1").style.backgroundImage =  "url(images/menu-1_on.gif)";
            document.getElementById("menu-1").onmouseout = function() {
                document.getElementById("menu-1").style.backgroundImage =  "url(images/menu-1.gif)";
            }
        }
    }
    
    if (curr != 2) {
        document.getElementById("menu-2").style.backgroundImage =  "url(images/menu-2.gif)";
        document.getElementById("holder-2").style.backgroundImage =  "none";
        document.getElementById("holder-2").onmouseover =  function() {
            document.getElementById("menu-2").style.backgroundImage =  "url(images/menu-2_on.gif)";
            document.getElementById("menu-2").onmouseout = function() {
                document.getElementById("menu-2").style.backgroundImage =  "url(images/menu-2.gif)";
            }
        }
    }
    
    if (curr != 3) {
        document.getElementById("menu-3").style.backgroundImage =  "url(images/menu-3.gif)";
        document.getElementById("holder-3").style.backgroundImage =  "none";
        document.getElementById("holder-3").onmouseover =  function() {
            document.getElementById("menu-3").style.backgroundImage =  "url(images/menu-3_on.gif)";
            document.getElementById("menu-3").onmouseout = function() {
                document.getElementById("menu-3").style.backgroundImage =  "url(images/menu-3.gif)";
            }
        }
    }
    
    if (curr != 4) {
        document.getElementById("menu-4").style.backgroundImage =  "url(images/menu-4.gif)";
        document.getElementById("holder-4").style.backgroundImage =  "none";
        document.getElementById("holder-4").onmouseover =  function() {
            document.getElementById("menu-4").style.backgroundImage =  "url(images/menu-4_on.gif)";
            document.getElementById("menu-4").onmouseout = function() {
                document.getElementById("menu-4").style.backgroundImage =  "url(images/menu-4.gif)";
            }
        }
    }
    
    if (curr != 5) {
        document.getElementById("menu-5").style.backgroundImage =  "url(images/menu-5.gif)";
        document.getElementById("holder-5").style.backgroundImage =  "none";
        document.getElementById("holder-5").onmouseover =  function() {
            document.getElementById("menu-5").style.backgroundImage =  "url(images/menu-5_on.gif)";
            document.getElementById("menu-5").onmouseout = function() {
                document.getElementById("menu-5").style.backgroundImage =  "url(images/menu-5.gif)";
            }
        }
    }
    

}

function switchflavors(section) {
if (section == 4) { document.getElementById("button1Link").href = "?2"; 
    document.getElementById("coupon1").src = "images/coupon" + 4 + ".jpg";
    document.getElementById("coupon1").onmouseover = function() {  
            document.getElementById("coupon1").src =  "images/coupon" + section + "-over.jpg";
            document.getElementById("coupon1").onmouseout = function() {  
                document.getElementById("coupon1").src =  "images/coupon" + 4 + ".jpg";
            }
        }
 }
 
if (section == 3) { document.getElementById("button3Link").href = "?2"; 
    document.getElementById("coupon3").src = "images/coupon" + 4 + ".jpg";
    document.getElementById("coupon3").onmouseover = function() {  
            document.getElementById("coupon3").src =  "images/coupon" + section + "-over.jpg";
            document.getElementById("coupon3").onmouseout = function() {  
                document.getElementById("coupon3").src =  "images/coupon" + 4 + ".jpg";
            }
        }
 }
}

function ReadCookie(n) {
var cookiecontent = new String();
if(document.cookie.length > 0) {
	var cookiename = n+ '=';
	var cookiebegin = document.cookie.indexOf(cookiename);
	var cookieend = 0;
	if(cookiebegin > -1) {
		cookiebegin += cookiename.length;
		cookieend = document.cookie.indexOf(";",cookiebegin);
		if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
		cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
	}
return unescape(cookiecontent);
} // function ReadCookie()



function SetCookie() {
if(arguments.length < 2) { return; }
var n = arguments[0];
var v = arguments[1];
var d = 0;
if(arguments.length > 2) { d = parseInt(arguments[2]); }
var exp = '';
if(d > 0) {
	var now = new Date();
	then = now.getTime() + (d * 24 * 60 * 60 * 1000);
	now.setTime(then);
	exp = '; expires=' + now.toGMTString();
	}
document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} // function SetCookie()

function ReadCookie(n) {
var cookiecontent = new String();
if(document.cookie.length > 0) {
	var cookiename = n+ '=';
	var cookiebegin = document.cookie.indexOf(cookiename);
	var cookieend = 0;
	if(cookiebegin > -1) {
		cookiebegin += cookiename.length;
		cookieend = document.cookie.indexOf(";",cookiebegin);
		if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
		cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
	}
return unescape(cookiecontent);
} // function ReadCookie()


