﻿// remap jQuery to $
(function($){
//tabbed navs
$(function () {
    //Reengeneered to work multiple times on one page    
    $('a.tab-control').click(function () {
        var _this = $(this);
        _this.parent("div").parent("div").find(".tabbed-links").hide()
        $(_this.attr("href")).show();
        _this.parent("div").find("a.tab-control").removeClass('selected');
        _this.addClass("selected");
        //Test if a homepagemainimage is present and if yes, test if the related anchor 
        //has a class that shows him referring to the image with class "homepageMainImage"
        //Than change the image
        var homepageImage = $("#homepageMainImage");
        var anchorlink= $("#anchorhome");
        if (homepageImage.length>0 && _this.hasClass("homepageMainImage")) {
            homepageImage.attr("src",_this.attr("rel"));
            anchorlink.attr("href", _this.attr("rev"));
        }
        return false;
    });
    $('a.tab-control').parent("div").each(function(index) {
        //$(this).find("a.tab-control").filter(':first').click();
    });
    //
    
});
//end tabbed navs
//tabbed slide
$(function () {
    var tabContainers = $('.tabbed-links-slide');
    
    $('a.tab-control-slide').click(function () {
    
        tabContainers.slideUp().filter(this.hash).slideDown();
        
        $('a.tab-control-slide').removeClass('selected');
        $(this).addClass('selected');
        
        return false;
    }).filter(':first').click();
});

//Brandswitch 
$(function () {    
    $('.brand-logos ul li a').live("click", function() {
        var lis = $('.brand-logos li');
        lis.removeClass("on");
        var _this = $(this);
        _this.parent("li").addClass("on");
        $(".content-box.brand-info:visible").each(function(index) {
            $(this).hide();
        });
        
        var id  = _this.attr("href");
        var img  = _this.attr("rel");
        var brandteaser = $("#brandteaser");
        if (brandteaser.length>0 && img!=="") {
            //Brandpage
            brandteaser.attr("src",img);
        } else if (id!=="#") {
            //Homepage
            $(id).show();
        }
        return false;
    });
});


//Brandswitch for brands page
$(function () {    
    $('.brand-logos1 ul li a').live("click", function() {
        var lis = $('.brand-logos1 li');
        lis.removeClass("on");
        var _this = $(this);
        _this.parent("li").addClass("on");
        $(".content-box.brand-info:visible").each(function(index) {
            $(this).hide();
        });
        
        var id  = _this.attr("href");
        var img  = _this.attr("rel");
        var brandteaser = $("#brandteaser");
        if (brandteaser.length>0 && img!=="") {
            //Brandpage
            brandteaser.attr("src",img);
        } else if (id!=="#") {
            //Homepage
            $(id).show();
        }
        return false;
    });
});

//Main Nav Helper 
$(function () {    
    $('#brands-sub').show();
    $('#about-sub').show();
    $('#press-sub').show();
    $('#careers-sub').show();
    $('#main-navigation li.nav-button').live("mouseover", function() {
        adjustMainNav($(this),false);   
           
        return false;
    });
//     $('#main-navigation #home.nav-button').live("click", function() {
////    alert("mani"); 
//       // adjustMainNav($(this),false);        
//        return true;
//    });
//    $('#main-navigation li.nav-button').live("click", function() {
//       // adjustMainNav($(this),false);  
//              
//        return true;
//    });
   
    $('#main-navigation li.nav-button').live("mouseout", function() {
        reinitMainNav();        
        return false;
    });
    
    //Inital setup of withs
    $('#main-navigation li.nav-button').each(function(index) {
        adjustMainNav($(this),true);        
    });
    
});
//Initials
$(function () {    
    
//    $("select").selectbox();
    $("input[type=text]").live("focus", function() {
        $(this).val("");
    });
    
});
})(this.jQuery);
//Main Nav helper
//Adjust left and width
adjustMainNav = function (elem,initial) {
    var mainNav = $('#main-navigation');
    mainNav.find("li.nav-button").removeClass("selected");
    var _this = elem;
    _this.addClass("selected");
    var width = 0;
    var theUL = _this.find("ul");
    theUL.find("li").each(function(index) {
        width += $(this).width();
    });
    var mainWidth = mainNav.width();
    var mainPos = $('#main-navigation').position();
    var mainCorr = mainPos.left+mainWidth/2;
    
    var actParentCorr = 0;
    if (theUL.length>0) {
        var actParent = theUL.offsetParent();
        var actParentPos = actParent.position();
        actParentCorr = mainPos.left;
        actParentCorr = actParentPos.left+actParentCorr;
    
        var absCorr = Math.round((mainCorr-actParentCorr)-width/2);
        
        //This is for the rest
        theUL.css({"width":Math.round(width+50),"left":absCorr});
        mainNav.find("ul:not(ul ul)").removeAttr("class").addClass(_this.attr("id"));
    } else {
        mainNav.find("ul:not(ul ul)").removeAttr("class");
    }
    if (initial===true) {
        reinitMainNav();
    }
}
//Set initial view (if active)
reinitMainNav = function () {
    var mainNav = $('#main-navigation');
    mainNav.find("li.nav-button").each(function(index) {
        var _this = $(this);
        _this.removeClass("selected");
        _this.parent("ul").removeAttr("class");
    });
    mainNav.find("li.nav-button").each(function(index) {
        var _this = $(this);
        if (_this.hasClass("active")===true) {
            _this.addClass("selected");
            _this.parent("ul").removeAttr("class").addClass(_this.attr("id"));
        }
    });
}
// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
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) );
  }
};
// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);

//FAQMEDIA SECTION
//Displaying Adress of continent selected 
 
$(function () {    
    $('.continents ul li a').live("click", function() {
        var liss = $('.continents li a');
        liss.removeClass("selectedcountry");
        var _this = $(this);
        _this.addClass("selectedcountry");
        $(".details-info:visible").each(function(index) {
            $(this).hide();
        });
         
        
        var id1  = _this.attr("href");
        var img  = _this.attr("rel");
       var brandteaser = $("#brandteaser");
        if (brandteaser.length>0 && img!=="") {
            //Brandpage
            brandteaser.attr("src",img);
        } else if (id1!=="#") {
            //Homepage
            $(id1).show();
        }
      
        return false;
    });
});
 
//Initials
$(function () {    
    if ($("#map").length>0) { initializeMap();}
    
    $('body').live("onunload", function() {
        if (typeof GUnload() === "function" && $("#map").length>0) { GUnload();}
        return false;
    });

    $("input[type=text]").live("focus", function() {
        $(this).val("");
    });
    
});
