// JavaScript Document

var status = 'on';
var event_id;

$(window).resize(function(){fixHeight();});
function fixHeight(){
	//Footer positioning fix
	$(".wrap").css("min-height",window.innerHeight);
}

function doFocus(obj){
	$(obj).val("");
}

function send(value)
{
	get('player').send(value);
};

function receive(value) {
	alert(value);
};

function get(name)
{
	return navigator.appName.indexOf("Microsoft") != -1 ? window[name] : document[name];
};

function popout(l,id,w,h) {
	var url = 'http://www.nowlive.com/popup/'+(l == "ev" ? "event" : "video")+'/'+id;
	var popout = window.open(url,'popout','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+w+',height='+h+',left='+(($(window).width()-w)/2)+',top='+(($(window).height()-h)/2));
}

function lights() {
	if (status == 'on') {
		$('#lights').fadeIn();
		status = 'off';
	} else {
		$('#lights').fadeOut();
		status = 'on';
	}
};

function init() {
	fixHeight();
	$("#browse-link").click(
		function(e){
		    $("#nav_wrap").toggleClass("nav_open");
			$("#browse-menu").fadeToggle(); 
			var h = $("#header").height + $("#browse-menu").height;
			$("#nav_wrap").height(h);
			return false;
		}
	);
	$("#nav_wrap").mouseleave(function(){
	    $("#browse-menu").fadeOut(); 
	    $("#nav_wrap").removeClass("nav_open");
	});
}

function trend_control(cat,metric,sub){
	var rand = Math.floor(Math.random()*1001);
	$(".trendctrl").html("<img src='http://trend.nowlive.com/event.gif?event_id="+event_id+"&cat="+cat+"&metric="+metric+"&sub="+sub+"&rand="+rand+"' border='0' />");
}

function do_login(id) {
	fb_event_id = id;
	FB.init({appId:"137113059677955",status:true,cookie:true,xfbml:true});	
	FB.getLoginStatus(
		function(response) {
			if (response.session) {
				do_rsvp();					
			} else if(!response || response.error) {
				alert("We're sorry, there was an error connecting with Facebook. Please try again.");
			} else {			
				FB.login(
				function(response) {
					if (response.session) {
						if (response.perms)	{
							do_rsvp();											
						} else {
							alert("We're sorry, you must allow NowLive to connect to your Facebook to use this application.");
						} 
					} else if (!response || response.error) {
						alert("We're sorry, there was an error connecting with Facebook. Please try again.");
					} else {
						alert("We're sorry, you must allow NowLive to connect to your Facebook to use this application.");
					}
				},
			{perms:"publish_stream,rsvp_event"});			
		}
	});
};
function do_rsvp() {
	FB.api("/"+fb_event_id+"/attending","post",
	function(response) {
		if (!response || response.error) {
			alert("We're sorry, there was an error connecting with Facebook. Please try again.");
		} else {
			alert("You've successfully RSVPd to the event!");
		}
	});
};

function do_reminder() {
	if($("#email_signup_box").length > 0){
		if ($("#email-agree-checkbox").is(":checked")) {
			$.ajax({url:"../../act/remind/save.php",type:"POST",dataType:"json",data:"email="+$("#email-input").val()+"&agree=1",success:reminded});
		} else {
			alert("You must agree to the Privacy Policy and Terms & Conditions.");
		}
	} else {
		if ($("#agree-checkbox").is(":checked")) {
			$.ajax({url:"../../act/remind/save.php",type:"POST",dataType:"json",data:"email="+$("#email-input").val()+"&agree=1",success:reminded});
		} else {
			alert("You must agree to the Privacy Policy and Terms & Conditions.");
		}
	}
};

function reminded(json) {
	if($("#email_signup_box").length > 0){
		$("#email_signup_box").html("<div id=\"widget_email\"><p style=\"margin-top: 20px\">Thank you, you will begin receiving e-mail reminders for our upcoming events.</p></div>").delay(3000).fadeToggle(function(){$("#email_signup_box").remove()});
	} else {
		$("#widget_email_static").html("<p style=\"margin-top: 20px\">Thank you, you will begin receiving e-mail reminders for our upcoming events.</p><p class=\"widget_close_link\"><a href=\"#\" onclick=\"toggle_remind(); return false;\">Close window.</a></p>");
	}
};

function toggle_remind(id) {
	event_id = id;
	if ($("#remind-me").length > 0) {
		$("#remind-me").fadeToggle(function(){$(this).remove()});
		
	} else {
		$.ajax({url:"inc/widgets/remind_me.php?event_id="+event_id,type:"GET",dataType:"html",success:renderRemind});
	} 
};

function renderRemind(html){
	$("body").append(html);
	$("#remind-me").css("position","absolute").css("left",($("#remind_me_"+event_id).offset().left+70)+"px").css("top",($("#remind_me_"+event_id).offset().top-19)+"px").fadeToggle();
}

function toggle_email_signup() {
	if ($("#email_signup_box").length > 0) {
		$("#email_signup_box").fadeToggle(function(){$("#email_signup_box").remove()});
	} else {
		$.ajax({url:"inc/widgets/email_signup.php",type:"GET",dataType:"html",success:renderEmail_signup});
	} 
};

function renderEmail_signup(html){
	$("body").append(html);
	$("#email_signup_box").css("position","absolute").css("left",($(".email_signup").offset().left+130)+"px").css("top",($(".email_signup").offset().top-200)+"px").fadeToggle();
}

$(document).ready(function(e){init();});
