﻿//nav.js
//10/2/2007
//using for Philip Morris USA .com
//global navigation menu

var Divider = new Image(); Divider.src = '/assets/images/nav/global_nav_divider.gif';
var LeftDivider = new Image(); LeftDivider.src = '/assets/images/nav/nav_divider_left_on.gif';
var RightDivider = new Image(); RightDivider.src = '/assets/images/nav/nav_divider_right_on.gif';
var SelectedLeftDivider = new Image(); SelectedLeftDivider.src = '/assets/images/nav/nav_divider_left_selected.gif';
var SelectedRightDivider = new Image(); SelectedRightDivider.src = '/assets/images/nav/nav_divider_right_selected.gif';
var revertNavTimer;
var originalHeight = new Array();
var submenus = [];
var pageNav = [];
var incrementHeight = 30;
var totalPrimaryNav;


$(function() {
    
    if ( $('.currentNav').find('img:first').attr('src')) {
        pageNav.LeftDivider = $('.currentNav').prev().find('img:first').attr('src');
        pageNav.RightDivider = $('.currentNav').next().find('img:first').attr('src');
        $('.currentNav').prev().find('img:first').attr('src', SelectedLeftDivider.src);
        $('.currentNav').next().find('img:first').attr('src', SelectedRightDivider.src);   
	}	
	totalPrimaryNav = $('#primary-nav').find('li.menu').length; 	

    $('.subnav').each(function(inx) {
        originalHeight[inx] = $(this).find('.subnav-body').height();
        if ( inx >= totalPrimaryNav ) {
            var SubNavWidth = $(this).find('.subnav-body').width() + 7;
            var ParentWidth = $(this).prev().width();
            var marLeft = (SubNavWidth - ParentWidth - 2) * -1;
			$(this).css('display', 'none');
			$(this).css('margin-left', marLeft + 'px');     
			var arrowMarLeft = (SubNavWidth - (ParentWidth/2) - 8);
            $('#' + $(this).parent().attr('id') + "_dropdown_arrow").css('margin-left', arrowMarLeft + 'px');   
        }
        else {
			$(this).css('display', 'none');
            $(this).css('margin-left', '-2px');
        }
    });
    
	$('li.menu').each(function(inx) {
	    submenus.push($(this));       
	    submenus[inx].defaultImage = $('#Nav1-' + (inx+1) + '-Default').attr('src');
	    submenus[inx].hoverImage = $('#Nav1-' + (inx+1) + '-Hover').attr('src');
	    if ( $(this).attr('class').indexOf('currentNav') != -1 ) {
	        pageNav.index = inx;
	    }
       
        $(this).hover(function() {
            submenus[inx].showNav = true; 
            $(this).find('.subnav:first').css('z-index', '8501'); 
            mover($(this), $(this).attr('id'), inx);
            slideIn($(this).find('.subnav:first').attr('id'), inx);           
	    }, function() {
		    submenus[inx].showNav = false;
	        $(this).find('.subnav:first').css('z-index', '500');
		    mout($(this), $(this).attr('id'), inx);
	        slideOut($(this).find('.subnav:first').attr('id'), inx);
		}); 	 
		
		$(this).find('a').each(function() {
		    $(this).focus(function() {                          
                submenus[inx].showNav = true; 
                submenus[inx].find('.subnav:first').css('z-index', '8501'); 
                mover(submenus[inx], submenus[inx].attr('id'), inx);
                submenus[inx].find('.subnav:first').css('height', (originalHeight[inx]+5) + 'px');
            });    
        });       		         
        $(this).find('a').each(function() {
            $(this).blur(function() {
	            submenus[inx].showNav = false;            
	            submenus[inx].find('.subnav:first').css('z-index', '500');
		        mout(submenus[inx], submenus[inx].attr('id'), inx);
                submenus[inx].find('.subnav:first').css('height', '0px');
            });
        });
    });		

    $('.subnav').css('height', '0px');
    $('.subnav').css('display', 'block');

 }); 

    

function toggleSelectVis(hide){
    var allselects = $("select");
    for(x=0;x<allselects.length;x++){
        allselects[x].blur();
    }
    if(isIE6()){
        var hideOrShow;
        if(hide){
            hideOrShow = 'hidden';
        } else {
            hideOrShow = 'visible';
        }
        for(x=0;x<allselects.length;x++){
            allselects[x].style.visibility = hideOrShow;
        }
    }
}

