/* Requires jquery.js */

jQuery.noConflict();

jQuery(document).ready(function(){
	init_zTool();
   	init_dropNavs();
	init_pressTicker();
	init_subNav();	
});

function init_zLocSelect(){		
	// Attach expand/collapse toggle events to location selector	
	jQuery('#zLocSelect .close a').click(function(){
		return tog_zLocSelect();
	});
}

function init_zTool(){
	jQuery('.zSearch_input .txt').focus(function(){
	 	jQuery(this).addClass('focus');
	});
	jQuery('.zSearch_input .txt').blur(function(){
	 	jQuery(this).removeClass('focus');
	});
}

function tog_zLocSelect(_userID) {

    if (jQuery('#zLocSelect').css('display') == 'block') {        
        jQuery('#zLoc .zLocSelect_tog').removeClass('open');
        // I just want to wait for 600 ms...
        jQuery('#zLocSelect').animate({ opacity: 1.0 }, 600, function() {
            jQuery('#zLocSelect').slideUp(100);
        });
        jQuery('#zLocSelectInner').slideUp(700);
        jQuery('#zLocSelectFtr').slideUp(700);
        jQuery('#zLocJump').slideUp(700);
    }
    else {

        if (jQuery('#zLocDomestic').length == 0) {
            // Retrieve and inject #zLocDomestic, #zLocIntl, and #zLocRecent (if _userID is set)
            jQuery.post('/RecentLocations/recentLocHandler.aspx', { userID: _userID }, function(d, stat) {               
                jQuery(d).insertAfter('#masthead');
                tod_zExpand();
                init_zLocSelect();
            });
        }
        else 
            {tod_zExpand();}        
    }  
	return false;
}

function tod_zExpand() { 
    jQuery('#zLoc .zLocSelect_tog').addClass('open');
    jQuery('#zLocSelect').slideDown(150);
    jQuery('#zLocSelectInner').slideDown(700);
    jQuery('#zLocSelectFtr').slideDown(700);
    jQuery('#zLocJump').slideDown(700);
}

function init_dropNavs(){
	var i,w;
	    
    /* Set dropdown widths depending on text length */
	try{
		for(i=0;i<jQuery('.zSubNav').length;i++){
			curObj = jQuery('.zSubNav').eq(i);
			if(curObj){
			    w=curObj.width();
				w=(w<130)?130:w;				
				curObj.attr('style','width:'+w+'px');
			}
		}
    }
    catch (e){}
	
	/* Add hover events to dropdown navigation */
	jQuery('#pageMainNav li').hover(function(){
		if(!jQuery(this).hasClass('curr')) jQuery(this).addClass('dropped');
	},function(){
		jQuery(this).removeClass('dropped');
	});
}

function init_subNav () {
	/* Centers sub-navigation around currently selected main navigation tab */
	var mainTabOffset = jQuery('#pageMainNav li.curr').offset();
	var subNavOffset  = jQuery('#pageSubNav').offset();
	var mainTabW      = jQuery('#pageMainNav li.curr').width();
	var subNavW,i;
	
	for(i=0,subNavW=0;i<jQuery('#pageSubNav li').length;i++){
		subNavW+=jQuery('#pageSubNav li').eq(i).width();
	}
	
	if((mainTabOffset=='undefined')||(subNavOffset=='undefined')||(mainTabW=='undefined')||(subNavW=='undefined'))
		return;
	else {
		var mainTabX           = mainTabOffset.left - subNavOffset.left;
		var subNavCenterOffset = mainTabX + (mainTabW/2) - (subNavW/2);
		
		if(subNavCenterOffset > 0) 
			jQuery('#pageSubNav li').eq(0).css('margin-left',subNavCenterOffset);
		
		jQuery('#pageSubNav').css('visibility','visible');
	}
}

function init_pressTicker(){
	/* Random */
	jQuery('#pressTicker li').eq(Math.floor(Math.random()*10) % jQuery('#pressTicker li').length).addClass('curr');
	
	/* Time of Day */
	/*
	var d = new Date();
	jQuery('#pressTicker li').eq(Math.floor((d.getHours()/24) * jQuery('#pressTicker li').length)).addClass('curr');
	*/
}