
$(document).ready(function(){
	// Bind escape key to hide popups
	$(document).bind("keyup.popup", function(event){
			if (event.keyCode == 27) { closePopup(); }
	});	

	
	// Setup labels
	$("label").inFieldLabels();

/****** ZOOMING *******/

//	$(".zoom").hover(
//		function(){
//			$(this).find(".mag").fadeIn(200);
//		},
//		function(){
//			//$(this).find(".mag").clearQueue();
//			$(this).find(".mag").fadeOut(200);
//		}
//	).bind("click",function(){
//	
//	});

	$('.gallery a, .zoom').fancyZoom({scaleImg: true, closeOnClick: true,directory:"/javascript/libraries/fancyzoom/images"});
	
	//$(".zoom").click(function(){
	
	//});

	
	/* $('#screenshoots a').fancyZoom({scaleImg: true, closeOnClick: true,directory:"/javascript/libraries/fancyzoom/images"}); */
	
	//youtube video
	$('.zoom-video').fancyZoom({closeOnClick: false,directory:"/javascript/libraries/fancyzoom/images"});
	
	$('.dropdown').find('.value').hide().end().find('.title').click(function(){
		var height = $(this).parent().find('.value').outerHeight(true);
		//alert(height)
		var visible = $(this).parent().find('.value').is(':visible');
		
		//$(this).parent().find('.value').slideToggle();
		$(this).parent().find('.value').toggle();
		
		var html = '&#9660;';
		if (visible) { html = '&#9654;' ; }
		$(this).parent().find('.arrow').html(html);
		$('#tabSlider').css('height','auto');
	});
/*
	$('#tabSlider').codaSlider({
	});
*/

/****** MENU *******/

	$("#menu-items li").hover(
		//fade in upon entering
		function(){
			$(this).find(".dropdown").fadeIn(0);
		}, 
		//fade out upon exiting
		function(){
			$(this).find(".dropdown").fadeOut(0);
		}
	);
	//if you hover over the first menu item, have the arrow change its bg color
	$("#menu-items .first").hover(
		function(){ 
			var id = $(this).parent().attr("id").split("-")[0];
			$("#"+id+"-arrow").addClass("hover"); 
		},
		function(){
			var id = $(this).parent().attr("id").split("-")[0];
			$("#"+id+"-arrow").removeClass("hover"); 
		}
		
	)
/****** END *******/
	
	$('.options .option').click(function(){
		$(this).parent().find(".clicked").removeClass("clicked");
		$(this).addClass("clicked");
		$(this).find("input").attr("checked",true);
		
	});
	
	$("form .field").click(function(){
		$(this).find(":input")[0].focus();
	});



});



function getJsonValues(id){
	var json ='{';
	 id.find(":input").each(function(i){
		var val = '';

		switch(this.type){
			case 'radio':
				val = $(this).is(':checked') ? 1 :0;
				
			break;
			default:
				val = $(this).val();
			break;
		}
		json += (i!=0 ? ',':'')+'"'+$(this).attr("id")+'":"'+val+'"';
	});
	json += '}';
	return json;

}

function showPopup(html,style){
	$("#popup-container").html(html);
	if(style){
		$("#popup-container").addClass(style);
	}
	
	// ie6 and 7 (black overlay only covering top of page, and not bottom) bugfix
//	$("#popup-window").show(1, function() {
//		$("#popup-overlay").css('height','50%').css('height','100%').show();
//	});
	
	$("#popup-overlay").css("height", $(document).height() + "px").show();
	//$("#popup-overlay").css("height", document.getElementsByTagName('body')[0].scrollHeight + "px").show();
	$("#popup-window").fadeIn();
	
	/*
	for testing:
		javascript:void(alert(document.getElementsByTagName('body')[0].scrollHeight + "px"))
		javascript:void($("#popup-overlay").css("height", document.getElementsByTagName('body')[0].scrollHeight + "px").show())
	*/	
	
	// if ie6, scroll to the top of the page since fixed doesn't work
	if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
		window.scroll(0,0);
	}
}

function closePopup(){
	$("#popup-overlay").hide();
	$("#popup-window").hide();
	$("#popup-container").html("").removeClass();
}


//newsletter form
function signUpNewsletter(){
	
	var email = $("#newsletter").val();
	$.ajax({ 
		dataType:"json",
		type:"POST",
		url:"/ajax.php",
		data:"p=newsletter&action=add&email="+email,
		success:function(j){
			if(j.success==1){
				$("#newsletter").val("");
				showPopup(j.html);
			}
			if(j.success==0){
				showPopup(j.html);
			}
		}
	});
}