function slideIn(id, inx) {
    toggleSelectVis(true);
    clearInterval(submenus[inx].slideTimerOut);
    submenus[inx].slideTimerIn = setInterval("showSubNav('" + id + "', " + inx + ")", 10);
}
function slideOut(id, inx) {
    toggleSelectVis(false);
    clearInterval(submenus[inx].slideTimerIn); 
    submenus[inx].slideTimerOut = setInterval("hideSubNav('" + id + "', " + inx + ")", 10);
}
function showSubNav(id, inx) {
    var hIncrement = originalHeight[inx] / 5;
    var currentHeight = parseInt($("#" + id).css('height'));
    if ( currentHeight < originalHeight[inx] ) {
        var h = currentHeight + hIncrement;
        $("#" + id).css('height',  h + 'px');
    }
    else {
        clearInterval(submenus[inx].slideTimerIn);    
        $("#" + id).css('overflow', 'visible');
    }
}

function hideSubNav(id, inx) {
    $("#" + id).css('overflow', 'hidden');
    var currentHeight = parseInt($("#" + id).css('height'));
    if ( currentHeight > 0 ) {
        var h = ((currentHeight - incrementHeight) < 0) ? 0 : (currentHeight - incrementHeight);        
        $("#" + id).css('height',  h + 'px');
    }
    else {
        clearInterval(submenus[inx].slideTimerOut);
    }
}

//change the style of selected (hover) primary nav
function setCurrentNav(liObj, inx) { 
        $(liObj).find('img:first').attr('src', submenus[inx].hoverImage);
        $(liObj).prev().find('img:first').attr('src', LeftDivider.src);
        $(liObj).next().find('img:first').attr('src', RightDivider.src);
        if ($(liObj).attr('class').indexOf('currentNav') != -1) {
            $('.currentNav').css('background-image', 'url(/assets/images/nav/global_nav_bg_on.gif)');
        }
}

//function being called when user hover over the nav
function mover(liObj, objId, inx) {  
    $(liObj).addClass('curr'); //add class, which change the background   
    setCurrentNav(liObj, inx);
	if ( inx >= totalPrimaryNav ) {
	    changeNavBg('over', liObj, objId); 
    }
    else {
        var imgWidth = $(liObj).find('img:first').width();
        var marginPos = Math.round((imgWidth + 30) / 2);
        $("#" + objId + "_dropdown_arrow").css('margin-left', marginPos);
    }
}   
 
function mout(liObj, objId, inx) { 
    if ( inx >= totalPrimaryNav ) changeNavBg('out', liObj, objId); 
    if ( inx != pageNav.index ) {   
        $(liObj).find('img:first').attr('src', submenus[inx].defaultImage);
        $(liObj).removeClass('curr');        
        if ( inx < totalPrimaryNav ) {  
            if ( inx == 0 ) $(liObj).prev().find('img:first').attr('src', '/assets/images/s.gif'); 
            else if ( (inx-1) == pageNav.index )
                $(liObj).prev().find('img:first').attr('src', SelectedLeftDivider.src);
            else if ( submenus[inx-1].showNav != true ) 
                $(liObj).prev().find('img:first').attr('src', Divider.src);
            
            if (inx + 1 == $(liObj).parent().find('li.menu').length) {
                $(liObj).next().find('img:first').attr('src', Divider.src);
            }
            else if ( (inx+1) == pageNav.index ) {
                $(liObj).next().find('img:first').attr('src', SelectedRightDivider.src);
            }
            else if ( !submenus[inx+1].showNav ) {
                $(liObj).next().find('img:first').attr('src', Divider.src);
            }
        }
        else {
            if ( inx == totalPrimaryNav ) 
                $(liObj).prev().find('img:first').attr('src', '/assets/images/s.gif');
            else if ( submenus[inx-1].showNav != true ) 
                $(liObj).prev().find('img:first').attr('src', '/assets/images/s.gif');
            
            if (inx + 1 == submenus.length)
                $(liObj).next().find('img:first').attr('src', '/assets/images/s.gif');
            else if ( !submenus[inx+1].showNav )
                $(liObj).next().find('img:first').attr('src', '/assets/images/s.gif');
        }        
    }
    else {
        revertPageNav();
    }
} 

