// Drummond Central Website jQuery file
// Author: Kevin Brady
// Created on: 20 Sep 2010

var currentSlide = 0;
var delay;
var currentStaffID = 10;

/* Suprisingly, <=IE8 do not support the new HTML5 semantic tags. Until we find a way of destroying these so-called "browsers", we can use the following as a workaround. */ 
document.createElement('header');
document.createElement('nav');
document.createElement('section');
document.createElement('article');
document.createElement('aside');
document.createElement('footer');

$(function(){
		   
	/* Trigger a user click */
    $('.staffRightArrow').trigger('click');		   
		
  $( "#emailPopup" ).draggable();					
															
  $(".work, .staffLarge, .staffPhotoSmall").mouseover(function(){
		$(this).children(".dsHelper").show();
	});
	
	$(".work, .staffLarge, .staffPhotoSmall").mouseout(function(){
		$(this).children(".dsHelper").hide();
	});
        			
	$(".contactHomeButton").click(function(){
	  	$.blockUI({ message : "<div class='contactLoadingBox' style='text-align:center;padding:10px;background:#333;border:1px solid #444'>One moment...</div>" });	
      			
      var emailVal = $("input[name=contactEmail]").val();
      var messageVal = $("textarea[name=contactMessage]").val();		 
      		
      if(emailVal.length <= 0){
        alert("Please provide your email address.");
      	$.unblockUI();
      }		 
      else if(messageVal.length <= 0){
      	alert("Please provide a message.");
        $.unblockUI();
      }
      else{
        $.get("AJAX_sendMessage.php",{name:'unknown',email:emailVal,message:messageVal},function(data){
      	  alert("Thank you. Your message has been sent successully.");
      				
      	  $("input[name=contactEmail]").val(""); 
      	  $("textarea[name=contactMessage]").val("");			 
      	  $.unblockUI();
        });
      }			
    });
								
		var dynamicStaffName;
		var staffTwitterName;

				staffTwitterName = $(".staffTwitterName"+currentStaffID+"").html();
        var username = staffTwitterName; // set user name
						      
				dynamicStaffName = $(".dynamicStaffName"+currentStaffID+"").html();
				
				if (username == null){
				  //
				}
				else{
				  $.getJSON('http://api.twitter.com/1/statuses/user_timeline/'+username+'.json?callback=?',
            function(data) {
						  //$(".tweetURL").append(data[0].id);
						  //alert(data[0].id);
						  $(".tweetsHolder").attr("title", "Read "+dynamicStaffName+"'s latest Tweet|http://www.twitter.com/"+username+"/status/"+data[0].id_str);
						  $(".tweetsHolder").html("<span style='font-size:17px;color:#86BDCE'>What is "+dynamicStaffName+" thinking?</span><br/>"+data[0].text+" <br/><img style='cursor:move;position:relative;top:4px;width:15px;height:15px' src='img/drag_icon.png' /> <span style='cursor:move;color:#999;font-size:9px'>Drag to share</span>"); // get the first tweet in the response and place it inside the div
          });
			  }
							
				
				$("#header .right .content").fadeOut(0).fadeIn(6000);

				// fade in the top right image on load...

				// DC Share Tool v1.0
				$(".draggableBlock").draggable({

								"zIndex":1000,
								cursorAt: { cursor:"pointer", top:-5, left:-5 },
								revert: true,
								opacity:0.4,
								helper: "clone",
								handle:$(".handle"),
								start: function(event, ui) {
								//$.blockUI({message:'<div class="socialShare1"><div style="position:relative"><div class="droppableHover" style="display:none;position:absolute;top:5px;margin:5px 0 0 20px; font-size:16px"></div><div class="shareTitle" style="display:none"></div><div class="shareSummary" style="display:none"></div><table style="margin-top:13px"><tr><td style="width:90px"><img class="Twitter" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_twitter.png" /></td><td><img id="LinkedIn" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_linkedin.png" /></td><td><img id="Facebook" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_facebook.png" /></td><td style="width:90px"><img id="Digg" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_digg.png" /></td><td style="width:90px"><img id="Google" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_google.png" /></td><td style="width:90px"><img id="StumbleUpon" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_stumbleupon.png" /></td><td style="width:90px"><img id="Email" class="droppable" src="'+ROOT_PATH+'img/social/drag_to_share_email.png" /></td></tr><tr><td class="dragToShareTitle">Twitter</td><td class="dragToShareTitle">LinkedIn</td><td class="dragToShareTitle">Facebook</td><td class="dragToShareTitle">Digg</td><td class="dragToShareTitle">Google</td><td class="dragToShareTitle">Stumble Upon</td><td class="dragToShareTitle">Email</td></tr></table></div></div>'});

								
								
								var title = $(this).closest("a").attr("title");
								
								// if the above title var is undefined (it will be on the homepage) fallback on the current element's title attribute
								if (title === undefined){
							    var title = $(this).attr("title");
								}
								var img_src = $(this).attr("src");
								$(".ogImage").attr("content",img_src);
								var shareSummary = $(this).html();
								var splitResult = title.split("|");
								
								$(".shareName").html(splitResult[0]);
								$(".shareURL").html(splitResult[1]);
								$(".shareTitle").html(($(this)).attr("title"));
								
								$(".metaTitle").attr("content",splitResult[0]);
								
								$(".socialShare").slideDown(250); },

								stop: function(event, ui) { $(".socialShare").slideUp(250); $.unblockUI(); }
				});
				
				//test mouseover functionality
				$("#Twitter").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_twitter_hover.png");				
				});
				$("#Twitter").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_twitter.png");				
				});
				//test mouseover functionality
				$("#LinkedIn").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_linkedin_hover.png");				
				});
				$("#LinkedIn").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_linkedin.png");				
				});
				//test mouseover functionality
				$("#Facebook").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_facebook_hover.png");				
				});
				$("#Facebook").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_facebook.png");				
				});
				//test mouseover functionality
				$("#Digg").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_digg_hover.png");				
				});
				$("#Digg").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_digg.png");				
				});
				//test mouseover functionality
				$("#Google").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_google_hover.png");				
				});
				$("#Google").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_google.png");				
				});
				//test mouseover functionality
				$("#StumbleUpon").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_stumbleupon_hover.png");				
				});
				$("#StumbleUpon").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_stumbleupon.png");				
				});
				//test mouseover functionality
				$("#Email").mouseover(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_email_hover.png");				
				});
				$("#Email").mouseout(function(){
				  $(this).attr("src",ROOT_PATH+"img/social/drag_to_share_email.png");				
				});

				
				// Let's set up the droppable functionality 
				$(".droppable").droppable({
		  	  tolerance: "pointer",
				  over: function() {	var id = $(this).attr("id");
													 		var staticSRC = $(this).attr("src");
																		                      
															//store droppable ID. Used in next drop function.
															$(".droppableHover").html(id); },
				  drop: function() {	id = $(".droppableHover").html();
												      var shareTitle = $(".shareName").html();
												      var url = $(".shareURL").html();
																				
															//When a user drops the draggable onto an icon direct user to respective site																																												
												      if (id == "Facebook") {	window.location.href = "http://www.facebook.com/sharer.php?u="+url+"&t="+shareTitle+"";	}
												      else if (id == "Twitter") {	window.location.href = "http://twitter.com/home?status="+shareTitle+" "+url+"";	}
												      else if (id == "LinkedIn") { window.location.href = "http://www.linkedin.com/shareArticle?mini=true&url="+url+"&title="+shareTitle+""; }
												      else if (id == "Digg") { window.location.href = "http://digg.com/submit?phase=2&url="+url+"&title="+shareTitle+"";	}
												      else if (id == "Google") { window.location.href = "http://www.google.com/bookmarks/mark?op=edit&bkmk="+url+"&title="+shareTitle+"&annotation="+shareTitle+""; }
												      else if (id == "StumbleUpon") { window.location.href = "http://www.stumbleupon.com/submit?url="+url+"&title="+shareTitle+"";	}
															else if (id == "Email") { 
															  
																
																$("#emailPopup").show();
																$("#emailPopup .defaultMessage").text("Check out this page... "+url);
																//window.location.href = "mailto:?subject=Check out the Drummond Central website!&body=I have just came across this fantasic website: "+ROOT_PATH+"";	
															}
															
						               },
					out: function()  {	$(".droppable").removeClass("ui-state-highlight");
								              $(".droppableHover").css({"display":"none"}); 
													 }
				});
				
				//Email popup send
				$(".emailPopupSend").click(function(){
				  var email_popup_name = $("#emailPopup .name").val();
					var email_popup_email = $("#emailPopup .toEmail").val();
					var email_popup_msg = $("#emailPopup .message").val();
										
					if (email_popup_name == "" || email_popup_email == "" || email_popup_msg == ""){
						alert("Please complete all fields.");
					}
					else{
					  //everthing is OK -> send email
					  $.get(ROOT_PATH+"AJAX_sendReferral.php" ,{ name:email_popup_name, email:email_popup_email, message:email_popup_msg }, function(data){
						  alert("Thank you. Your message has been sent successfully.");
						  $("#emailPopup").fadeOut(1000);										
					  }); 
				  }
					
					
				});
				
				$("#closeEmailPopup").click(function(){
				  $("#emailPopup").fadeOut(1000);	 
				});

				$('.slide1_content, .slide2_content, .slide3_content, .slide4_content, .slide5_content').hide();
				$('.slideNumberBlock').eq(0).css({'background':'#85bdcd','height':'33px','position':'relative','bottom':'10px'});

				/******************** US PAGE JQUERY START ********************/
        var current_div_eq = 4;
				$(".staffPhotoSmall").css({opacity:0.6});
				$(".staffPhotoSmall").eq(8).css({opacity:1});
				$(".staffHeroDetailsOverlay_"+currentStaffID+"").show();

				var num_of_staff = $(".staffPhotoSmall").size();

				// STAFF CYCLE
				
				//alert(current_div_eq);

				$(".staffPhotoSmall").click(function(){               
								
								var thisStaffID = $(this).attr("id");
								
								if ( thisStaffID == 99 ){
									//do nothing
								}
								else{
									// faux-loading
									$(".staffLarge").html("<img src='../img/loader2.gif' alt='Loading...' />").css( { "position":"relative","top":"100px" } );
									
									//$(".slideContainer").hide();
									$(".tweetsHolder").html("Loading tweet...");
	
									$(".staffHeroDetailsOverlay").hide();
									$(".staffPhotoSmall").css({opacity:0.6});
									
									
									
									$.get(ROOT_PATH+"AJAX_getImage.php",{staffID:thisStaffID},function(data){
									  //alert(data);
									  $(".staffLarge").html(data).css({"color":"#FFF","position":"relative","top":"0px"});
										
										currentStaffID = thisStaffID;
	
									  $("#"+currentStaffID+"").css({opacity:1});
	
									  $(".staffHeroDetailsOverlay_"+currentStaffID+"").show();
										$(".slideContainer").show();
										getLatestTweet(currentStaffID);
									});	
								}
				});

				$(".staffLeftArrow").click(function(){

								if (currentStaffID == 2) {
												//
								}
								else{
								       												
												$.blockUI();
												
												var $staff_large = $(".staffLarge");
																		
												$staff_large.html("<img src='"+ROOT_PATH+"img/staff/staff_large_"+currentStaffID+".jpg' alt='Large image coming soon.' />").css({color:"#FFF"});											
												
												// faux-loading
        								$staff_large.html("<img src='../img/loader2.gif' alt='Loading...' />").css( { "position":"relative","top":"100px" } );
												//$(".staffLarge").html("<img src='../img/loader2.gif' alt='Loading...' />").css( { "position":"relative","top":"100px" } );
        								
        								//$(".slideContainer").hide();
        								$(".tweetsHolder").html("Loading tweet...");
        
        								$(".staffHeroDetailsOverlay").hide();
        								$(".staffPhotoSmall").css({opacity:0.6});
        								
        								var thisStaffID = parseInt(currentStaffID) - 1;
																								        
        								$.get(ROOT_PATH+"AJAX_getImage.php",{staffID:thisStaffID},function(data){
        								  //alert("IN");
        								  $staff_large.html(data).css({"color":"#FFF","position":"relative","top":"0px"});
        									
        									currentStaffID = thisStaffID;
        
        								  $("#"+thisStaffID+"").css({opacity:1});
        
        								  $(".staffHeroDetailsOverlay_"+thisStaffID+"").show();
        									$(".slideContainer").show();
        									getLatestTweet(thisStaffID);
													$.unblockUI();
												  
										    });

								}

				});

				$(".staffRightArrow").click(function(){
              //alert(currentStaffID+'~'+num_of_staff);
								if (currentStaffID == num_of_staff+1) {
												currentStaffID = 1;
								}
								
								else{
                       	//alert(currentStaffID);
												$.blockUI();
												//alert(currentStaffID);					
												$(".staffLarge").html("<img src='"+ROOT_PATH+"img/staff/staff_large_"+currentStaffID+".jpg' alt='Large image coming soon.' />").css({color:"#FFF"});											
												
												// faux-loading
        								$(".staffLarge").html("<img src='../img/loader2.gif' alt='Loading...' />").css( { "position":"relative","top":"100px" } );
        								
        								//$(".slideContainer").hide();
        								$(".tweetsHolder").html("Loading tweet...");
        
        								$(".staffHeroDetailsOverlay").hide();
        								$(".staffPhotoSmall").css({opacity:0.6});
        								
        								var thisStaffID = parseInt(currentStaffID) + 1;
												        
        								$.get(ROOT_PATH+"AJAX_getImage.php",{staffID:thisStaffID},function(data){
        								  
        								  $(".staffLarge").html(data).css({"color":"#FFF","position":"relative","top":"0px"});
        									
        									currentStaffID = thisStaffID;
        
        								  $("#"+thisStaffID+"").css({opacity:1});
        
        								  $(".staffHeroDetailsOverlay_"+thisStaffID+"").show();
        									$(".slideContainer").show();
        									getLatestTweet(thisStaffID);
													$.unblockUI();
												  
										    });
									}
				})

				/******************** US PAGE JQUERY END ********************/

				/* User clicks right arrow */

				$(".sliderRightArrow").click(function(){
                $.blockUI({overlayCSS:{opacity:0}});
								clearTimeout(delay);

								// let's initially hide all slide images and content

								$('.slide0, .slide1, .slide2, .slide3, .slide4, .slide5').hide();

								$('.slide0_content, .slide1_content, .slide2_content, .slide3_content, .slide4_content, .slide5_content').hide();

								$('.slideContentWrapper').hide();

								// set all number blocks back to their inactive state

								$('.slideNumberBlock').css({'background':'#333','height':'29px','position':'relative','bottom':'0px'});

								var selectedSlideNumber = parseInt($(this).text());

								// slide numbers are indexed from zero so let's add 1 to selectedSlideNumber

								var adjustedSelectedSlideNumber = currentSlide + 1;

								if (adjustedSelectedSlideNumber > 5) {
												adjustedSelectedSlideNumber = 0;
								}

								// set selected slide number to the active state (raised)

								$('.slideNumberBlock').eq(adjustedSelectedSlideNumber).css({'background':'#85bdcd','height':'33px','position':'relative','bottom':'10px'});

								$('.slide'+adjustedSelectedSlideNumber+'').css({opacity:1}).show();

								// A simple animation. Slide the content div out. Slight opacity and a nice little fade in effect.

								$('.slide'+adjustedSelectedSlideNumber+'_content').animate({

												width: 'show'

				  						}, 750, function() {

				    				$('.slideContentWrapper').fadeIn(500);
										$.unblockUI();

								});

								// the slides must flow nicely i.e. if a user was to click to slide 4, then next slide to show must be slide 5. Therefore we'll adjust the currentSlide var accordingly.

								currentSlide = adjustedSelectedSlideNumber;

								// at the start of this click we cleared the delay timeout which stopped the slider switch completely.

								// We'll now start it back up again, but will a slightly longer delay until the next slide so the user can view their selection without it disappearing!

								delay = setTimeout(function() { changeSlide(currentSlide) }, 10000);
				});

				/* User clicks left arrow */

				$(".sliderLeftArrow").click(function(){

				        $.blockUI({overlayCSS:{opacity:0}});
								clearTimeout(delay);

								// let's initially hide all slide images and content
								$('.slide0, .slide1, .slide2, .slide3, .slide4, .slide5').hide();

								$('.slide0_content, .slide1_content, .slide2_content, .slide3_content, .slide4_content, .slide5_content').hide();

								$('.slideContentWrapper').hide();

								// set all number blocks back to their inactive state
								$('.slideNumberBlock').css({'background':'#333','height':'29px','position':'relative','bottom':'0px'});

								var selectedSlideNumber = parseInt($(this).text());

								// slide numbers are indexed from zero so let's subtract 1 away from selectedSlideNumber

								var adjustedSelectedSlideNumber = currentSlide - 1;

								if (adjustedSelectedSlideNumber < 0) {
												adjustedSelectedSlideNumber = 5;
								}

								// set selected slide number to the active state (raised)

								$('.slideNumberBlock').eq(adjustedSelectedSlideNumber).css({'background':'#85bdcd','height':'33px','position':'relative','bottom':'10px'});

								$('.slide'+adjustedSelectedSlideNumber+'').css({opacity:1}).show();

								// A simple animation. Slide the content div out. Slight opacity and a nice little fade in effect.

								$('.slide'+adjustedSelectedSlideNumber+'_content').animate({

												width: 'show'

				  						}, 750, function() {

				    				$('.slideContentWrapper').fadeIn(500);
										$.unblockUI();

								});

								// the slides must flow nicely i.e. if a user was to click to slide 4, then next slide to show must be slide 5. Therefore we'll adjust the currentSlide var accordingly.

								currentSlide = adjustedSelectedSlideNumber;

								// at the start of this click we cleared the delay timeout which stopped the slider switch completely.

								// We'll now start it back up again, but will a slightly longer delay until the next slide so the user can view their selection without it disappearing!

								delay = setTimeout(function() { changeSlide(currentSlide) }, 10000);



				});

				/* Manually slide select */

				$(".slideNumberBlock").click(function(){


								$.blockUI({overlayCSS:{opacity:0}});
								clearTimeout(delay);



								// let's initially hide all slide images and content

								$('.slide0, .slide1, .slide2, .slide3, .slide4, .slide5').hide();

								$('.slide0_content, .slide1_content, .slide2_content, .slide3_content, .slide4_content, .slide5_content').hide();

								$('.slideContentWrapper').hide();



								// set all number blocks back to their inactive state

								$('.slideNumberBlock').css({'background':'#333','height':'29px','position':'relative','bottom':'0px'});

								var selectedSlideNumber = parseInt($(this).text());

								// slide numbers are indexed from zero so let's subtract 1 away from selectedSlideNumber

								var adjustedSelectedSlideNumber = selectedSlideNumber - 1;

								// set selected slide number to the active state (raised)

								$('.slideNumberBlock').eq(adjustedSelectedSlideNumber).css({'background':'#85bdcd','height':'33px','position':'relative','bottom':'10px'});

								$('.slide'+adjustedSelectedSlideNumber+'').css({opacity:1}).show();

								// A simple animation. Slide the content div out. Slight opacity and a nice little fade in effect.

								$('.slide'+adjustedSelectedSlideNumber+'_content').animate({

												width: 'show'

				  						}, 750, function() {

				    				$('.slideContentWrapper').fadeIn(500);
										$.unblockUI();

								});

								// the slides must flow nicely i.e. if a user was to click to slide 4, then next slide to show must be slide 5. Therefore we'll adjust the currentSlide var accordingly.

								currentSlide = adjustedSelectedSlideNumber + 1;

								if (currentSlide > 5) {

												currentSlide = 0;

								}

								// at the start of this click we cleared the delay timeout which stopped the slider switch completely.

								// We'll now start it back up again, but will a slightly longer delay until the next slide so the user can view their selection without it disappearing!

								delay = setTimeout(function() { changeSlide(currentSlide) }, 10000);



				});

				/*************** HOME PAGE BOTTOM SOCIAL CHANGER JQUERY START ***************/

				$(".socialMediaSelection tr").click(function(){

								var selectedSocialItem = $(this).attr("title");

								$(".socialMediaSelection tr").each(function(index){
												$(this).removeClass("highlightRow");
								});

								$(this).addClass("highlightRow");

								$(".currentSocialItem").html(selectedSocialItem);

								$(".socialContent").hide();							
								$(".content_"+selectedSocialItem+"").show();

				});

				/*************** HOME PAGE BOTTOM SOCIAL CHANGER JQUERY END ***************/

				/*************** WORK - FILTER BY CLIENT SELECT FUNCTIONALITY ***************/

				$(".clientSelectBox").change(function(){

								$.blockUI();
								var clientVal = $(".clientSelectBox").val()

								$(".filterByClientButton").html("Wait...").attr("disabled",true);



								$(".workHolder").html("<div style='height:600px; width:600px; color:#FFF; text-align:center; padding-top:50px'><br/><img src="+ROOT_PATH+"img/loader2.gif alt='Loading' /></div>");



								$.get("AJAX_searchByClient.php",{client:clientVal},function(data){

												$(".workHolder").html("<div style='width:600px; height:600px'>&nbsp;</div>");

												$(".workHolder").html(data).fadeOut(0).fadeIn(2000);

												$(".filterByClientButton").html("Go").attr("disabled",false);

												$.unblockUI();

								});

				});

				/****************************************************************************/
});

