// JavaScript Document

$(function () {	
	h = $("#SlideTabs").position();
	Wrap_W = $("#ContentWrap").width();         /*Equal to the right side*/
	Tabs_L = h.left;	
	Adj = (Wrap_W - (Tabs_L + 368) - 102) / 2;     /*368 is the width of the #SlideTabs div.  102 is the width of the #ContactButton div*/ 
	
	$("#ContactButton").css("right", Adj);      /*Centers contact button on document ready*/
	
	$(".accorLink").click(function(){                             //Opens accordion links in the same new window. 
		i = this.href;
		v = "Premium Recovery Links";
		p = "width=900,height=900,left=400";
		LinkWindow = window.open(i,v,p);
		LinkWindow.focus();
		return false;
	});
	
    $("#Accordion").accordion({});
	
	$("#ShowInstr").toggle(
		function(){
				$("#HiddenInst").show("blind", {}, 500);
				$("#ShowInstr a").text("Close Help");
				return false;
			},
		function(){
				$("#HiddenInst").hide("blind",{},500);
				$("#ShowInstr a").text("Click For Help");
				return false;
			}
	);
	

	
	$(window).resize(function(){	  //Keeps contact button in proper placement on window resize.
	  h = $("#SlideTabs").position();
	  Wrap_W = $("#ContentWrap").width();         /*Equal to the right side*/
	  Tabs_L = h.left;	
	  Adj = (Wrap_W - (Tabs_L + 368) - 102) / 2;
	  
	  $("#ContactButton").css("right", Adj);
	  
	});
setTimeout(function(){hideControls()},1500);                                 //Hide slider controls after 1.5 seconds

function hideControls(){$(".controlPanelWrap").children().animate({ "top" : '-75px'}, 700)};           //Control panel animate up on load

$(".controlPanelWrap").hover(function(){                                 //control panel appear/ disappear on hover
			$(this).children().stop().animate({ "top" : '0px'}, 500);								   
			},
			function(){
			$(this).children().stop().animate({ "top" : '-75px'}, 500);	
			}		
);

$('.slideWrap').fadeIn(3000);                                           //fades in slideshow on page load.

$("#Loader, #NextLink, #LastLink").click(function(){
	/*Variables for both (NEXT & PREVIOUS) directions*/
	CurSlideID = $("#Loader").children().attr('id');	        //Get the ID of the current slide.
	CurSlideNum = CurSlideID.split("IL");                       //Split the ID into an array separating the number from "IL"
	i= parseFloat(CurSlideNum[1]);                              //Grab the separated string number and turn it into a regular number
	hotTab = $(".ui-tabs-nav .ui-state-active").attr('id');    //gets the id of the slideshow navigation button that is active
	if (hotTab==undefined)
	{
	  h=100
	}
	else 
	{
	  hotTabNum = hotTab.split("IL");                             //Split ID into array separating the number from IL
	  h = parseFloat(hotTabNum[1]);                             //grab the number part of the string and make it into an actual number	
	} 
	n = 10;                                                  // number of slides in the show
	stime = 500;                                             // time it takes for slides to slide in & out
	dtime = 550;                                          // delay between slide loading.  Needs to be at least 1 millisecond longer than stime;
	effect = new Array("slide");
	dir = new Array("up","down","left","right");
	
	/*Variables for NEXT functionality*/
	context = new Array(0,3,6,9);                           /*Each number in the array is the slide before the one in which you want a new context 	                                                            (picture/section).  Set this for next functionality, back functionaly will adjust                                                            automatically;*/
	switch(i)                                        /*k is equal to the id number value of the wanted picture when the specified (case #) slide is listed*/
	{
	  case context[0]:
	  	k=20;                                        //Why pic
		break;
	  case context[1]:
		k=21;                              //Who Pic. SlideMoneyHead pic.  Make equal to id of first new picture wanted on Slides.html
		break;
	  case context[2]:
		k=22;                             //How Pic. SlideFindMoney pic Make equal to id of second new picture wanted on Slides.html
		break;
	  case context[3]:
		k=23;                            //When Pic. SlideSuccess Pic Make equal to id of third new picture wanted on Slides.html
		break;
	  default:
		k=i;
		break;
	}	
	if (this.id=="LastLink"){          /* this is functionaly for the back button.  No context 0 for back button b/c we don't want the slideshow to go back                                        to the openning.*/
		/*Back Only Variables*/
		if(i==1){return}else{j=i-1};         //value of j declines to create appropriate ID number value from  Slides.html
	  	switch(i)                          /*k is equal to the id number value of the wanted picture when the specified (case #) slide is listed*/
		{
		  case context[1]+1:              //Adds one to each context specified above to correct for the fact that we are going backwards
			k=20;                        //Why pic.  Make equal to id of opening picture in your slideshow.
			break;
		  case context[2]+1:
			k=21;                         //Who SlidemoneyHead pic Make equal to id of second pic in slideshow
			break;
		  case context[3]+1:
			k=22;                           //How SlideFindMoney Pic Make equal to id of third pic in slideshow
			break;
		  default:
			k=i;
			break;
		}
		
		/*Back-Only functions*/
		function PicAndSlideB(){
		  $('.slideWrap').hide();                                  //Hide this whole new slide			
		  NewSlideID = '#IL' + j + '';                           //Make the new number (slide) a string adds # for JQuery and IL to complete id name
		  NewPicID = '#IL'+ k + '';                                    //Makes new picture a string and adds pound sign to be added into Jquery funtion;
		  $("#Loader").load("Slides.html "+NewSlideID+"'");          //Load the previous slide from the Slides.html file into the loader div.
		  $('.slidePicture').load("Slides.html "+NewPicID+"'");      //Load the wanted picture from Slides.html into the .slidePicture div.
		  $('.slideWrap').show(effect[0],{direction:dir[1]},stime);  //Slide the newly constructed slide and picture group from the bottom
		};
		function SlideOnlyB(){
			NewID = '#IL' + j + '';                                        //Make the new number a string					
			$("#Loader").load("Slides.html "+NewID+"'",function(){       //Load the previous slide from the Slides.html file, run callback function.
			  $('.slideText').hide();                                    //hide the loaded slide.
			  $('.slideText').show(effect[0],{direction:dir[2]},stime);  //slide it in from the left.
			});	
		};
		
		 function UpdateNavB(){
			jhotTab = '#'+hotTab;
			nT =h-1;
			newTab = '#IL'+nT+''
			$(jhotTab).removeClass('ui-state-active').addClass('ui-state-default');
			$(newTab).removeClass('ui-state-default').addClass('ui-state-active');
		};
		/*Back Functionality*/
		switch (i)                                                    // 1 added to context given that we are now going backwards.
		{
		  case context[1]+1:
			$('.slideWrap').hide(effect[0],{direction:dir[0]},stime);    //slide out current slide and picture through the top;
			setTimeout(function(){PicAndSlideB()},dtime);
			setTimeout(function(){UpdateNavB()},dtime);
			break;
		  case context[2]+1:
			$('.slideWrap').hide(effect[0],{direction:dir[0]},stime);    //slide out current slide and picture through the top;
			setTimeout(function(){PicAndSlideB()},dtime);
			setTimeout(function(){UpdateNavB()},dtime);
			break;
		  case context[3]+1:
			$('.slideWrap').hide(effect[0],{direction:dir[0]},stime);      //slide out current slide and picture through the top;
			setTimeout(function(){PicAndSlideB()},dtime);
			setTimeout(function(){UpdateNavB()},dtime);
			break;
		  default:
			$("#Loader").children().hide(effect[0],{direction:dir[2]},stime);  //slide out current slide to the left
			setTimeout(function(){SlideOnlyB()},dtime); 
			break;
		}		  
	  }
	
	/*ELSE controlls the NEXT functionality*/  
	else {
	  /*NEXT-only Variables*/
	  if(i==n){return}else{j=i+1};                //Add one to the number (=j), if on last slide, keep the show there. j is number value of the new slide ID;	
	 
	 /*NEXT-only functions*/
	  function PicAndSlide(){
		$('.slideWrap').hide();                               //Hide this whole new slide			
		NewSlideID = '#IL' + j + '';                           //Make the new number (slide) a string and adds pound sign to be added into Jquery function;
		NewPicID = '#IL'+ k + '';                               //Makes new picture a string and adds pound sign to be added into Jquery funtion;
		$("#Loader").load("Slides.html "+NewSlideID+"'");        //Load the next slide from the Slides.html file into the loader div.
		$('.slidePicture').load("Slides.html "+NewPicID+"'");      //Load the wanted picture from Slides.html into the .slidePicture div.
		$('.slideWrap').show(effect[0],{direction:dir[0]},stime);       //Slide the newly constructed slide and picture group from the top
	  };
	  
	  function SlideOnly(){
		  NewID = '#IL' + j + '';                                        //Make the new number a string					
		  $("#Loader").load("Slides.html "+NewID+"'",function(){     //Load the next slide from the Slides.html file, run callback function.
			$('.slideText').hide();                                      //hide the loaded slide.
			$('.slideText').show(effect[0],{direction:dir[3]},stime);         //slide it in from the right.
		  });	
	  };
	  
	  function UpdateNav(){                                              //Updates Navigation buttons on context change.  
		  jhotTab = '#'+hotTab;
		  nT =h+1;
		  newTab = '#IL'+nT+''
		  $(jhotTab).removeClass('ui-state-active').addClass('ui-state-default');
		  $(newTab).removeClass('ui-state-default').addClass('ui-state-active');
	  };
	  
	  function InitNav() {
			$("#IL31").addClass('ui-state-active');                          //Gives first navigation nav button the active state class.
	  };
	  
	  /*NEXT-only functionality*/
	  switch (i) 
		{
		 case context[0]:
			$('.slideWrap').hide(effect[0],{direction:dir[1]},stime);       //slide out current slide and picture through the bottom;
			setTimeout(function(){PicAndSlide()},dtime);
			setTimeout(function(){InitNav()},dtime);
			break;	
		  case context[1]:
			$('.slideWrap').hide(effect[0],{direction:dir[1]},stime);          //slide out current slide and picture through the bottom;
			setTimeout(function(){PicAndSlide()},dtime);
			setTimeout(function(){UpdateNav()},dtime);
			break;
		  case context[2]:
			$('.slideWrap').hide(effect[0],{direction:dir[1]},stime);           //slide out current slide and picture through the bottom;
			setTimeout(function(){PicAndSlide()},dtime);
			setTimeout(function(){UpdateNav()},dtime);
			break;
		  case context[3]:
			$('.slideWrap').hide("explode",{number:10},1000);                      //Explode last section before last slide.
			setTimeout(function(){PicAndSlide()},dtime);
			setTimeout(function(){UpdateNav()},dtime);
			break;
		  default:
			$("#Loader").children().hide(effect[0],{direction:dir[3]},stime);       //slide out current slide to the right
			setTimeout(function(){SlideOnly()},dtime); 
			break;
		}	
	}
	return false;
}); 

$(".ui-tabs-nav .ui-state-default").click(function(){                              //Slider navigation button switch active status on click.
	CurrID = this.id;
	CurrNum = CurrID.split("IL");
	i = parseFloat(CurrNum[1]);
	$(".ui-tabs-nav .ui-state-active").removeClass('ui-state-active').addClass('ui-state-default');
	$(this).removeClass('ui-state-default').addClass('ui-state-active');
	switch(i)
	{
		case 31:                                                                   //Why?
			$('.slideWrap').fadeOut(500,function(){
			  $('#Loader').load('Slides.html #IL1');
			  $('.slidePicture').load('Slides.html #IL20');
			  $('.slideWrap').fadeIn(500);
			});  
			break;
		case 32:                                                                    //Who?
			$('.slideWrap').fadeOut(500, function(){
			  $('#Loader').load('Slides.html #IL4');
			  $('.slidePicture').load('Slides.html #IL21');
			  $('.slideWrap').fadeIn(500);
			}); 
			break;
		case 33:                                                                     //How?
			$('.slideWrap').fadeOut(500, function(){
			  $('#Loader').load('Slides.html #IL7');
			  $('.slidePicture').load('Slides.html #IL22');
			  $('.slideWrap').fadeIn(500);
			}); 
			break;
		case 34:                                                                  //When?
			$('.slideWrap').fadeOut(500, function(){
			  $('#Loader').load('Slides.html #IL10');
			  $('.slidePicture').load('Slides.html #IL23');
			  $('.slideWrap').fadeIn(500);
			}); 
			break;
	}

});		

$(".ui-state-default").hover(function(){                                         //Slider navigation hover function
			$(this).addClass("tabHover")										   
			},
			function(){
			$(this).removeClass("tabHover")		
			}		
);

var hoveroff = {
			'background-image':'url(images/PRS_ContactButton.png)',
			'cursor':'default'
			};

$("#ContactButton").hover(function(hoveroff){                                                          //Contact Button Hover funtion
						var hoveron = {
							'background-image':'url(images/PRS_HoverContactButton.png)',
							'cursor':'pointer'
							};								   
						$(this).css(hoveron)										   
					},
					function(){
						$(this).css(hoveroff)		
					})
					.click(function(){
						$("#SignUpForm").dialog('open');				
					});

$("#SignUpForm").dialog({   //I preferred the 'scale' option for open and close but this was buggy.
	autoOpen: false,
	modal: true,
	show:'blind',
	width: 600,
	hide:'blind',
	minHeight:141
});
			

$("#CloseDialog").click(function(e,ui){
	$("#SignUpForm").dialog('option','hide','blind');			   
});
                                          

$(":input").blur(function(Val_helpers){
	RE_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;              
	RE_phoneNum =  /^\(?(\d{3})\)?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/;     
	input = $(this).val();														 
	length_50 = input.length>50;										 								   
	length_5000 = input.length>5000;		                               								   
	blank = $(this).val()==''; 
	
	blank_err = "This is a required field.";
	email_err = "Please enter a valid email address.";
	phone_err = "Example: 111-222-3333.";
	length_err_50 = "50 or fewer characters for this field.";
	length_err_5000 = "5000 or fewer characters for this field.";
	
	i=this.id;
	switch(i){
		case "EmailAddress":
			hasEmailErr = false;
			if(blank) {
				$(this).next().removeClass('pass');
				$(this).next().hide().addClass("fail").text(blank_err).fadeIn(1000);
				hasEmailErr = true;
			}else if(!RE_email.test(input)){
				$(this).next().removeClass('pass');
				$(this).next().hide().addClass("fail").text(email_err).fadeIn(1000);
				hasEmailErr = true;
				}else if(length_50){
					$(this).next().removeClass('pass');
					$(this).next().hide().addClass("fail").text(length_err_50).fadeIn(1000);
					hasEmailErr = true;
					};
			if(!hasEmailErr){
				$(this).next().removeClass('fail');
				if($(this).next().hasClass("pass")){break;}
				$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			}
			break;
		
		case "FirstName":
			hasF_NameErr = false;
			if(blank) {
				$(this).next().removeClass('pass');
				$(this).next().hide().addClass("fail").text(blank_err).fadeIn(1000);
				hasF_NameErr = true;
				}else if(length_50){
					$(this).next().removeClass('pass');
					$(this).next().hide().addClass("fail").text(length_err_50).fadeIn(1000);
					hasF_NameErr = true;
					};
			if(!hasF_NameErr){
				$(this).next().removeClass('fail');
				if($(this).next().hasClass("pass")){break;}
				$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			}		
			break;
		
		case "LastName":
			hasL_NameErr = false;
			if(blank) {
				$(this).next().removeClass('pass');
				$(this).next().hide().addClass("fail").text(blank_err).fadeIn(1000);
				hasL_NameErr = true;
				}else if(length_50){
					$(this).next().removeClass('pass');
					$(this).next().hide().addClass("fail").text(length_err_50).fadeIn(1000);
					hasL_NameErr = true;
					};
			if(!hasL_NameErr){
				$(this).next().removeClass('fail');
				if($(this).next().hasClass("pass")){break;}
				$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			}		
			break;
		
		case "Company":
			hasCompErr = false;
			if(blank) {
				$(this).next().removeClass('pass');
				$(this).next().hide().addClass("fail").text(blank_err).fadeIn(1000);
				hasCompErr = true;
				}else if(length_50){
					$(this).next().removeClass('pass');
					$(this).next().hide().addClass("fail").text(length_err_50).fadeIn(1000);
					hasCompErr = true;
					};
			if(!hasCompErr){
				$(this).next().removeClass('fail');
				if($(this).next().hasClass("pass")){break;}
				$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			}		
			break;
		
		case "PhoneNumber":
			hasPhoneErr = false;
			if(!RE_phoneNum.test(input)&!blank){
				$(this).nextAll('span').removeClass('pass');
				$(this).nextAll('span').hide().addClass("fail").text(phone_err).fadeIn(1000);
				hasPhoneErr = true;
				}else if(length_50){
					$(this).nextAll('span').removeClass('pass');
					$(this).nextAll('span').hide().addClass("fail").text(length_err_50).fadeIn(1000);
					hasPhoneErr = true;
					};
			if(!hasPhoneErr){
				$(this).nextAll('span').removeClass('fail');
				if($(this).nextAll('span').hasClass("pass")){break;}
				$(this).nextAll('span').hide().addClass("pass").text('').fadeIn(1000);
			}
			break;		
		
		case "Ext":
			break;
		
		case "Position":
			hasPosErr = false;
			if(length_50){
					$(this).next().removeClass('pass');
					$(this).next().hide().addClass("fail").text(length_err_50).fadeIn(1000);
					hasPosErr = true;
			};
			if(!hasPosErr){
				$(this).next().removeClass('fail');
				if($(this).next().hasClass("pass")){break;}
				$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			}			
			break;	
		
		case "Premium":
			if($(this).next().hasClass("pass")){break;}
			$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			break;
		
		case "UserComments":
			hasCommentErr = false;
			if(length_5000) {
				$(this).next().removeClass('pass');
				$(this).next().hide().addClass("fail").text(length_err_5000).fadeIn(1000);
				hasCommentErr = true;
			};
			if(!hasCommentErr){
				$(this).next().removeClass('fail');
				if($(this).next().hasClass("pass")){break;}
				$(this).next().hide().addClass("pass").text('').fadeIn(1000);
			}			
			break;
		
		default:
			break;		
	};
	//console.log(i);

}); //end validation function

$("form").submit(function(){
	Email = $("#EmailAddress").val();	
	FirstName = $("#FirstName").val();
	LastName = $("#LastName").val();
	Company = $("#Company").val();
	Position = $("#Position").val();
	PhoneNumber = $("#PhoneNumber").val();
	Premium = $("#Premium").val();
	UserComments = $("#UserComments").val();
	$("#Table1").fadeOut(1000, function(){
	  	  $("<div id='LoadingOverlay'/>").hide().appendTo('#SignUpForm').fadeIn(250); 	 									  
	  });	
	$.ajax({
		type: "post",
		url: "PRS_FormHandler.php",
		data: {email: Email, first_name: FirstName, last_name: LastName, company: Company, position: Position, phone_num: PhoneNumber, premium: Premium, user_comments:UserComments},
		timeout: 15000,                                        //If a response doesn't come within 15 seconds, abort ajax attempt.
		error: function(XMLHttpRequest,timeout){               //The function to run in the event of a timeout.    
			  	  to_response = "<p class='errorWarning'>An error occurred when saving your request, please try again.  If this problem persists, please send an email to Support@WCAssist.com so we can correct this ASAP.</p>"
				  $('#LoadingOverlay').fadeOut(250,function(){
					$(this).remove();
				  });  
				  $('<div id="Response" class="responseClass">'+to_response+'</div>').hide().appendTo("#SignUpForm").fadeIn(1000);
			      $("<div id='Refresh' class='responseClass'><p><a href ='#'>Click Here</a> to try your request again.</div></p>").hide().appendTo("#SignUpForm").fadeIn(1000);
				  $('#SignUpForm').dialog('option','buttons',{"Close": function(){$(this).dialog('close'); $('#Response').hide()}});
				  $('#Refresh').click(function(){
						$('#Response, #Refresh').fadeOut(500,function(){
							$('#Response, #Refresh').remove();										  
							$("#Table1").fadeIn(500);											  
						});
  				   });	 
				 },
		success: function(r){
			setTimeout (function(){
				$('#LoadingOverlay').fadeOut(250,function(){
					$(this).remove();										
				});				 
				$('<div id="Response">'+r+'</div>').hide().appendTo("#SignUpForm").fadeIn(1000);
				$("<div id='Refresh' class='responseClass'><p><a href ='#'>Click Here</a> to re-enter your information.</div></p>").hide().appendTo("#SignUpForm").fadeIn(1000);
				$('#Refresh').click(function(){
					$('#Response, #Refresh').fadeOut(500,function(){
							$('#Response, #Refresh').remove();										  
							 $("#Table1").fadeIn(500);											  
					});
  			    });  
				$('#SignUpForm').dialog('option','buttons',{"Close": function(){$(this).dialog('close'); $('#Response').hide() }});
			},1000)	
		}
 	}); //End $.ajax
	return false;
});



/*
$(window).resize(function(){	
	h = $("#SlideTabs").position();
	Wrap_W = $("#ContentWrap").width();         Equal to the right side
	Tabs_L = h.left;	
	Adj = (Wrap_W - (Tabs_L + 368) - 102) / 2;
	
	$("#ContactButton").css("right", Adj);
});
*/
})