$(document).ready(function() {	
	
	if($("table#cv-overview td.last a").eq(0).attr("href")!=undefined)
		$("table#cv-overview td.last a").eq(0).attr("href",$("table#cv-overview td.last a").eq(0).attr("href").replace("/nl/","/"));
	
	positionSkyscraper();
	dropdown();
	
	$("#btnSearch").attr("value","");
	
	$("ul").find("li:first").addClass("first");
	$("ul").find("li:last").addClass("last");
	$("#advanced-search .grid_8").find("fieldset:last").addClass("last");
	$("#advanced-search").find("h6:first").addClass("first");
	$(".jobLinks:first").css("border-bottom","none");
	
	$(".teaserRow2").each(function(i, item){
	
		$(item).find(".col_320:first").addClass("alpha");
		$(item).find(".col_320:last").addClass("omega");
		$(item).find(".col_320.alpha .teaser:last").css("border-bottom","0");
		$(item).find(".col_320.omega .teaser:last").css("border-bottom","0");
		
		$(item).find(".alpha .teaser")
		.each(function(i, item){
	
				c1 = $(item);
				c2 = $(item).parent().parent().find(".omega").find(".teaser").eq(i);
				c1H = $(c1).height();	
				c2H = $(c2).height();
				
				if(c1H < c2H){
					c1.height(c2H);
				}
				else {
					c2.height(c1H);
				}				
	
	})});
	
	$("#sectionManual .promo").each(function(i,item){		
		if(i%2==0){
			$(item).addClass("alpha");
			c1 = $(item);
			c2 = $(item).next();
			if(c2!=undefined){
				c1H = $(c1).height()+20;	
				c2H = $(c2).height()+20;				
				if(c1H < c2H){
					c1.height(c2H);
					c2.height(c2H);
				}
				else {
					c1.height(c1H);
					c2.height(c1H);					
				}
			}
		}else{
			$(item).addClass("omega");
		}
	});
	
	$(".map").children('a.dot').each(function(){
  		var coords = $(this).attr('rel').split('-');
		var jobs = parseInt($(this).text());
  		
		if(jobs <= 300){
			$(this).addClass("dotSize1");
			$(this).css({left: coords[0]-5 + 'px', top: coords[1]-5 + 'px'});
		}
		else if(jobs > 300 && jobs <= 400){
			$(this).addClass("dotSize2");
			$(this).css({left: coords[0] + 'px', top: coords[1]-15 + 'px'});
		}
		else if(jobs > 400 && jobs <= 500){
			$(this).addClass("dotSize3");
			$(this).css({left: coords[0] + 'px', top: coords[1]-5 + 'px'});
		}
		else if(jobs > 500 && jobs <= 600){
			$(this).addClass("dotSize4");
			$(this).css({left: coords[0]-10 + 'px', top: coords[1]-15 + 'px'});
		}
		else if(jobs > 600 && jobs <= 700){
			$(this).addClass("dotSize5");
			$(this).css({left: coords[0]-10 + 'px', top: coords[1]-15 + 'px'});
		}
		else {
			$(this).addClass("dotSize6");
			$(this).css({left: coords[0]-15 + 'px', top: coords[1]-15 + 'px'});
		}
	});
	
	$("#top-employers a").find("img").vAlign();
	
	setEqualHeight($("#advanced-search > div"));
	
	$(".pagination:last").css("border-top","0");
	
	$("#filters .filterContainer:first").addClass("first");
	$("#filters .filterContainer:last").addClass("last");
	$("#filters").find(".flyoutContainer").css("display","none");
	
	var filterContainterZ = 20;
	$("#filters div.filterContainer").each( 
		function(intIndex){
			//$(this).css("z-index",filterContainterZ);
			filterContainterZ = filterContainterZ - 1; 
		}	
	);
	
	$("#filters a.srEdit").each( 
		function( intIndex ){
			$(this).click(function() {
				$("#filters").find(".flyoutContainer:visible").hide();
				$(this).prev(".flyoutContainer").toggle();
			});
		} 
	);
	$("#filters a.hlkCancel").each( 
		function( intIndex ){
			$(this).click(function() {
				$(this).parent().parent().hide();
			});
		} 
	);
	$(".flyoutContainer").draggable({ handle: 'h2' }, { opacity: 0.8 }, { stack: "#filters" });

	
	var jobHeaderHeight = $("#job .jobHeader").height();
	var jobTitleHeight = $("#job .jobHeader .grid_6").height();
	$("#job .jobHeader .grid_3").height(jobTitleHeight);
	//$("#companyLogo").height(jobHeaderHeight);
	$("#companyLogo").find("img").vAlign();
	
	try{
		$("ul.browse-tabs").tabs("div.browse-panes > div");
		$("ul.browse-tabs li a.current").parent().addClass("selected");
		$("ul.browse-tabs a").click(function() { 
			 if ($(this).parent().is('.selected')) {
			 }
			 else {
				$("ul.browse-tabs li").removeClass('selected');
				$(this).parent().addClass('selected')
			 }
		});
	}
	catch(err){}
	
	//myJobVlan 
	if($('#myJobVlanForm').length) {
    	$('#myJobVlanForm').ketchup({
			validationAttribute: 'rel',
			errorContainer: $('<div>', { 'class': 'jobvlan-error-container', html: '<ol></ol><span></span>'  })
		});
 	}
	$("#txtNr").prev().css("display","inline");
	$("#txtNrFact").prev().css("display","inline");
	$("#myJobVlanForm fieldset table").find("th:first").addClass("first");
	$("#myJobVlanForm fieldset table").find("td:first").addClass("first");
	
	$(".lnkSaveSearch").click(function() { 
		$("#saveJobSearchPopup").modal({ minHeight: 10, persist: true }); 
	});
	$(".lnkEmailAlert").click(function() { 
		$("#createJobAlertPopup").modal({ minHeight: 10, persist: true }); 
	});
	
	
	try{
		$.featureList(
			$("#tabs li a"),
			$("#output li"), {
				start_item	:	1
			}
		);
	}
	catch(err){}
	
	$(".hlkJobDetails").each( 
		function( intIndex ){
			$(this).click(function() {
				$(this).next(".jobDetails").slideToggle();
			});
		} 
	);
/*	
	$("table.tablesorter tr th:first-child").addClass("first");
	$("table.tablesorter tr th:last-child").addClass("last");
	$("table.tablesorter tr td:first-child").addClass("first");
	$("table.tablesorter tr td:last-child").addClass("last");
	$("table.tablesorter").tablesorter({ 
        widgets: ['zebra']
    }); 
	$("table.tablesorter tr").mouseover(function(){ $(this).addClass("over"); }); 
	$("table.tablesorter tr").mouseout(function(){ $(this).removeClass("over"); });
*/	
	
	
	
});

