// JavaScript Document
	var attentionInterval;
	$(document).ready(function(){
		
		/****************************************************************
		Navigation Dropdown Logic
		****************************************************************/
		$(".dropDown").mouseenter(function(){if($(this).hasClass("open")){return false;}else{$(this).addClass("open").children("ul").slideDown("fast");}});		
		$(".dropDown").mouseleave(function(){$(this).children("ul").slideUp("fast", function(){$(".dropDown").removeClass("open");});});
		/****************************************************************
		Form Input Blur Logic
		****************************************************************/
		$('input[type="text"]').blur(function () {
    	$(this).css({ backgroundColor: "#FFFFFF" });
    	if (this.value == "") { this.value = this.defaultValue; }
  	});
		/****************************************************************
		Form Input Focus Logic
		****************************************************************/
		$('input[type="text"], textarea').live("focus", function () {
			if ($(this).val() == this.defaultValue || $(this).val() == "" || $(this).val() == this.defaultValue + " * Required" || $(this).val() == this.defaultValue + " * Invalid Email" || $(this).val() == this.defaultValue + " * Invalid Phone Number"){
				$(this).css({borderColor:"#999", color: "#666666"}).val(this.defaultValue);
			}
			if (this.value == this.defaultValue) {this.value = ''; $(this).css({ backgroundColor: "#FFFFFF" }); return false; }
			else if (this.value != this.defaultValue) {$(this).select(); }
			else if (this.value == "") { this.value = this.defaultValue; }
		});	
		/****************************************************************
		Form Submission Logic
		****************************************************************/
		$("#submit, #submit0, #submit1, #submit2").click(function(){
			var errors = false;
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			var phoneRegExp = /^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$/;
			$(this).parent("form").children(".required").each(function (i) {
				if ($(this).val() == this.defaultValue || $(this).val() == "" || $(this).val() == this.defaultValue + " * Required" || $(this).val().length < 2) {
					$(this).css({borderColor:"red", color:"red"}).val(this.defaultValue + " * Required");
					errors = true;
				}
				else if (!emailReg.test($(this).val()) && $(this).hasClass("email")) {
					$(this).css({borderColor:"red", color:"red"}).val(this.defaultValue + " * Invalid Email");
					errors = true;
				}
				else if($(this).hasClass("phone")){
					var numbers = $(this).val().split("").length;
					console.log(numbers);
					if (numbers < 10 || numbers > 20 || !phoneRegExp.test($(this).val())) {
						$(this).css({borderColor:"red", color:"red"}).val(this.defaultValue + " * Invalid Phone Number");
						errors = true;
					}
					
				}
			});
			if(!errors){
				var id = $(this).parent("form").attr("id");
				var popup;
				$("#submit").prop('disabled', true);
				if(id == "freeAccount0" || id == "freeAccount1" || id == "freeAccount2"){ 
					if(id == "freeAccount0"){popup="#formMessage0";}
					else if(id == "freeAccount1"){popup="#formMessage1";}
					else{popup="#formMessage2";}
					var name = $(this).parent("form").children('#Name').val();
					$.ajax({
						type: 'POST',
						url: 'account.php',
						dataType: 'json',
						data: {
							Name : $(this).parent("form").children('#Name').val(),
							Email : $(this).parent("form").children('#Email').val(),
							Phone : $(this).parent("form").children('#Phone').val()
						},
						success : function (data){
							$(popup +" p").append(data.msg);
							$(popup).fadeIn(500).delay(5000).fadeOut(500);
							$("#submit").prop('disabled', false);
						},
						error : function (){ 
							$(popup +" p").append("We appoligize "+name+" the form submission failed. We are working hard to fix the issue, please call us for immediate assistance.");
							$(popup).fadeIn(500).delay(5000).fadeOut(500);
						}
					});
				}
				else{
					var name =  $('#FirstName').val() +" "+ $('#LastName').val();
					$.ajax({
						type: 'POST',
						url: 'contactForm.php',
						dataType: 'json',
						data: {
							FirstName : $('#FirstName').val(),
							LastName : $('#LastName').val(),
							Company : $('#Company').val(),				
							Email : $('#Email').val(),
							Phone : $('#Phone').val(),
							Comments : $('#Comments').val()
						},
						success : function (data){
							$("#formContact p").append(data.msg);
							$("#formContact").fadeIn(500).delay(5000).fadeOut(500);
							$("#submit").prop('disabled', false);
						},
						error : function (){
							$("#formContact p").append("We appoligize "+$('#FirstName').val()+ " "+ $('#LastName').val() +" the form submission failed. We are working hard to fix the issue, please call us for immediate assistance.");
							$("#formContact").fadeIn(500).delay(5000).fadeOut(500);
						}
					});	
				}
				$("#" +id+ " input[type='text']").each(function(){
					$(this).val	("");
				});
				$("form textarea").each(function(){
					$(this).val	("");
				});
			}
		});	
		/****************************************************************
		Home page Video Logic
		****************************************************************/
		$(".prizes, .gifts, .places").click(function(e){
			e.preventDefault();
			if($(this).hasClass("current")){return false;}
			//var param;
			var val = $(this).attr("class");
			$("#prizes, #gifts, #places").hide();
			//var myPlayer = $("#"+val);
			//document.getElementById('#prizes');

			//myPlayer.stopVideo();

			//$("#prizes").attr("src", $("#prizes").attr("src"));
//			$("#gifts").attr("src", $("#gifts").attr("src"));
//			$("#places").attr("src", $("#places").attr("src"));
			//$('#prizes').get(0).stopVideo();
//			$('#gifts').get(0).stopVideo();
//			$('#places').get(0).stopVideo();

			//$(".video").empty();
			//if($(this).hasClass("prizes")){param = "http://www.youtube.com/embed/5l-fbaljPJ8?rel=0&wmode=Opaque";}
//			if($(this).hasClass("gifts")){param = "http://www.youtube.com/embed/yTE4qjHr1z8?rel=0&wmode=Opaque";}
//			if($(this).hasClass("places")){param = "http://www.youtube.com/embed/BafM67_cBoY?rel=0&wmode=Opaque";}
			if($(this).hasClass("prizes")){$("#prizes").show();}
			if($(this).hasClass("gifts")){$("#gifts").show();}
			if($(this).hasClass("places")){$("#places").show();}
			$(".videoText div").each(function(){$(this).hide();});
			$(".video-links a").each(function(){$(this).removeClass("current");});
			$("."+val+"Text").show();
			$(".video-links a."+val).addClass("current");
			//$("#youtube").attr("src", param);
			//$(".video").append('<iframe width="435" height="251" src="http://www.youtube.com/embed/'+param+'?rel=0&wmode=Opaque" frameborder="0" allowfullscreen></iframe>');		
		});	
		/****************************************************************
		Business page Video Logic
		****************************************************************/
		$(".understand, .engage, .grow").click(function(e){
			e.preventDefault();
			if($(this).hasClass("current")){return false;}
			var param;
			var val = $(this).attr("class");
			$(".video").empty();
			if($(this).hasClass("understand")){param = "Understand";}
			if($(this).hasClass("engage")){param = "Engage";	}
			if($(this).hasClass("grow")){param = "Grow";}
			$(".videoText div").each(function(){$(this).hide();});
			$(".video-links a").each(function(){$(this).removeClass("current");});
			$("."+val+"Text").show();
			$(".video-links a."+val).addClass("current");
			$(".video").append('<img src="images/'+param+'.png">');
		});
	});
	function clearAttention(){
		$("#attention").hide();
		clearInterval(attentionInterval);	
	}