function getLatestTweet(currentStaffID){
        
				dynamicStaffName = $(".dynamicStaffName"+currentStaffID+"").html();
				staffTwitterName = $(".staffTwitterName"+currentStaffID+"").html();
				
				//alert(dynamicStaffName+"|"+staffTwitterName);
				var username=staffTwitterName; // set user name
				
				if ( username == 'xxx' ) {
					
								$(".tweetsHolder").html("<span style='font-size:17px;color:#666'>Kevin's not allowed on Twitter as he's got a<br/>potty mouth.</span>"); // get the first tweet in the response and place it inside the div
								$('.twitter_identifier img').hide();
				}
        else if ( username == '' ) {
          $(".tweetsHolder").html(''); // get the first tweet in the response and place it inside the div
					$('.twitter_identifier img').hide()
        }
				else {
				
						$.getJSON('http://api.twitter.com/1/statuses/user_timeline/'+username+'.json?callback=?',
												function(data) {
							//alert(data[0].id_str);
							$(".tweetsHolder").html("<span style='font-size:17px;color:#86BDCE'>What is "+dynamicStaffName+" thinking?</span><br/>"+data[0].text);
							$(".tweetsHolder").attr("title", "Read "+dynamicStaffName+"'s latest Tweet|http://www.twitter.com/"+username+"/status/"+data[0].id_str);
							//call another script to create clickable links
							$.get(ROOT_PATH+"AJAX_convertLinks.php",{content:data[0].text},function(data){
									//alert(data);
											
								$(".tweetsHolder").html("<span style='font-size:17px;color:#86BDCE'>What is "+dynamicStaffName+" thinking?</span><br/>"+data+" <br/><img style='cursor:move;position:relative;top:4px;width:15px;height:15px' src='img/drag_icon.png' /> <span style='cursor:move;color:#999;font-size:9px'>Drag to share</span>"); // get the first tweet in the response and place it inside the div
							});
																						
										});
						
						$('.twitter_identifier img').fadeIn(500);
						
				}
				
}

