// JavaScript Document

var slideShowPicsXML;
var slideShowPicsARR;
var image_idx=0;
var slideShowDelay=10; // Seconds

/* Tell a Friend Info */
var dlftMessage='You could volunteer for any number of organizations that help children, but CASA is something special. Unlike any other volunteer opportunity, CASA empowers people like you to serve as officers of the court and work with and on behalf of a foster child. You can make a difference in the life of a child and stop the cycle of abuse and neglect for generations to come. Contact your local program today for more information.';
var valid;
var type;
var isr;
var Sender_Name;
var Sender_Email;
var Name;
var Email;
var Message;
var ts;

/*function orderLicensePlate() {
	
	$('#orderPlateDiv').remove();
	$('#innerContent').prepend('<div id="orderPlateDiv" class="ui-corner-all" style="display:none;"></div>');
	
	$('#orderPlateDiv').load('includes/licensePlate.php', function() {
										
											$('#orderPlateDiv').show();
																		   
										});
	
	
}*/


function specialEmail(title,header,toPerson,subject) {
	
	$('#specialEmailDiv').remove();
	$('#innerContent').prepend('<div id="specialEmailDiv" class="ui-corner-all" style="display:none;"></div>');
	$('input[type=button]').fadeTo('slow', 0);
	$('button').fadeTo('slow', 0);
	
	$('#specialEmailDiv').load('includes/forms/special_email.php', function() {
			
			//$('input.button').button();
			
			$('#response').html('');
			$("#message_title").html(title);
			$("#message_header").html(header);
			$('#closeButton').click(function(){ 
											 	$('#specialEmailDiv').remove();
												$('input[type=button]').fadeTo('slow', 1);
												$('button').fadeTo('slow', 1);
											});
			
			//bindButtons();
			$("#sendmail").button({ label: "Send", icons: {primary:'ui-icon-mail-open'} })
				.click(function(){
								
				$('#response').html('');  
				
				//var valid=validateTellAFriend();
				
				var valid = true;
    			var f=$('#specialEmailForm');
				
				$(f).find('.ui-error-text').remove();
				$(f).find('.input').removeClass('ui-state-error'); 
				$(f).find('.input').not('input-normal').addClass('input-normal'); 
				
				var text = trim($(f).find("#text").val());
				
				$(f).find('.required').each(function(){  
													 
													 if ( $(this).val().length<1) {
														valid = false;
														$(this).addClass('ui-state-error');
														errorInfo( this , msg_required);
													}
												});
				$(f).find('.required.email').not('.ui-state-error').each(function(){  
													 
													 if ( !valid_email( $(this) ) ) {
														valid = false;
														$(this).addClass('ui-state-error');
														errorInfo( $(this) , msg_invalied_email);
													}
												});
				
				
				
				if (!valid) {
					$("#response").fadeIn("slow");
					$("#response").html('One or more errors have ocurred');
					
				} else {
					$('#specialEmailForm .input').attr("disabled","disabled");
					$("#response").css("display", "block");
					$("#response").html("Sending message .... ");
					$("#response").fadeIn("slow");
					$('#specialEmailDiv  .msg').hide();
					 
					
					$("#loading").show();
					
					var Message = trim( $("#Message").val() );
					var from=person($('#Sender_Name').val(), $('#Sender_Email').val() );
					
					$.get('includes/token.php', function(data) {	
														result = jQuery.parseJSON(data);
														sendEmail(f,from, toPerson, Message, result.token, subject);
												});
					
					
				}
				
				
				return false;
				
			});


			$("#resetForm").button({ label: "Reset", icons: {primary:'ui-icon-refresh'} })
										.click(function(){ 
															$('#response').html('');
															$('input').removeClass('ui-state-error'); 
															var f=$('#specialEmailForm');
															$(f).find('.ui-error-text').remove();
															$(f).find('.input').removeClass('ui-state-error'); 
															$(f).find('.input').not('input-normal').addClass('input-normal'); 
															$('#specialEmailForm .input[type != button]').val('');
															return false; 
													});
			
			$('#specialEmailDiv').show();
		});
}