function revertPageNav() {
    if ($('.currentNav').find('img:first').attr('src')) {
        $('.currentNav').find('img:first').attr('src', submenus[pageNav.index].hoverImage);
        if (pageNav.index < totalPrimaryNav) {
            $('.currentNav').css('background-image', 'url(/assets/images/nav/global_nav_selected_bg.gif)');    
        }
        else {
            $('.currentNav').css('background-image', 'url(/assets/images/nav/secondary_nav_bg_on.gif)');    
        }
        $('.currentNav').prev().find('img:first').attr('src', SelectedLeftDivider.src);
        $('.currentNav').next().find('img:first').attr('src', SelectedRightDivider.src);
    } 
}
 
function changeNavBg(state, liObj, objId) { 
    if (state == 'out') {
        $(liObj).css({background: 'url(/assets/images/nav/secondary_nav_bg.gif) no-repeat'});        
    }else {
        $(liObj).css({background: 'url(/assets/images/nav/secondary_nav_bg_on.gif) no-repeat'});            
   }
}   

/*tertiaryNav */
$(function() {
    $('#tertiaryNav').find('a').each(function(inx) {
        $(this).find('img:first').hover(
            function() {$(this).attr('src', $('#Nav3-' + (inx+1) + '-Hover').attr('src')); },
            function() {
                if (!$(this).attr('class')) {
                    $(this).attr('src', $('#Nav3-' + (inx+1) + '-Default').attr('src'));
                }
        });            
    });
});

/**** send content container *****/
$(function() {       
    $('#sendContainer').find('.close').click( function() { 
        $('#sendContainer').animate({ height: 'toggle', opacity: 'toggle' }, 'slow');
        document.getElementById("sendThankyouDiv").style.display = 'none';
        document.getElementById("sendFailureDiv").style.display = 'none';
        document.getElementById("sendingMsg").style.display = 'none';
        document.getElementById("sendFormDiv").style.visibility = 'visible';
        document.getElementById("sfNameField").value = '';
        document.getElementById("sfEmailField").value = '';
        document.getElementById("sfRecipientsField").value = '';
        document.getElementById("sfMessageField").value = '';
        document.getElementById("sfCopymeField").checked = false;
    });
    $('.send').click( function() { 
        $('#sendContainer').animate({ height: 'toggle', opacity: 'toggle' }, 'slow');
        document.getElementById("sendThankyouDiv").style.display = 'none';
        document.getElementById("sendFailureDiv").style.display = 'none';
        document.getElementById("sendingMsg").style.display = 'none';
        document.getElementById("sendFormDiv").style.visibility = 'visible';
        document.getElementById("sfNameField").value = '';
        document.getElementById("sfEmailField").value = '';
        document.getElementById("sfRecipientsField").value = ''; 
        return false; 
    });

});  

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop + 35;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}


/***** check send form ***/
function checkForm(formName) {

    var regExEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var seeError = false;
    var form = document.forms[formName];
    var firstEmptyField = false;
    
    $('.error').hide();    
    if (form.name.value.length < 1) {
        $('#nameError').html('please fill in your name').show();
        seeError = true;
        form.name.focus();
        firstEmptyField = true;
    }
    
    if (form.email.value.length < 1) {
        $('#emailError').html('please fill in your email').show();
        seeError = true;
        if (firstEmptyField == false){
             form.email.focus();
             firstEmptyField = true;
        }
    }
    else if (!regExEmail.test(form.email.value)) {
        $('#emailError').html('your email is invalid').show();
        seeError = true;
        if (firstEmptyField == false){
             form.email.focus();
             firstEmptyField = true;
        }
    }
    
    if (form.recipient.value.length < 1) {
        $('#recipientError').html('please fill in recipient email').show();
        seeError = true;
        if (firstEmptyField == false){
             form.recipient.focus();
             firstEmptyField = true;
        }
    }
    else {
        
        var emails = form.recipient.value.replace(/\s+/g, '').split(',');
        
        for (var i = 0; i < emails.length; i++) {
            if (!regExEmail.test(emails[i])) {
                $('#recipientError').html('invalid recipient email').show();
                seeError = true;
                if (firstEmptyField == false){
                     form.recipient.focus();
                     firstEmptyField = true;
                }
                break;            
            }
        }
    }
    
    if (seeError) {
        $('#sendFormError').show();
        return false;        
    }
    else {
        $('#sendContainer').find('fieldset').css('visibility', 'hidden');
        $('#thankyouDiv').show();
        return false;
    }
}