/// <reference path="jquery-1.3.2-vsdoc.js"/> 
/*********************
Secret JS V1
*********************/

$(document).ready(function() {
    try {

        navigationHover(); //Load Navigation Hover
        subnavHover();
    }
    catch (e) {
        //swallow 
        alert("navigationHover() error: " + e);
    }
    try {
        theBasics(); // Load the basic Control
    }
    catch (e) {
        //swallow
        alert("theBasics() error: " + e);
    }

    //Empty out search box

    $('#searchBox input').focus(searchInput);
    $('#searchBox input').blur(offSearch);
    $('#countrySelector').val('US');

    //disable right-click on img
    //$(document).bind("contextmenu",function(e){return false});
});

(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);


$(function(){
    $.preload([
		    ImagesBaseUrl + "/Images/Navigation/navigation_homeActive.png",
		    ImagesBaseUrl + "/Images/Navigation/navigation_productActive.gif",
		    ImagesBaseUrl + "/Images/Navigation/navSweat_active.gif",
		    ImagesBaseUrl + "/Images/Navigation/navWhatsNew_active.gif",
		    ImagesBaseUrl + "/Images/Navigation/navSpecialOffer_active.gif",
		    ImagesBaseUrl + "/Images/Common/btnSocialize_on.png"
	    ]);
});
function searchInput() {
    var searchValue = $(this).val();
    if (searchValue == "Search") {
        $(this).val('');
    }
    else {
        return false;
    }
}

function offSearch() {
    var searchValue = $(this).val();
    if (searchValue == "Search" || searchValue == "") {
        $(this).val('Search');
    }
    else {
        return false;
    }
}

$('#countrySelector').change(function() {
    var country = $(this).val();
    switch (country) {
        case "US":
            window.location = "http://www.secret.com/";
            break;

        case "CAE":	     
	     window.location = "http://www.secret.com/en-ca/Default.aspx";
            break;

        case "CAF":
//          window.location = "http://www.secret.com/fr_ca/Secret.do";
            window.location = "http://www.secret.com/fr-ca/Default.aspx";
            break;

        default:
            window.location = "http://www.secret.com";
    }

});


function navigationHover() {
    if (!document.getElementById('navigation')) return false;
    $('#navigation ul li#productsNav').hover(function() {
        $(this).children('div').show();
        $(this).children('a').addClass('active');
    }, function() {
        $(this).children('div').hide();
        $(this).children('a').removeClass('active');
    });
    $('#navigation ul li#sweatNav').hover(function() {
        $(this).children('div').show();
        $(this).children('a').addClass('active');
    }, function() {
        $(this).children('div').hide();
        $(this).children('a').removeClass('active');
    }); 
}

function subnavHover(){
    $('a#protectionTab, #wantTab').css({'text-indent':'0', 'font-size':'18px','padding-top':'5px', 'opacity':'0'});
    $('a#protectionTab, #wantTab').click(function(){
        return false;
    });
    $('a#protectionTab, #wantTab').bind('mouseover', function(){
        var container = $(this).attr('href');
        $('#protectionSubnav, #wantDeodorantSubnav').hide();
        $(container).show();
        
        switch(container)
        {
            case "#protectionSubnav":
            $('#subNavigation').css('background','url(/Images/Navigation/bkgdSubnavProtection.png) no-repeat');
            break;
            
            case "#wantDeodorantSubnav":
            $('#subNavigation').css('background','url(/Images/Navigation/bkgdSubnavWant.png) no-repeat');
            break;
            
            default:
            $('#subNavigation').css('background','url(/Images/Navigation/bkgdSubnavProtection.png) no-repeat');
            break;
        }
    });
}

function theBasics() {
    if ($('#leftColumn div').attr('class') != "basicContainer") return false;

    $('.basicContainer h2').click(function() {
        if($(this).hasClass('active'))
        {   
            return false;
        }
        
        $('.basicContent').slideUp();
        $('.basicContent').css('background', 'transparent');
        $('.basicActiveTop').css('background', 'transparent');
        $('.basicActiveBtm').hide();
        $('.basicContainer h2 span').removeClass('active');
        $('.basicContainer h2').removeClass('active');

        var activeContent = $(this).parent().next('.basicContent');
        var activeArrow = $(this).children('span');

        if ($(this).hasClass('active')) {
            activeArrow.removeClass('active');
            activeContent.slideUp(500, function() { $(this).prev().css({'background':'none transparent'});$(this).next().hide();});
            $(this).removeClass('active');
            return false;
        }
        else 
        {
            $(this).parent().css({'background':'url(/Images/sweat101/basicTop.gif) #e1f1f9 no-repeat'});
            $(this).parent().next().next().show();
            $(this).parent().next().css('background', '#e1f1f9');
            activeContent.slideDown(500);
            activeArrow.addClass('active');
            $(this).addClass('active');

        }
    });
}



function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function isWhitespace(s) {
    var i;
    var whitespace = " \t\n\r";

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if ((" \t\n\r").indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName) {
    var strField = new String(objField.value);
    if (isWhitespace(strField)) {
        alert("You need to enter information for " + FieldName);
        objField.focus();
        objField.select();
        return false;
    }

    return true;
}
/****************************************************************/

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceNumber(objField, FieldName) {
    var strField = new String(objField.value);

    if (isWhitespace(strField)) return true;

    var i = 0;    
    var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}|\d{9}$)/;
    var matchArray = strField.match(objRegExp);    
    
    if (matchArray == null)
    {
        //alert(FieldName + " must be numerics only");
	 //alert(FieldName + " should be a properly formatted 5 digit or 5 digit+4 digit, like '12345-6789'. Please check your " + FieldName + .");
	 alert(FieldName + " should be a properly formatted 5 digit or 5 digit+4 digit, like '12345-6789'.");
        objField.focus();
        return false;
    }

    return true;
}

function ForceZipFormat(ctl, FieldName)
{
    var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}|\d{9}$)/;
    var usZip = ctl.value;
    var matchArray = usZip.match(objRegExp);
    var usZipSize = usZip.length;
    var usZipfirstpart;
    var usZipSecondpart;
    var usZipResult;
    
    if(ctl.value!="")
    {
        if (matchArray == null)
        {
                
                alert('Please enter your 5 digit zip code.');
                ctl.focus();
                return false;
        }
        
        if(usZipSize==9)
        {
            usZipfirstpart = ctl.value.substring(0,5);
            usZipSecondpart = ctl.value.substring(5,9);
            ctl.value= usZipfirstpart+"-"+usZipSecondpart;
            return true;
        }
        
        return true;    
        
    }
    else
    {
        return true;                
    }
}

