// the active page
var active, timer, loginTimer, loginShade

function setupNavigation() {

	var navigation = $('#navigation div.topLevel ul');
	
	// find the active page
	active = $('#navigation .topLevel li').index($('.active'))  ;
	$('#navigation .active a').addClass('init');
	$('#navigation .active').removeClass('active');
	
	// iterate through topLevel links and makes them ready for .tabs()
	$('#navigation div.topLevel a').each(function(i){
		$(this).attr('rel', $(this).attr('href'));
		$(this).attr('href', '#sub'+i);
	});
	
	// iterate through topLevel links and gives id's
	$('#navigation div.content').each(function(i){
		$(this).attr('id', 'sub'+i);
	});
	

	
  	// initialize the ul as tabs
  	navigation.tabs();
  	
  	// do not allow the deselection of tabs
    navigation.tabs({ unselect: false });
    
    // select tabs on mouse-over event
    navigation.tabs({ event: 'mouseover' });
    
    // register the mouse-leave event on the main head-navigation
    // navigation.bind('mouseleave', resetHeadNavigation);
    $('div#navigation').bind('mouseleave', startTimeOutToResetHeadNav);
    $('div#navigation').bind('mouseenter', resetTimeOutToResetHeadNav);
  
    // trigger navigation for main and sub top-navigations
    $('#navigation li[class!=separator]').bind('click',function(){
      //navigateTo(this);
    });
    
    
	// iterate through topLevel links and makes them ready for .tabs()
	$('#navigation div.topLevel a').bind('click',function(){
	    if ($(this).attr('target') == "_blank") window.open($(this).attr('rel'));
      else window.location.href = $(this).attr('rel');
    });

    
    // reset the head main-navigation to initial state
    resetHeadNavigation();
	
}

function showShade() {
	if((jQuery.browser.msie==true)&&(jQuery.browser.version.substr(0,1)=="6")){
	}else{
		if(loginShade) {
			loginShade.show();
		} else {
			// init
			loginShade = $('#logo img').clone().attr('src', 'images/shadow_popup.png').attr('id', 'loginShade').insertAfter('#auth');
			$('#loginShade').height($('#auth').height() + 18);
		}
	}
}

function setupLogin() {
	var auth = $('#auth');
	var login = $('#login a');
	login.bind('mouseover', function(){
		showShade()
		auth.show();
	});
	login.bind('click', function(){
		showShade()
		auth.show();
	});
	login.bind('mouseleave', function(){
		loginTimer = setTimeout( function(){
			auth.hide();
			loginShade.hide();
		}, 5000 );
	});
	login.bind('mouseenter', function(){
		clearTimeout(loginTimer);
	});
	auth.bind('mouseenter', function(){
		clearTimeout(loginTimer);
	});
	auth.bind('mouseleave', function(){
		loginTimer = setTimeout( function(){
			auth.hide();
			loginShade.hide();
		}, 5000 );
	});
}


function handleRollover(event) {
    var is_over = event.type === "mouseover" || event.type === "mouseenter";
	var images = jQuery(this).find("img");
	images.attr("src", function() {
		return is_over ? this.active_url : this.inactive_url;
	});
}

function mouseEnterFunc () {
  jQuery(this).attr('src', jQuery(this).attr("active_url"));
}

function mouseLeaveFunc () {
  jQuery(this).attr('src', jQuery(this).attr("inactive_url"));
}

function setupMouseOver(buttons) {
	var weekend = (jQuery(document.body).hasClass('weekend')) ? '_weekend' : '';
	jQuery(buttons).each(function(i){
		var el = jQuery(this);
		if (el.attr("mouseOverSetup") == "1") return; // don't do setup if it is already done
		el.attr("inactive_url", el.attr('src'));
		el.attr("active_url", el.attr('src').replace("-n.", weekend+"-a."));
		el.attr("mouseOverSetup", "1");
		el.bind('mouseenter', mouseEnterFunc);
		el.bind('mouseleave', mouseLeaveFunc);
	});
}

function removeMouseOver(buttons) {
	jQuery(buttons).each(function(i){
		var el = jQuery(this);
		el.attr("mouseOverSetup", "0");
		el.attr('src', el.attr("inactive_url"));
		el.unbind('mouseenter', mouseEnterFunc);
		el.unbind('mouseleave', mouseLeaveFunc);
	});
}

function setupFontSizer() {
	jQuery("#content").fontSizer({
		fontStates: 3, // normal, bigger1, bigger2 --> number of resize-steps INCLUDING normal font size
		decreaseButtonId: ".decreaseText",
		increaseButtonId: ".increaseText",
		fontSizeClass: "biggerFontSize",
		setButtonActive: function (button, state) {
			//button.show(); // needed if buttons are hidden by css-default
			if((jQuery.browser.msie!=true)&&(jQuery.browser.version.substr(0,1)!="6")){
				button.fadeTo("slow", state?1:0.5);
			}
			if (!state) button.addClass("inactiveFontButton");
			else button.removeClass("inactiveFontButton");
		}
	});
}

