

//default sizes of window
var MINWIDTH = 960;
var MAXWIDTH = 1280;
var MINHEIGHT = 550;
var MAXHEIGHT = 670;
var MARGINTOPOFFSET = -30;
var MARGINLEFTOFFSET = -20;
var marginTop = 0;
var marginLeft = 0;
var topAlign=285;
var leftAlign=480;
var FOOTERHEIGHT = 20;
//might need to change this
var TOPPADDING=20;
var LEFTPADDING=20;
var flashLeft = 0;
var flashTop = 0;
var flash = true;
var width, height;
var maxWidthLeftOffset = 0; // the difference between the real width and the MAXWIDTH
var windowHeight = 0; // the full height of the browser window
var windowWidth = 0; // the full width of the browser window

var url = window.location.href;
baseTemp = url.split('/');
base="";
for(var i=0;i<3;i++){
    base+=baseTemp[i]+"/";
}
fla = url.substring(url.lastIndexOf('?')+1,url.lastIndexOf('?')+9);
lang = baseTemp[3];
var onloadHandlers = [];
var onresizeHandlers = [];

onloadHandlers[onloadHandlers.length] = 'showScreenSize()';
onresizeHandlers[onresizeHandlers.length] = 'showScreenSize()';
window.onload = function(){
    for(var i=0;i<onloadHandlers.length;i++){
            eval(onloadHandlers[i]);
    }

}
window.onresize = function(){
        for(var i=0;i<onresizeHandlers.length;i++){
        eval(onresizeHandlers[i]);
    }
}

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;
    }
    windowHeight = height;
    windowWidth = width;
    
    if (width <= MINWIDTH){
        marginLeft = LEFTPADDING + MARGINLEFTOFFSET;
        leftAlign = width/2;
        width = MINWIDTH;
    }else{
        if (width >= MAXWIDTH){
            maxWidthLeftOffset = (width-MAXWIDTH)/2;
            width = MAXWIDTH;
        }
        marginLeft = ((width-MINWIDTH)/2)+LEFTPADDING + MARGINLEFTOFFSET;
        leftAlign = width/2;
    }
    if (height <= MINHEIGHT ){
        marginTop = TOPPADDING + MARGINTOPOFFSET;
        topAlign = height/2;
        height = MINHEIGHT;
        footerTop = height-topAlign;
        breadcrumbTop = 0;
    }else{
        if (height >= MAXHEIGHT){
            height = MAXHEIGHT;
            breadcrumbTop = 0;
        }
        marginTop = ((height-MINHEIGHT)/2)+TOPPADDING + MARGINTOPOFFSET;
        topAlign = height/2;
        footerTop = height-topAlign-FOOTERHEIGHT;
    }     
    
}
function showScreenSize(){
    
    getScreenSize();
    
    //fix, shrink width by 1 pixel
    if (window.XMLHttpRequest) {
        width = width - 1;
        height = height -1;
    }         

    var elemFrame = $('frame');
    elemFrame.style.width=width+"px";
    elemFrame.style.height=height+"px";
    elemFrame.style.left=-leftAlign+"px";
    elemFrame.style.top=-topAlign+"px";
   
    var elemFooter = $('footer');
    elemFooter.style.width=width+"px";
    elemFooter.style.left=-leftAlign+"px";
    elemFooter.style.top=footerTop+"px";
    
    var elemContent = $('content');
    elemContent.style.marginTop=marginTop+"px";
    elemContent.style.marginLeft=marginLeft+"px";

    var elemBreadcrumb = $('breadcrumb-links');
    elemBreadcrumb.style.left=(marginLeft)+"px";
    
    //var elemUtility = $('utility-links');
    //elemUtility.style.right=(marginLeft-LEFTPADDING)+"px";
    
    if(flash == true){
        try{
            getSWF("loader").SetVariable("htmlHeight", height);
            getSWF("loader").SetVariable("htmlWidth", width);
        }catch(e){}
    }
}

var qs = window.location.href;
fla = qs.substring(qs.lastIndexOf('?')+1,qs.lastIndexOf('?')+9);
/*flash update callback functions*/
function installCancelledRedirect() {
    //$("no-flash").style.display="block"; //'no-flash' is HTML option parent div
    //$("upgrade-flash").style.display="none"; //'upgrade-flash' is install flash div
    window.location.href = '/'+lang+'/download-flash.jsp?ref='+url;
}
function installFailedRedirect() {
    window.location.href = '/'+lang+'/download-flash.jsp'; //redirected to page that contains link to download and a link back to page (with a param to ensure not to flash detect)
}

