﻿/** 
* Common Javascripts
*/


// =========== jQuery ========================================================== >>

/* for testing jQuery - unobtrusively adds an alert dialog to all hyperlinks
$(document).ready(function() {
    // Add alert to link
    $("a").click(function(event) {
        alert("jquery is running");
        event.preventDefault();
    });

});*/



/** IE6 & 7 z-index & position:relative fix
* http://www.vancelucas.com/blog/fixing-ie7-z-index-issues-with-jquery/
* IE browser detection
* http://stackoverflow.com/questions/566303/detect-internet-explorer-6-or-below-in-jquery

$(function () {
    if ($.browser.msie && $.browser.version == "6.0" || $.browser.msie && $.browser.version == "7.0") {
        var zIndexNumber = 1000;
        $('div').each(function () {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
        //alert("Im Using Internet Explorer");
    }
});
*/



$(document).ready(function () {
	
		
// NEW Tabs - jQueryTools Tabs: newtabs_tabs - Variable Width / CSS Rounded Corners
//Check and run if ul.newtabs is present
if ($("ul.newtabs").length > 0) { 
	$("ul.newtabs").tabs("div.newtabs_panes > div");
	
	//Optional: Disable a tab -- so you can make it a hyperlink, 
	//Example Usage: Add the "tabDisabled" class to the tab <li> like: <li class="tabDisabled"><a id="tabRedirect" href="#RedirectLink"> REDIRECT TAB </a></li> 
	//Then add the "gotothislink(url)" function, C#.NET code behind eample: tabRedirect.Attributes.Add("onClick", "javascript:gotothislink('http://www.disney.com');");
	var api = $("ul.newtabs").tabs("div.newtabs_panes > div", { api: true });
	if (api != null) {
		api.onBeforeClick(function (e, i) {
			if ($("ul.newtabs > li").eq(i).hasClass("tabDisabled")) {
				e.preventDefault();
			}
		});
	}
	
}


// Tabs - jQueryTools Tabs Default: jtools_tabs - Fixed Width / Sprite Style Tab Background Image
//Check and run if ul.jtools_tabs is present
if ($("ul.jtools_tabs").length > 0) { 
	$("ul.jtools_tabs").tabs("div.jtools_panes > div");
	
	//Optional: Disable a tab -- so you can make it a hyperlink, 
	//Example Usage: Add the "tabDisabled" class to the tab <li> like: <li class="tabDisabled"><a id="tabRedirect" href="#RedirectLink"> REDIRECT TAB </a></li> 
	//Then add the "gotothislink(url)" function, C#.NET code behind eample: tabRedirect.Attributes.Add("onClick", "javascript:gotothislink('http://www.disney.com');");
	var api = $("ul.jtools_tabs").tabs("div.jtools_panes > div", { api: true });
    if (api != null) {
		api.onBeforeClick(function (e, i) {
			if ($("ul.jtools_tabs > li").eq(i).hasClass("tabDisabled")) {
				e.preventDefault();
			}
		});
	}
}


// Tabs - jQuery & jQueryTools Tabs: itabs - old rounded corner w/ jQuery wrap function
//Check and run if ul.itabs is present
if ($("ul.itabs").length > 0) {
	$("ul.itabs").tabs("div.ipanes > div");
	$("ul.itabs span").addClass("itab");
	
	//Optional: Disable a tab (so you can make it a hyperlink, 
	//Example Usage: Add the "tabDisabled" class to the tab <li> like: <li class="tabDisabled"><a id="tabRedirect" href="#RedirectLink"> REDIRECT TAB </a></li> 
	//Then add the "gotothislink(url)" function, C#.NET code behind eample: tabRedirect.Attributes.Add("onClick", "javascript:gotothislink('http://www.disney.com');");
	var api = $("ul.itabs").tabs("div.ipanes > div", { api: true });
    if (api != null) {
		api.onBeforeClick(function (e, i) {
			if ($("ul.itabs > li").eq(i).hasClass("tabDisabled")) {
				e.preventDefault();
			}
		});
	}
}


// jQuery iTABS: Rounded Corners Wrapper
//Check and run if span.itab is present
if ($("span.itab").length > 0) {
	$("span.itab")
	.wrap('<span class="jtabs">' +
		'<span class="jtabs_t"><span class="jtabs_b">' +
		'<span class="jtabs_l"><span class="jtabs_r">' +
		'<span class="jtabs_bl"><span class="jtabs_br">' +
		'<span class="jtabs_tl"><span class="jtabs_tr">' +
		'</span></span></span></span>' +
		'</span></span></span></span></span>');
}


// jQuery iPANES: Rounded Corners Wrapper
//Check and run if div.ipanes is present
if ($("div.ipanes").length > 0) {
	$("div.ipanes")
	.wrap('<div class="jtabsbox">' +
	'<div class="jtabsbox_t"><div class="jtabsbox_b">' +
	'<div class="jtabsbox_l"><div class="jtabsbox_r">' +
	'<div class="jtabsbox_bl"><div class="jtabsbox_br">' +
	'<div class="jtabsbox_tl"><div class="jtabsbox_tr">' +
	'</div></div></div></div>' +
	'</div></div></div></div></div>');
}


// jQuery BOX: Rounded Corners Wrapper ---- Deprecated in MegaSticky Template */
//Check and run if div.box is present
if ($("div.box").length > 0) {
	$("div.box")
		.wrap('<div class="jbox floatcontainer">' +
		'<div class="jbox_t"><div class="jbox_b">' +
		'<div class="jbox_l"><div class="jbox_r">' +
		'<div class="jbox_bl"><div class="jbox_br">' +
		'<div class="jbox_tl"><div class="jbox_tr">' +
		'</div></div></div></div>' +
		'</div></div></div></div></div>');
}


// *NEW - jQuery BOX: Rounded Corners Wrapper for Temps w/out Rounded Corners
//Check and run if div.box_roundedcorners is present
if ($("div.box_roundedcorners").length > 0) {
	$("div.box_roundedcorners")
		.wrap('<div class="jbox_roundedcorners floatcontainer">' +
		'<div class="jbox_t"><div class="jbox_b">' +
		'<div class="jbox_l"><div class="jbox_r">' +
		'<div class="jbox_bl"><div class="jbox_br">' +
		'<div class="jbox_tl"><div class="jbox_tr">' +
		'</div></div></div></div>' +
		'</div></div></div></div></div>');
}
	
});