function sendReferral(){

				var referral_to = $(".referral_to").val();
				var referral_message = $(".referral_message").val();

				$.get("AJAX_sendReferral.php",{to:referral_to,message:referral_message},function(data){
								alert(data);
								$.unblockUI();
				});
}

function closeReferBox(){
				$.unblockUI();
}

function signupToNewsletter(){

				$.blockUI();

				var nameVal = $("#newsletterName").val();
				var emailVal = $("#newsletterEmail").val();
				var feedback;
				var valid = true;

				// remove any previous error messages

				$(".newsletterError").remove();

				if (nameVal == "" || emailVal == "") {
				  $(".newsletterFormTable").prepend("<span class='newsletterError' style='color:#993300;font-size:11px'>Please fill out all fields...</span>");
				  valid = false;
				}

				if (valid == false) {
					$.unblockUI();
				}
				else{
			    $(".newsletterFormTable").prepend("<span class='newsletterError' style='color:#339900;font-size:11px'>Thank you for signing up!</span>");
					$.unblockUI();
				}

}

function signpostSwitch(next,prev){
  $.blockUI();
  $('.signpostTableCell'+next+'').html('<img class="squareCross" src="img/square_cross.gif" onclick="signpostSwitch('+next+','+prev+')" />').show();
  
  $('.signpostTableCell'+prev+'').html('<img class="squareCross" src="img/square_blank.gif" onclick="signpostSwitch('+prev+','+next+')" />').show();
  
  $('.block'+prev+'').hide();
  $('.block'+next+'').fadeIn(1300);
  $.unblockUI();
}