/* DOM */
// Safari DOM workaround by Johnnie Blevins
// http://www.blevins.cx/javascripting/DOMParsing.html
if (!String.prototype.toDocumentFragment) {
    String.prototype.toDocumentFragment = function toDocumentFragment(context) {
        var aRange=document.createRange();
        context= context || document.getElementsByTagName("body")[0];
        aRange.selectNodeContents(context);
        return aRange.createContextualFragment(this);
    }
}

onloadHandlers[onloadHandlers.length] = 'killFlickerOnResize()';
function killFlickerOnResize(){
    var boddyArray = [];
    bodyArray = document.getElementsByTagName('body');
    bodyArray[0].style.visibility='visible';
}

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


/******************/
/* Page Tracking */
/******************/
function pageTrack(xiti_section, xiti_page, spotlight_section, spotlight_page) {
    if (((xiti_section) && (xiti_page)) &&
        ((xiti_section!=null) && (xiti_page!=null)) &&
        ((xiti_section!="null") && (xiti_page!="null")) &&
        ((xiti_section!=undefined) && (xiti_page!=undefined)) &&
        ((xiti_section!="undefined") && (xiti_page!="undefined")) &&
        ((xiti_section!="") && (xiti_page!=""))) {
        xitiFlashEvent(xiti_section, xiti_page);
    }
    if (((spotlight_section) && (spotlight_page)) && 
        ((spotlight_section!=null) && (spotlight_page!=null)) &&
        ((spotlight_section!="null") && (spotlight_page!="null")) &&
        ((spotlight_section!=undefined) && (spotlight_page!=undefined)) &&    
        ((spotlight_section!="undefined") && (spotlight_page!="undefined")) &&
        ((spotlight_section!="") && (spotlight_page!=""))) {
        spotlightEvent(spotlight_section, spotlight_page);
    }
}
/* XiTi */
// wait until the XiTi hit has been completed before changing window location
function redirectAfterXiti(url){
    var img_is_loaded = 0;
    var redirect = function() {
        if (img_is_loaded==0){
            document.location = url;
            img_is_loaded = 1;
        }
    }
    xt_img.onload = function(){redirect;}
    self.setTimeout(redirect, 2000)
}
function xitiDownloadClick(section, url, popup, site_version) {
    if ((popup) && (popup!="'false'")) {
        xt_med("C", section, url, "T", url, 1, null, null, site_version);
    }
    else {
        xt_med("C", section, url, "T", null, null, null, null, site_version);
        if (window.ActiveXObject){ // if this IE6 or IE 7, do not wait for the XiTi request to be completed
            document.location = url;
        } else {
            redirectAfterXiti(url);
        }        
    }
}
function xitiExitClick(section, url, popup, site_version, destination_type) {
    if ((popup) && (popup!="'false'")) {
        xt_med("C", section, url, "S", (popup == "''" ? null : url), 1, destination_type, null, site_version);
    }
    else {
        xt_med("C", section, url, "S", null, null, destination_type, null, site_version);
        redirectAfterXiti(url);
    }
}
function xitiNavClick(section, name, site_version, url, targetUrl) {
    xt_med("C", section, name, "N", null, null, null, (targetUrl? targetUrl : url), site_version);
    // if this is HTML, redirect only after the XiTi hit has been completed
    if ((site_version=="1") && (url)){
        redirectAfterXiti(url);
    }
}
function xitiNavClick2(section, name, site_version, url, targetUrl) {
    xt_med("C", section, name, "N", url, 1, null, (targetUrl? targetUrl : url), site_version);
}
function xitiFlashEvent(section, name) {
    xt_med("F", section, name, null, null, null, null, null, "2");
}
function xitiFlashMediaEvent(section, medianame, control, seconds, total) {
    if ((control == "play") && (seconds == "0")) {
        xitiFlashEvent(section, medianame);
    }
    var mediaformat = parseInt(section) + 100;
    xt_med("M", mediaformat, medianame, control, seconds, "4", "2", "5", "2", total);
}
function xitiFlashSlowConnection(loadTime, seenSlowMessage) {
    xt_med("slow", loadTime, seenSlowMessage, "slow");
}
function setupXitiOnclicks(){
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("rel") != null){
        if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="link-external")) {
            anchor.onclick = function(){
                xitiExitClick(xiti_section_id, this.href, 1);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="link-new-window")) {
            anchor.target = "_blank";
        }

        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="link-download")) {
            anchor.onclick = function(){
                xitiDownloadClick(xiti_section_id, this.href, 0);
                return false;
            }
        }

        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="link-download-new-window")) {
            anchor.onclick = function(){
                xitiDownloadClick(xiti_section_id, this.href, 1);
                return false;
            }
        }

        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-logo-box")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "nav-logo-box", "1", this.href);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-homepage-flyout-features")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "nav-homepage-flyout-features", "1", this.href);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-footer-affiliates")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "Footer::Rolex_Dealers", "1", this.href);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-footer-download")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "Footer::Downloads", "1", this.href);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-footer-legal")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "Footer::Legal", "1", this.href);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-footer-sitemap")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "Footer::Sitemap", "1", this.href);
                return false;
            }
        }
        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel")=="nav-footer-language")) {
            anchor.onclick = function(){
                xitiNavClick(xiti_section_id, "Footer::Language", "1", this.href);
                return false;
            }
        }

        else if (anchor.getAttribute("href") && (anchor.getAttribute("rel").indexOf("XiTiLE") > -1)) { 
                 anchor.onclick = function(){
                    var completeRel = this.getAttribute("rel");
                    var completeRelArray = completeRel.split(" ");
                    xitiRel = completeRelArray[0];
                    var xitiRelString = xitiRel.substring(7, xitiRel.length);
                    xitiNavClick2(xiti_section_id, xitiRelString, "1", this.href);
                    return false;
                }
            }
        
         else if (anchor.getAttribute("href") && (anchor.getAttribute("rel").indexOf("XiTi") > -1)) { 
                 anchor.onclick = function(){
                    var xitiRel = this.getAttribute("rel"); 
                    var xitiRelString = xitiRel.substring(5, xitiRel.length);
                    xitiNavClick(xiti_section_id, xitiRelString, "1", this.href);
                    return false;
                }
            }        
        }

    }
}
onloadHandlers[onloadHandlers.length] = 'setupXitiOnclicks()';


