/* Main .JS file for Pardee Hospital. T. Markle, integritive.com 2009-03-30 */
var updating = false;

	// form validation functions

	function validate(input,type,form,title){
		value=input.value;
		note=input.name+"Note"; // Need this for notifications
		$('#submitNote').html(" ");
			// check exception (matches to previous input) needs to be JS for simplicity
			if(type=='check'){ // we assume that the check is named 'email2' or similar
				previousDiv="tM"+input.name.substr(0,input.name.length-1);
				previous=$('#'+previousDiv+' > input').val();
				if(previous!=value){ 
					$('#'+note).html("Please make sure this matches the previous value.");
				} else {
					checkFade(note);
				}
			} else { // usual ajax check
				$('#'+note).html(" ");
				$('#'+note).load("../classes/tables.class.php",{'ajax':'1','value':value,'type':type,'form':form,'title':title},function(a,b){
					if(a=='' && b=='success'){
						checkFade(note);
						checkFade("submitNote");
					} else if(a!='' && b=='success'){

						$('#submitNote').html("Please fix noted items");
					} else if(b=='error'){
						$('#'+note).html("");
						$('#submitNote').html("");
					}
					
			 });
		}
	}
	
	function cleared(){
	$('#submitNote').html("");	
	warning='';
	$('.note').each(function(){
	warning=warning+$(this).text(); 
	});
	if(warning!=''){
		$('#submitNote').html("Please fix items with notes");
		return false;
	} else {
		return true;
	}
	}


	function checkFade(note){
		$('#'+note).html("");
		$('#'+note+"Check").fadeOut(1000);
   	$('#'+note+"Check").queue(function () {
        $(this).remove();
        $(this).dequeue();
      });

	}


$(document).ready(function(){	   

	// $("body").prepend('<div id="overlay"><img src="./sprites/contact.jpg"></div>');
	// $("body").prepend('<div id="overlay"><img src="./sprites/pardeeWho_we_are.jpg"></div>'); // home
	 $("#overlay").css({'position' : 'absolute', 'top' : '0px', 'left' : '113px', 'z-index' : '1000'});
	 $("#overlay").fadeTo(1, 0.5);   
	 $("#overlay").toggle(
			function () {
				$(this).fadeTo(2, 1.0);   
    	},
			function () {
				$(this).fadeTo(2, 0.5);   
    	},
			function () {
				$(this).fadeTo(2, 0.01);   
			},
			function () {
				$(this).fadeTo(2, 0.5);   
    	}
			);

// font-resize
  $('#resizeDown').click(function(){
    size="12px";
    resizeContent(size);
  });

  $('#resizeZero').click(function(){
    size="16px";
    resizeContent(size);
  });

  $('#resizeUp').click(function(){
    size="18px";
    resizeContent(size);
  });

  function resizeContent(size){
    $('#content p').css('font-size',size);
    $('#content p a').css('font-size',size);
    $('#content li a').css('font-size',size);
    $('#content li').css('font-size',size);
    $('#content').css('font-size',size);
  }

	$('ul.faq li p').hide();
	$('ul.faq li').toggle(function(){
		$(this).children().slideDown();
	},function(){
		$(this).children().slideUp();
	});


	$('#editHandle').click(function() {
		$('#contentEdit').show();
		$('#content').hide();
		$('#editHandle').hide();
		return false;
	});

	$('#updateCancel').click(function() {
		$('#contentEdit').hide();
		$('#content').show();
		$('#editHandle').show();
		return false;
	});
	

	// Ajax handler for updating content on an editable page.
	$('#updateContent').click(function() {
		// Make the edit to the field
		if (updating == false) {
			updating = true;
			$("#ajax_spinny").show();
			// Make sure content from tinymce is copied back into the origianl text area.
			tinyMCE.triggerSave();
			// Get the value from the form.
			var data = $('#contentForm').serialize();

			$.ajax({
				url: '/admin/edit_page.php',
				type: 'POST',
		        dataType: 'xml',
				data: data,
		        timeout: 5000,
		        error: function(xhr, tstatus, error_thrown) {
		            var sout = $(xhr.responseXML).find('signedout').text();
		            if (sout == 1) {
		                alert('Oops! Your session is expired and you have been signed out. Please sign in again before attempting to perform this action.');
		            } else {
		                alert('Error performing action. Try again later. If the problem persists, contact support.');
		            }
					updating = false;
					$("#ajax_spinny").hide();
		        },
				success: function(xml) {
					// Update the text with the new field value
					var result = $(xml).find('result').text();
					if (result == 'success') { 
						var val = $(xml).find('content_body').text();
						$('#content_body').val(val);
						$('#content').html(val);
					} else {
						var error = $(xml).find('content_body').text();
						alert(error);
					}
					updating = false;
					$("#ajax_spinny").hide();
					$('#contentEdit').hide();
					$('#content').show();
					$('#editHandle').show();
				}
			});
		}
		return false;
	});
	
	$('.deleteFile').click(function() {
		if (updating == false) {
			if (!confirm('Are you sure you want to delete this file?')) {
				return false;
			}
			updating = true;
			$("#ajax_spinny").show();
			// Get the id of the parent element to get which record to remove.
			var id = $(this).attr('id').replace(/^file_([0-9]*)$/m, '$1');
			$.ajax({
				url: '/admin/delete_file.php',
				type: 'POST',
		        dataType: 'xml',
				data: "id=" + id + "&uid="+$.cookie("sid"),
		        timeout: 5000,
		        error: function(xhr, tstatus, error_thrown) {
		            var sout = $(xhr.responseXML).find('signedout').text();
		            if (sout == 1) {
		                alert('Oops! Your session is expired and you have been signed out. Please sign in again before attempting to perform this action.');
		            } else {
		                alert('Error performing action. Try again later. If the problem persists, contact support.');
		            }
					updating = false;
					$("#ajax_spinny").hide();
		        },
				success: function(xml) {					
					updating = false;
					$("#ajax_spinny").hide();
					// Refresh page.
					//http://pardeenew/admin/files.php?&u=1
					var u = (window.location.href.search(/\?/) == -1) ? '?u=1' : '&u=1';
					window.location.href=window.location.href + u;
				}
			});
		}
		return false;
	});
});