/* Main slider */
function changeSlide(currentSlide){
        
				// The user can switch to any slide manually.
				$('.slide0, .slide1, .slide2, .slide3, .slide4, .slide5').hide();

				$('.slide0_content, .slide1_content, .slide2_content, .slide3_content, .slide4_content, .slide5_content').hide();

				$('.slideNumberBlock').css({'background':'#333','height':'29px','position':'relative','bottom':'0px'});

				$('.slideNumberBlock').eq(currentSlide).css({'background':'#85bdcd','height':'33px','position':'relative','bottom':'10px'});

				$('.slideContentWrapper').hide();

				$('.slide'+currentSlide+'').css({opacity:1}).show();

				$('.slide'+currentSlide+'_content').animate({

								opacity: 0.7,
								width: 'show'

  						}, 550, function() {

    				$('.slideContentWrapper').show();

				});

				$('.sliderLeftArrow').hover(function () {

        this.src = ROOT_PATH+'img/left_arrow_big_hover.gif';

    }, function () {

        this.src = ROOT_PATH+'img/left_arrow_big.gif';

    });

				$('.sliderRightArrow').hover(function () {
       
        this.src = ROOT_PATH+'img/right_arrow_big_hover.gif';

    }, function () {
  
        this.src = ROOT_PATH+'img/right_arrow_big.gif';

    });

				$('.staffLeftArrow').hover(function () {

        this.src = ROOT_PATH+'img/left_arrow_big_hover.gif';

    }, function () {
        this.src = ROOT_PATH+'img/left_arrow_big.gif';

    });

				$('.staffRightArrow').hover(function () {
          this.src = ROOT_PATH+'img/right_arrow_big_hover.gif';

    }, function () {
        this.src = ROOT_PATH+'img/right_arrow_big.gif';
    });

				if (currentSlide < 5) {
				  currentSlide++;
				}

				else{
				  currentSlide = 0;
				}
				delay = setTimeout(function() { changeSlide(currentSlide) }, 5000);
}