onloadHandlers[onloadHandlers.length] = 'setupSitemapOnclicks()';
function setupSitemapOnclicks() {
    var sitemap = $('sitemap');
    if(sitemap){
        var sitemapLinks = sitemap.getElementsByTagName('a');
        for (i=0; i < sitemapLinks.length; i++){
            sitemapLinks[i].onclick =  function() {xitiNavClick(xiti_section_id, "nav-sitemap", "1", this.href); return false;}        
        }
    }
}



/* DART Tagging Functions */
function spotlightEvent(section,page) {
    if(document.cookie.indexOf("dblClickCookie="+page) === -1){
        var spotlightImage = new Image();
        var axel = Math.random()+"";
        var a = axel * 10000000000000;
        spotlightImage.src = "http://ad\.uk.doubleclick.net/activity;src=1359693;type="+section+";cat="+page+";ord=1;num="+a+"?";
    }
}
     

/*cookie redirects*/

var expireOffset = new Date(new Date().getTime()+(180*24*60*60*1000)).toGMTString();

function goLocation(lang) {
    if(lang)
        document.location.href= '/'+lang+'/';
}
function goLangDeeplink(redirURL){
    document.location.href=redirURL;
}
function goDeeplink(redirURL) {
    document.location.href=redirURL;
}
function setCookie(me){
    document.cookie = "lang2="+me+"; expires="+expireOffset+"; path=/;";
}
function setCookieHome(me){
    document.cookie = "lang2="+me+"; expires="+expireOffset+"; path=/;";
    goLocation(me);
}