/*
 * tabs2 by Enrique Erne @ ROSA
 * adds tabulator functionality to meto swiss and meteo world map
 */  
function tabs2(tabs) {
	var container = new Array();
	jQuery(tabs).each(function(i) {
		var el = jQuery(this);
		if(el.attr('href') != '#') {
			jQuery(el.attr('href')).each(function() { // if id exists
				container.push(jQuery(this));
				el.bind("click", function(e) {
					jQuery(tabs).parent().removeClass('active');
					el.parent().addClass('active');
					jQuery(container).each(function() {
						var content = jQuery(this);
						content.css('display', ('#'+content.attr('id')==el.attr('href'))?'block':'none');
					});
					return false;
				});
			});
		}
	});
	// init
	jQuery(container).each(function(i) {
		var content = jQuery(this);
		content.css('display', (i==0)?'block':'none');
	});
}

function initWekendTopNews() {
	$('div.topNews').each(function(){
		var topNews = jQuery(this);
		var eHeading = new Array();
		var hHeading = 0;
		var eBox = new Array();
		var hBox = 0;
		// get heading:first height
		jQuery('.column', topNews).each(function(i){
			var column = jQuery(this);
			eHeading.push(column.find('.heading:first'));
			//eHeading[i].css('visibility', 'hidden');
			if(eHeading[i].height() > hHeading) hHeading = eHeading[i].height();
		});
		// set heading height
		jQuery(eHeading).each(function(i){
			eHeading[i].css('height', hHeading);
		});
		// get box height
		jQuery('.column', topNews).each(function(i){
			var column = jQuery(this);
			eBox.push(column.find('.box:last'));
			if(eBox[i].height() > hBox) hBox = eBox[i].height();
		});
		// set content:last height
		jQuery(eBox).each(function(i){
			var lastContent = eBox[i].find('.content:last');
			lastContent.css('height', lastContent.height() + ( hBox - eBox[i].height() ));
		});
	});
	
	$('.box').addClass('clear');
	$('.box').removeClass('clear');
}

$(document).ready(function() {
	// header main navigation initialization // removed 21.oct 08
	// setupNavigation();

	// tabs
	var tabContainer = $('.tab .nav ul');
	tabContainer.tabs();
	tabContainer.tabs({ unselect: false });
	
	// map ie6 pngfix
	if((jQuery.browser.msie==true)&&(jQuery.browser.version.substr(0,1)=="6")) {
	  // check if an object is found
    if($('.division .map div').length != 0) {
      // apply png fix for IE6   
      $(function(){$('.division .map div').pngFix( { blankgif:'../images/blank.gif' } );});
    }
	}
	// map tabs
	tabs2('.tab .hlist a');

	setupLogin();
	setupMouseOver('#searchButton, #financeSearchBotton, #loginButton, .controllBar input.image');
	
	
	setupFontSizer();

	// fixes height of weekend news
	initWekendTopNews();
});

function navigateTo(el) {
	alert("navigate to: " + $(el).children().attr('href'));
}

function resetHeadNavigation() {
	$('#navigation div.topLevel ul').tabs("select", active ); // removed - 1
}

function startTimeOutToResetHeadNav(){
	timer = setTimeout( function(){resetHeadNavigation();}, 2000 );
}

function resetTimeOutToResetHeadNav(){
	clearTimeout(timer);
}

/* MEDIA SLIDER*/
jQuery(window).bind("load", function() { 
	jQuery("#slider1").slideView({
		//easeFunc: "easeInOutExpo",
		//easeTime: 750,
		//showPicNum: 2,
		nextButtId: "slider1Next",
		prevButtId: "slider1Prev",
		setButtonActive: function (button, state) {
			button.show();
			button.fadeTo("slow", state?1:0.5);
			if (state) setupMouseOver(button);
			else removeMouseOver(button);
		}
	});
});

/* ADVERTISEMENT CHECKER */
jQuery(window).bind("load", function() {
  // add / remove classes to page object if no advertisement (leaderboard and/or skyscreaper) are present
    var pageDiv = jQuery('DIV#page');
    var findStr = "EMBED[id!=ebReportingFlash], OBJECT[id!=ebReportingFlash], IMG:not([src*=ATCollapse.gif])";
    if (jQuery('DIV#leaderboard').find(findStr).length == 0) pageDiv.addClass("no_space_top");
    else pageDiv.removeClass("no_space_top");
  // hide unused advertisement containers
    jQuery('div.box.banner:not(:has(IMG[src*=ATCollapse.gif]))').show();
});