$(document).ready(function() { 
	VideoJS.setupAllWhenReady() 
});

function submit_form(theForm) {
				document.form_selection.submit();
			}
function doPrint()
{
	document.Grant.Submit.value='Print'; 
	window.open("about:blank", "print", "width=500,height=400");
	document.getElementById('Grant').target = "print";
	//document.Grant.submit();
	return false;
	
}
$(function() {
	$("#FiscalYear,#PeriodStart,#PeriodEnd").datepicker({showOn: 'button', buttonImage: '_images/calendar.png', buttonImageOnly: true});
});

 var NN = false, Mac = false; IE = false, changeCount = 0;



	   if (navigator.appName == 'Microsoft Internet Explorer') IE = true;

	   if (navigator.appName == 'Netscape') NN = true;

	   if (navigator.platform == 'MacPPC') Mac = true;	



	   function fixWindow() {

	      if (Mac && NN) {

	         window.menu.location.reload(true);

		 window.body.location.reload(true);

	      }

	   }
	   
	   
	   
	   function ChangeApplySection(p_Section) {
	   
	   		changeCount++;
			var x;
			/* turn off all the layers */
			x = document.getElementById("Instructions");
			x.style.display = "none";		
			x = document.getElementById("SectionLogin");
			x.style.display = "none";		
			x = document.getElementById("SectionOneLayer");
			x.style.display = "none";		
			x = document.getElementById("SectionTwoLayer");
			x.style.display = "none";		
			x = document.getElementById("InfoSaved");
			x.style.display = "none";		
			x = document.getElementById("ButtonsDiv");
			x.style.display = "";		
			x = document.getElementById("submitButton");
			x.style.display = "none";		

			switch (p_Section) {
				case "0":
					x = document.getElementById("Instructions");
					x.style.display = '';					
					x = document.getElementById("ButtonsDiv");
					x.style.display = "none";		
					break;
				case "-1":
					x = document.getElementById("SectionLogin");
					x.style.display = '';			
					x = document.getElementById("ButtonsDiv");
					x.style.display = "none";				
					break;
				case "1":
					x = document.getElementById("SectionOneLayer");
					x.style.display = '';	
					break;
				case "2":
					x = document.getElementById("SectionTwoLayer");
					x.style.display = '';	
					x = document.getElementById("submitButton");
					x.style.display = '';					
					break;
			}							
			
	   }
	   
	   function ExplanationWindow(p_URL) {
		var newWindow = open(p_URL,'Explanation','width=776,height=240,status=no,menubar=no,toolbar=no,scrollbars=yes,resizable=no');	   
	   }
	   
	   function CheckState() {
	    if (document.Grant.State.value != "PA" && document.Grant.State.value != "Pa" && document.Grant.State.value != "pa"
	        && document.Grant.State.value != "Pennsylvania" && document.Grant.State.value != "pennsylvania")
			document.getElementById('divStateWarning').style.display = '';
		else
			document.getElementById('divStateWarning').style.display = 'none';
			
			//var newWindow = open('info/GrantWarn.html','Explanation','width=400,height=400,status=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes');	   	   
	   }

        var sessionTimeout = "<%= Session.Timeout %>";
        function DisplaySessionTimeout()
        {
            sessionTimeout = sessionTimeout - 2;
            window.setTimeout("alert('Your session is about to expire.\\n\\n Please save or submit your application to prevent losing your data.')", ((sessionTimeout) * 60000));
        }

		function doSave() {
			document.getElementById('Grant').target = "";
			document.Grant.Submit.value='Save'; 
		}
		function doSubmit() {
			document.getElementById('Grant').target = "";
			document.Grant.Submit.value='Submit'; 
		}
		

	$(document).ready(function(){
    	var validator = $("#Grant").validate({
		  	errorClass: "invalid",
			errorPlacement: function(error, element) { 
				if ( element.is(":radio") ) 
					error.appendTo( element.parent() ); 
				else if ( element.is(":checkbox") ) 
					error.appendTo ( element.next() ); 
				else 
					error.appendTo( element.parent() ); 
        	},
			rules: {

				FiscalYear: {
				  required: function(element) {
					return $("#AuditYes").is(':checked');
				  }
				},
				PastFundingDesc: {
				  required: function(element) {
					return $("#PastFundingYes").is(':checked');
				  }
				}
			},
			invalidHandler: function(form, validator) {
				alert('Some fields are missing. Please complete the entire application before submitting.');
				document.getElementById("submitButton").disabled = false;
			}
	
	   });
	   //DisplaySessionTimeout();
       
       if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
			$('#prevBtn').hide();
            $('#nextBtn').hide();
		}
	   
  	});
	
	
	$(document).ready(function() {

	$(".deleteMedia").click( function() {
		if(confirm('Delete this media?'))
		{
			var opener = window.open($(this).attr("href"));
		}
		return false;
	});
	
});

