/* $Id$ */





// nav tab stuff
var pkgs = ['basic','bronze','silver','gold','platinum'];
var pkgsTabPos = {};
var curPkg = 'silver';
var hoverPkg;


var myDomain = document.domain;
var re = new RegExp(/^(\w+)/);
var m = re.exec(document.domain);
if (m != null && m.length > 0) curPkg= m[0];

// allowed Access stuff
var allowedAccess = ['basic','bronze','silver','gold','platinum'];


// prototypes
String.prototype.ucwords = function() {
	return (this+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
};

$(document).ready(function() {

	/*-------- global vars --------*/

	/*-------- global functions --------*/
	function setWxUnits(type) {
		$.cookie('wxunits', type);
	}
	function getWxUnits() {
		return $.cookie('wxunits');
	}
	function swapWxUnits() {
		var units = getWxUnits();
		if (units == 'met') {
			setWxUnits('eng');
		}
		else {
			setWxUnits('met');
		}
		showWxUnits();
	}
	function showWxUnits() {
		var units = getWxUnits();
		if (units == 'met') {
			$('.u-eng').hide();
			$('.u-met').show();
			$('.u-switcher span').text('Switch to english units');
		}
		else {
			$('.u-eng').show();
			$('.u-met').hide();
			$('.u-switcher span').text('Switch to metric units');
		}
	}

	// header package tabs
	function shiftPkgTabs(curNum, init) {
		var tot = pkgs.length;
		var i, x = 0;
		var dist = (init === true) ? 178 : 178;

		// shift prev package elements left
		for (i = 0; i < curNum; i++) {
			x = (pkgsTabPos[pkgs[i]] - dist) + 'px';
			var el = $('#tab-' + pkgs[i]);
			if (el.css('left') != x) {
				el.animate({
					left: x
				}, 150, 'linear', ((pkgs[i] == hoverPkg && pkgs[i] != curPkg) ? showPkgTooltip : null));
			}
			else {
				if (pkgs[i] == hoverPkg && pkgs[i] != curPkg) showPkgTooltip(el);
			}
		}
		for (i = curNum; i < tot; i++) {
			x = pkgsTabPos[pkgs[i]] + 'px';
			var el = $('#tab-' + pkgs[i]);
			if (el.css('left') != x) {
				el.animate({
					left: x
				}, 150, 'linear', ((pkgs[i] == hoverPkg && pkgs[i] != curPkg) ? showPkgTooltip : null));
			}
			else {
				if (pkgs[i] == hoverPkg && pkgs[i] != curPkg) showPkgTooltip(el);
			}
		}
	}

	function showPkgTooltip(el) {
		el = (el != null) ? el : $(this);
		if (el.attr('id')) {
			var pkg = el.attr('id').replace(/tab-(\w+)/, "$1");
			if (pkg == hoverPkg) {
				el.qtip({
					content: {
						title: pkg.ucwords() + ' Package',
						url: './getPackageDetails.php',
						data: {
							pkg: pkg
						},
						method: 'get'
					},
					show: {
						ready: true,
						solo: true
					},
					style: {
						tip: 'topMiddle',
						name: 'dark'
					},
					position: {
						corner: {
							target: 'bottomRight',
							tooltip: 'topMiddle'
						},
						adjust: {
							x: -33
						}
					}
				});
			}
		}
	}


	function showErrorMsg(el,msg, msgTitle) {
	    el = (el != null) ? el : $(this);

		if (el.attr('id')) {
    	    var pkg = el.attr('id').replace(/tab-(\w+)/, "$1");
    	    if (msgTitle == null) msgTitle = pkg.ucwords() + ' Package';

			$(document.body).qtip({
				content: {
                    title: {
                        text: msgTitle,
                        button: 'Close'
                     },
                     text: msg

				},
                position: {
                         target: $(document.body), // Position it via the document body...
                         corner: 'center' // ...at the center of the viewport
                },
				show: {
					ready: true,
					solo: true
				},
                style: {
                 width: { max: 350 },
                 padding: '14px',
                 border: {
                    width: 9,
                    radius: 9,
                    color: '#666666'
                 },
                 name: 'light'
                },

                  api: {
                     beforeShow: function()
                     {
                        // Fade in the modal "blanket" using the defined show speed
                        $('#qtip-blanket').fadeIn(this.options.show.effect.length);
                     },
                     beforeHide: function()
                     {
                        // Fade out the modal "blanket" using the defined hide speed
                        $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
                     },
                     onHide: function()
                     {
                        // Fade out the modal "blanket" using the defined hide speed
                        this.destroy();
                     }
                  }
			});
		}


	}



   // Create the modal backdrop on document load so all modal tooltips can use it
   $('<div id="qtip-blanket">')
      .css({
         position: 'absolute',
         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
         left: 0,
         height: $(document).height(), // Span the full document height...
         width: '100%', // ...and full width

         opacity: 0.7, // Make it slightly transparent
         backgroundColor: 'black',
         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
      })
      .appendTo(document.body) // Append to the document body
      .hide(); // Hide it initially


	/*-------- global processes --------*/

	// header package navigator
/*
	$('#header .tab-item').mouseover(function(e){
		var pkg = $(this).attr('id').replace(/tab-(\w+)/, "$1");
		var pkgNum = pkgs.indexOf(pkg);
		hoverPkg = pkg;

		shiftPkgTabs(pkgNum);
	});
	$('#header .tab-item').mouseout(function(e){
		hoverPkg = null;
	});
*/


	function tabMouseover(){
		var pkg = $(this).attr('id').replace(/tab-(\w+)/, "$1");
		var pkgNum = jQuery.inArray(pkg,pkgs);
		hoverPkg = pkg;

		shiftPkgTabs(pkgNum);
	}
	function tabMouseout(){
		hoverPkg = null;
	}

    var hoverConfig = {
         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
         interval: 50, // number = milliseconds for onMouseOver polling interval
         over: tabMouseover, // function = onMouseOver callback (REQUIRED)
         timeout: 0, // number = milliseconds delay before onMouseOut
         out: tabMouseout // function = onMouseOut callback (REQUIRED)
    };



	$('#header .tab-item').click(function(e){
		var pkg = $(this).attr('id').replace(/tab-(\w+)/, "$1");
		//return false;
		var url = $('#header .tabs #tab-' + pkg + ' a').attr('href');

		if (url) {
		    if (jQuery.inArray(pkg,allowedAccess) < 0) {
		        var el = $(this);
		        showErrorMsg(el,'The ' + pkg + ' does not provide access to this content.');
		    }
		    else {
    		    url += location.pathname;
    		    location.href = url;
    		}
		}
		return false;
	});
	$('#header .tab-item').each(function(e){
		var pkg = $(this).attr('id').replace(/tab-(\w+)/, "$1");
		pkgsTabPos[pkg] = Math.round($(this).css('left').replace(/px/, ''));
	});
	$('#header .tabs').mouseout(function(e){
		shiftPkgTabs(jQuery.inArray(curPkg,pkgs), true);
	});


	// top search
	$('#topinfo :text').focus(function(e){
		$(this).val('');
		$(this).removeClass('empty');
	});
	$('#topinfo :text').blur(function(e){
		if ($(this).val() == '') {
			$(this).val('Enter city,state or zip code...');
			$(this).addClass('empty');
		}
	});
	$('a#topsearch-submit').click(function(e){
		$('form#topsearch').submit();

		return false;
	});



	// open proper package tab
	shiftPkgTabs(jQuery.inArray(curPkg,pkgs), true);

});
