// Methods

function click() {
    if (event.button == 2) {
        alert("Right click disabled.")
    }
}

if (document.all) document.onmousedown = click;
window.oncontextmenu = function () {
    return false
}

var submitForm = function(str){
	$(str).submit();
};

var googleEvent = function(cat, name, desc){
	var url = document.URL;
	
	var eventArr = ["_trackEvent", cat, name, desc + " :: " + url];
	
	_gaq.push(eventArr);
	
	//_gaq.push([cat, name, desc]);
};

var setDefaultText = function(strJquery, strDefault, colorDefault, colorActive){
	if(!colorDefault){
		colorDefault = "#999";
	}
	
	if(!colorActive){
		colorActive = "#000";
	}
	
	var chosenInput = $(strJquery);
	
	if(chosenInput.val() == strDefault){
		chosenInput.css({ color: colorDefault });
	}

	chosenInput.click(function(){
		if($(this).val() == strDefault){
			$(this).val("");
			$(this).css({ color: colorActive });
		}
	});
	
	chosenInput.blur(function(){
		if($(this).val() == ""){
			$(this).val(strDefault);
			$(this).css({ color: colorDefault });
		}
	});
}



var searchForENTDoctor = function(strFormIdentifier){
	var zip = $(strFormIdentifier + " input.zip").val();
	var radius = $(strFormIdentifier + " select.styled").val();
	
	if(zip == "Enter your zip code"){
		alert("Please enter a zip code.");
	} else {
		window.location.href = siteURL + "/find-an-ent-doctor/?radius=" + radius + "&zip=" + zip;
	}
}

var isBSPRightForMe = function(){
	if(isBSPRightForMe_validation1()){
		isBSPRightForMe_validation2();
	}
}

var isBSPRightForMe_validation1 = function(){
	var checked;
	
	checked = false;
	$("ul#symptoms_fields input").each(function(){
		if($(this).attr("checked")){
			checked = true;
			return false;
		}
	});
	
	if(!checked){
		alert("Please fill out: 'Do you experience any of the following symptoms?'");
		return false;
	}
	
	checked = false;
	$("table#prescribed_medications_fields input").each(function(){
		if($(this).attr("checked")){
			checked = true;
			return false;
		}
	});
	
	if(!checked){
		alert("Please fill out: 'Has a doctor prescribed sinus medication for you to address your condition(s)?'");
		return false;
	}
	
	checked = false;
	$("table#weekly_medications_fields input").each(function(){
		if($(this).attr("checked")){
			checked = true;
			return false;
		}
	});
	
	if(!checked){
		alert("Please fill out: 'In the past year, how many weeks have you taken sinus medication for your condition?'");
		return false;
	}
	
	checked = false;
	$("table#doctor_referral_fields input").each(function(){
		if($(this).attr("checked")){
			checked = true;
			return false;
		}
	});
	
	if(!checked){
		alert("Please fill out: 'Has your doctor referred you to an Ear, Nose & Throat (ENT) specialist?'");
		return false;
	}
	
	return true;
}

var isBSPRightForMe_validation2 = function(){
	if($("#symptoms_004").attr("checked")){
		//window.location.href = siteURL + "/speak-to-your-physician/";
		BSPIsNotRightForYou();
	} else if($("#prescribed_medications_001").attr("checked")){
		//window.location.href = siteURL + "/speak-to-your-physician/";
		BSPIsNotRightForYou();
	} else if($("#weekly_medications_000").attr("checked") || $("#weekly_medications_001").attr("checked") || $("#weekly_medications_002").attr("checked")){
		//window.location.href = siteURL + "/speak-to-your-physician/";
		BSPIsNotRightForYou();
	} else {
		window.location.href = siteURL + "/you-may-be-a-good-candidate-for-bsp/";
	}
}

var BSPIsNotRightForYou = function(){
	alert("Based on your responses you are not a good match for the Balloon Sinuplasty Technology at this time.\n\nHowever, we recommend you speak to your physician or contact an Ear, Nose and Throat specialist to discuss other options that may be available and appropriate for you.");
}

var addThisMod = function(){
	var eHandler = function(e){
		if(e.data.pane == "email"){
			$$ = $("#at_email");
			$$.find("label:eq(2)").css({"display":"none"});
			$$.find("#at_msg").css({"display":"none"});
			$$.find("#ateml").css({"display":"none"});
		}
	}
	
	addthis.addEventListener('addthis.menu.open', eHandler);
}

