/*
 * Scrips included here are applicable to every page (for example, Header search, etc)
 */
$(document).ready(function(){

	/************ Header Search ************/
	$("#txtHeaderSearch").focus(function(){
		if ($(this).val() == "Search..."){ $(this).val("");	}
	});
	$("#txtHeaderSearch").blur(function(){
		if ($(this).val() == ""){ $(this).val("Search...");	}
	});
	$("#btnHeaderSearch").click(function(){
		doSearch();
	});
	$('#txtHeaderSearch').keyup(function(e){
		if(e.keyCode == 13){ doSearch(); }
	});
	function doSearch()
	{
		if ($('#txtHeaderSearch').val() == "Search..." || $('#txtHeaderSearch').val() == ""){ 
			alert("Please enter a search term.");
		} else {
			var searchString = $("#txtHeaderSearch").val().replace(/ /g, "+");
			window.location = "/11/training_search.php?searchString="+searchString;
		}		
	}
	
	/************ HEADER MENU : DROPDOWNS ************/
	var delay = 20;
	var tID;
	$("#training").hover(
		function(){
			$(this).find('a').addClass('over');
			if(tID){clearTimeout(tID);}
			$("#training-dropdown").stop(true,true).fadeIn();
		},
		function(){
			if(tID){clearTimeout(tID);}
			tID = setTimeout(function(){$("#training-dropdown").stop(true,true).fadeOut()}, delay);
			$(this).find('a').removeClass('over');
		}
	);  
	$("#training-start").hover(
		function(){
			$(this).find('a').addClass('over');
			if(tID){clearTimeout(tID);}
			$("#training-dropdown").stop(true,true).fadeIn();
		},
		function(){
			if(tID){clearTimeout(tID);}
			tID = setTimeout(function(){$("#training-dropdown").stop(true,true).fadeOut()}, delay);
			$(this).find('a').removeClass('over');
		}
	);  
	$("#training-dropdown").hover(
		function(){
			$("#training").find('a').addClass('over');
			$("#training-start").find('a').addClass('over');
			if(tID){clearTimeout(tID);}
			$(this).stop(true,true).fadeIn();
		}, 
		function(){
			if(tID){clearTimeout(tID);}
			tID = setTimeout(function(){$("#training-dropdown").stop(true,true).fadeOut()}, delay);
			$("#training").find('a').removeClass('over');
			$("#training-start").find('a').removeClass('over');
		}
	);
	
	
	/************ STANDARD ERRORS, MESSAGES ************/
	$(".std-success").live('click', function(){
		$(this).hide();
	});
	$(".std-error").live('click', function(){
		$(this).hide();
	});
	
	/************ PANELS ************/
	if ($("div[class^=panel]").length != 0){
		$(".panel-collapse")
			.find(".panel-header")
			.prepend('<span class="tbarrow"></span>')
			.end()
			.find(".panel-content");
		$(".panel-collapse .panel-header").click(function() {
			$(this).parents(".panel-collapse:first").find(".panel-content").slideToggle("fast");
			$(this).parents(".panel-collapse:first").find(".panel-header .tbarrow").toggleClass("tbarrow-closed"); 
			return false;	
		});
	}
	
	/************ PROGRESS BAR ************/
	//if ($(".progress-bar").length != 0){		
	//	$(".progress-bar").each(function(){
	//		var progress = $(this).text();
	//		$(this).text("");
	//		$(this).append('<div class="progress" style="width:'+progress+';">&nbsp;</div>');
	//		$(this).after('<div class="progress-bar-text">'+progress+'</div>');
	//	});		
	//}
	
	/************ GROUP SESSION, QUEUE ************/
	$("#lnkRefreshGroupSession").click(function(e){
		e.preventDefault();
		location.reload();
	});
	$("#lnkGiveUpGroupSeat").click(function(e){
		e.preventDefault();
		$.getJSON("/11/gateway/validate_session.php", {action: "FreeSeat"}, function (json){
			location.reload();
		});
	});
	$("#lnkLeaveGroupQueue").click(function(e){
		e.preventDefault();
		$.getJSON("/11/gateway/validate_session.php", {action: "LeaveQueue"}, function (json){
			location.reload();
		});
	});
	
	/************ EMERGENCY MESSAGE ************/
	$("#btnCloseEmergencyMessage").click(function(e){
		e.preventDefault();
		var doRemove = confirm("Are you sure you want to close this message?  We will not show it to you again.");
		if (doRemove){
			$.getJSON("/11/gateway/global.php", {action: "RemoveEmergencyMessage", dlp:document.location.protocol}, function (json){
				location.reload();
			});
		}
	});
	
	/************ NEW TRAINING PGS POPUP ************/
	$("#lnkRemoveNewTrainingPopup").click(function(e){
		e.preventDefault();
		//var doRemove = confirm("Are you sure you want to close this message?  We will not show it to you again.");
		//if (doRemove){
			$.getJSON("/11/gateway/global.php", {action: "RemoveNewTrainingPopup", dlp:document.location.protocol}, function (json){
				location.reload();
			});
		//}
	});
	
	/************ UPDATE VIEW VIEWS ************/
	//var intervalVideoViews = window.setInterval('updateVideoViews()', 30000); //30 sec
	
});

function updateVideoViews(){
//	$.getJSON("/11/gateway/global.php", {action: "UpdateSiteVideoViews", dlp:document.location.protocol}, function (json){
//		if (json.success){
//			$("#site-video-views-count").html(json.msg[0]);
//		}
}