function tellFriend(dlftMessage) {
	
	$('#tellafriendDiv').remove();
	$('#innerContent').prepend('<div id="tellafriendDiv" class="ui-corner-all" style="display:none;"></div>');
	$('input[type=button]').fadeTo('slow', 0);
	$('button').fadeTo('slow', 0);
					
	$('#tellafriendDiv').load('includes/forms/tell_a_friend.php', function() {
			
			//$('input.button').button();
			
			$('#response').html('');
			$("#Message").val(dlftMessage);
			
			$('#closeButton').click(function(){ 
											 	$('#tellafriendDiv').remove(); 
											 	$('input[type=button]').fadeTo('slow', 1);
												$('button').fadeTo('slow', 1);
											 });
			
			//bindButtons();
			$("#sendmail").button({ label: "Send", icons: {primary:'ui-icon-mail-open'} })
				.click(function(){
								
				$('#response').html('');  
				
				var valid=validateTellAFriend();
				
				if (!valid) {
					$("#response").fadeIn("slow");
					$("#response").html('One or more errors have ocurred');
					
		
				} else {
		
					$('#tellAFriendForm .input').attr("disabled","disabled");
					$("#response").css("display", "block");
					$("#response").html("Sending message .... ");
					$("#response").fadeIn("slow");
					$('#tellafriendDiv  .msg').hide();
					 
					
					$("#loading").show();
					
					
					var from=person(Sender_Name,Sender_Email);
					var to=person(Name,Email);
					var f=$('#tellafriendDiv');
					var subject='Check out Casa for Kids';
					
					
					$.get('includes/token.php', function(data) {	
														result = jQuery.parseJSON(data);
														sendEmail(f,from, to, Message, result.token, subject);
												});
					
					
				}
				
				
				return false;
				
			});


			$("#resetForm").button({ label: "Reset", icons: {primary:'ui-icon-refresh'} }).click(function(){ resetTellAFriend(); return false; });
			
			$('#tellafriendDiv').show();
		});
}
function validateTellAFriend() {
	valid = true;
	type = 'tellafriend';
	isr = ' is required.';
	Sender_Name = $('#Sender_Name').val();
	Sender_Email = $('#Sender_Email').val();
	Name = $('#Name').val();
	Email = $('#Email').val();
	Message = Name+'<br><br>Your friend '+Sender_Name+' thought you would be interested to know:<br><br>'+trim($("#Message").val());
	ts = trim($("#ts").val());
	var messages=[];
	
	
	$('input').removeClass('ui-state-error'); 
	   
	if (Sender_Name.length<1) {
		valid = false;
		messages.push('Please enter your name');
		$('#Sender_Name').addClass('ui-state-error'); 
	} 
	if (Sender_Email.length<1) {
		valid = false;
		$('#Sender_Email').addClass('ui-state-error');
		messages.push('Please enter your email address');
	} else if (!Sender_Email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
		valid = false;
		messages.push('Please enter your valid email adress');
		$('#Sender_Email').addClass('ui-state-error');
	}
	if (Name.length<1) {
		valid = false;
		messages.push('Please enter your friends name');
		$('#Name').addClass('ui-state-error'); 
	} 
	if (Email.length<1) {
		valid = false;
		messages.push('Please enter your friends email address');
		$('#Email').addClass('ui-state-error'); 
	} else if (!Email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
		valid = false;
		messages.push('Please enter your friends valid email address');
		$('#Email').addClass('ui-state-error');
	}
	if (Message.length<1) {
		valid = false;
		messages.push('Please enter a messahe');
		$('#Message').addClass('ui-state-error'); 
	}

	return valid;
}

function resetTellAFriend() {

	$('#response').html('');
	$('input').removeClass('ui-state-error'); 
	$('#tellAFriendForm .input[type != button]').val('');
	$("#Message").val(dlftMessage);
	
}

function popUpVideoNew (vidSrc, thumbSrc, vidWidth, vidHeight, vidName, vidDescription ) {
	scroll(0,0);
	var vidHtml = '<div id="vidBody"><div align="center" style="float:left; padding:10px; width:350px;"><a href="'+vidSrc+'" style="display:block;width:'+vidWidth+';height:'+vidHeight+';" id="player1"><img src="'+thumbSrc+'" width="'+vidWidth+'" /><br>Click to start video</a></div><div id="vidText" style="float:right; padding:10px; visibility:hidden; overflow: auto; vertical-align:top; text-align:left; ">'+vidDescription+'</div><br style="clear:both" /></div>';
	
	titleBar = '<b>'+vidName+'</b><span style="font-size:11px; font-style:italic; padding-left: 100px;">(Press escape or click X to close)</span>';
	var $dialog = $('<div id="popUpDialog" ></div>')
			.html(vidHtml)
			.dialog({
				autoOpen: false,
				position: 'top',
				show: 'slide',
				buttons: { "Close": function() { $(this).dialog("close"); } },
				open: function(event, ui) { dlgOpened(event, ui); },
				close: function(event, ui) { dlgClosed(event, ui); },
				width: 900,
				height: 750,
				modal: true,
				resizable: false,
				title: titleBar,
				 dialogClass: 'popUp' 
			});
	
		function dlgOpened(event, ui) {
			$('#vidText p').css('font-size','12px');
			flowplayer("player1", "http://thebarj.com/js/flowplayer-3.1.5.swf");
			$('#vidText').css('visibility','visible');
		}
		function dlgClosed(event, ui) {
			$("#popUpDialog").remove();
		}
		
		

		$dialog.dialog('open');
	
};