//FOR TABS! ... Go to this LINK from a tab onClick!
//Example Usage: custom tab link - adds the "#target" to URL in the browser's location bar, so tabs can be bookmarked
//C#.NET code behind: tabNews.Attributes.Add("onClick", "javascript:gotothislink('#news');");
//HTML hyperlink: <li><a href="javascript:gotothislink('#news');">Open Tab 2</a></li>
function gotothislink(url) {
    location.href = url;
}



//FOR TABS! ... Go to this TAB from a hyperlink
//Example Usage: Add the onClick function to a hyperlink in the page outside of tabs api
//C#.NET code behind, lnkMoreLinks.NavigateUrl = "javascript:gotothistab(1, 'ul.newstabs');"
//HTML hyperlink: <a href="javascript:gotothistab(1, 'ul.newtabs');">Open Tab 2</a>
//NOTE: Tab index starts at 0
function gotothistab(index, ulClassName) {
	$(ulClassName).tabs().click(index).getIndex();
}







/** 
* activate accordian tabs with JavaScript
* jQuery Accordion: Used for SideNav w/ Flyouts
* Override added: <h2 class="paneDisabled"> Disables unwanted accordion and creates link from h2 a href in page
*/
$(function () {
	var api = $("#accordionSideNav").tabs("#accordionSideNav div.paneAccordionSideNav",
		{ tabs: 'h2', effect: 'slide', initialIndex: null, api: true });
	
	/* iterate - wanting to add top border to first li
	$("ul.sidenavAccordion li a").each([0], function (index) {
	//$('li a').addClass('link2');
	alert("YO!");
	});*/
	
	if (api != null) {
		api.onBeforeClick(function (e, i) {
			//Disable accordion, set link to href
			if ($("#accordionSideNav > h2").eq(i).hasClass("paneDisabled")) {
				var url = $("#accordionSideNav > h2 a").eq(i).attr('href')
				$(location).attr('href', url);
				e.preventDefault();
			}
		});
	}
	/*
	if ($("#accordionSideNav > h2 a").eq(i).hasClass("current")) {
	//api.initialIndex(1);
	//e.addClass('link2');
	alert("Current");
	}*/
	
});






