﻿var fromFlash = false; 
var cookieSet = false;
var cleaningUrl = false;
//var firstDeepLink = "#the-ale";

function historyHandler () {      
    // this is the public method that will be called to add an entry
    this.addHistory = function(data) {
        if(cookieSet == false){
            document.cookie = "authorized=true; path=/";
            cookieSet = true;
        }
        fromFlash = true;
        if (firstDeepLink != ""){
            pushDeepLink (firstDeepLink);
            firstDeepLink = "";
        } else{
            unFocus.History.Keeper.addHistory(data);
        }
    };
    this.writeTitle = function(title) {
        document.title = unescape(title);
    }
    // the method that will take updates from the History Keeper
    this.onHistoryChange = function(data) {
        if (data != null && !fromFlash){
            pushDeepLink (data);
            fromFlash = true;
        }
        fromFlash = false;
        //xiti calls
    };
    // add the event listener
    // A private method would not be properly scoped, and could not update state.
    unFocus.History.Keeper.addEventListener('historyChange', this.onHistoryChange);
    this.getCurrentState = function() {
        //always was here alert(state);
    };

    //setCookie method for internal Flash pages
    /*
    this.setCookie = function(lang){
        document.cookie = "lang="+lang+"; expires="+expireOffset+"; path=/;";
        goLocation(lang);
    }
    */

    this.setCookie = function(deeplink, lang){
        document.cookie = "lang="+lang+"; expires="+expireOffset+"; path=/;";
        goLangDeeplink(deeplink);
    }   

    // These page tracking functions must live inside the HistoryHandler object
    // in order to prevent a Flash/JS display bug in IE6
    this.pageTrack = function(xiti_section, xiti_page, spotlight_section, spotlight_page) {
        pageTrack(xiti_section, xiti_page, spotlight_section, spotlight_page);
    };
    
    this.xitiDownloadClick = function(section, url, popup) {
        xitiDownloadClick(section, url, popup, "2");
    };
    
    this.xitiExitClick = function(section, url, popup) {
        xitiExitClick(section, url, popup, "2");
    };
    
    this.xitiFlashEvent = function(section, name) {
        xitiFlashEvent(section, name);
    };
    
    this.xitiFlashMediaEvent = function(section, medianame, control, seconds, total) {
        xitiFlashMediaEvent(section, medianame, control, seconds, total);
    };

    this.xitiNavClick = function(section, trackString, targetUrl) {
        xitiNavClick(section, trackString, "2", null, targetUrl);
    };

    this.disableFlash = function() {
        document.cookie = "disableFlash=true; path=/";
        document.location.href = document.location.href;
        //send a xiti/omniture hit to record flash disabling 
        xitiNavClick("flash_disabled", "flash_disabled", "2", null, document.location.href);
    };
    this.recordSlowConnectionMetrics = function(loadTime, seenSlowMessage){
        //send a xiti/omniture hit to record slow connection
        xitiFlashSlowConnection(loadTime, seenSlowMessage);
    };
    this.trackPage = function(pageName, channel){
        try{track(pageName, channel);}catch(e){}
    };
    this.setZipCode = function(zipCode){
        try{recordZip(zipCode);}catch(e){}
    };
};
historyHandler = new historyHandler ();
function getSWF (movieName) {
if(fla!='flash=no'){
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
}
function pushDeepLink (id) {
try{
if(fla!='flash=no'){
    if(fo.installedVer.major>=8){
          getSWF("loader").pushDeepLink(id);
    }
}
}catch(e){
  getSWF("loader").pushDeepLink(id);  
}
}

var url = window.location.href;
var baseTemp = url.split('/');
var base = "";
for(var i=0;i<3;i++){
    base+=baseTemp[i]+"/";
}
var ext = ".aspx";
var fla = "";
var deeplink = "";
var urlUpdate = "";
var hashIndex = url.indexOf('#');
var cleanUrl = "";
if (hashIndex > 0 && url.lastIndexOf('?') > 0){
    var hash = url.substring(url.indexOf('#')+1,url.length);
    cleanUrl = url.substring(0, url.lastIndexOf('?')) + "#" + hash;
} else if (url.lastIndexOf('?') > 0){
    cleanUrl = url.substring(0, url.lastIndexOf('?'));
}

if (cleanUrl != "" && window.location.href != cleanUrl){
    cleaningUrl = true;
    window.location.href = cleanUrl;
}
if (url.indexOf('#') > 0){
    firstDeepLink = url.substring(url.indexOf('#')+1,url.length);
}

///en/media/swf/index.swf will now be pulled by proxy.swf and located on Akamai
if(fla!='flash=no'){

    var browserUserAgent = navigator.userAgent;
    //if (window.console){console.log("userAgent = "+browserUserAgent);}

    ///en/media/swf/proxy.swf
    ///en/media/swf/proxy.swf
    var fo = new SWFObject("/pub/preloader.swf", "loader", "100%", "100%", "8", "#2c0303", true);
    if(document.cookie.match("s_cc=true") && document.cookie.match("authorized=true")){
        fo.addVariable("legalage", "1");
    }  
    fo.addParam("allowScriptAccess", "always");
    fo.addParam("wmode", "window");
    fo.addParam("scale", "showall");
    fo.addParam("fullscreen", "true");
    fo.addVariable("baseTitle", baseTitle);
    fo.addVariable("breadcrumbHomeText", breadcrumbHomeText);
    fo.addVariable("browserUserAgent", browserUserAgent);
    fo.setAttribute('xiRedirectUrl', url);
    
    //fo.addVariable("countDown", countDown);
    if(fo.installedVer.major>=9){
        flashWritten = true; //required for omniture & xiti
        $('container').style.display="block";
        $('flashcontent').style.height="100%";
        $('flashcontent').style.width="100%";
        //$('flashwarning').style.visibility="hidden";
        $('htmlcontainer').style.display="none";
        if (!cleaningUrl){
            fo.write("flashcontent");
        }
    } else {
        //$("flashwarning").className="flash";
        //$("downloadflash").style.visibility = "visible";
        //$("flashwarning").style.display = 'block'; 
    }
    
}else{
    //$("flashwarning").className="flash";
    $("downloadflash").style.visibility = "visible";
    $('flashcontent').style.display='none';
    //$("flashwarning").style.display = 'block'; 
    flash = false;
}

var width;
var height;
function getScreenSize(){
 if (self.innerHeight) {    // all except Explorer
        width = self.innerWidth;
        height = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    }
}
window.onload = window.onresize = function(){
    try{$('loader').focus();}catch(e){}
    getScreenSize();
    if (height < 768){
        $('container').style.height = 768 + "px";
    }else{
        $('container').style.height = "100%";
    }
    
    if (width < 1024){
        $('container').style.width = 1024 + "px";
    }else{
        $('container').style.width = "100%";
    }
    try{document.title = "Budweiser American Ale";}catch(e){}
}