/*
Project: 		Pacific Hair Site Content Element Controls
Author: 		Niń0 Morales
Date Created: 	December 07, 2010
*/
var helpers = {
	showOverlay : function(){
		if($("#overlay").length <= 0){
			$("#main").before('<div id="overlay"></div>');
			$("#overlay").css(
				{
					"width":$(window).width(),
					"height":$(window).height(),
					"top":0,
					"left":0
				}
			);
		}
		
		$("#overlay").fadeIn("fast");
	},
	
	hideOverlay : function(){
		$("#overlay").fadeOut("fast");
	},
	popUpContent : function(content, width, height,showVid,isPage){
		if($("#popContainer").length <= 0){
			$("#main").before( 
				'<div id="popContainer"><div id="popUp">' +
				'	<div>' +	
				'		<div id="popTopLeft"></div>' +	
				'		<div id="popTopCenter"></div>' +	
				'		<div id="popTopRight"></div>' +	
				'	</div>' +	
				'	<div>' +	
				'		<div id="popMiddleLeft"></div>' +	
				'		<div id="popContent">' +
				'			<div id="popInnerContent" class=""><div id="hiddenContent">' + content + '</div></div>' +
				'			<div id="popClose" class=""></div>' +
				'		</div>' +	
				'		<div id="popMiddleRight"></div>' +	
				'	</div>' +	
				'	<div>' +	
				'		<div id="popBottomLeft"></div>' +	
				'		<div id="popBottomCenter"></div>' +	
				'		<div id="popBottomRight"></div>' +	
				'	</div>' +	
				'</div></div>' 
				);
		}else{
			$("#hiddenContent").html(content);	
		}
		$("#hiddenContent").css({"display":"none"});
		if(width > $(window).width()){
			width = $(window).width();
		}
		if(height > $(window).height()){
			height = $(window).height();
		}
		var finalWidth = width;
		var finalheight = height;
		width = width - 200;
		height = height - 200
		var left = ($(window).width() - width) / 2;
		var top = ($(window).height() - height) / 2;
		var midWidth = width - 50;
		var midHeight = height - 50;
		width = width == null || width == undefined ? "200px" : width + "px";
		height = height == null || height == undefined ? "100px" : height + "px";
		
		$("#popContainer").css(
			{
				"width":width,
				"height":height,
				"top":top,
				"left":left
			}
		);
		$("#popUp").css(
			{
				"width":width,
				"height":height
			}
		);
		$("#popTopCenter, #popBottomCenter, #popContent").width(midWidth);
		$("#popMiddleLeft, #popMiddleRight, #popContent").height(midHeight);
		$("#popContainer").fadeIn(500);
		
		left = ($(window).width() - finalWidth) / 2;
		top = ($(window).height() - finalheight) / 2;
		midWidth = finalWidth - 50;
		midHeight = finalheight - 50;
		
		$("#popContainer").animate(
			{
				"width":finalWidth,
				"height":finalheight,
				"top":top,
				"left":left
			},
			{
				duration:200,
				step: function(){
					width = $(this).width() - 10;
					height = $(this).height();
					midWidth = width - 50;
					midHeight = height - 50;
					$("#popUp").css(
						{
							"width":width,
							"height":height
						}
					);
					$("#popTopCenter, #popBottomCenter, #popContent").width(midWidth);
					$("#popMiddleLeft, #popMiddleRight, #popContent").height(midHeight);	
				},
				complete: function(){
					$("#hiddenContent").fadeIn(500);
				}
			}
		);
		
		if(showVid){
			$("#popUp .playerContainer2").html('<div id="videoHolder2"></div>');
			helpers.createPlayer2(isPage);
			this.bindImgThumb();
		}else{
			helpers.bindCheckBox();
			helpers.bindForm();
		}
		$("#popClose").unbind();
		$("#popClose").click(
			function(){
				$("#popContainer").fadeOut("fast",
					function(){
						helpers.hideOverlay();		
					}
				);
			}
		);
		$("#popUp").fadeIn("fast");
	},
	echeck : function(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(dot,(lat+2))==-1){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(" ")!=-1){
			alert("Invalid E-mail ID")
			return false
		}
		return true					
	}, 
	bindCheckBox : function(){
		$(".frm_checkbox input").unbind();
		$(".frm_checkbox input").click(
			function(){
				if($(this).is(":checked")){
					$(this).parent().css({"background-position" : "left bottom"});	
				}else{
					$(this).parent().css({"background-position" : "left top"});		
				}
			}
		)
	},
	bindImgThumb : function(){
		$("#thumbContainer .thumbItem img").unbind();
		$("#thumbContainer .thumbItem img").click(
			function(){
				var vid = $(this).attr("alt");
				var title = $(this).parent().attr("title");
				var player2 = window.document["player2"];
				player2.sendEvent('STOP');
				player2.sendEvent('LOAD', vid);
				$("#VidTitle").html(title);
			}
		);
	},
	bindImgThumb2 : function(obj, vplayer){
		$(obj).click(
			function(){
				var vid = $(this).attr("alt");
				var _title = $(this).attr("title");
				var player = window.document[vplayer];
				if(vplayer == "player1"){
					$("#currntVid").val(vid);	
					$("#currntTitle").val(_title);	
				}else{
					$("#currntVid2").val(vid);	
					$("#currntTitle2").val(_title);	
				}
				player.sendEvent('STOP');
				player.sendEvent('LOAD', vid);
				player.sendEvent('PLAY');
			}
		);
	},
	createPlayer : function() {
		var flashvars = {
			file:templateURL + "/videos/elliott_philosophy.flv", 
			image:templateURL + "/images/video.jpg", 
			autostart:"false"
		}
	
		var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always",
			wmode:"transparent"
		}
	
		var attributes = {
			id:"player1",  
			name:"player1"
		}
	
		swfobject.embedSWF(templateURL + "/player.swf", "vidHolder1", "400", "260", "9.0.115", false, flashvars, params, attributes);
	},
	createPlayer2 : function(isPage) {
		var _file = $("#currntVid").val();
		var _title = $("#currntTitle").val();
		if(isPage){
			_file = $("#currntVid2").val();
			_title = $("#currntTitle2").val();
		}
		var flashvars = {
			file:_file, 
			autostart:"true"
		}
	
		var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always",
			wmode:"transparent"
		}
	
		var attributes = {
			id:"player2",  
			name:"player2"
		}
		$("#VidTitle").html(_title);
		swfobject.embedSWF(templateURL + "/player.swf", "videoHolder2", "800", "500", "9.0.115", false, flashvars, params, attributes);
	},
	createPlayer3 : function() {
		var flashvars = {
			file:templateURL + "/videos/mauro_s_interview.flv", 
			image:templateURL + "/images/video2.jpg", 
			autostart:"false"
		}
	
		var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always",
			wmode:"transparent"
		}
	
		var attributes = {
			id:"player3",  
			name:"player3"
		}
	
		swfobject.embedSWF(templateURL + "/player.swf", "vidHolder3", "272", "194", "9.0.115", false, flashvars, params, attributes);
		
	},
	bindForm : function(){
		$("form").unbind();
		$("form").submit(
			function(){
				var canSubmit = true;
				$("input, textarea", this).each(
					function(index,item){
						if($(this).parent().hasClass("form-required")){
							if(($(this).val() == "") || ($(this).val() == $(this).attr("title"))){
								if($(this).attr("id") != "field_phone-number"){
									alert("Please fill up [" +  $(this).attr("title") + "] field");	
									$(this).val("");
									$(this).focus();
									canSubmit = false;
									return false;
								}
							}	
						}
						if($(item).attr("title") == "Email*:" || $(item).attr("title") == "Your Email"){
							if(!helpers.echeck($(item).val())){
								$(item).val("");
								$(item).focus();
								canSubmit = false;
								return false;	
							}
						}
					}
				);
				return canSubmit;
			}
		);	
	}

}


