/*functions for search box*/
function submitForm(formId) {
	document.getElementById(formId).submit();
}
function clearquery(id,text) {
	if(!document.getElementById(id)) return false;
	if (document.getElementById(id).value == text) {
		document.getElementById(id).value = "";
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
//slide down for quicklinks
$(document).ready(function() {
	// show quicklinks
	$('#vt_ql_link').css('display', 'block');
	// left tab grup this when clicked
	$('#vt_ql_link').click(function(){
		$('#vt_ql_list').slideToggle('fast'); 
	});	

/* stripe table rows */
	$("table tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$("table tr:even").addClass("alt");
	$("div.vt_table_no_stripe table tr:even").removeClass("alt");

/* no table styles */
	$("div.vt_table_no_styles table tr").mouseover(function() {$(this).removeClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$("div.vt_table_no_styles table tr:even").removeClass("alt");
	
//for columns
	runEqualHeights(".vtColumns");
	
//Checking for IE
	if($.browser.msie){
		if($.browser.version < 9){
			// code to try to make ie standards-compliant
			$('head').prepend('<script type="text/javascript" src="/global_assets/js/ie_fixer.js"></script>');
		}
	}
	
});

// see if the set of tabs or accordions should be open to a tab other than the first one
var winloc = window.location.toString();
var itemSelect = winloc.search(/#select=/i);
if(itemSelect>0){
	var itemToOpen = Number(winloc.substring(itemSelect+8, winloc.length));
	if(itemToOpen=="NaN"){
		itemToOpen = -1;
	}else{
		itemToOpen -= 1;
	}
}else{
	var itemToOpen = -1;
}

//for tabs
$(document).ready(function() {
	if(jQuery.isFunction(jQuery.fn.tabs)){
		var tabCounter = 0;
		$('.vtTab').each(function(aTabClass){
			// only allow the first set to be opened using apage.html#select=2
			if(tabCounter == 0 && itemToOpen > -1){
				var aTab = $(this).tabs();
				if(itemToOpen <= aTab.tabs("length")){
					aTab.tabs("select", itemToOpen);
				}
			}else{
				$(this).tabs();
			}
			tabCounter++;
		});
	}
});

//for accordions
$(document).ready(function() {
	if(jQuery.isFunction(jQuery.fn.accordion)){
		$('.vtAccordion_closed').accordion({autoHeight: false, collapsible: true, active: false, change: function() {runEqualHeights();}});
		var aCounter = 0;
		$('.vtAccordion_open').each(function(anItemClass){
			// only allow the first set to be opened using apage.html#select=2
			if(aCounter == 0 && itemToOpen > -1){
				// accordion doesn't have a length function like tabs, look for h6 (generally the heading)
				var itemSize = $(this).children('h6').size();
				if(itemToOpen < itemSize){
					$(this).accordion({autoHeight: false, collapsible: true, active: itemToOpen, change: function() {runEqualHeights();}});
				}else{
					$(this).accordion({autoHeight: false, collapsible: true, active: 0, change: function() {runEqualHeights();}});
				}
			}else{
				$(this).accordion({ autoHeight: false, collapsible: true, change: function() {runEqualHeights();}});
			}
			aCounter++;
		});
	}
});


//javascript form validation
$(document).ready(function(){
	// only load necessary files if we have a special class to trigger it												 
	if($(".vt_js_form").size()>0){
		// loading necessary javascript files for validation, wysiwyg editor, datepicker
		$.getScript("/global_assets/js/forms/jquery.ui.core.pack.js", function(){
			$.getScript("/global_assets/js/forms/jquery.wysiwyg.pack.js", function(){
				$(".vt_js_form .wysiwyg").wysiwyg({
					controls : {
						separator01 : { visible: true},
						indent : { visible: true },
						outdent: { visible: true },
						separator04 : { visible : true },
						insertOrderedList : { visible : true },
						insertUnorderedList : { visible : true },
						insertImage : { visible : false },
						increaseFontSize : {visible: false },
						decreaseFontSize : {visible: false },
						separator09 : { visible: false}
					}
				});				
				$.getScript("/global_assets/js/forms/jquery.validate.js", function(){
					$.getScript("/global_assets/js/forms/additional-methods.js", function(){	
						$.getScript("/global_assets/js/forms/ui.datepicker.js", function (){
							$(".vt_js_form .date").datepicker();
							$(".vt_js_form").validate();
						});
					});
				});																	 
			});	
		});
		$('head').prepend('<link rel="stylesheet" href="/global_assets/js/forms/jquery.wysiwyg.css" type="text/css" media="screen" />');
	}
});


var o_vt_dataTable = null;
//Trigger dataTable on a table with class .vt_dataTable (table must have thead and tbody defined)
$(document).ready(function(){
	if($("table.vt_dataTable").size()>0){
		
		// loading necessary javascript files for validation, wysiwyg editor, datepicker
		if(!(jQuery.isFunction(jQuery.fn.dataTable))){
			$('head').prepend('<script type="text/javascript" src="/global_assets/js/datatables/jquery.dataTables.min.js"></script>');
		}
		o_vt_dataTable = $('table.vt_dataTable').dataTable( {
			"bJQueryUI": true,
			"aLengthMenu":[[25, 50, 100, -1],[25, 50, 100, "All"]],
			"sPaginationType": "full_numbers",
			"aaSorting":[[0,'asc']],
			"iDisplayLength": 25,
			"oSearch": {"sSearch": "", "bRegex":false, "bSmart": false},
			"fnDrawCallback" : function() {runEqualHeights();}
		});
	}
});

// Include necessary code for table sorting
$(document).ready(function(){
	if($("table.tablesorter").size()>0){
		if(!(jQuery.isFunction(jQuery.fn.tablesorter))){
			$('head').prepend('<script type="text/javascript" src="/global_assets/js/tablesort/jquery.tablesorter.js"></script>');
			$('head').prepend('<link rel="stylesheet" type="text/css" href="/global_assets/js/tablesort/vt_tablesorter.css"/>');
		}
	}
});

//Rerun equal heights, default to 3 columns
function runEqualHeights(columns){
	columns = (typeof columns == 'undefined') ?
			"#vt_right_col, #vt_body_col, #vt_nav_col" : columns
	if(jQuery.isFunction(jQuery.fn.equalHeights)){
		$(columns).equalHeights();
	}else{
		$.getScript("/global_assets/js/plugins/jquery.equalheights.js", function (){
			$(columns).equalHeights();
		});
	}
}
function checkLinkBack(aUrl){
	var regExpMatch = new RegExp("https:\/\/[a-z]*\.cms\.vt\.edu.*|https:\/\/ensemble\.vt\.edu.*");
	var regRes = regExpMatch.exec(aUrl);
	if(regRes && regRes[0].length>0){
		return true;
	}
	return false;
}


//Starts with function
if (typeof String.prototype.startsWith != 'function') {
	  String.prototype.startsWith = function (str){
	    return this.slice(0, str.length) == str;
	  };
	}
// ends with function
if (typeof String.prototype.endsWith != 'function') {
	  String.prototype.endsWith = function (str){
	    return this.slice(-str.length) == str;
	  };
	}

/*
// shows preview bar and gives end users ability to change context to show
// what should be published by the cms
$(document).ready(function(){
	if(checkLinkBack(window.location.href)){
		var pscript = document.createElement('script');
		pscript.type = 'text/javascript';
		pscript.src = '/global_assets/js/vt_cms_preview_utils.js';
		$('head').append(pscript);
		showPreviewBar("false");
	}
});
 */

// For asset developers
$(document).ready(function(){
	if(hasLocalAssetsCookie()){
		var pscript = document.createElement('script');
		pscript.type = 'text/javascript';
		pscript.src = '/global_assets/web_designer/vt_cms_web_designer_utils.js';
		$('head').append(pscript);
	}
});

function setLocalAssetsCookie(){
	$.setCookie("LA_ENABLE", "Default", 
			{
				duration: 1825,
				path: '/',
				domain: 'vt.edu',
				secure: false
			}
	);
	window.location.reload();
}

function hasLocalAssetsCookie(){
	var la_cookie=$.readCookie("LA_ENABLE");
	if(la_cookie!=null && la_cookie!= ""){
		return true;
	}
	return false;
}


