// TODO: move into sitewide JS file

// Fix CSS background image caching in IE6
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// TODO: move into its own JS file
var TopSeller = {

    // Constants
    CONTENT_BOX_MAX_HEIGHT: 248, // in pixels
    TRIGGER_SELECTOR: 'div#top-seller ul li', // The CSS selector for elements which trigger content switching
    MOUSEOVER_DELAY: 200, // in milliseconds
    
    initialize: function() {
        this.timer = null;
        this.registerEventHandlers();
    },
    
    registerEventHandlers: function() {

        var TopSeller = this; 
    
        function showTopSellerContent(trigger) {
            //we do not highlight the last element which is the See-All-Phones link
            //and we do not highlight if there's no html in the element. This implies that it's empty.
            var html = $.trim($(trigger).html());
            if (!$(trigger).hasClass('last') && html !== "") {
                $('div#top-seller-content .active').removeClass('active');
                var contentId =  ("#" + $(trigger).find("input[name='mapping']").val()); 
                $(contentId).addClass("active");
                $('ul#top-ten-list .active').removeClass("active");
                $(trigger).addClass("active");
                TopSeller.shiftImage($(contentId).find("img.phone").attr("id"));
            }               
        }
        
        // Register mouseover/out events for Top Ten in left nav
        // This code uses timers to meet the following requirements:
        // * Users should be able to quickly mouse over and off the nav without triggering it
        // * There should be a slight configurable delay on initial mouse over before triggering
        // * Subsequent mousing around should not have this delay, unless the user mouses off the menu
        //   for the amount of time specified by the delay
        
        $(this.TRIGGER_SELECTOR).each(function(i, trigger) {
            var trigger = $(trigger);
            trigger.bind('mouseenter', function() {
                if (TopSeller.timer) { // If timer exists, cancel it and show immediately
                    clearTimeout(TopSeller.timer);
                    TopSeller.timer = null;
                    showTopSellerContent(trigger);
                } else { // If timer does not exist, create one
                    TopSeller.timer = setTimeout(function() { 
                        showTopSellerContent(trigger);
                        TopSeller.timer = null;
                    }, TopSeller.MOUSEOVER_DELAY);
                }
            }).bind('mouseleave', function() {
                if (TopSeller.timer) { // When leaving a trigger, cancel the timer if it exists
                    clearTimeout(TopSeller.timer); 
                    TopSeller.timer = null; 
                } else { // Otherwise create a new timer which nullifies itself after the delay
                    TopSeller.timer = setTimeout(function() {
                        TopSeller.timer = null;
                    }, TopSeller.MOUSEOVER_DELAY);      
                }                
            }); 
        });        
    },

    shiftImage: function(obj) { // Takes either id or HTML element
        if (typeof obj == 'undefined') return false;
        var obj = (obj.constructor == String) ? $('#' + obj) : $(obj);
        var top = this.CONTENT_BOX_MAX_HEIGHT - obj.height();
        if (top < 0) { //this is for the custom bestseller image.
            obj.css('marginTop', -1);
        } else {
            obj.css('marginTop', parseInt(top * .95)); // call parseInt() to ensure pixel is whole number, e.g. 50px instead of 49.8px
        }
    }
}


var Upgrade = {
	FORM: $('#zipCodeForm'),
	SUBMIT: $('#upgrade-submit'),
	ZIPCARRIERMESSAGE: $('#enterZipAndCarrierMessage'),
	ZIPMESSAGE:$('#enterZipMessage'),
	CARRIERMESSAGE:$('#enterCarrierMessage'),
	isDisabled: false, 
	initialize: function() {
//	    this.submit = $('#upgrade-submit');
	    this.registerEventHandlers();
	},
	
	registerEventHandlers: function() {
	    Upgrade.SUBMIT.mouseover(this.checkValidity)
	    			.mouseout(this.resetView);
	    Upgrade.FORM.submit(function(e){
	    				if (Upgrade.isDisabled){
	    					return false;
	    				}
	    			});
	},

	checkValidity: function() {
		var isChecked = $('div#upgrade-phone input').is('input:checked');
		//var zip = $('#inputZipCode').val();
		//var zipRegexMatch = zip.search(/^\d{5}$/);		
		//if (!isChecked && zipRegexMatch != 0){
		//	Upgrade.ZIPCARRIERMESSAGE.removeClass("hidden");
		//	Upgrade.SUBMIT.addClass("disabled");
		//	Upgrade.isDisabled = true;
		//}
		//else
		if (!isChecked){
			Upgrade.CARRIERMESSAGE.removeClass("hidden");
			Upgrade.SUBMIT.addClass("disabled");
			Upgrade.isDisabled = true;
		}
		//else if (zipRegexMatch != 0){
		//	Upgrade.ZIPMESSAGE.removeClass("hidden");
		//	Upgrade.SUBMIT.addClass("disabled");
		//	Upgrade.isDisabled = true;
		//}
		
	},
	
	resetView: function(){
		Upgrade.ZIPCARRIERMESSAGE.addClass("hidden");
		Upgrade.ZIPMESSAGE.addClass("hidden");
		Upgrade.CARRIERMESSAGE.addClass("hidden");
		Upgrade.SUBMIT.removeClass("disabled");
		Upgrade.isDisabled = false;
	}
}