/* Toggle blocks */
function toggle(newElem, prevElem) {
  $('.'+prevElem).hide();
	$('.'+newElem).fadeIn(1500);
}

// Function to allow the user to close the Jobs box
function newsletterPopup(){
  
  $("#emailPopup").html("<div style='float:right'><img onclick='closeNewsletterPopup()' src='"+ROOT_PATH+"img/video_close.jpg' alt='Close'></div><p style='clear:both;padding:5px;margin-bottom:5px;font-size:11px'>To subscribe to our next newsletter please fill out your details below.</p><table style='float:left;margin-left:5px'><tr><td style='font-size:11px'>First Name</td><td><input class='firstName' style='margin-left:5px;width:200px;padding:2px' /></td></tr><tr><td style='font-size:11px'>Last Name</td><td><input class='lastName' style='margin-left:5px;width:200px;padding:2px' /></td></tr><tr><td style='font-size:11px'>Email</td><td><input class='email' style='margin-left:5px;width:200px;padding:2px' /></td></tr><tr><td></td><td><button style='height:22px;margin-left:5px;width:65px;padding:2px' onclick='joinMailingList()'>Submit</button></td></tr>").css( { "left":"228px","width":"384px","height":"195px","padding":"10px" } ).hide().fadeIn(250);
}

