/**
* @version 1.0
* @requires jQuery Core 1.4.2+
*/
/**
* @namespace Root namespace for holding all objects
*/
var truPhone = window.truPhone || {};
/**
* @namespace Container namespace for holding all navigational related objects created by truPhone
* @memberOf truPhone
*/
truPhone.nav = window.truPhone.nav || {};
/**
* @namespace Container namespace for holding all JS to ensure the navigation functions as designed
* @memberOf truPhone
*/
truPhone.nav = function () {	
    var init = function () {
        var self = this;
		moveDropNav();
		bindLinks();
		homeMouseOver();
		sublandingMouseOver();
		setTimeout(function() {
			balanceNavHeight();
		}, 200);
    };
	var dropNavVisible = false;
	var bindLinks = function(){
		//stuff to control the drop down navigation at the top of the page
		$('#topBar li:not(.lang) a').bind('click', function(){
			//the link that was clicked 
			var link = this,
			//the anchor it links to
				index_substring = link.href.substring(link.href.indexOf('#'));
			//if the menu is currently visible
			if(dropNavVisible){
				$('.dropNav:visible:not('+index_substring+')').fadeOut(200, function(){
					$('.dropNav'+index_substring).fadeIn(200);
				});
			}
			else{
				$('.dropNav'+index_substring).slideDown(200, function(){
					//ie fix
					$('.dropNav'+index_substring).show();
				});
				//ie fix
			}
			dropNavVisible = true;
			return false;
		});
		//bind the links within the drop down nav
		$('.dropNav a').bind('click', function(){
			$(this).parents('.dropNav').slideUp(200);
			dropNavVisible = false;
			//if the close button was clicked
			if($(this).hasClass('dropHide')){
				return false;
			}
		});
		//hover class added to home CTA
		$('.homeCTA').bind('mouseenter', function(){
			$(this).addClass('hover');
		});
		$('.homeCTA').bind('mouseleave', function(){
			$(this).removeClass('hover');
		});
	};
	//the markup for the drop down nav starts at the bottom of the page so that it is visible without javascript
	var moveDropNav = function(){
		$('.dropNav').removeClass('inline').hide().insertAfter('#topBar');
		$('.dropNav ul').append('<li><a href="#" class="dropHide"><img src="/assets/img/buttons/close.png" alt="' + truPhone.data.alt_text.nav_close + '"/></a></li>');
	};
	var homeMouseOver = function(){
		$('.home #crosslinking .col').bind('mouseenter', function(){
			$(this).addClass('hover');
		});
		$('.home #crosslinking .col').bind('mouseleave', function(){
			$(this).removeClass('hover');
		});
		$('.home #crosslinking .col').bind('click', function(){
			if($(this).find('a:eq(0)').length > 0){
				window.location = $(this).find('a:eq(0)')[0].href;
			}
			
		});
	};
	var sublandingMouseOver = function(){
		$('.panelBlock').bind('click', function(){
			if($(this).find('a:eq(0)').length > 0){
				window.location = $(this).find('a:eq(0)')[0].href;
			}
		});
	};
	var balanceNavHeight = function(){
		var navHeight = $('#nav').height();
		var linkHeight = $('#nav > ul > li > a').height();
		
		if(navHeight > 80){
			//$('#nav > ul > li > a').css('padding-top', (navHeight/2)-(linkHeight / 2)+'px');
			//$('#nav > ul > li > a').css('background-position', '100% '+(-2969+(navHeight/2)-(18))+'px')
		}
	}
    return {
        init: init
    };
}();

$(function(){
	truPhone.nav.init();
});