// Menu Behavior
$('.leftNav').ready(function(){
	// hide subnavs.
	$('.subNavHolder').each(function(){
		if($(this).children("ul").children(".selected").length==0){$(this).hide();}
	});
	// show subnavs that have an element that is selected
	$('.subNav > .selected').parent().parent().show();
	
	// show subnavs that have the preceeding LI selected
	$('.selected + li').show();

/*
var config = {    
     sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: doSubs, // function = onMouseOver callback (REQUIRED)    
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: doNothing // function = onMouseOut callback (REQUIRED)    
};
 //this function includes all necessary js files for the application  
 function include(file)  
 {  
   
   var script  = document.createElement('script');  
   script.src  = file;  
   script.type = 'text/javascript';  
   script.defer = true;  
   
   document.getElementsByTagName('head').item(0).appendChild(script);  
   
 }  
   

 include('jquery.hoverIntent.minified.js');  
$('.leftNav > li').hoverIntent( config )


	// add hover behavior
function doSubs(){}

	$('.leftNav > li').each(function(){$(this).removeClass("currentHover");}); // strip hover flag from all lis
	if($(this).next().hasClass("subNavHolder")){ // if it's a title for a subNav
		$(this).next().addClass("currentHover"); // add the hover flag to this li
		$('.subNav').each(function (){
			// don't hide subnavs that have thir children selected or are siblings of the current leftNav LI, or are the current subnav
			if($(this).children(".selected").length==0 && $(this).parent().prev().hasClass("selected")==false && !$(this).parent().hasClass("currentHover")){ 
			$(this).parent().slideUp(420);
			}
		}); 
		$(this).next().stop(true,true);
		if($(this).next(":hidden").length==1){$(this).next().slideDown(420);} // dont re-slide already open subNavs
	}
}
*/
function doNothing(){}
});

// Run the news ticker (triggered in the pod itself)
var tickerLine=0;
function cycleTicker(){
	//Get the subs of ticker div into an array
	var subs=$('#newsPodTicker').children();
	//if(tickerLine==0){$(subs).hide();}
	// fade all tickers
	$(subs).fadeOut(800);
	$(subs[tickerLine]).queue(function(){	
		$(this).fadeIn(800);
        $(this).dequeue();
	});
//	$(subs[tickerLine]).show();

		// show the next one


		// set cycle for next
			setTimeout(function(){ 
				tickerLine=tickerLine+1;	
				if(tickerLine>=subs.length){tickerLine=0;} 	
				cycleTicker();},4000);
}

$(function() {
 //for home rotating images for both main site and foundation
  $('#homeGraphic').cycle({ 
    fx:    'fade', 
    speed:  2000,
	timeout:  6000,
	prev:   '#prev', 
    next:   '#next'
 });
 });//end whole thing



