var req;

jQuery.extend({
	
	//Global variables
	loginShowing: false,
	aboutShowing: false,
	directorsShowing: false,
	directorHeight: -1,
	/*****************************************************************
	* Global Search Methods
	*/
	initGlobalSearch: function() {

		// Fade out the suggestions box when not active
		$("#searchInput").focus(function(){
			if (this.value == "Search") {
				this.value = "";
				$(this).css("color", "#000")
			}
		});
		
		$("#searchInput").blur(function(){
		 	$('#suggestions').animate({height:'0px',marginBottom:'0px'}, 250, function() {
				$('#suggestions').hide();
			});
			if (this.value == "") {
				this.value = "Search";
				$(this).css("color", "#AAA")
			}
		 });
		
		var d = $('<div id="suggestions"></div>');
		var header = $('<h3 class="header">Search Results</h3>');
		var results = $('<div id="searchresults"></div>');
		d.append(header,results);
		$(".content").prepend(d);
	},

	globalSearch: function(input) {
		if (req != null) req.abort();
		if(input.length == 0) {
			$('#suggestions').animate({height:'0px',marginBottom:'0px'}, 250, function() {
				$('#suggestions').hide();
			});
			$("#globalSearchLoader").animate({opacity:0},500);
		} else if (input.length < 2) {
			return;
		} else {
			var resultLimit = 12;
			$('#suggestions').fadeIn();
			$('#searchresults').html('');
			$("#globalSearchLoader").animate({opacity:1},500);
			req = $.ajax({
				type: "POST",
				url: "/search/all/",
				data: "terms="+input,
				success: function(data) {
					if (data == '') return;
					var results = eval(data);
					if (results.length ==0 ) {
						$('#searchresults').html("<h5>No Results Found</h5>");
						$("#suggestions").fadeIn();
						$('#suggestions').animate({height:'100px'}, 250);
						$("#globalSearchLoader").animate({opacity:0},500);
						return;
					}
					$("#globalSearchLoader").animate({opacity:0},500);
					if (results.length > 0) {
						resultStr = "";
						var colNum = 0;
						for (var i=0;i<Math.min(resultLimit,results.length);i++) {
							resultStr += "<div class='search_item";
							if (colNum ==2) {
								resultStr +=" last";
								colNum =0;
							} else {
								colNum++;
							}
							var pressStr = "";
							if (results[i].type == "press") pressStr = "/press/#";
							resultStr += "'><a href='"+pressStr+results[i].url+"'><img src='"+results[i].splash+"' width='196' height='110' alt='' class='searchImg' />";
							resultStr += "<h2>"+results[i].secondaryText+"</h2></a><h3 h3 class='feature'>"+results[i].mainText+"</h3></div></a>";
							
						}
						$('#searchresults').html(resultStr);
						
						var num_items = Math.min(results.length, resultLimit);
						var rows = Math.ceil(num_items/3);
						$('#suggestions').show().animate({height:(rows*190)+80+'px',marginBottom:'20px'}, 250);
						$.scrollTo(0, 250);
					}
				}
			});
				
		}
	},
	
	/*****************************************************************
	* Login methods
	*/
	
	initLoginForm : function() {
		
		var d 			= $('<div id="login_form" class="hidden"></div>');
		var header 		= $('<h3 class="header">Login</h3>');
		var f 			= $('<form action="http://clients.motiontheory.com/review.php" method="POST"></form>')
		var user		= $('<input type="text" name="username" class="login_input" placeholder="User Name" />');
		var pass		= $('<input type="password" name="password" class="login_input" placeholder="Password" /><br/>');
		var type		= $('<input type="hidden" name="user_login" value="Login" class="login_input" />')
		var submit		= $('<input type="submit" value="Submit" />')
		var cancel		= $('<input type="button" id="login_cancel" class="cancel" value="Cancel"/> ')
		f.append(user,pass,type,submit,cancel);
		d.append(header, f);
		$(".content").prepend(d);
		
		$("#login").click(function(e) {
			$.hideOpenSections('login');
			(!$.loginShowing) ? $.showLogin() : $.hideLogin();
			e.preventDefault();
		});
		
		$("#login_cancel").click(function(e) {
			$.hideLogin();
			e.preventDefault();
		})
	},
	
	showLogin : function() {
		if ($.loginShowing) return;
		$(".content").prepend($("#login_form"));
		$("#login_form").stop().height(0).show().animate({height:'120px', marginBottom:'20px'}, 250);
		$.loginShowing = true;
		$.scrollTo(0, 250);
	},
	
	hideLogin : function() {
		if (!$.loginShowing) return;
		$("#login_form").animate({height:'0px', marginBottom:'0px'}, 250, function() {
			$("#login_form").hide();
		});
		$.loginShowing = false;
	},
	
	/*****************************************************************
	* About info methods
	*/
	initAbout : function() {
		$("#about_link").click(function(e) {
			$.hideOpenSections('about');
			(!$.aboutShowing) ? $.showAbout() : $.hideAbout();
			e.preventDefault();
		});
	},
	
	showAbout : function() {
		if ($.aboutShowing) return false;
		$(".content").prepend($("#about_content"));
		$("#about_content").show().animate({height:'200px', marginBottom:'20px'}, 250);
		$.aboutShowing = true;
		$.scrollTo(0, 250);
	},
	
	hideAbout : function() {
		if (!$.aboutShowing) return false;
		$("#about_content").animate({height:'0px', marginBottom:'0px'}, 250, function() {
			$("#about_content").hide();
		})
		$.aboutShowing = false;
	},
	
	/*****************************************************************
	* Director links methods
	*/
	initDirectors: function() {
		$("#directors_link").click(function(e) {
			$.hideOpenSections("directors");
			(!$.directorsShowing) ? $.showDirectors() : $.hideDirectors();
			e.preventDefault();
		});
	},
	
	showDirectors: function() {
		if ($.directorsShowing || $('.director_desc').length > 0) return false;
		if ($.directorHeight == -1) {
			$.directorHeight = $("#directors_content").height();
		}
		$("#directors_content").height(0);
		$(".content").prepend($("#directors_content"));
		$("#directors_content").show().animate({height:$.directorHeight+'px', marginBottom:'20px'}, 250);
		$.directorsShowing = true;
		$.scrollTo(0, 250);
	},
	
	hideDirectors: function() {
		if (!$.directorsShowing) return false;		
		$("#directors_content").animate({height:'0px', marginBottom:'0px'}, 250, function() {
			$("#directors_content").hide();
		})
		$.directorsShowing = false;
	}, 
	
	/*****************************************************************
	* 
	*/
	hideOpenSections: function(id) { 
		if ($.loginShowing && id != 'login') $.hideLogin();
		if ($.aboutShowing && id != 'about') $.hideAbout();
		if ($.directorsShowing && id != 'directors') $.hideDirectors();
	}
	
});
$(document).ready(function() {
	$.initGlobalSearch();
	$.initLoginForm();
	$.initAbout();
	$.initDirectors();
	
	$(window).resize(function() {
			var winH = $('body').height()
			$(".content").css('minHeight',winH-169+'px');
			
			var b = Math.max(460, winH - 147);
			$(".float_bottom").css("top",b+"px");
			//Left bar
			var w = Math.max(($(document).width()-1000)/2, 0);
			$("#left_dark").width(w);
	})

	var winH = $('body').height()
	$(".content").css('minHeight',winH-169+'px');

	var b = Math.max(460, winH - 147);
	$(".float_bottom").css("top",b+"px");

	var w = Math.max(($(document).width()-1000)/2, 0);
	$("#left_dark").width(w);
})