$(document).ready(function() {
	helpers.bindImgThumb2(".sidebarBG1 .thumbContainer img","player1");
	helpers.bindImgThumb2(".testi .thumbContainer img", "player3");
	helpers.bindForm();
	helpers.bindCheckBox();
	$("div.ngg-galleryoverview").before('<a href="' + siteURL + '/before-and-after/" class="backToAlbum">Back to Album</a>');
	$("a.popout, a.pTesti, a.sideVideo").click(
		function(){
			var player1 = window.document["player1"];
			if($(this).hasClass("pTesti")){
				player1 = window.document["player3"];
			}
			player1.sendEvent('STOP');
			helpers.showOverlay();
			var content =  $("#videoContent").html();
			var isPage = false;
			if($(this).hasClass("pTesti")){
				content =  $("#videoContent2").html();
				isPage = true;
			}
			helpers.popUpContent(content,900,720,true,isPage);
			return false;
		}
	);
	$("#video_gallery img").click(
		function(){
			var vid = $(this).attr("alt");
			var _title = $(this).attr("title");
			$("#currntVid2").val(vid);	
			$("#currntTitle2").val(_title);	
			helpers.popUpContent("<div class='vidInner'><div class='playerContainer2'></div></div>",900,600,true,true);
			return false;
		}
	);
	
//	$("a.call, span.phone").click(
//		function(){
//			helpers.showOverlay();	
//			var cTitle = $(this).html();
//			if(cTitle == ""){
//				$("#clickToCall .txtCTitle").html("Click To Call");	
//			}else{
//				$("#clickToCall .txtCTitle").html(cTitle);	
//			}
//			var content =  $("#clickToCall").html();
//			helpers.popUpContent(content,500,620,false,null);
//			return false;
//		}
//	);
	
	$("#menu ul").children("li").each(
		function(index,item){
			$(item).hover(
				function(){
					toplineHover = true;
					var pos = $(this).offset();
					$("ul:first",this).stop(true, true).fadeIn(500);
					$(this).siblings().each(
						function(index,item){
							if($("ul:first",$(item)).is(":visible")){
								$("ul:first",$(item)).fadeOut(500);
							}
						}
					);
				},
				function(){
					$("ul:first",this).fadeOut(500);
					toplineHover = false;
				}
			)
		}
	);
	$("#menu ul li a").click(
		function(){
			if($(this).next().is("ul")){
				return false;	
			}
		}
	)
	$(".clickToCall").mouseover(
		function(){
			$("#menu").children().each(
				function(index,item){
					if($("ul:first",$(item)).is(":visible")){
						$("ul:first",$(item)).fadeOut(500);
					}
				}
			);	
		}
	);
	
	$(".inputlbl").click(
		function(){
			if($(this).val() == $(this).attr("title")){
				$(this).val("");	
			}
		}
	);
	$(".inputlbl").blur(
		function(){
			if($(this).val() == ""){
				$(this).val($(this).attr("title"));	
			}
		}					
	)
	
	$(".next").click(
		function(){
			var _slider = null;
			if($(this).hasClass("galBeforeAfter")){
				_slider = $(".galBeforeAfter .thumbGroup");
				var nML = (parseInt(_slider.css("marginLeft")) - 192) * (-1);
				var aWidth = $(".galBeforeAfter .thumbGroup img").length * 192;
				if(aWidth > nML){
					_slider.animate({"marginLeft": "-=192px"}, 400);
				}else{
					_slider.animate({"marginLeft": "0px"}, 400);	
				}
			}else{
				_slider = $(".thumbGroup", $(this).prev(".thumbContainer"));
				if($(this).hasClass("galTesti")){
					var nML3 = (parseInt(_slider.css("marginLeft")) - 116) * (-1);
					var aWidth3 = $("img",_slider ).length * 116;
					if(aWidth3 > nML3){
						_slider.animate({"marginLeft": "-=116px"}, 400);
					}else{
						_slider.animate({"marginLeft": "0px"}, 400);	
					}
				}else{
					var nML2 = (parseInt(_slider.css("marginLeft")) - 121) * (-1);
					var aWidth2 = $("img",_slider ).length * 121;
					if(aWidth2 > nML2){
						_slider.animate({"marginLeft": "-=363px"}, 400);
					}else{
						_slider.animate({"marginLeft": "0px"}, 400);	
					}
				}
			}
			return false;
		}
	);
	$(".prev").click(
		function(){
			var _slider = null;
			if($(this).hasClass("galBeforeAfter")){
				_slider = $(".galBeforeAfter .thumbGroup");
				
				var nML = (parseInt(_slider.css("marginLeft")) + 192) * (-1);
				var aWidth = (($(".galBeforeAfter .thumbGroup img").length * 192) - 192) * (-1);
				if(nML > 0){
					_slider.animate({"marginLeft": "+=192px"}, 400);
				}else{
					_slider.animate({"marginLeft": aWidth + "px"}, 400);	
				}
			}else{
				_slider = $(".thumbGroup", $(this).next(".thumbContainer"));
				if($(this).hasClass("galTesti")){
					var nML3 = (parseInt(_slider.css("marginLeft")) + 116) * (-1);
					var aWidth3 = (($("img",_slider ).length * 116) - 116) * (-1);
					if(nML3 > -1){
						_slider.animate({"marginLeft": "+=116px"}, 400);
					}else{
						_slider.animate({"marginLeft": aWidth3 + "px"}, 400);	
					}
				}else{
					var nML2 = (parseInt(_slider.css("marginLeft")) + 121) * (-1);
					var aWidth2 = (($("img",_slider ).length * 121) - 121) * (-1);
					if(nML2 > 0){
						_slider.animate({"marginLeft": "+=363px"}, 400);
					}else{
						_slider.animate({"marginLeft": aWidth2 + "px"}, 400);	
					}
				}
			}
			return false;
		}
	);
	$(".galBeforeAfter .thumbGroup img").click(
		function(){
			var imgPath = $(this).attr("src");
			$("#imgMain").attr("src",imgPath);
			return false;	
		}
	)
	//$("div.accordionContent:first").slideDown(500);
	$("h1.accordion").click(
		function(){
			var _next = $(this).next("div.accordionContent");
			$("div.accordionContent:visible").not(_next).slideToggle(500);
			_next.slideToggle(500);
		}
	);
	$("a[rel='beforeAndAfter']").colorbox({width:"400px"});
});

