$(document).ready(function() {
    $("#ka_imageBoxWrapper").cornerz( {radius:5, background:"white" });
    $("#ka1_switcher_mov").click( function(){ showKA1VidTab(); } );
    $("#ka1_switcher_img").click( function(){ showKA1ImgTab(); } );
        
    $("#ka_caseStudy").cornerz( {radius:5, background:"white" });    
    $("#ka_upcoming").cornerz( {radius:5, background:"white" });    
    $("#ka2").cornerz( {radius:5, background:"white" });    

	$("#bdTriSelector").cornerz( {radius:8, corners:"bl br"} );
	$("#bdBackgroundDefault").cornerz( {radius:4, background:"#d31e35"} );
	$("#bdLeftNav").cornerz( {radius:4, corners:"tl bl", background:"#d31e35" });
	
	// add hoverIntent bindings for selectors
    $(".ka4Tab").hoverIntent( function(){ KA4LoadInner(this); }, function(){ } );
    $(".ka4Tab").hoverIntent( function(){ KA4LoadInner(this); }, function(){ } );
        
    initImageSlideBox();
    $("#ka_imageBox_magnifyingGlass").click( function() { zoomOnSlideImage(imageSlideState); } );
        
    initKAAccordion();
    initializeRoundedTabs();

    replaceDivContentsWithUrl("#ka4ContainerInner_1", "ka4inner_1.asp");
    replaceDivContentsWithUrl("#ka4ContainerInner_2", "ka4inner_2.asp");
    replaceDivContentsWithUrl("#ka4ContainerInner_3", "ka4inner_3.asp");
    replaceDivContentsWithUrl("#ka4ContainerInner_4", "ka4inner_4.asp");
    replaceDivContentsWithUrl("#ka4ContainerInner_5", "ka4inner_5.asp");
    
    var thisUrl = document.location.toString();
    if (thisUrl.indexOf("?openTab_") > 0) {
        var tabNum = document.location.toString().split("_");
        KA4LoadInner("#ka4Tab_"+tabNum[tabNum.length-1]);
    }

});

/******************** global vars *********************/
var ka4TabOnNumber = 1;
var imageSlideState = 1;
var imageSlideMax = null;

/******************************************************/

function showKA1VidTab() {
    $("#ka_vidBox").show(); 
    $("#ka_imageBox").hide(); 
    $("#ka1_switcher_mov").css("font-weight", "bold");
    $("#ka1_switcher_img").css("font-weight", "normal");
}
function showKA1ImgTab() {
    $("#ka_imageBox").show(); 
    $("#ka_vidBox").hide(); 
    $("#ka1_switcher_img").css("font-weight", "bold");
    $("#ka1_switcher_mov").css("font-weight", "normal");
}

function initImageSlideBox() {
    imageSlideMax = $('#ka_imageBox ul').children().size(); 
    
	// add onclick bindings for imageBox
    $("#ka_imageBox_prev").click( function(){ prevImageInSlideBox(); }, function(){ } );
    $("#ka_imageBox_next").click( function(){ nextImageInSlideBox(); }, function(){ } );  
    
    var thumbCaption = $("#ka_imageBoxInner #gallery img:first").attr("alt");    
    $("#ka_imageBox_caption").html(thumbCaption);  
}

function zoomOnSlideImage(num) {
    $("#gallery ul li:nth-child("+num+") a").click();
}

function setSlideBoxArrows() {
    if (imageSlideState == 1) {
        $("#ka_imageBox_prev img").css("opacity","0.5");
        $("#ka_imageBox_prev img").css("filter","alpha(opacity = 50)");
    }

    if (imageSlideState > 1) {
        $("#ka_imageBox_prev img").css("opacity","1.0");
        $("#ka_imageBox_prev img").css("filter","alpha(opacity = 100)");
    }
    
    if (imageSlideState == imageSlideMax) {
        $("#ka_imageBox_next img").css("opacity","0.5");
        $("#ka_imageBox_next img").css("filter","alpha(opacity = 50)");       
    }

    if (imageSlideState < imageSlideMax) {
        $("#ka_imageBox_next img").css("opacity","1.0");
        $("#ka_imageBox_next img").css("filter","alpha(opacity = 100)");
    }
}

