
var _HEIGHT=598;
var _WIDTH=924;
var _HEIGHT_OFFSET=0;
var _MIN_TOP=30;
var _HEIGHT_TRIGGER=598;
var delayTabAnimation=false;
var readyForAni=true;

var closeMenuTimerA;
var closeMenuTimerB;
var hoverMenuItem='';
var hoverMenuMenu='';
var clickMenuOn=true;

var isiOS = ((navigator.userAgent.match(/iPad/i) != null) || (navigator.userAgent.match(/iPhone/i) != null));
var isiPhone = (navigator.userAgent.match(/iPhone/i) != null);

$(window).resize(function() { windowResized() });

$(document).ready(function() {
    windowResized();
    // main animation
    nextAni();
    
    if (isiOS)
        document.addEventListener("orientationChanged", function() { setTimeout(windowResized,500); }); 
    if (isiPhone)
        window.scrollTo(0,1);
        
    // initialize menus
    $('#menuPanel ul.menuFirstLevelUL > li').click(function() {
        if (clickMenuOn) {
            setTimeout("closeMenu(false,'"+$(this).attr('rel')+"')",50);
            clickMenuOn=false;
        } else {
            clickMenuOn=true;
            hoverMenuItem=$(this).attr('rel');
            showMenu(this);
            setTimeout("closeMenu(false,'"+$(this).attr('rel')+"')",10000);
        }
    });
    $('#menuPanel ul.menuFirstLevelUL > li').mouseenter(function() {
        hoverMenuItem=$(this).attr('rel');
        showMenu(this);
    });
    $('#menuPanel ul.menuFirstLevelUL > li').mouseleave(function() {
        hoverMenuItem='';
        setTimeout("closeMenu(false,'"+$(this).attr('rel')+"')",500);
        log("cleared menuName");
    });

});

function showMenu(t) {
//    if ($(t).attr('rel')!=menuItemNameOn) closeMenu(true);
    var menu='menu'+$(t).attr('rel');
    log("show menu: " + menu);
    
    if ($(t).is('.hovering')) {
        log("already hovering");
        // already over it
    } else {
        closeMenu(true);
        $('#'+menu).slideDown(250);
        $(t).addClass('hovering');
        $('#'+menu).mouseenter(function() {
            hoverMenuMenu=$(t).attr('rel');
        });
        $('#'+menu).mouseleave(function() {
            hoverMenuMenu='';
            setTimeout("closeMenu(false,'"+$(t).attr('rel')+"')",500);
        });
    }
}

function closeMenu(clearAll,menuName) {
    if (clearAll) {
        $('#menuPanel ul li').each(function() {
            $(this).removeClass('hovering');
        });
        $('#menuPanel ul li ul').each(function() {
            $(this).slideUp(100);
        });
    } else {
        log('#menuPanel ul li[rel='+menuName+']: '+$('#menuPanel ul li[rel='+menuName+']').is('.hovering'));
        log('#menu'+menuName+': '+$('#menu'+menuName).is(':visible'));
        log('hoverMenuItem: ' + hoverMenuItem);
        log('hoverMenuMenu: ' + hoverMenuMenu);
        if (
            (!$('#menuPanel ul li[rel='+menuName+']').is('.hovering') && !$('#menu'+menuName).is(':visible'))
            || (hoverMenuItem=='' && hoverMenuMenu=='')
            ) {
            $('#menuPanel ul li[rel='+menuName+']').removeClass('hovering');
            $('#menu'+menuName).slideUp(100);
        }
    } 
}

function closeAllMenus() {
    $('menuPanel ul li a').each(function() {
        $(this).removeClass('hovering');
    });
    $('menuPanel ul li ul').slideUp(250);
}

var thisAni=0;
var aniElem = Array();
var aniType = Array();
var aniTime = Array();
var menuIcons = Array();

// XP log function
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
//  if(this.console){
//    console.log( Array.prototype.slice.call(arguments) );
//  }
};

