var loadingImg = webroot+'img/ajax-loader.gif';

function navigationActions () {
	$active_head = null;
	$an_sub_is_active = false;
	$timeout = null;
	
	$('#navigatie > ul > li > a.head, #navigatie > ul > li > ul').mouseover(function(){
		$this_head = $(this).parent('li').children('a');
		showSubnav($this_head);
		$this_head.addClass('active');
	});
	$('#navigatie > ul > li').mouseout(function(){
		if ($an_sub_is_active==true) {
			$timeout = setTimeout('hideSubnav()', 500);
		}
		else{
			if ($active_head != null) {
				$active_head.removeClass('active');
			}
		}
	});
	
}
function showSubnav($this_head) {
	$this_head_index = $('#navigatie > ul > li > a').index($this_head); // get this navigation index
	if ($active_head != null) {
		$active_sub_nav = $active_head.parent('li').children('ul'); // get active subnav
		$active_head_index = $('#navigatie > ul > li > a').index($active_head); // get this navigation index
		$active_sub_nav_length = $active_sub_nav.length;
		$active_sub_nav.stop(true, true);
		$active_head.removeClass('active');
	}
	else{
		$active_sub_nav = '';
		$active_head_index = null;
		$active_sub_nav_length = 0;
	}
	$this_sub_nav = $this_head.parent('li').children('ul'); // get this subnav
	$this_sub_nav_length = $this_sub_nav.length;
	clearTimeout($timeout); // stop hide subnav
	if ($this_head_index != $active_head_index) {
		// Old and new items has subitems
		if ($active_sub_nav_length > 0 && $this_sub_nav_length > 0) {
			$('#navigatie > ul > li > ul').hide();
			$this_sub_nav.show();
			$active_head = $this_head;
			$an_sub_is_active = true;
		}
		// only new item has subitems
		else if($this_sub_nav_length > 0) {
			$active_head = $this_head;
			$this_sub_nav.show();
			$this_sub_nav.css({'height':0});
			$this_sub_nav.animate({
		      "height": "57px"
		    }, { "duration": 200, queue: false}); // "easing": "easeInSine"
		    $an_sub_is_active = true;
		}
		// only old item had subitems
		else if($active_sub_nav_length > 0){
			$('#navigatie > ul > li > ul').hide();
			$active_head = $this_head;
			$an_sub_is_active = false;
		}
		// Old and new items has no subitems
		else {
			$active_head = $this_head;
		}
	}
}

function hideSubnav() {
	$active_head = null;
	$an_sub_is_active = false;
	$('#navigatie > ul > li > ul').hide();
	$this_head.removeClass('active');
	/*
	$('#navigatie > ul > li > ul').animate({
      "height": "0px"
    }, { "duration": 500, queue: false, "easing": "easeOutSine", complete: function(){$('#navigatie > ul > li > ul').hide();} });
    */
}


function getId(ele_id) {
	if (ele_id == undefined) {
		return null;
	}
	else{
		var split = ele_id.split("_");
		return split[1];
	}
}

function setFlash(message) {
	if ($('#flashMessage').length == 0){
		$('#content_links').prepend('<div id="flashMessage" class="message"></div>');
	}
	$('#flashMessage').html(message);
	
}

function externalLinks() {
	$("a[rel='external']").click(function(){
    	window.open($(this).attr('href'));
    	return false;
    });
}

function fadePhotoRight() {
	$('#fade_photo_in')
		.css({'opacity':'0', 'display':'block'})
		.animate({opacity: "1"}, { "duration": 1550, "easing": "easeInSine" });
}

function getArrayKey ($array, $value) {
	$ret = -1;
	$.each($array, function(k, v) {
	    if (v == $value) {
	    	$ret = k;
	    }
	});
	return $ret;
}

function highlightFirst(name){
	/*
	$('#'+name+' ul li').each(function(i){
		$(this).animate({
			opacity:'1',
			marginLeft:'20',
			queue:false
		},400,'easeInQuad',
				function(){
			$(this).animate({
				opacity:'1',
				marginLeft:'0',
				queue:false
			},500,'easeOutQuad');
			}	
		);
	});		
	*/
		$('#'+name+' ul').animate({
			opacity:'1',
			marginLeft:'20',
			queue:false
		},400,'easeInQuad',
				function(){
			$(this).animate({
				opacity:'1',
				marginLeft:'0',
				queue:false
			},500,'easeOutQuad');
			}	
		);
}

function reorder_rank() {
	$.each($('.adminTable tr td input.rank'), function(i, val) {
      $(this).val(i+1);
    });
}
function reset_rank() {
	$('.adminTable tr td input.rank').val(0);
}
