/*
Project: 		Addiction Recovery Content Element Controls
Author: 		Niń0 Morales
Date Created: 	January 01, 2011
*/

var ctrl = {
	showContent : function(){
		
	},
	animateInde : function(){
		$(".suc").animate({"marginLeft":"182px"},300,
			function(){
				$(".fac2").animate({"marginLeft":"205px"},300,
					function(){
						$(".staff").animate({"marginTop":"-8px"},300,
							function(){
								$(".res").animate({"marginTop":"106px"},300,
									function(){
										$(".rc").animate({"marginLeft":"134px"},300,
											function(){
												$(".rat").animate({"marginLeft":"363px"},300,
													function(){
														$(".rev").animate({"marginLeft":"84px"},300,
															function(){
																$(".fac").animate({"marginTop":"19px"},300,
																	function(){
																		$(".cen").animate({"marginLeft":"186px"},300,
																			function(){
																				$(".wor").animate({"marginLeft":"358px"},300,
																					function(){
																						$(".unb").animate({"marginTop":"176px"},300,
																							function(){
																								$(".res2").animate({"marginLeft":"298px"},300,
																									function(){
																										$(".vs").animate({"marginLeft":"175px"},300,
																											function(){
																												$(".rts").animate({"marginLeft":"377px"},300,
																													function(){
																														$(".based").animate({"marginTop":"103px"},300,
																															function(){
																																$(".ind2").animate({"marginLeft":"148px"},300,
																																	function(){
																																		$(".add").animate({"marginLeft":"8px"},300,
																																			function(){
																																				$(".pat").animate({"marginLeft":"86px"},300,
																																					function(){
																																						$(".revd").animate({"marginLeft":"289px"},300,
																																							function(){
																																								$(".rec").animate({"marginTop":"-12px"},300);	
																																							}
																																						);	
																																					}
																																				);	
																																			}
																																		);	
																																	}
																																);	
																															}
																														);
																													}
																												);
																											}
																										);
																									}
																								);
																							}
																						);
																					}
																				);
																			}
																		);
																	}
																);
															}
														);
													}
												);
											}
										);
									}
								);
							}
						);
					}
				);
			}
		);
	},
	
	bindColorbox : function(){
		$(".btnContactUs, .learMore").unbind();
		$(".btnContactUs, .learMore").colorbox({width:1000, 
			onComplete : function(){
				$(".findTreatment").colorbox({width:1000,
					onComplete : function(){
						ctrl.bindForm();
					}
				});
				$(".reviewTreatment").colorbox({width:1000,
					onComplete : function(){
						ctrl.bindForm();
					}
				});
				$(".listFacility").colorbox({width:1000,
					onComplete : function(){
						ctrl.bindForm();
					}
				});
				$("#employment").colorbox({width:1000,
					onComplete : function(){
						$("input.file:file").change(
							function(){
								$(this).prev("input.fake_file").val($(this).val());
							}
						);
						$("#btnUploadResume").click(
							function(){
								return ctrl.ajaxFileUpload();
							}
						)
						ctrl.bindForm();
					}
				});
			}
		});
	},
	
	bindForm : function(){
		$("form").submit(
			function(){
				var fID = $(this).attr("id");
				var _data = $(this).serialize() + "&ajax=yes";
				var _url = $(this).attr("action");
				
				if(fID == "frmNewsletter"){
					$("#email").val("Sending...");
				}else{
					$("#p_msg")
					.css("display","none")
					.html("<p class='info'>Sending data. Please wait...</p>")
					.fadeIn("slow");
				}
				
				jQuery.ajax({
				   type: "POST",
				   url: _url,
				   data: _data,
				   success: function(msg){
					   if(fID == "frmNewsletter"){
							$("#email").val(msg);
					   }else{
						   $("#p_msg").html(msg)
							.delay(3000)
							.fadeOut("slow");
					   }
					   if(fID == "frmNewsletter"){
							if(msg == "Thank you!")   {
								//$("#email").delay(3000);
							}
					   }else{
					   		if(msg.indexOf("<!--RESET-->") > -1){
						   
							   $("input[type=text], textarea").each(
									function(index,item){
										$(item).val("")
									}
							   );
							   if(fID == "frmEmployment"){
									$("#fileToUpload").val(""); 
									$("#fileUploaded").val("") 
							   }
						   }
					   }
				   }
				});
				return false;
			}
		)
	},
	
	animInde : function(){
		if($(".independent").length > 0){
			$(".c_independent .innerContent h1.innerTitle, .c_independent .innerContent h2.subTitle, .c_independent .innerContent p, .c_independent .innerContent ul")
			.css({"display":"none"});
			$(".c_inspiring, .c_interactive").css({"margin-top":"-999999px"});
			$(".independent img, .independent span").stop(true, false).attr("style","");
			$(".c_independent").css({"margin-top":"0px", "display":"none"});
			
			$(".c_independent").fadeIn("slow",
				function(){
					$(".independent").fadeIn("slow",
						function(){
							ctrl.animateInde();
							$(".c_independent .innerContent h1.innerTitle").fadeIn("slow",
								function(){
									$(".c_independent .innerContent h2.subTitle").fadeIn("slow",
										function(){
											$(".c_independent .innerContent p, .c_independent .innerContent ul").fadeIn("slow");	
										}
									);	
								}
							);						
						}
					);
				}
			);
		}
	},
	
	animInter : function(){	
		if($(".interactive").length > 0){
			$(".c_interactive .innerContent h1.innerTitle, .c_interactive .innerContent h2.subTitle, .c_interactive .innerContent p, .c_interactive .innerContent ul")
			.css({"display":"none"});
			$(".c_independent, .c_inspiring").css({"margin-top":"-999999px"});
			$(".c_interactive").css({"margin-top":"0px", "display":"none"});
			$("#phone").attr("style","");
			
			$(".c_interactive").fadeIn("slow",
				function(){
					$("#phone").animate({"marginTop":0},300,
						function(){
							$(".c_interactive .innerContent h1.innerTitle").fadeIn("slow",
								function(){
									$(".c_interactive .innerContent h2.subTitle").fadeIn("slow",
										function(){
											$(".c_interactive .innerContent p").fadeIn("slow");	
										}
									);	
								}
							);
						}
					);	
				}
			);
		}
	
	},
	
	animIns : function(){	
		if($(".inspiring").length > 0){
			$(".c_inspiring .innerContent h1.innerTitle, .c_inspiring .innerContent h2.subTitle, .c_inspiring .innerContent p, .c_inspiring .innerContent ul")
			.css({"display":"none"});
			$(".c_independent, .c_interactive").css({"margin-top":"-999999px"});
			$(".c_inspiring").css({"margin-top":"0px", "display":"none"});
			$(".inspiring h1, .inspiring h2").attr("style","");
			$(".inspiring h1").animate({"marginRight":20},500);
			
			$(".c_inspiring").fadeIn("slow",
				function(){
					$(".inspiring h2").animate({"marginRight":20},500,
						function(){
							$(".c_inspiring .innerContent h1.innerTitle").fadeIn("slow",
								function(){
									$(".c_inspiring .innerContent h2.subTitle").fadeIn("slow",
										function(){
											$(".c_inspiring .innerContent p, .c_inspiring .innerContent ul").fadeIn("slow");	
										}
									);	
								}
							);
						}
					);	
				}
			);
		}		
	},
	contentResize : function(){
		if($(window).width() < 1310){
			var _w = (1310 - $(window).width()) * -1
			$("#sun").css("right", _w);
		}else{
			$("#sun").css("right", 0);	
		}
	},
	
	ajaxFileUpload : function(){
		/*$("#loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});*/
		if($("#fileToUpload").val() != "" && $("#fileUploaded").val() == ""){
			$("#p_msg")
			.css("display","none")
			.html("<p class='info'>Sending data. Please wait...</p>")
			.fadeIn("slow");
			$.ajaxFileUpload
			(
				{
					url:'ajax/doAjaxUpload.php',
					secureuri:false,
					fileElementId:'fileToUpload',
					dataType: 'json',
					success: function (data, status)
					{
						if(typeof(data.error) != 'undefined')
						{
							if(data.error != '')
							{
								alert(data.error);
							}else
							{
								$("#fileUploaded").val(data.msg);
								$("#frmEmployment").submit();
							}
						}
					},
					error: function (data, status, e)
					{
						alert(e);
					}
				}
			)
			
			return false;
		}
	}
	
}
$(document).ready(function() {
	$("#sidebar a.inter").click(
		function(){
			ctrl.animInter();
			return false;
		}
	);
	$("#sidebar a.ind").click(
		function(){
			ctrl.animInde();
			return false;
		}
	);
	$("#sidebar a.ins").click(
		function(){
			ctrl.animIns();
			return false;
		}
	);
	
	$("a.sideLink").hover(
		function(){
			$(this).stop(true,false).animate({"marginRight":"10px"},300);		
		},
		function(){
			$(this).stop(true,false).animate({"marginRight":"0px"},300);
		}
	)
	
	
	$(".txtInput").click(
		function(){
			if($(this).val() == $(this).attr("title")){
				$(this).val("");	
			}
		}
	);
	$(".txtInput").blur(
		function(){
			if($(this).val() == ""){
				$(this).val($(this).attr("title"));	
			}
		}					
	);
	
	ctrl.animInter();	
	
	if(jQuery.browser.msie){
		//$("img#phone").trigger("load");
	}
	
	ctrl.bindColorbox();
	ctrl.bindForm();
	ctrl.contentResize();
	$(window).resize(
		function(){
			ctrl.contentResize();
		}
	);
	$(window).scroll(
		function(){
			var posTop = $(window).scrollTop() * (-1);
			$("#sun").stop(true, false).animate({"top":posTop}, 4000);
		}
	);
});