function checkVisible() { // works around a weird WebKit bug with CSS3 and font-face
    var visibleElem=Array('mainTile','logoTile');
    for(var x=0;x<visibleElem.length;x++) {
        if ( !$('#'+visibleElem[x]).is(':visible') ) {
            $('#'+visibleElem[x]).show();
        }
    }
}
function nextAni() {
    if (!readyForAni) {
        setTimeout(nextAni,250);
    } else {
        thisAni++;
//            console.log("ANI LOOP: " + thisAni);
        switch (thisAni) {
            case 1:
                $('#mainTile').fadeIn(halfSec,function() { nextAni(); });
                break;
            case 2:
                log("menu panel");
                $('#menuPanel').slideUp(halfSec,function() { $('#menuPanel').show(); nextAni(); });
                break;
            case 3:
                $('#logoTile').fadeIn(halfSec,function() { nextAni(); });
                break;
            default:
                if (thisAni<30) {
                    if (aniElem[thisAni]) {
//                            console.log('ANI ID: ' + aniElem[thisAni]);
                        if (aniType[thisAni]=='slide') {
                            $(aniElem[thisAni]).slideToggle(aniTime[thisAni],function() { nextAni(); });
                        } else {
                            $(aniElem[thisAni]).fadeIn(aniTime[thisAni],function() { nextAni(); });
                        }
                    } else {
                        setTimeout(nextAni,10);
                    }
                }
        }
    }
}

var winH=0, winW=0, origWinH=0;

function windowResized() {
    if (isiPhone) window.scrollTo(0,1);
    if (isiOS) {
        $('body').width('100%');
        $('body').height('100%');
    }
    if (document.body && document.body.offsetWidth) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' &&
        document.documentElement &&
        document.documentElement.offsetWidth ) {
            winW = document.documentElement.offsetWidth;
            winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
        winW = window.innerWidth;
        winH = window.innerHeight;
    } 

    if (origWinH==0) origWinH=winH;
    if (origWinH<_HEIGHT_TRIGGER) {
        winH = 598+60;
        //_HEIGHT_OFFSET=20;
        //_HEIGHT=610;
    }
    if (winW<924+30) {
        winW=924+30;
        window.innerWidth=winW;
    }
    var left=(winW-_WIDTH)/2;
    var top=((winH-_HEIGHT)/2)+_HEIGHT_OFFSET;
    if (top<_MIN_TOP) top=_MIN_TOP;
    if (left<0) left=0;

    var mainTile=document.getElementById('mainTile');
    var menuPanel=document.getElementById('menuPanel');
    var mainContent=document.getElementById('mainContent');

    mainTile.style.left=left+'px';
    mainContent.style.height=winH+'px';
    $('#mainTileShadow').css('left',left+'px').css('top',top+'px');
    $('#mainTileStrip').css('top',top+'px');
    $('#logoTile').css('top',top-25+'px');
    $('#logoTile').css('left',left+'px');
    var mpWidth=$('#menuPanel').width();
    var mtWidth=$('#mainTile').width();
    
        $('#menuPanel').css('top',(top-31)+'px');

            if (isiPhone) {
            $('#menuPanel').css('left',(left+427)+'px');
        } else {
            $('#menuPanel').css('left',(left+443)+'px');
        }
    
    if (document.getElementById('mainBottomDrawer')) {
        if (isiPhone) {
            $('#mainBottomDrawer').css('top',(top+598+8)+'px').css('left',(left+800)+'px');
        } else {
            $('#mainBottomDrawer').css('top',(top+598+8)+'px').css('right',(left)+'px');
        }
    }


    if (document.getElementById('mainTileCaption')) {
        $('#mainTileCaption').css('left',(left-265)+'px').css('bottom',(top+260)+'px');
    }
    
    if (document.getElementById('introSkip')) {
        $('#introSkip').css('left',(left-24)+'px');
        $('#introSkip').css('bottom',(top+5)+'px');
    }
    if (document.getElementById('fourOhFourText')) {
        var fourOhFourCopy=document.getElementById('fourOhFourText');
        fourOhFourCopy.style.left=(left+175)+'px';
        fourOhFourCopy.style.top=(top+100)+'px';
    }
}

function trim(text) {
    return text.replace(/^\s+|\s+$/g,"");
}

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}
function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    cook=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    cook=cook+';path=/';
    document.cookie=cook;
}
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}