function nextImageInSlideBox() {
    var slideAmount = parseInt($("#gallery ul li a img").css("width")) +
                      parseInt($("#gallery ul li a img").css("margin-left")) + 
                      parseInt($("#gallery ul li a img").css("margin-right")) + 4;    

    var newMarginLeft = parseInt($("#gallery").css("margin-left")) - slideAmount;
    
    if (!$("#gallery").is(':animated')) {
        if (imageSlideState < imageSlideMax) {    
            $("#gallery").animate(
                {marginLeft: newMarginLeft}, 
                600
            );

            imageSlideState++;
            setSlideBoxArrows();

            var thumbCaption = $("#ka_imageBoxInner #gallery ul li:nth-child("+imageSlideState+") img").attr("alt");
            $("#ka_imageBox_caption").html(thumbCaption);              
        }
    }
}

function prevImageInSlideBox() {
    var slideAmount = parseInt($("#gallery ul li a img").css("width")) +
                      parseInt($("#gallery ul li a img").css("margin-left")) + 
                      parseInt($("#gallery ul li a img").css("margin-right")) + 4;    

    var newMarginLeft = parseInt($("#gallery").css("margin-left")) + slideAmount;

    if (!$("#gallery").is(':animated')) {    
        if (imageSlideState > 1) {    
            $("#gallery").animate(
                {marginLeft: newMarginLeft}, 
                600
            );
            imageSlideState--;
            setSlideBoxArrows();

            var thumbCaption = $("#ka_imageBoxInner #gallery ul li:nth-child("+imageSlideState+") img").attr("alt");
            $("#ka_imageBox_caption").html(thumbCaption);  

        }
    }
}

function initializeRoundedTabs() {
	$(".ka4Tab").cornerz( {radius:4, corners:"tl tr", background:"white"} );    
}

function initKAAccordion() {
    $("#ka_accordion_wrapper").cornerz( {radius:5, background:"white" }); 

    $('#ka_accordion').accordion({
    	header: ".ka_accordionElement",
    	event: 'mouseover',
    	change: function(event, ui) { 
    	    $(".ka_accordionElement").css('background-color','#eeeeee');
            $("#" + ui.newHeader.attr("id")).css('background-color','white');
            
            $(".ka_accordionDownArrow").hide();
            $(".ka_accordionRightArrow").show();
            $("#" + ui.newHeader.attr("id") + " .ka_accordionRightArrow").hide();
            $("#" + ui.newHeader.attr("id") + " .ka_accordionDownArrow").show();
    	}                    
    });    
}

function KA4LoadInner(thisElement) {
    var tabNum = $(thisElement).attr('id').split("_");

    if (tabNum[1] != ka4TabOnNumber) {
        var trackLabel = $(thisElement).children(".ka4TabLabel").html();
        trackLabel = 'Adv-' + trackLabel;
        pageTracker._trackEvent('Adv Module', 'Mouseover', trackLabel);
        // unselect the currently selected tab
        $("#ka4DownArrow_"+ka4TabOnNumber).hide();
        $("#ka4RightArrow_"+ka4TabOnNumber).show();
        $("#ka4ContainerInner_"+ka4TabOnNumber).hide();		

        $("#ka4Tab_"+ka4TabOnNumber).css('background-color','#EEEEEE');

        // make all the tabs of height 24px
        $(".ka4Tab").css('height','24px');
        $(".ka4Tab").css('border-bottom-width','1px');

        // select the currently selected tab
        $("#ka4RightArrow_"+tabNum[1]).hide();
        $("#ka4DownArrow_"+tabNum[1]).show();
        $("#ka4ContainerInner_"+tabNum[1]).show();		

        $("#ka4Tab_"+tabNum[1]).css('background-color','white');
        $("#ka4Tab_"+tabNum[1]).css('height','25px');
        $(".ka4Tab").css('border-bottom-width','0px');

        ka4TabOnNumber = tabNum[1];
    }
}

/* ****************************************************************************** */
function replaceDivContentsWithUrl(divToReplace, urlToCall) {
    $.ajax({
        url: urlToCall,
        // TODO - make sure cache is true on production
        cache: true,
        success: function(html){
            $(divToReplace).empty().append(html);
        }
    });
}

function MM_openBrWindow(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}