/* GLOBAL DOWNLOADS CONTROL */
// downloads panel
function DownloadsPanel(){
    var d = this;
    this.init = function(){
        d.downloadsDiv = $("downloads");
        d.divs = d.downloadsDiv.getElementsByTagName("div");
        d.divs = $A(d.divs); // convert to Prototype's extended Array object
        d.divs.each(function(currentDiv, divIndex){
            if (currentDiv.className == "download-item"){
                currentDiv.onmouseover = function(){
                    currentDiv.divs = currentDiv.getElementsByTagName("div");
                    currentDiv.divs = $A(currentDiv.divs); // convert to Prototype's extended Array object
                    currentDiv.divs.each(function(childDiv, childDivIndex){
                        if (childDiv.className == "download-description-bg short"){
                            childDiv.className = "download-description-bg";
                        }
                        if (childDiv.className == "download-description"){
                            childDiv.className = "download-description hidden";
                        }
                        if (childDiv.className == "download-links hidden"){
                            childDiv.className = "download-links middle";
                        }
                    });
                };
                currentDiv.onmouseout = function(){
                    currentDiv.divs = currentDiv.getElementsByTagName("div");
                    currentDiv.divs = $A(currentDiv.divs); // convert to Prototype's extended Array object
                    currentDiv.divs.each(function(childDiv, childDivIndex){
                        if (childDiv.className == "download-description-bg"){
                            childDiv.className = "download-description-bg short";
                        }
                        if (childDiv.className == "download-description hidden"){
                            childDiv.className = "download-description";
                        }
                        if (childDiv.className == "download-links middle"){
                            childDiv.className = "download-links hidden";
                        }
                    });
                };
            }
            if (currentDiv.className == "download-description-bg"){
                currentDiv.className = "download-description-bg short";
            }
            if (currentDiv.className == "download-links"){
                currentDiv.className = "download-links hidden";
                currentDiv.anchors = currentDiv.getElementsByTagName("a");
                currentDiv.anchors = $A(currentDiv.anchors);
                currentDiv.anchors.each(function(currentAnc, currentAncIndex){
                    currentAnc.onclick = function() {
                        if(document.cookie.match("RolexDownloads=true")){
                            var targetUrl;
                            if (this.savedHref) {
                                targetUrl = this.savedHref;
                            } else {
                                targetUrl = this.href;
                            }
                            xitiDownloadClick(xiti_section_id, targetUrl, 0);
                        }else{
                            this.savedHref = this.href;
                            runEula(this.savedHref);
                        }
                        return false;
                    }
                });
            }
        });
    }
}
function runEula(assetLocation){
    $("downloads-list").style.visibility = "hidden";
    $("downloads-eula").style.visibility = "visible";
    $("downloads-eula-accept").href = assetLocation;
    $("downloads-eula-accept").onclick = function () {
        xitiNavClick(xiti_section_id, "Downloads::Accept", "0", this.href);    
        closeEula();
        if($("downloads-eula-accept-check").checked == true){
            document.cookie = "RolexDownloads=true;expires=" + expireOffset +";";
        }
        xitiDownloadClick(xiti_section_id, this.href, 0);
        return false;
    }                                                                       
}
function closeEula(){
    $("downloads-list").style.visibility = "visible";
    $("downloads-eula").style.visibility = "hidden";
}
function initDownloads(){
    if ($("downloads")){
        var thisDownloadsPanel = new DownloadsPanel();
        thisDownloadsPanel.init();
    }
}
function alignElementsGlobal(){
    var BOTTOM_NAV_HEIGHT = 96;
    var WIDTH_OFFSET = 20;
    var leftPosition = 0;
    var topPosition = 0; 
    var newOffsetMaxMid = 1;
    if ($('downloads') || $('lang-select')) {               
        if (height >= MAXHEIGHT){
            topPosition = MAXHEIGHT - BOTTOM_NAV_HEIGHT - TOPPADDING - marginTop + newOffsetMaxMid;
        }
        else if (height <= MINHEIGHT){
            topPosition = MINHEIGHT - BOTTOM_NAV_HEIGHT - TOPPADDING - marginTop + FOOTERHEIGHT;
        }
        else{
            topPosition = height - BOTTOM_NAV_HEIGHT - TOPPADDING - marginTop + newOffsetMaxMid;
        }
        leftPosition = LEFTPADDING - marginLeft - WIDTH_OFFSET;
        if(downloadsNode = $('downloads')){
            var downloadsHeight = downloadsNode.offsetHeight;
            downloadsNode.style.left = (leftPosition - 480) +  "px";
            downloadsNode.style.top = footerTop - downloadsHeight + "px";
            downloadsNode.style.width = width+"px";
            var downloadsListNode = $('downloads-list');
            downloadsListNode.style.left = (marginLeft + LEFTPADDING) + "px";
            var downloadsListNode = $('downloads-eula');
            downloadsListNode.style.left = (marginLeft + LEFTPADDING) + "px";
            var lang = "de";
            var downloadsHeaderNode = $('downloads-header');
            if(lang == "fr"){
                downloadsHeaderNode.style.left = (marginLeft + 700) + "px";        
            }
            else{
                downloadsHeaderNode.style.left = (marginLeft + 750) + "px";        
            }
            var downloadsHeaderNode = $('dl-button-close');
            downloadsHeaderNode.style.left = (marginLeft + 923) + "px";
            downloadsHeaderNode.onclick=function(){
                xitiNavClick(xiti_section_id, "Downloads::Close", "1", this.href);    
            }
        }
        if(langSelectNode = $('lang-select')){
             var langSelectHeight = langSelectNode.offsetHeight;
            langSelectNode.style.left = (leftPosition - 480) +  "px";
            langSelectNode.style.top = footerTop - langSelectHeight + "px";
            langSelectNode.style.width = width+"px";
            langSelectNode = $('lang-select-button-close');
            langSelectNode.style.left = (marginLeft + 923) + "px";
            langSelectNode = $('lang-select-header');
            langSelectNode.style.left = (marginLeft + 780) + "px";
            langSelectNode = $('lang-select-container');
            langSelectNode.style.left = (marginLeft + 180) + "px";
        }
    }
}
onresizeHandlers[onresizeHandlers.length] = 'alignElementsGlobal()';
onloadHandlers[onloadHandlers.length] = 'alignElementsGlobal()';
onloadHandlers[onloadHandlers.length] = 'initDownloads()';