var ie = false;
var menuStatus = false;
var preloadedBackgrounds = Array();
var allPages = Array();
var subpages = Array();
var hoverTimer = null;

allPages.push('index','campaigns','branding','directmail','interactive','promotions','ourwork','broadcast','packaging','b2b','contactus','ourlatest','whoweare','ourclientsspeak','miriamisms','clientlogin','privacypolicy','brandkinetix');


//Functions that initialize this page
function initialize(page){
    $('#submenu').hide();

    //Get this filename so we can highight the menu
    parts = page.split('/');
    page = parts[parts.length - 1];
    
    fixScreen(); //expands window to max
    fixImage(); //fixes image to fill background
    ajaxlinks(); //looks for ajaxlinks and converts their HREF to onclick events (seo friendly ajax)
    highlightMenu(page); //highlight page
    
    preloadBackgrounds();
}

//parse through all links and add ajax functionality
//this only works if javascript is on, so it makes the page friendly for search engines
var url ="";
function ajaxlinks() {
    
    //Ajax Links
    $('a.ajaxlink_main').click( function(){
        myStr = String($(this).attr('href'));
        fullUrl = myStr.split("?", 2); //should only be a max of two parts
        
        //parse it if there's anything to parse
        if(fullUrl.length > 1){
            url = fullUrl[0]; post = fullUrl[1];
        }else{
            url = myStr; post = ''; //otherwise just a regular plain link, let it go
        }
        
        $.post(url + "?ajax=1", post, function(data){
            $('#maincontent').html(data);
            highlightMenu(url);
            ajaxlinks(); //call itself once loaded.
               
            //Find background and load it into
            for(x=0; x < allPages.length; x++){
                if(url.replace('.php', '') == allPages[x]){
                    //replace background image
                    $('#backgroundImage').attr('src', preloadedBackgrounds[x].src);
                }
            }
            fixImage(); //if you need to
            //Activate navigator scroll
            navigatorScroll();
        });
        
        return false;
    });
    
    //This is for hover over effect on Brand Kenetics
    size = getWindowSize();
    size = size.split('x');
    
    $('span.hoverBrand').hover(
        function(event){
            pos = getMousePos(event);
            pos = pos.split(',');
            pos[0] = (pos[0] * 1) + 10 ;
            pos[1] = (pos[1] * 1) + 10;
            
            var divSize = (Math.ceil(size[0] * 0.3) < 400) ? Math.ceil(size[0] * 0.3) : 400;
            divSize = divSize * 1;
            
            //Test so we don't go off the screen
            if((pos[0] + divSize) > size[0]){
                //We're over..let's find out by how much and adjust
                var rem = (pos[0] + divSize) - size[0]
                pos[0] = pos[0] - rem - 30;
            }
            x = pos[0] + 'px'; y = pos[1] + 'px';
            $('#hoverBrand').css({'left' : x, 'top' : y});
            $('#hoverBrand').fadeIn(600);
        },
        function(){
            hoverTimer = setTimeout("$('#hoverBrand').fadeOut(600)", 1000);
        }
    );
    return true;
}

function getMousePos(e){
    var posx = 0;
    var posy = 0;
    //alert(event.pageX);
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) 	{
        posx = e.pageX;
        posy = e.pageY;
    }else if (e.clientX || e.clientY) 	{
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }
    return posx + ',' + posy;
}


//This needs to be more dynamic, can't assume index.php
var lastUrl = 'index.php';
function highlightMenu(url){    
    if(url == null) url = lastUrl;
    
    menuLinks = document.getElementById('menu').getElementsByTagName('a');
    for(x=0; x < menuLinks.length; x++){
        
        menuURL = String(menuLinks[x].getAttribute("href"));
        menuURL = menuURL.split('/');
        menuURL = menuURL[menuURL.length - 1];
        
        image = menuLinks[x].getElementsByTagName('img');
        
        if(image[0]){
            if(menuURL == url){
                //We have a match - replace image
                image[0].src = image[0].src.replace(/menu1$/,'menu1hover');
                image[0].src = image[0].src.replace(/menu2$/,'menu2hover');
                //Cancel out mouseover events
                image[0].onmouseout = null;
                image[0].onmouseover = null;
                
            }else{
                //replace any that are stuck on hover mode (if it was last selected);
                image[0].src = image[0].src.replace(/menu1hover$/,'menu1');
                image[0].src = image[0].src.replace(/menu2hover$/,'menu2');
                
                //On hover change it
                image[0].onmouseover = function(){
                    this.src = this.src.replace(/menu1$/,'menu1hover');
                    this.src = this.src.replace(/menu2$/,'menu2hover');
                }
                //On out change it
                image[0].onmouseout = function(){
                    this.src = this.src.replace(/menu1hover$/,'menu1');
                    this.src = this.src.replace(/menu2hover$/,'menu2');
                }
            }
        }
    }
    
    //Show hide submenu
        subpages = Array('ourwork.php', 'campaigns.php','branding.php','directmail.php','interactive.php','promotions.php','broadcast.php','packaging.php','b2b.php');
    if(in_array(url, subpages)){
        toggleMenu(true);
    }else{
        toggleMenu(false);
    }
    lastUrl = url;
}

function toggleMenu(value){
    if($('#submenu').length){
        if(value){
            size = getWindowSize().split('x');
            
            if(size[1] < 600){
                if(size[1] < 500) $('#iso').hide();
                $('#fadeClientsDiv').hide();
                return;
            }
            $('#submenu').slideDown(2000);
            menuStatus = true;
        }else{
            if(size[1] < 600){
                if(size[1] < 500) $('#iso').fadeIn();
                $('#fadeClientsDiv').fadeIn();
            }
            $('#submenu').slideUp(1000);            
            menuStatus = true;
        }
    }
    return;
}

function preloadBackgrounds(){
    for(x=0; x < allPages.length; x++){
        preloadedBackgrounds[x] = new Image(1920,960);
        preloadedBackgrounds[x].src="template/backgrounds/" + allPages[x] + '.jpg';
        
    }
}

function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}