/**
* jQuery Table: Alternate row color
*/
$(document).ready(function(){  
    $("table.table tr:even").addClass("altrow");
        /* Do Nothing for even rows */
        //$("table.table tr:odd").css(""); 
});




// Show Element [Javascript]-[OnClick]-------------------------------------------->>
function show_element(control, id) {
    var element = document.getElementById(id);
    element.style.display = '';
}


// Hide Element [Javascript]-[OnClick]-------------------------------------------->>
function hide_element(control, id) {
    var element = document.getElementById(id);
    element.style.display = 'none';
}


// Toggle Element [Javascript]-- Toggle Element (OnInit - Show If Is Checked)----->>
function toggle_element(checkbox, ID) {
    var switcharoonie = document.getElementById(ID);
    if (checkbox.checked) {
        switcharoonie.className = 'show';
    }
    else {
        switcharoonie.className = 'hide';
    }
}


// Pop-Up Window [Javascript] --------------------------------------------------->>
function zoomView(zoomfile, zoomwidth, zoomheight)
{
    zoomWindow = window.open(
    zoomfile,
    'find', 
    ['width=' + zoomwidth + '',
    'height=' + zoomheight + '',
    'left=0', 'top=0',
    'scrollbars=yes',
    'menubar=no',
    'resizable=no']);
   
    zoomWindow.top.focus();
}


// Today's date [Javascript] --------------------------------------------------->>

var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

function fourdigits(number)	{
return (number < 1000) ? number + 1900 : number;
								}
today =  days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear()));





// Mega Menu - jQuery HoverIntent Plugin (hosted institutionally) required ------------>>

//check and run if JQuery hoverIntent plugin is installed first
if(jQuery().hoverIntent) {
     
	$(document).ready(function () {
	
		function megaHoverOver() {
			$(this).find(".sub").stop().fadeTo('fast', 1).show();
	
			//Calculate width of all ul's
			(function ($) {
				jQuery.fn.calcSubWidth = function () {
					rowWidth = 0;
					//Calculate row
					$(this).find("ul").each(function () {
						rowWidth += $(this).width();
					});
				};
			})(jQuery);
	
			if ($(this).find(".row").length > 0) { //If row exists...
				var biggestRow = 0;
				//Calculate each row
				$(this).find(".row").each(function () {
					$(this).calcSubWidth();
					//Find biggest row
					if (rowWidth > biggestRow) {
						biggestRow = rowWidth;
					}
				});
				//Set width
				$(this).find(".sub").css({ 'width': biggestRow });
				$(this).find(".row:last").css({ 'margin': '0' });
	
			} else { //If row does not exist...
	
				$(this).calcSubWidth();
				//Set Width
				$(this).find(".sub").css({ 'width': rowWidth });
	
			}
		}
	
		function megaHoverOut() {
			$(this).find(".sub").stop().fadeTo('fast', 0, function () {
				$(this).hide();
			});
		}
	
		var config = {
			sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
			interval: 100, // number = milliseconds for onMouseOver polling interval    
			over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
			timeout: 500, // number = milliseconds delay before onMouseOut    
			out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
		};
	
			//Check if main nav mega menu exists
			if ($("ul#mainnav_mega").length > 0) {
				$("ul#mainnav_mega li .sub").css({ 'opacity': '0' });
				$("ul#mainnav_mega li").hoverIntent(config);
			}
			
			//Check if top nav mega menu exists
			if ($("ul#topnav_mega").length > 0) {
				$("ul#topnav_mega li .sub").css({ 'opacity': '0' });
				$("ul#topnav_mega li").hoverIntent(config);
			}

	});

}
