/**
 * menu.js
 * This is the main script that controls the folding
 * of content and the menu for the template engine.
 * @version 1.0
 * @revision 5/3/2011
 * @author Joseph Pahl
 * @copyright Virginia Tech
 */
//Activating the menu system
$(document).ready(function () {	

	hide_a_content();
	
	//activating accordion containers
	$('.acc_top').each(function (ind,ele) { 
			bind_click_events(ele);
	});
	
	$('#search-query').click(function (e) {
		if($('#search-query').val() == 'Enter your search here') {
			$('#search-query').val('');
		}
	});
	
	/*activates the top panel of the page*/
	$(".btn-slide").click(function(){
		$("#panel").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});
	
	//handles missing target attribute on anchors
	$('.target_blank').click(function (e) {
		$(this).attr('target','_blank');
	});
	
	$('#breadcrumb_text').html($('#bread_crumb').attr('content'));
	
	$('a').click(function (e) {
		delete_cookie("unfold_target");
		if ($(this).hasClass('duplicate_unfold')) {
			set_cookie("unfold_target",$(this).attr('rel'),1);
		}
		return true;
	});
		
	//Loading fancy box
	if (typeof fancybox_load == 'function') {
		fancybox_load();
	}
	
	//Loading tab controller
	if (typeof tab_load == 'function') {
		tab_load();
	}
		
	//Loading internal page JavaScript
	if (typeof page_load == 'function') {
		page_load();
	}
	
	//Loading internal page JavaScript
	if (typeof hours_dialog == 'function') {
		hours_dialog();
	}
	
	//Load table striping
	stripe_tables();
}); //end document.ready

function stripe_tables() {
	$('.table_stripe').each(function (e) {
		$(this).find('tr:even:has(td)').each(function (e) {if (!$(this).hasClass('tr_no_stripe')) {$(this).addClass('tr_stripe')}});
	});
}//end function stripe_tables

function clearquery(id) {
	if(!document.getElementById(id)) return false;
	if (document.getElementById(id).value == "Enter your search here")
	{
		document.getElementById(id).value = "";
	}
}

/**
 * Hide_a_content
 * Folds all content.
 */
function hide_a_content() {
	//hiding the divs
	$('.acc_content').each(function (ind, ele) {
		$(ele).hide();
	});
}//end function hide_a_content

/**
 * activate_a_container
 * Activates the fold containers.
 * @param object reference to the fold container.
 */
function activate_a_container(container) {
	//searching for toggles and binding click events
	container.children('.acc_toggle').each(function (ind, ele) {
		bind_click_events(ele);
	});
		
}//end function activate_a_container

/**
 * bind_click_events
 * Creates the triggers that causes the folds to open.
 * @param String Target that is going to get the click bind.
 */
function bind_click_events(ele) {
		var ss = 350; //speed of fold
		var sc = false; //fold all the subcontents on parent close
		$(ele).find('.acc_toggle').click(function (elm) {
			var p = $(this);
			var t = p.next().next('.acc_content');
			var s = p.siblings('.acc_content');
			/*expand and collapse code*/
			if (p.hasClass('has_CE_notify')) {
				var si = p.siblings('.acc_toggle');
				si.each(function (e) {($(this).hasClass('has_CE_notify'))?$(this).children('.is_CE_notify:first').html('Expand'):null;});
				(p.children('.is_CE_notify:first').html() == 'Expand')?p.children('.is_CE_notify:first').html("Collapse"):p.children('.is_CE_notify:first').html("Expand");
			}
			/*end expand and collapse code*/
			s.each(function (e) {
				if($(this).css('display')!='none'){(sc)?$(this).children('.acc_content').slideUp(ss):null;$(this).slideUp(ss);}});
				(t.css('display')!='none')?t.slideUp(ss):t.slideDown(ss);
			});
}//end function bind_click_events

/**
 * set_cookie
 * Sets a cookie on the client side
 * @param String name Name of the value to be set
 * @param String value Value to be set
 * @param int days number of days before value exspires
 */
function set_cookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * get_cookie
 * Returns the value of a cookie or null on no cookie set
 * @param String name Name of the value
 * @return String
 */
function get_cookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/**
 * delete_cookie
 * Deletes a variable from the cookies
 * @param String name Name of the value to be deleted
 */
function delete_cookie(name) {
    set_cookie(name,"",-1);
}


/**
 * open_menu
 * Opens a triggerd fold.
 * @param String Id of the target fold the system is going to open.
 */
function open_menu(targetID) {
	var ss = 350; //speed of fold
	$(targetID).show().parents('.acc_content').map(function (ind, ele){$(this).show();});
}