function joinMailingList(){
  
  var firstNameVal = $("#emailPopup .firstName").val();
	var lastNameVal = $("#emailPopup .lastName").val();
  var emailVal = $("#emailPopup .email").val();
  
	if (firstNameVal == "" || lastNameVal == "" || emailVal == ""){
	  alert("Please fill in all fields.");
	}
	else{
	  
	  $.get("AJAX_newsletterSignup.php", {firstName:firstNameVal,lastName:lastNameVal,email:emailVal}, function(data){
        
		$("#emailPopup").html("<p style='padding:5px;margin-bottom:5px;font-size:11px'>Thank you. You have been added to our mailing list.<br/><br/>An email has been sent to <span style='color:#85BDCD;font-size:11px'>"+emailVal+"</span>.</p>").css( { "left":"228px","width":"384px","height":"106px","padding":"10px" } ).show(); 
      
			//$("#emailPopup").delay(4000).fadeOut(250);		
			
			$('#emailPopup').animate({
								
								height: '65px'

  						}, 550, function() {

    				$('#emailPopup').delay(3000).fadeOut(250);

				});	 
		
    });  
	}
}

function closeNewsletterPopup(){
  $("#emailPopup").fadeOut(250);
}

setTimeout("changeSlide(currentSlide)",1000);

function start_main_video() {
	
				$.blockUI();
				$( '.main-video' ).show();
				$( '.main-video .close-button' ).show();
				
}

function close_main_video() {
	
				$.unblockUI();
				$( '.main-video' ).hide();
				$( '.main-video .close-button' ).hide();
				window.location.reload()
}

