$(document).ready(function() {

	//highlight current Tab

	$("a.tab").filter(function(){
		var link = $(this).attr("href");
		link = link.replace("..","");
		return (location.href.indexOf(link)>0);
	}).addClass("selected");

	//Show correct search form on results pages

	var type = $("#SearchType").html();
	if(type)
		$("#"+type).show();

	//prefs

	$('.prefs-btn').colorbox({
		iframe:true,
		innerHeight:"650",
		innerWidth:"534",	
		overlayClose:false,
		opacity:0.75,
		escKey:false,
		onLoad: function() {		
		    $('#cboxClose').remove();
		}
	});

 });

function showSearchTile(name){
	$(".SearchTile").hide();
	$("#"+name).show();
}

$(function(){
	$(".drawer-header").click(function(e) {	
	        //$(e.target).closest(".drawer").find(".prefs-location").stop(true,true).slideToggle('slow');        
	        //$(e.target).toggleClass("highlight");
	        return false;
	});
});



function TabMenu(tab,tabmenu){
	var that = this;
	
	this.selector = tab;
	
	this.shouldHide=true;
	this.hideEvent = false;
	
	this.tab = $('#'+this.selector);
	this.tab.mouseout(function(){
			that.hide(100);
	});
	this.tab.mouseover(function(){
			that.startShow();
	});
	
	this.tabOptions = $('#'+tabmenu);
	this.tabOptions.css({position:'absolute',display:'none'});
	this.tabOptions.mouseout(function(){
			that.hide(500);
	});
	this.tabOptions.mouseover(function(){
			that.show();
	});
	
	this.startShow = function(){
		var x = that.tab.offset().left;
		var y = that.tab.offset().top+that.tab.outerHeight();
		that.tabOptions.css({top:y,left:x});
		that.tabOptions.slideDown(150);
		that.show();
	}
	
	this.show = function(){
		that.shouldHide=false;
	}
	
	this.hide = function(count){
		that.shouldHide=true;
		if(that.hideEvent)
			clearTimeout(that.hideEvent);
		that.hideEvent = setTimeout(that.selector+'.doHide()',count);
	}
	
	this.doHide = function(){
		if(that.shouldHide){
			that.tabOptions.slideUp(150);
		}
	}

	this.updateHeader = function(){
		that.tabOptions.children("a").filter(function(){
			var link = $(this).attr("href");
			link = link.replace("..","");
			return (location.href.indexOf(link)>0);
		}).each(function(){
			var link = $(this).attr("href");
			var text = $(this).html();
			that.tab.attr({href:link});
			that.tab.html(text);
		});
	}
}
