$(document).ready(function(){
	
	setLinks();
	
	setSortable();
	
});

//Common Functions

function setLinks(){
	$('a[rel="external"], a.external, form[rel="external"], div#quantiaContent p.content a').each(function(e){
		$(this).attr('target', '_blank');
	});
}

//Sortable Functions

function setSortable(){
	$('.sortable').tablesorter({ 
	    widgets: ['zebra'] 
	});

	$('.sortable').bind("sortStart",function() { 
		$("#overlay").show(); 
	}).bind("sortEnd",function() { 
		$("#overlay").hide(); 
	});
}

//Additional Content Tabs

function setTabs(){
	$('a.more').show();
	$('.tabs .tab').each(function(e){
		if( $(this).hasClass('selected') ){
			var data = $(this).data('data');
			$(this).parent().css('height', data.closedHeight);
			$('#tabPlaceholder').css('height', data.closedHeight);
		}
	});
	bindTabExpand();
}

function bindTabExpand(){
	$('a.more').unbind('click').bind('click', function(){
		
		//Set Vars
		var data = $(this).parent().data('data');
		var tabs = $(this).parent().parent();
		var target = $(this).siblings('div.expandable');
		var placeholder = $('#tabPlaceholder');
		
		//Determine Action
		if( $(this).attr('rel') == 'open' ){
			target.hide();
			$(this).attr('rel', 'closed');
			$(this).html('more');
			tabs.css('height', data.closedHeight);
			placeholder.css('height', data.closedHeight);
		}else{
			target.show();
			$(this).attr('rel', 'open');
			$(this).html('less');
			tabs.css('height', data.openHeight);
			placeholder.css('height', data.openHeight);
		}
		return false;
	});
}


function openWindow(elem){
	window.open(elem.href, "_blank", "status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
	return false;
}


// ------------ COOKIES ------------ //

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	} else {
	    begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
	    end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + 
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

if (!getCookie("hasViewedJobs")) {
	$(window).bind("unload", function(){
		var date = new Date(new Date().getTime() + (15552000000));
		setCookie("hasViewedJobs", '1', date, "/");
		var theWindow = window.open("/providers/job_alert/?popup=true", "theWindow","location=0,status=0,scrollbars=1,width=670,height=450");
	});
	$(function() {
		var anchors = $("a").filter(function(indx) {
			if (this.href) {
				var isLocumRegex = new RegExp("^"+document.location.protocol+"//"+document.location.hostname+"/");
				var isMailtoRegex = new RegExp("^mailto:");
				var isProvidersSection = new RegExp("^"+document.location.protocol+"//"+document.location.hostname+"/providers/");
				var providersSectionRegex = new RegExp("^"+document.location.protocol+"//"+document.location.hostname+"/providers/(?!apply_online|job_alert|(?!.+))");
				if (isProvidersSection.test(document.location.href) && providersSectionRegex.test(this.href)) {
					return false;
				} else {
					return (isLocumRegex.test(this.href) || isMailtoRegex.test(this.href));
				}
			};
			return true;
		});
		anchors.bind('click',function(evt) {
			$(window).unbind("unload");
			return true;
		});
		$("form").bind("submit",function(evt) {
			$(window).unbind("unload");
		});
	});
}