function randomFromTo(from, to){
		return Math.floor(Math.random() * (to - from + 1) + from);
	}
	function randomImage(){
		return 	"images/scratch-post-"+randomFromTo(1,2)+".jpg";
	}
	function getInternetExplorerVersion()
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
	{
		var rv = -1; // Return value assumes failure.
		if (navigator.appName == 'Microsoft Internet Explorer')
		{
			var ua = navigator.userAgent;
			var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				rv = parseFloat( RegExp.$1 );
		}
		return rv;
	}

	function Attention(){	
		$('#attention').css({height: 135, left:0, top:0, width:140, opacity:1, display: 'block'}).animate({height: 107, left:13, top:14, width:114, opacity:0},1500);
	}
	function AttentionInterval(){
		if(getInternetExplorerVersion() == -1 || getInternetExplorerVersion() == 9){
			attentionInterval=setInterval("Attention();",1750);					
		}
		else{ $("#attention").hide(); return false;}
	}
	function ReScan(){
		$('#attention').css({height: 50, left:101, top:405, width:76, opacity:1, 'z-index':7, display:'block'}).animate({height: 32, left:110, top:413, width:58, opacity:0},1500);
	}
	$(".scan-btn").live("click",function(){
			if(getInternetExplorerVersion() == -1 || getInternetExplorerVersion() == 9){
				$("#attention").hide();
				clearInterval(attentionInterval);
				$("#iphone").removeClass('iphoneInit').addClass('iphoneScan');
				$("#scratcher, .scan-btn").hide();
				$("#scratcher").attr("src", $("iframe").attr("src"));
				$("#dots").show();
				for(i=0; i<3; i++){$("#dots hr").show().animate({opacity:0},400).delay(300).animate({opacity:1},400);}
				$("#iphone").animate({'margin-left': 624}, 2000, function(){
					$("#dots span").show().each(function(i){				
						var dots = new Array(Array(20,50,20,50),Array(110,140,20,50),Array(20,50,110,140),Array(110,140,110,140),Array(20,80,55,105),Array(85,140,55,105))
						$(this).hide().css({left:randomFromTo(dots[i][0],dots[i][1]), top:randomFromTo(dots[i][2],dots[i][3]) }).fadeIn(125).delay(randomFromTo(100,600)).fadeOut(125);
					});								
					setTimeout("$('#dots hr').hide();$('#iphone').removeClass('iphoneScan').addClass('iphoneInit'); $('.scan-btn').show(); ",850);						
				}).delay(1150).animate({'margin-left': 319}, 2000, function(){$('#scratcher').show();attentionInterval=setInterval("Attention();",1750);});
			}
		});
	var isMobile = false;
	var isAndroid = navigator.userAgent.toLowerCase().indexOf("android") > -1;
	var iPhone = navigator.userAgent.toLowerCase().match(/iphone/i);
	var iPad = navigator.userAgent.toLowerCase().match(/ipad/i);
	var iPod = navigator.userAgent.toLowerCase().match(/ipod/i);
	if(isAndroid || iPad || iPod || iPhone) {isMobile=true}
	var topImage = new Image();
	var bottomImage = new Image();
	var scratchImage = new Image();
	var overlayctx, mainctx, context, context2;


	function randomFromTo(from, to){return Math.floor(Math.random() * (to - from + 1) + from);}	
	bottomImage.src = "images/scratch-post-"+randomFromTo(1,2)+".jpg";
	scratchImage.src = "images/circle.png";
	topImage.src = "images/scratch-pre.jpg";	
	function CreateScratcher(){				
		overlayctx = new Kinetic("overlay");
		mainctx = new Kinetic("canvas");
		var radius = 15;	
		overlayctx.setStage(function(){
			overlayctx.clear();
			context = overlayctx.getContext();
			overlayctx.beginRegion();
			context.drawImage(scratchImage, 0, 0, 117, 109);
			context.beginPath();
			context.rect(0, 0, 117, 109);
			context.closePath();
			if(isMobile){
				overlayctx.addRegionEventListener("touchmove", function(){	
					var touchPos = overlayctx.getTouchPos();
					if (touchPos !== null) {
						$("#attention").hide();
						clearInterval(attentionInterval);
						context.clearRect(0, 0, 117, 109);
						context.drawImage(scratchImage, touchPos.x-radius, touchPos.y-radius);		
						context2.save();
						context2.beginPath();
						context2.arc(touchPos.x,touchPos.y,radius,0,Math.PI*2,false); // we don't fill or stroke the arc intentionally
						context2.clip();
						context2.drawImage(bottomImage, 0, 0);
						context2.restore();			
					}
				});	
			}
			else{
				overlayctx.addRegionEventListener("mousemove", function(){		
					var mousePos = overlayctx.getMousePos();
					if (mousePos !== null) {
						clearInterval(attentionInterval);
						context.clearRect(0, 0, 117, 109);
						context2.drawImage(scratchImage, mousePos.x-radius, mousePos.y-radius);		
						context2.save();
						context2.beginPath();
						context2.arc(mousePos.x,mousePos.y,radius,0,Math.PI*2,false); // we don't fill or stroke the arc intentionally
						context2.clip();
						context2.drawImage(bottomImage, 0, 0);
						context2.restore();		
					}
				});
			}
		overlayctx.closeRegion();
		});
		mainctx.setStage(function(){
			context2 = mainctx.getContext();
			mainctx.beginRegion();
			context2.drawImage(topImage, 0, 0, 117, 109);
			context2.rect(0, 0, 117, 109);
			context2.closePath();
			overlayctx.closeRegion();
		});
	}

