// SideMenu Toggles
function toggleSideMenuSubItems(subItem) {
	if ( jQuery(subItem).hasClass("sub-arrow-up") ) {
		jQuery(subItem).removeClass("sub-arrow-up");
		jQuery(subItem).addClass("sub-arrow-down");
	} else {
		jQuery(subItem).removeClass("sub-arrow-down");
		jQuery(subItem).addClass("sub-arrow-up");
	}
	jQuery(subItem).parent().find("ul.sub").slideToggle("fast");
};

// Manual Featuring Tools
function updateFeaturedStatus(actionVal, etidVal, eidVal, featureVal, divId) {
	var postTarget = "/intranet/updateLastFeatured";
	jQuery.post(postTarget, {
		action :actionVal,
		etid :etidVal,
		eid :eidVal
	}, function(data) {
		handleFeaturedStatusResponse( eidVal, featureVal, divId );
	});
	jQuery("#featured_ajax_loader").show();
};

function handleFeaturedStatusResponse(eidVal, featureVal, divId) {	
	var spanTxt = document.getElementById( eidVal );
	var featureDiv = document.getElementById( divId + '_featureContent' );
	var unfeatureDiv = document.getElementById( divId + '_unfeatureContent' );
	
	// window.location.reload();
	if ( featureVal == 'true' ) {
		var time = new Date();
		var lmonth = time.getMonth() + 1;
		var date = time.getDate();
		var year = computeYear(time);
		var minutes = time.getMinutes();
		var hours = time.getHours();
		
		if ( minutes < 10 ) { minutes = '0' + minutes; }
		
		// make the featured thing show the current date
		spanTxt.innerHTML = "<font color='green'>" + lmonth + "/" + date + "/" + year + " " + hours + ":" + minutes + "</font>";
		
		featureDiv.style.display = "none";
		unfeatureDiv.style.display = "block";
	} else {
		// make it show 'not featured'
		spanTxt.innerHTML = "No longer featured";
		featureDiv.style.display = "block";
		unfeatureDiv.style.display = "none";
	}
};

// Year Nicename
function computeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
};

String.prototype.trimStr = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

// Enable and initialize designated set of date fields to use the datePicker
function enableCalSelectDateField(calIconId, monthId, dayId, yearId, beginYear, endYear) {
	// initialize the "calendar" link
	jQuery('#'+calIconId)
		.datePicker(
		// associate the link with a date picker
		{
			createButton:false,
			startDate:'01/01/'+beginYear,
			endDate:'31/12/'+endYear
		}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				//updateSelects(jQuery(this).dpGetSelected()[0]);
				jQuery(this).dpDisplay();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects(selected[0]);
			}
		);
			
	var updateSelects = function(selectedDate) {
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		
		jQuery('#'+dayId).val(d);
		jQuery('#'+monthId).val(m+1);
		jQuery('#'+yearId).val(y);
	}
	
	// listen for when the selects are changed and update the picker
	jQuery('#'+dayId+', #'+monthId+', #'+yearId).bind(
		'change',
		function() {
			var d = new Date(
				jQuery('#'+yearId).val(),
				jQuery('#'+monthId).val(),
				jQuery('#'+dayId).val()
			);
			jQuery('#'+calIconId).dpSetSelected(d.asString());
		}
	);
};

// cTip -- Custom Tooltips
function initToolTip(){
	jQuery(".cTip").hover(
		function () {
			jQuery(this).find("span").css("display", "block");
		},
		function () {
			jQuery(this).find("span").css("display", "none");
		}
	);
};

// Generic javascript map
var JsMap = function() {
	var _internalMap = {};

	this._put = function(key, value) {
		_internalMap[key] = value;
	};

	this._get = function(key) {
		return _internalMap[key];
	};

	this._remove = function(key) {
		if( _internalMap[key] != null) {
			delete(_internalMap[key]);
		}
	};

	this.size = function() {
		count = 0;
		for(var i in _internalMap) {
			count++;
		}
		return count;
	};
};

JsMap.prototype.put = function(key, value) {
	this._put(key, value);
};

JsMap.prototype.get = function(key) {
	return this._get(key);
};

JsMap.prototype.remove = function(key) {
	this._remove(key);
};

// IE6 Update Message
function IECheck() {
	if (jQuery.browser.msie && jQuery.browser.version == 6){
		if( jQuery.cookie('svie6notice')!='closed' ) {
			call_tb_init();
			tb_show(null,"/help/overlays/ie6Message.jsp?height=300&width=500&modal=true",false);
			//jQuery("body").prepend("<div id=\"ie6-notice\"><p><strong>Welcome! Looks like you're using Internet Explorer 6?</strong> We try to support all browsers, but some features are not possible with IE6.<br />To access all of Sportsvite's features, consider upgrading to <a href=\"http://www.mozilla.com\">Firefox</a>, <a href=\"http://www.apple.com/safari/download/\">Safari</a>, or <a href=\"http://www.google.com/chrome\">Google Chrome</a>. (They're all free!)</p><p id=\"closeie6notice\"><a href=\"#null\" onclick=\"closeie6notice(); return false;\">x</a></p></div>");
		}
	}
};
function closeie6notice() { 
	jQuery('#ie6-notice').hide();
	jQuery.cookie('svie6notice', 'closed', { expires: 30, path: '/' });
}

jQuery(document).ready(function() {
	/* Main Nav Dropdowns */
	jQuery("#new-nav>li").hover(
		function () {
			jQuery(this).find(".drop").show();
			jQuery(this).find("a").addClass("drop-on");
			jQuery(".leaderboard .ad object").hide();
			jQuery("select").addClass("ieHide");
		}, 
		function () {
			jQuery(this).find(".drop").hide();
			jQuery(this).find("a").removeClass("drop-on");
			jQuery(".leaderboard .ad object").show();
			jQuery("select").removeClass("ieHide");
		}
	);
});