function clickTab(id) {
	//alert(id); 
	$('#navbar_content a').removeClass('currentLink').css('color','#d5916d');
	$('#'+id).addClass('currentLink').css('color','#fff');
}
function loadSlideShow(xml) {
	
	slideShowPicsXML=xml;
	slideShowPicsARR=[];

	$(xml).find("item").each(function() {
		var src= $(this).attr('source');
		slideShowPicsARR.push(src);
		imageObj = new Image();
		imageObj.src=src;
	});

	setTimeout("startSlideShow();",1000*slideShowDelay);
	 
};
function startSlideShow() {
	if (image_idx >= slideShowPicsARR.length-1) {image_idx=0;} else{image_idx+=1;}
	$('#slideShowPic').fadeOut('slow',function() {
					
		$('#slideShowPic').attr('src',slideShowPicsARR[image_idx]).fadeIn('slow');
	
	});
   setTimeout("startSlideShow();",1000*slideShowDelay);
}

/*function contactUs() {
	$('#pageBody').fadeOut('slow',function(){
										  var url='includes/contact.php';
											$('#pageBody').empty().load(url, function() {
											  							$('#pageBody').fadeIn();
											  						}); 
										   });
	
								
}*/
//function logOut() {
//    setTimeout( function() {
//                $.ajax({
//                type: "POST",
//                url: "includes/processLogin.php?logout",
//                cache: false,
//                success: function(html){
//                                $.cookie('admin_page', null);
//								location.href='admin.php';
//                            }
//        });
//    }, 2000 );    
//}
function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
//function getOriginal(id) {
//	var result='';
//	
//	for (var i=0; i<contextArr.length; i++) {
//		if (contextArr[i].id==id) {
//			result=contextArr[i].value;
//			i=contextArr.length+1;
//		}	
//	}
//	
//	return result;
//}
//function saveOriginals() {
//	contextArr=[];	
//	// Save original values
//	$('.infoSection').each(function() {
//							
//							var c={};
//							c.id=$(this).find('.innerContent').attr('id');
//							c.value=$(this).children('.innerContent').html();
//							contextArr.push(c);
//							
//						});
//	
//}
//function resetSection(id) {
//	$('#'+id).html(getOriginal(id));
//	$("#saveImage"+ id ).hide();
//	$("#cancelImage"+ id ).hide();
//	$("#" + id+'-eBar').removeClass('preview').hide();
//	
//}
//function showEditBar(t) {
//	var p = parseFloat($(t).css('padding-left').replace("px",""));
//	var l = parseFloat($(t).position().left)+p;
//	p = parseFloat($(t).css('padding-right').replace("px",""));
//	var w = parseFloat($(t).width())+(p-2);
//	$(t).children(':first-child').css({'left':l,'width':w}).fadeIn();
//	$(t).addClass("infoSectionHover");
//}
//function setupEdit() {
//	if ($('div#popup').length==0) {
//		$('#mainContent').append('<div id="popup"><span id="popupContent"></span> <div align="right" id="popupButtonBar"><input type="button" class="editorButton bClose" id="saveButton" value="Apply" /><input type="button" class="editorButton bClose" id="closeButton" value="Close" /></div> </div>');
//	}
//	$.get('includes/forms/edit_bar.php', function(data) {
//		  $('.infoSection').prepend(data);
//		  
//		  $('.slider').each(function(){
//					$(this).attr('id',$(this).siblings('.innerContent').attr('id')+'-eBar');
//				});
//	  
//					$('.infoSection').hover(function () {
//							showEditBar($(this));
//						  },
//						  function () {
//							$(this).children(':first-child').not('.preview').fadeOut();
//							$(this).removeClass("infoSectionHover");
//						  }
//					); 
//					
//					$('img.editImage').each(function(){
//						$(this)
//							.attr('id', 'editImage'+$(this).parent().siblings('.innerContent').attr('id'))
//							.click(function() {
//							  editText($(this).parent().siblings('.innerContent').attr('id'));
//							});
//					});	 
//					
//					$('img.saveImage').each(function(){
//						$(this)
//							.attr('id', 'saveImage'+$(this).parent().siblings('.innerContent').attr('id'))
//							.click(function() {
//							  saveText($(this).parent().siblings('.innerContent').attr('id'));
//							});
//					});
//				  
//					$('img.cancelImage').each(function(){
//						$(this)
//							.attr('id', 'cancelImage'+$(this).parent().siblings('.innerContent').attr('id'))
//							.click(function() {
//							  cancelText($(this).parent().siblings('.innerContent').attr('id'));
//							});
//					});
//					$('img.loadingImage').each(function(){
//						$(this).attr('id', 'loadingImage'+$(this).parent().siblings('.innerContent').attr('id'))
//						
//					});
//			  });
//
//	
//		contextArr=[];	
//		// Save original values
//		$('.infoSection').each(function() {
//								
//			var c={};
//			c.id=$(this).find('.innerContent').attr('id');
//			c.value=$(this).find('.innerContent').html();
//			contextArr.push(c);
//			
//		});
//	
//}
//
//function cancelText(id) {
//	resetSection(id);
//	if ($('#nl-buttons input').length>0) {
//		$('#nl-buttons input').removeAttr("disabled").fadeTo('slow', 1);
//		$('#newletterList li')
//					.fadeTo('slow', 1)
//					.click(function() {
//							 loadNewsletter($(this).attr('id'));
//						});
//	 }
//}
//function saveText(id) {
//	
//	var mode='update';
//	var type = $("#" + id ).attr('type')
//	var section_content= encodeURIComponent($("#" + id ).html());
//	var key=id;
//	if (type=='newsletter') { key=id.replace('nl-',''); }
//	var datastr = 'mode=' + mode + '&type=' + type + '&section_id=' + key + '&section_content=' + section_content;
//	
//	$("#loadingImage"+ id).show();
//	setTimeout( function() {
//				$.ajax({
//				type: "POST",
//				url: "includes/processSection.php",
//				data: datastr,
//				cache: false,
//				success: function(result){
//						$("#loadingImage"+ id).hide();	
//						if (type=='newsletter') { 
//													
//							NewsletterXML=result;
//							NewsletterArr=[];
//							$(result).find("Row").each(function() {
//								var nl=buildNewsletter($(this));
//								NewsletterArr.push(nl);
//							});
//						
//						} //else if (result=='ok') {
//						
//							saveOriginals();
//							resetSection(id);
//							if ($('#nl-buttons input').length>0) {
//								$('#nl-buttons input').removeAttr("disabled").fadeTo('slow', 1);
//								$('#newletterList li')
//													.fadeTo('slow', 1)
//													.click(function() {
//															loadNewsletter($(this).attr('id'));
//												});
//							 }
//						//} else { $("#response").html(result).show(); }
//						
//					}
//		});
//	}, 2000 );
//	
//	
//}
//
//function editText(id) {
//	
//	$('#popupContent').load('includes/forms/text_editor.php', function(){
//											
//											var editorID = 'editorbody';
//											 
//											$('#saveButton').unbind('click').click(function() {
//																		 if ($('#nl-buttons input').length>0) {
//																		 	$('#nl-buttons input').attr("disabled","disabled").fadeTo('slow', 0.5);
//																			$('#newletterList li').unbind('click').fadeTo('slow', 0.5);
//																	   	 }
//																		 $("#" + id ).html(dmxedit_getHTML(editorID));
//																		 $("#saveImage"+ id).show();
//																		 $("#cancelImage"+ id).show();
//																		 $("#" + id+'-eBar').addClass('preview');
//																		 $('#' +id+'-eBar').show();
//																		
//																	 }); 
//											
//											
//											$('.editorButton').button();
//											
//											$('.bClose').hover(function () {
//													$(this).css({'color':'#f00','background-color':'#fff'});
//												  },
//												  function () {
//													$(this).css({'color':'#fff','background-color':'transparent'});
//												  }
//											);
//											
//											$("#popup").openPopup();
//											
//											dmxedit_init();
//											dmxedit_setHTML(editorID, $("#" + id).html() );
//											
//									});
//	
//  	
//}