//trial form
function signUpTrial(){
	form.submit("trial");

//	$.ajax({ 
//		dataType:		"json",
//		type:			"POST",
//		url:			"/ajax.php",
//		data:			"p=trial&action=submit&"+$('#trial-form').serialize(),
//		success:function(j){
//			if(j.success==1){
//				$("#trialform").clearForm();
//				showPopup(j.html);
//			}
//			
//		}
//	});
}

var form = {
	id:		false,
	form:	false,
	submit: function(id){
		form.id			= id;
		form.form		= $("#form-"+form.id);
		$("#"+form.id+"-submit").addClass("ting");
		
		var json		= form.form.serialize();
		
		$.ajax({
			dataType:		"json",
			type:			"POST",
			url:			"/ajax.php",
			data:			"p="+form.id+"&action=submit&"+json,
			success:function(j){
				$("#"+form.id+"-submit").removeClass("ting");
/*
				$('html, body').animate({scrollTop:0}, 'slow',function(){
					if(j.success==1){
						showPopup(j.html);
						$('#'+form.id+"-error").hide();
						
						if(j.formhtml){
							$("#form-trial").html(j.formhtml);
						}
						
					} else if(j.success==0){
						form.errors(j.errors);
					}
				
				});
*/
				
				var offsetTop = 0;
				
				if(j.success==1) {
					
					form.errors([]);
					
					$('#'+form.id+"-error").hide();
					if(j.formhtml) { $("#form-"+form.id).html(j.formhtml); }
					
				} else if (j.success==0) {
					form.errors(j.errors);
					
					offsetTop = parseInt($('.error:eq(0)').offset().top,10) - 50;
				}
				
				
				

				
				$('html, body').animate({scrollTop:offsetTop+"px"}, 'slow',function(){
					if(j.success==1){
						showPopup(j.html);
					}
				
				});

				
			}
		});
		
		
	},
	errors: function(errors){
		$("."+form.id+"-error").animate({
   			opacity: 0.25
  		}, 5000);
  		$("#"+form.id+"-error").show();
  		//$("#content").effect('shake', { times: 2,distance:5 }, 200);
  	//	form.form.find(".form").

  		form.form.find("li").removeClass("error");
		//form.form.find(".form").html("fd");
		//j				= [{"id":"name","text":"Field is required"}]
		$(errors).each(function(i){
			var html	= '<div class="picker">'+
							'<div class="arrow png"></div><div class="right-end png"></div><div class="bg png"></div>'+
						  	'<div class="text">'+this.text+'</div>'+
						  '</div>';

			$("#"+form.id+"-"+this.id).addClass("error");
			$("#"+form.id+"-error-"+this.id).html(html);//('<div class="error-field">'+html+'</div>');
		});
		
	},
	focusField: function(){
		//$(".form input").bind("focus",function(){
			$("#form-"+form.id).find("li").removeClass("focus");
			$(this).closest("li").addClass("focus");
			
		//});
	}
}



//feedback form
function feedbackForm(){
	form.submit("reach");
	
	//var json = getJsonValues($('#feedback'));
//	var json = $("#feedbackform").serialize();
//	
//	$.ajax({
//		dataType:"json",
//		type:"POST",
//		url:"/ajax.php",
//		data:"p=feedback&action=submit&"+json,
//		success:function(j){
//			if(j.success==1){
//				$("#feedbackform").clearForm();
//				showPopup(j.html);
//				
//			}
//
//		}
//	});

}

function schoolLogin(){
	var html 	= ''+
				  '<p>'+
				  	'When you first register for eLearn, we\'ll send you a welcome email with your <b>elearn</b> URL. '+
				  	'It will look something like this, always ending in <b>elearnapp.com</b>:'+
				  	'</p>'+
				  	'<div class="href">http://<span>schoolname</span>.elearnapp.com</div>'+
				  	'<p>Head over to that web address to login. If you are not sure what your URL is, '+
				  	'<a href="http://help.elearnapp.com/#/basics/sign-in-or-out" class="link" style="text-decoration:underline;">click here</a> to visit the Support Center help topic. '+
				  	'If you still need assistance, please <a href="/contact" class="link" style="text-decoration:underline;">contact us directly.</a></p>'+
				  	'<a id="popup-close" class="button2 button2green png" href="#" onClick="closePopup(); return false;">Close</a>';
	showPopup(html,"text");

}

function gallery(){


}







$.fn.clearForm = function(){
	return this.each(function() {
		$(':input', this).each(function() {
	        switch(this.type) {
	            case 'password':
	            case 'text':
	            case 'textarea':
	                $(this).val('');
	                break;
	            case 'checkbox':
	            case 'radio':
	                this.checked = false;
	                break;
	             case 'select-multiple':
	            case 'select-one':
	   				this.selectedIndex = 0;
	            break;
	
	        }
		
		});
	});
}


