
$(function() {		

	$("#logout").click(function() {
		return account.logout();	 
	}); 	
	
//{{{ messy highlighting stuff
  
	// highlight global nav
	var els = location.href.split("/");	
	var f = "/" + els[3];	
	if (f.indexOf("#") != -1) f = f.substring(0, f.indexOf("#"));
	
	if (f == '/tools') f = '/plugins';
	else if (f == '/demos' || f == '/tutorials') f = '/documentation';
	else if (f == '/index.html' || f == '/') f = "";		
	$("#globalnav a").filter("[@href=" + f + "/index.html]").addClass("active");	


	// hightlight subnav
	var loc = location.href;
	var page = loc.substring(loc.indexOf("/", 10), loc.indexOf("?") > 0 ? loc.indexOf("?") : loc.length);
	if (page.indexOf("#") != -1) {
		page = page.substring(0, page.indexOf("#"));	
	}
	var el = $("#right ul a[href=" +page+ "]");
	if (!el.length && (f == 'documentation' || f == 'account')) el = $("#right ul a:first");
	
	el.addClass("selected").click(function(e) {
		e.preventDefault();		
	});
	
	// remove redundant borders from subnav
	$("#right ul").each(function() {
		$(this).find("a:last").css("borderBottom", 0);		
	});  
	
	
	// setup main title background image
	f = f.substring(1);
	
	if (f == 'plugins' && els[4] && els[4] != 'index.html') {
		f = els[4]; 
		if (els[3] == 'tools') f = "tools";		
	}
	
	if (els[3] == 'demos') {
		f = "flowplayer";	
	}
	
	var title = $("#content h1:first");
	if (f && title.length && title.css("backgroundImage") == 'none') {
		if (f == 'admin') f = 'flowplayer';
		title.css("backgroundImage", "url(http://static.flowplayer.org/img/title/" + f + ".png)");		
	}	
	//}}}

	
	// lazy download of jquery.chili.js
	if ($("code[@class]").length) {
		$.getScript("/js/jquery.chili.js");
	} 
	
	// set initial focus on first input field
	var input = $("#right :input[type=text]:visible");
	if (input.length) input.get(0).focus();
	
	// drawer	
	$("#right div.box a.header").click(function(e) {
		var el = $(this);
		var cl = "active";
		
		if (!el.hasClass(cl)) {
			el.parent().find(".active").removeClass(cl).eq(1).slideUp();
			el.addClass(cl).next().addClass(cl).slideDown(function()  {
				$(this).find(":input").get(0).focus();		
			});
			e.preventDefault();
		}
	});

	// mouse tip
	$("div.tip").prev().hover(function(evt) {
		var el = $(this);
		el.next().css({top:evt.pageY - 40, left:evt.pageX + 40}).show();
		
	}, function()  {
		$(this).next().hide();	
	});  
	
	
	// button.custom, span.play hover and mousedown
	$("button.custom").each(function() {
		var el = $(this);
		if (!el.find("span").length) el.html("<span>" + el.html() + "</span>");		
	});	
	
	$("button.custom, span.play").each(function()  {

		var el = $(this);
		var xPos = el.attr("id") == 'searchButton' ? '-100px' : '0';
		
		el.hover(function() {
			var el =	$(this);
			el.css("backgroundPosition", xPos + " -" + el.css("height"));			
		}, function() {
			$(this).css("backgroundPosition", xPos + " 0");		
		}).mousedown(function()  {
			var el =	$(this);
			el.css("backgroundPosition", xPos + " -" + (parseInt(el.css("height")) * 2) + "px");		
		});
		
	});
	
			
	
	
	$("a.player").hover(function() {
		$("img", this).fadeTo(400, 1); 			
	}, function() {
		$("img", this).fadeTo(400, 0.7);
		
	}).find("img").css({opacity:0.7});
	
	
	// latest posts
	$("#latestPosts h2").click(function() {
		var posts = $(this).next("ul");		 
		posts.slideToggle(function()  {			
			document.cookie = 'postsDisplay=' + posts.css("display");
			$("#toggle").css("backgroundPosition", "0 " + (posts.is(":visible") ? "-13" : "0") + "px"); 
		});			 
	});
	
	
	$("#latestPosts a").click(function() {
		$("#latestPosts a").removeClass("selected");
		$(this).addClass("selected");
	});	
	
	

	// make right side picks clicable
	$("#right div.pick").click(function() {
	  location.href = $(this).find("a").attr("href");
	});
	
});


// global functions
var account = {
	
	admin: false,
	
	login: function(form) {
		form = $(form);
		$.post("/account/login?" + form.serialize(), function(res) {
			res = eval("(" + res + ")");
			if (res.message) {
				form.find("div.error").html(res.message).show();	
			} else {
				
				if (location.href.indexOf("download") != -1) {
					location.href = "/account/products.html";	
				} else {
					location.reload();		
				}  
			}  
		});
		
		return false;
	},
	
	
	create: function(form) {
		form = $(form);
		$.post("/account/create?" + form.serialize(), function(res) {
			res = eval("(" + res + ")");
			if (res.message) {
				form.find("div.error").html(res.message).show();	
			} else {
				form.html(
					"Your account was successfully created. " + 
					"Your new password was sent to you by email"
				); 
			} 
		});
		
		return false;			
	},
	
	
	requestPassword: function(form) {
		form = $(form);
		$.getJSON("/account/requestPassword?" + form.serialize(), function(json) {
			if (json.message) {
				form.find("div.error").html(json.message).show();	
			} else {
				form.html("Your new password was sent to you by email"); 		
			}  
		});
		
		return false;			
	},
	
	
	logout: function()  {
		$.get("/account/logout", function() {
			location.reload();		
		}); 
		return false;
	}
	
};

var site = {
	
	uploadDone: function(fileName, hostId) {
		var img = hostId ? $("div.hostPicture[hostId=" + hostId + "] img") : $("#picture");
		img.attr("src", "/img/users/" + fileName + "?_=" + Math.random());
	},
	
	submitForm: function(form) {
		form = $(form);
		form.fadeTo(400, 0.3);
		
		$.getJSON(form.attr("action") + "?" + form.serialize(), function(json) {
			form.fadeTo(400, 1);			
			
			if (json.message) {
				form.find("div.error").html(json.message).show();
				
			} else {
				location.reload(); 		
			}  
		});
		
		return false;			
	}
	
}
