//POP WINDOW
function popWindow(src, width, height) {
	var w = 800, h = 600;
	if (screen.width || screen.height) {
		w = screen.width;
		h = screen.height;
	}
	var leftPos = (w-width)/2;
	var topPos = (h-height)/2;
	window.open(''+src+'','','width='+width+',height='+height+',toolbar=no,status=no,menubar=no,scrollbars=no,resizeable=no,top=' + topPos + ',left=' + leftPos);
	void(0);
}


function submit_short_form() {
	$("#short_form button.get_started").text('sending...');
	$("#short_form input").each(function(){
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	$.ajax({
		type: "POST", 
		url: "/includes/helpers/jquery_short_form.php", 
		data: $("#short_form form").serialize(),
		dataType: "json", 
		success: function (data, textStatus) {
			if (data.errors) {
				$("#short_form button.get_started").text('get started!');
				$("input").removeClass('error');
				var errors = '<br />';
				for (i=0; i<data.errors.length; i++) {
					if (data.errors[i].display.length > 0) {
						$("#"+data.errors[i].input).addClass('error');
						errors = errors + '<p>'+data.errors[i].display+'</p>';
					}
				}
				$("#short_form_alert").html('Please correct errors in red:'+errors);
				if($("#short_form_alert").css('display') == 'none') {
					$("#short_form_alert").slideToggle();
				}
				$("#short_form input").each(function(){
					if ($(this).val() == '') {
						$(this).val($(this).attr('title'));
					}
				});
				return false;
			} else {
				window.location = "/get_started/thanks/";
				return true;
			}
		}
	});
}


function setCookie(c_name, value, expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


// Make Things Clickable
$(document).ready(function() {
	$(".clickable").click(function(){
		var linkURL = $(this).children("a").attr("href");
		if(!linkURL) linkURL = $(this).children("div").children("a").attr("href");
			window.location.href = linkURL;
			return false;
	});
	
	// Text Counter
	if($("textarea.limit").length>0){
		$("textarea.limit").textlimit('p.counter span',parseFloat($('textarea.limit').attr('rel')));
	};
	
	//Fancybox
	$("a.terms").fancybox({ 
		'hideOnContentClick': false,
		'padding': 20,
		'overlay': true,
		'centerOnScroll': true,
		'frameWidth': 600
	});
	
	// show / enable Short Form
	window.setTimeout(function() {
		$('#short_form_wrap').slideToggle(500, function(){ 
		
			// show graphic
			$('#short_form_graphic').fadeIn('slow');
			
			// Short form close button
			$('.short_form_close').click(function(){
				$('#short_form_wrap').fadeOut();
				// set cookie to hide short form for 7 days
				setCookie('sound_short_form', 'hide', 7);
				return false;
			});
			
			// Short form input value handler
			$("#short_form input").focus(function(){
				if ($(this).attr('title') == $(this).val()) {
					$(this).val('');
				}
			}).blur(function(){
				if ($(this).val() == '') {
					$(this).val($(this).attr('title'));
				}
			});
			
		});
	}, 6000);
});