﻿/** 
* Common Javascripts
* @author Mike Cooper, UTMB Public Affairs <mgcooper@utmb.edu>
* @author Toby Smith, UTMB EIS <tlsmith@utmb.edu>
* LastUpdated: September 13, 2011
*/


// =========== 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 7 or below");
    }
});*/




$(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, see: Directory tab on iUTMB
	//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, see: Directory tab on iUTMB
	//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, see: Directory tab on iUTMB
	//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>');
}
	

// *NEW - jQuery ADBOX: Rounded Corners Wrapper for Temps w/out Rounded Corners
//Check and run if div.adbox is present
if ($("div.adbox").length > 0) {
	$("div.adbox").wrap('<div class="adbox_wrapper">' +
		'<div class="adbox_t"><div class="adbox_b">' +
		'<div class="adbox_l"><div class="adbox_r">' +
		'<div class="adbox_bl"><div class="adbox_br">' +
		'<div class="adbox_tl"><div class="adbox_tr">' +
		'</div></div></div></div>' +
		'</div></div></div></div></div>');
}



}); //!!!! IMPORTANT Documetn Ready EOF / !!!! ------------------------------------------------------------------------- !!!!!!!!!!!!!!!! --------->>>



//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
*/

/* OLD Accordion ------------------>>>*/
$(function () {
	var api = $("#accordionSideNav").tabs("#accordionSideNav div.paneAccordionSideNav",
		{ tabs: 'h2', effect: 'slide', initialIndex: null, api: true });
	
	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();
			}
			
			//$("#accordionSideNav").tabs("#accordionSideNav div.paneAccordionSideNav", {initialIndex: null,tabs: 'h3', event: 'click'});
			//this.getPanes().hide();
			
			//$('div.paneAccordionSideNav').hide();
		});
		
		//api.getPanes().hide();
		
		//Disable accordion pane opening via #hashtag if #id does not exist
		//See this for woring on fix: http://flowplayer.org/tools/forum/25/73898
		//$("#cfg-sidebar .cfg-accordion").tabs(".cfg-tabs", {initialIndex: -1,tabs: 'h3', event: 'click'});
		//$("#accordionSideNav").tabs("#accordionSideNav div.paneAccordionSideNav", {initialIndex: null,tabs: 'h3', event: 'click'});
		//this.getPanes().hide();
		//$('div.paneAccordionSideNav').hide();
	}
});



//NEW Studip Simple Accordion: http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/
$(document).ready(function () {

	//ACCORDION BUTTON ACTION	
	$('div.accordionButton').click(function () {
		$('div.accordionContent').slideUp('normal');
		$(this).next().slideDown('normal');
	});

	//HIDE THE DIVS ON PAGE LOAD	
	$("div.accordionContent").hide();

});



//Accordion SideNav w/ Flyout Handler
//Added by mgcooper@utmb.edu 10/5/2011

$(document).ready(function () {
	$("div.paneAccordionSideNav ul").hover(
	function () { 
		$("div.paneAccordionSideNav ul").addClass("relativePosition");
	});
	
	/* NOTE: Moved the "removeClass" relativePosition to the MegaHoverOver() so nav flyouts overlap correctly */
	/*$("div.paneAccordionSideNav ul").mouseout(
	function () { 
		$("div.paneAccordionSideNav ul").removeClass("relativePosition");
	});*/	
});



/**
* 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 ------------>>
// Added by mgcooper@utmb.edu 8/23/2011

//check and run if JQuery hoverIntent plugin is installed first
if(jQuery().hoverIntent) {
     
	$(document).ready(function () {
	
		function megaHoverOver() {
			
			//Accordion Fix - NOTE: Moved the "removeClass" relativePosition to the MegaHoverOver() so nav flyouts overlap correctly ------------>>
			$("div.paneAccordionSideNav ul").removeClass("relativePosition");
			
			$(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);
			}

	});

}