function handleForm( which )
{
	if(document.entryForm.title.value == "")
	{
		alert("Title cannot be empty.");
		return false;
	}
	if(	CKEDITOR.instances.entry.getData() == "")
	{
		alert("Entry cannot be empty.");
		return false;

	}
	
	if(	which=='submit' && !document.entryForm.terms.checked)
	{
		alert("You must agree to the terms in order to continue.");
		return false;

	}
	
	document.entryForm.buttonClicked.value = which;
		
}
function addMedia( type, id )
{
	if(!id)
		id='';
	var map;
	window.name = "opener";
	if(type == 'p')
		map = window.open('UploadPhoto.aspx?mediaId=' + id + '','_media','width=600,height=500');
	else
		map = window.open('UploadVideo.aspx?mediaId=' + id + '','_media','width=600,height=500');
	return false;
}

function addVideo( id, title )
{
	$("#videoAdd").append("<div id=\"video" + id + "\" class=\"mediaBlock\"><a href=\"DeleteMedia.aspx?t=v&media=" + id + "\" class=\"deleteMedia\"><img src=images/delete.png align=right border=0 vspace=2></a><input type=\"hidden\" name=\"video\" value=\"" + id + "\"><a href=\"#\" onclick=\"addMedia('v','" + id + "'); return false;\" class=\"video\" target=\"_new\"><img src=\"http://img.youtube.com/vi/" + id + "/default.jpg\"  style='margin-left: 4px;' border=\"0\" width=66 height=47></a><br clear=\"all\"><input type=\"radio\" name=\"highlightedMedia\" value=\"v-" + id +  "\"><span style=\"font-size: 10;\">Featured</span></div>");
	
	$(".deleteMedia").click( function() {
		if(confirm('Delete this media?'))
		{
			var opener = window.open($(this).attr("href"));
		}
		return false;
	});	

											
}

function addPhoto( id, title )
{
	removePhoto(id);
	$("#photoAdd").append("<div id=\"photo" + id + "\" class=\"mediaBlock\"><a href=\"DeleteMedia.aspx?t=p&media=" + id + "\" class=\"deleteMedia\"><img src=images/delete.png align=right border=0 vspace=2></a><input type=\"hidden\" name=\"photo\" value=\"" + id + "\"><a href=\"#\" onclick=\"addMedia('p'," + id + "); \" class=photo><img src=\"userfiles/spotlight/thumb_" + title + "\" border=\"0\" align=center style='margin-left: 4px;'></a><br clear=\"all\"><input type=\"radio\" name=\"highlightedMedia\" value=\"p-" + id +  "\"><span style=\"font-size: 10;\">Featured</span></div>");
	$(".deleteMedia").click( function() {
		if(confirm('Delete this media?'))
		{
			var opener = window.open($(this).attr("href"));
		}
		return false;
	});

}

function removePhoto(id)
{
	$("#photo" + id).remove();
}

function removeVideo(id)
{
	$("#video" + id).remove();
}
