var chooseFrom = ['boston.jpg', 'hong-kong.jpg', 'london.jpg', 'new-york.jpg','singapore.jpg','sydney.jpg'];

function shuffle(a) {
    for (var i = a.length; i > 0; i--) {
        var j = Math.floor(Math.random() * i);
        var temp = a[i - 1];
        a[i - 1] = a[j];
        a[j] = temp;
    }
    return temp;
}

$(function() {
	
	var section = $(".gn_primary > li.selected").index(); // get selected navigation item
	
	$(".gn_primary > li").hover(
		function(){	
			$(this).siblings('li').removeClass("selected");
		},
		function(){
			$(".gn_primary > li:eq(" + section + ")").addClass("selected");
		}
	);

	// ie6 menu fix
	if (($.browser.msie) && ($.browser.version < 7)) { 
		
		$(".gn_primary > li  > a").addClass("topnav"); // add class for styling
		//$(".gn_primary li.selected .gn_secondary").css("display","block"); // show selected secondary menu
		
		$(".gn_primary > li").hover(
			function(){
				//$(".gn_primary li.selected .gn_secondary").css("display","none"); // hide selected menu
				$(this).children('a').addClass("topnavOn"); // styling for hovered li
				$(this).children('.gn_secondary').css("display","block"); // show hovered secondary menu
			}, 
			function(){
				$(this).children('a').removeClass("topnavOn");
				$(this).children('.gn_secondary').css("display","none");
				
				$selected = $(".gn_primary > li:eq(" + section + ")");
				$selected.addClass("selected");
				$($selected + " .gn_secondary").css("display","block");
			}
		);
	
		$("h1, .hdr_a, .hdr_b, .hdr_c, .hdr_d, .hdr_e").addClass("iepng");
	
	}

	
	

	
	if (($.browser.msie) && ($.browser.version < 8)) { 
		//$(".homenav .mod + .mod").css("margin-left","0").css("margin-right","0"); // for IE
	}
	
	//Main image randomizer
	$('#mastheadimage').attr('src', '/assets/images/masthead/' + shuffle(chooseFrom));
	

	$(".homenav .full li:first-child").addClass("first");
	$(".homenav .full li:last-child").addClass("last");
	
	// Webcasts overlay
	    // Setup a basic iframe for use inside overlays.
    var theframe = $('<iframe frameborder="0" scrolling="auto"></iframe>');
    // Overlay
		$("a[title='iframe']").overlay({
        mask: '#000',
        onBeforeLoad: function() {
            //First get the wrap
            var wrap = this.getOverlay().find(".contentWrap");
            //Get the URL from the trigger
            var link = this.getTrigger().attr("href");
            //Add the link and style attributes to the basic iframe
            $(theframe).attr({ src: link, style: 'height:540px; width:660px; border:none;' });
            //Write the iframe into the wrap
            wrap.html(theframe);
        }
    });

	
	// label over
	$("label").labelOver('lblover');
	
	// Jquery tools basic setup
	
	if ($(".tabs").length > 0) {
        $(".tabs").tabs(".panes > div");
		$(".tabs li").prepend("<span></span>");
		$(".tabs li").append("<b class='border'></b><b class='indent'></b>");
    }
	
	if ($(".accordion").length > 0) {
		$(".accordion").tabs(".accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: 1});
	}
	
    if ($(".scrollable").length > 0) {
        $(".scrollable").scrollable({
			circular : true
		});  
    }
	
});

// 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);