(function ($)  {
$.fn.vAlign = function(){ 
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

function setEqualHeight(columns){
	var tallestcolumn = 0;
 	columns.each( 
 		function(){
 			currentHeight = $(this).height();
			if(currentHeight > tallestcolumn){
 				tallestcolumn  = currentHeight;
 			}
 		}
 	);
 	columns.height(tallestcolumn);
}

function openWindow(url){
	window.open(url,'_blank','height=650,width=980,left=100,top=100,location=no,status=no,scrollbars=yes,resizable=yes');
	return false;
} 

function positionSkyscraper() {
	var bw = $("#body");
	var tb = $("#BAN");

	var bwOffset = bw.offset();
	var bwWidth = bw.width();
	var tbHeight = tb.outerHeight();
	
	var skyLeft = bwOffset.left + bwWidth + 10;
	var skyTop = bwOffset.top + 20;	
	
	$("#skyscraper").css({'left' : skyLeft, 'top' : skyTop});
	
	var top = $('#SKY').offset().top - parseFloat($('#SKY').css('marginTop').replace(/auto/, 0));
	
	$(window).scroll(function (event) {
    	// what the y position of the scroll is
    	var y = $(this).scrollTop();
  
    	// whether that's below the form
    	if (y >= top) {
      		// if so, ad the fixed class
      		$('#SKY').addClass('fixed');
    	} else {
      		// otherwise remove it
      		$('#SKY').removeClass('fixed');
    	}
  	});	
}

function dropdown(){
	$("#myJobVlanTop").find("li").each(function() {
					if ($(this).find("ul").length > 0) {
						$('<img src="/static/theme/design/images/nav_arrow.png" />').appendTo($(this).children(":first"));
						$(this).children(":first").css("cursor","default");

						//show subnav on hover
						$(this).mouseenter(function() {
							$(this).find("ul").css("left","0");
						});
						
						//hide submenus on exit
						$(this).mouseleave(function() {
							$(this).find("ul").css("left","-999em");
						});
					}
	});

}