var buttonSlideTime = 300;
var buttonFadeTime = document.all ? 0 : 300;
//buttonFadeTime = 1200;

$(function(){
	addThisMod();
	
	var caroView = new CarouselView();
	caroView.addItems("div.gallerypage");
	
	var caroMenu = new CarouselMenu();
	caroMenu.addItems("div.tabs div.tab");
	
	setDefaultText("div.search input", "Search");
	setDefaultText("input#s", "Search");
	setDefaultText("form.findadoctor input.zip", "Enter your zip code", "#000");
	setDefaultText("form.findadoctor-home input.zip", "Enter your zip code", "#000");
	
	var onTabHover = function(){
		//$(this).animate({opacity: 1}, {duration: 500, queue: false});
	};
	
	var onTabHoverOut = function(){
		//$(this).animate({opacity: 0}, {duration: 500, queue: false});
	};
	
	var onTabSelected = function(){
		var val = $(this).data("index");
		
		caroView.select(val);
		
		var videoNameArr = ["", "Procedure Animation :: Homepage", "Sally Mattson :: Homepage", ""];
		
		var videoButton = $(".gallerypage").eq(val).find(".bspvideobutton");
		if(videoButton.length){
			
			if(videoNameArr[val] != ""){
				googleEvent('Video', 'Play', videoNameArr[val]);
			}
			
			videoButton.trigger("mouseover");
			
			flagHideTimeout = setTimeout(function(){
				videoButton.trigger("mouseout");
			}, 3000);
		}
	};
	
	//$("div.tabs div.tab").hover(onTabHover, onTabHoverOut);
	
	$("div.tabs div.tab").click(onTabSelected);
	
	$("div.tabs div.tab").each(function(i){
		$(this).data("index", i);
	});
	
	
	
	$("div.nav div.tab").hover(function(){
		if(!$(this).hasClass("selected")){
			$(this).addClass("over");
		}
		
		if($(this).children("div.dropdown").length){
			$(this).children("div.dropdown").removeClass("hidden");
			$(this).children("div.dropdown").stop(true, true).fadeIn(buttonFadeTime);
		}
	}, function(){
		if(!$(this).hasClass("selected")){
			$(this).removeClass("over");
		}
		
		if($(this).children("div.dropdown").length){
			$(this).children("div.dropdown").stop(true, true).fadeOut(buttonFadeTime);
		}
	});
	
	
	
	// Carousel - auto rotate
	//var homeCarousel = $("div.home div.middle");
	//if(homeCarousel.length){
	//	// Model
	//	var hc = {
	//		currentSlide: 0,
	//		timePerSlide: 6000,
	//		clicked: false
	//	}
	//	
	//	// Control
	//	homeCarousel.mousedown(function(){
	//		homeCarousel.unbind('mousedown');
	//		hc.clicked = true;
	//	});
	//	
	//	$("video").each(function(){
	//		$(this).get(0).pause();
	//	});
	//	
	//	//loadstart
	//	
	//	hc.interval = setInterval(function(){
	//		if(hc.clicked){
	//			clearInterval(hc.interval);
	//			return false;
	//		}
	//		
	//		hc.currentSlide = (hc.currentSlide < 3) ? hc.currentSlide + 1 : 0;
	//		
	//		homeCarousel.find("div.tab").eq(hc.currentSlide).trigger("click");
	//		hc.clicked = false;
	//	}, hc.timePerSlide);
	//}
	
	
	var flagHideTimeout;
	
	$(".bspvideobutton").hover(function(){
		$$ = $(this).parent().find(".bspbutton");
		//if(allVidsButton.css("display") == "none"){
			//allVidsButton.stop(true, true).fadeIn(buttonFadeTime);
		clearTimeout(flagHideTimeout);
		$$.stop(true).animate({"margin-left":0}, buttonSlideTime, "easeInOutCubic");
		//}
	}, function(){
		//$(this).parent().find(".bspbutton.all-videos").stop(true, true).fadeOut(300);
		$$ = $(this).parent().find(".bspbutton");
		flagHideTimeout = setTimeout(function(){
			$$.stop(true).animate({"margin-left":"315px"}, buttonSlideTime, "easeInOutCubic");
		}, 33);
	});
	
	$(".bspvideooverlay").hover(function(){
		//$(this).stop(true, true).fadeIn(buttonFadeTime);
		//$(this).children(".bspbutton").stop(true, true).animate({"margin-left":0}, buttonFadeTime);
		
		$$ = $(this).children(".bspbutton");
		
		if($$.css("margin-left") != "315px"){
			clearTimeout(flagHideTimeout);
			$$.stop(true).animate({"margin-left":0}, buttonSlideTime, "easeInOutCubic");
		}
	}, function(){
		//$(this).stop(true, true).fadeOut(buttonFadeTime);
		$$ = $(this).children(".bspbutton");
		flagHideTimeout = setTimeout(function(){
			$$.stop(true).animate({"margin-left":"315px"}, buttonSlideTime, "easeInOutCubic");
		}, 33);
	});
	
	$('div.testimonial div.right div.desc').expander({
		slicePoint: 205,
		widow: 2,
		
		expandText: "<br/><br/> (Read more)",
		expandPrefix: "...",
		
		expandSpeed: 1000,
		
		userCollapseText: "<br/><br/> (Minimize)",
		
		beforeExpand: function(elem){
			$("div.testimonial div.right div.desc").each(function(){
				$(this).children("span.read-more").fadeIn(0);
				$(this).children("span.details").fadeOut(0);
			});
			
			elem.children("span.read-more").fadeOut(0);
		}
	});
	
	//$('.news .general .story.default .desc').expander({
	//	slicePoint: 110,
	//	widow: 2,
	//	
	//	expandText: "<br/> <span class='cta'>Read More</span>",
	//	expandPrefix: "...",
	//	
	//	expandSpeed: 1000,
	//	
	//	userCollapseText: "<br/> <span class='cta'>Minimize</span>",
	//	
	//	beforeExpand: function(elem){
	//		$(".news .general .story.default").each(function(){
	//			$(this).children("span.read-more").fadeIn(0);
	//			$(this).children("span.details").fadeOut(0);
	//		});
	//		
	//		elem.children("span.read-more").fadeOut(0);
	//	}
	//});
	
	//VideoJS.setup("test_video2");
	
	var fbOnStart = function(e){
		var strTarget = e + "";
		var selector = strTarget.substr(strTarget.indexOf("#"));
		
		var video = $(selector).find("video");
		if(video.length){
			video.get(0).play();
		}
		
		var gname = $(selector).attr("gname");
		if(gname){
			googleEvent('Video', 'Play', gname);
		}
	}
	
	var fbOnEnd = function(e){
		var strTarget = e + "";
		var selector = strTarget.substr(strTarget.indexOf("#"));
		
		var video = $(selector).find("video");
		if(video.length){
			video.get(0).pause();
		}
	}
	
	if($("a.view-video").length){
		$("a.view-video").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			//'overlayShow'	:	false,
			'hideOnContentClick' : false,
			'onComplete'		:	fbOnStart,
			'onCleanup'		:	fbOnEnd
			//'padding'			: 	0
		});
	}
	
	$("a.email-popup").each(function(i){
		var doctorName = $(this).attr("strName");
		var doctorID = $(this).attr("numId");
		$(this).fancybox({
			'transitionIn'		:	'elastic',
			'transitionOut'		:	'elastic',
			'speedIn'			:	600, 
			'speedOut'		:	200, 
			//'overlayShow'	:	false,
			'hideOnContentClick' :	false,
			'onStart'			:	function(){
				$("#email-popup .strName").text(doctorName);
				$("#email-popup .numId").val(doctorID);
			}
		});
	});
	
	$("a.send-email-confirmation").each(function(i){
		$(this).fancybox({
			'transitionIn'		:	'elastic',
			'transitionOut'		:	'elastic',
			'speedIn'			:	600,
			'speedOut'		:	200,
			//'overlayShow'	:	false,
			'hideOnContentClick' :	false,
			'onStart'			:	function(){
				
				$.ajax({
					data: {
						"id"		:	$("#email-popup input.numId").val(),
						"name"	:	$("#email-popup input.name").val(),
						"subject"	:	$("#email-popup input.subject").val(),
						"email"	:	$("#email-popup input.email").val(),
						"content"	:	$("#email-popup textarea.content").val()
					},
					
					url			:	"/wp-content/plugins/mailto/mailto.php",
					
					type			:	"POST"
				});
			}
		});
	});
	
	if (document.all && !document.querySelector) { // Testing for IE
		// Fixing dropdown absolute placement for IE7
		$("div.nav div.tabholder div.tab div.dropdown div.middle ul").each(function(i){
			$(this).css({
				"line-height": "18px"
			});
		});
	}
	
	if($.browser.webkit){
		$("div.header div.social-search span.st_sharethis_large").css({
			margin: 0
		});
	}
});
