var loadingImg = webroot+'img/ajax-loader.gif';
var loadingImgBlue = webroot+'img/ajax-loader-blue.gif';

function getId(ele_id) {
	var split = ele_id.split("_");
	return split[1];
}

function updateCoords(c) {
	$('#x').val(c.x);
	$('#y').val(c.y);
	$('#w').val(c.w);
	$('#h').val(c.h);
};

function checkCoords() 
{ 
  if ($('#x').val() != '') return true; 
  alert('Maak eerst een selectie.'); 
  return false; 
}; 

function popup() {
	$('a.popup').click(function(){
		var href = $(this).attr('href');
		var shref = href.split("/");
		window.open(href, shref[shref.length-1], 'width=700,height=500,scrollbars=yes,toolbar=no,location=no'); 
		return false;
	});
}

function slideAdminTools() {
	$('#adminTools li a#slideTools').click(function() {
		var top = $('#adminTools').css('top');
		if (top == '-208px'){
			$.post(webroot+"pages/adminToolsSession/1");
			$("#adminTools").animate({"top": "0px"}, { "duration": 300, "easing": "easeOutSine" });
		}
		else{
			$.post(webroot+"pages/adminToolsSession/0");
			$("#adminTools").animate({"top": "-208px"}, { "duration": "slow", "easing": "easeOutSine" });
		}
	});
	return false;
}

var selected_nav_a_index = undefined;
// dropdown functions for navigation
function dropDownNavigationTools() {
	
	var selected_nav_li, selected_nav_li_ul, selected_nav_li_ul_index, old_nav_li_ul_index; // general vars
	var navTimeout = new Array(); // timeout for all subnavs
	var heights = getSubNavHeights(); // heights for all subnavs
	
	$("ul#navigation > li > a").mouseover(function(){
		
		// set old index
		old_nav_a_index = selected_nav_a_index;
		
		// stop from hiding after a timeout
		clearTimeout(navTimeout[selected_nav_li_ul_index]);
		
		// set new values
		selected_nav_a_index = $('ul#navigation li a').index(this);
		selected_nav_li = $(this).parent("li");
		selected_nav_li_ul = $(selected_nav_li).children('ul');
		selected_nav_li_ul_index = $('ul#navigation li ul').index(selected_nav_li_ul);
		
		// if isnt already selected
		if (selected_nav_a_index !== old_nav_a_index){
			subNavHide(); // hide other subs
			subNavShow(selected_nav_li_ul, heights[selected_nav_li_ul_index]);
		}
		
	});
	
	$('ul#navigation > li > ul').mouseover(function(){
		clearTimeout(navTimeout[selected_nav_li_ul_index]);
	});
	
	$('ul#navigation > li > ul, ul#navigation > li > a').mouseout(function(){
		clearTimeout(navTimeout[selected_nav_li_ul_index]);
		navTimeout[selected_nav_li_ul_index] = setTimeout('subNavHide()', 500);
	});
	
}
function subNavShow(selected_nav_li_ul, height) {
	$(selected_nav_li_ul).css({'height':0});
	$(selected_nav_li_ul).animate({
      "height": height+"px"
    }, { "duration": 200, queue: false });
}
function subNavHide() {
	$("ul#navigation li ul:visible").stop(true, true);
	$('ul#navigation li ul:visible').animate({
      "height": "0px"
    }, { "duration": 100, queue: false, complete: function(){ $(this).hide(); clearActiveNav(); } });
}
function clearActiveNav() {
	selected_nav_a_index = undefined;
}
function getSubNavHeights() {
	var heights = new Array();
	//var val = '';
	$.each($("ul#navigation li ul"), function(i){
		heights[i] = $(this).innerHeight();
		//val = val+'-'+heights[i];
	});
	//setFlash(val);
	return heights;
}

function external() {
	$("a[rel='external']").click(function(){
    	window.open($(this).attr('href'));
    	return false;
    });
}

function fancyBox() {
	$("a.fancybox").fancybox();
}

function tooltip() {
	$('#educations ul li a').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250 
	});
}

function signup() {
	
	if($('#selectedEdu').val() != '')
		$('#educations ul li a[title="' + $('#selectedEdu').val() + '"]').parent().addClass('active');
	
	$('#educations ul li a').click(function() {
		var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		
		$('#educations ul li').removeClass('active');
		$(this).parent().addClass('active');
		$('#selectedEdu').val($(this).text().replace(' ', '_'));

		if(windowHeight < 690)
			$('html,body').animate({scrollTop: $('.persoonsgegevens').offset().top},'slow');
			
		
		
		return false;
		
	});	
}

function createTableSortable(table, action) {
	$(table).sortable({ 
		items: 'tr', 
		placeholder: 'ui-state-highlight', 
		forceHelperSize: true,
		helper: function(e, tr) {
			var $originals = tr.children();
			var $helper = tr.clone();
			$helper.children().each(function(index) {
				$(this).width($originals.eq(index).width())
			});
			return $helper;
		},
		axis: 'y', 
		handle: '.move',
		items: 'tr:not(.head)',
		update: function(e, ui){
            $.ajax({
                type: "POST",
                url: webroot+action,
                data: "itemId=&newOrder="+$(table).sortable('toArray'),
                error: function(msg){
                    $("#JQresult").html(msg);
                },
                success: function(msg){
                    $("#JQresult").html(msg);
                }
            });
		}
	});
}




