var FADE_SPEED = 200; // milliseconds, time to show or hide an element
var SCROLL_SPEED = 600; // milliseconds, autoscroll time. Only relevant if you have $load_all = true in _config.php
var SCROLL_OFFSET = 0 // pixels, extra offset for the autoscroll to scroll to.
var HIDE_MENU_SPEED = 200 // milliseconds, speed to hide the menu.
var NEWNAME = "work"; /* this will be what's diplayed at the browser
						URL when you are viewing the portfolio, default is #work
						This has to match the name of the file in parts/pages,
						aswell as the variable $menuitems (but without the .php extension) in _config.php */

/*

TO FOR EXAMPLE HIDE ELEMENT BY SLIDING INSTEAD OF FADING, CHANGE ALL
fadeIn(FADE_SPEED) TO slideDown(FADE_SPEED) AND ALL fadeOut(FADE_SPEED)
TO slideUp(FADE_SPEED).

*/

function isnum(what){
      var anum=/(^\d+$)|(^\d+\.\d+$)/
      if(anum.test(what)){
		  return true
	  }else{
		  return false
	  }
}

function entry_nav(id){
	div = $("#entry"+id);
	y = div.offset().top;
	scroll_to(y);
}

function scroll_to(y){
	jQuery('html,body').animate({scrollTop: y-SCROLL_OFFSET}, SCROLL_SPEED);
}


var animating = false;
var loadthemall = false;
var topint;

function setGlobal() {
	topint = $("#core_entries").attr("name");
}

function init_load_all(what){
	loadthemall = true;
	init_load(what, null);
}

function init_load(what, extra){
	
	if(!animating){
		if(isnum(what) && loadthemall && $("#core_content").length > 0) entry_nav(what);
		else{
			animating = true;
			$("#core_body").fadeOut(FADE_SPEED, function(){
				if(!what) what = topint;
				if(isnum(what)) {
					if($("#core_content").length > 0){
						obj = what;
						extra = null;
					}else{
						$("#core_entries > *").fadeOut(FADE_SPEED);
						obj = NEWNAME;
						extra = what;
					}
				}else{
					if(what == NEWNAME) {
						$("#core_entries > *").fadeOut(FADE_SPEED);
						obj = NEWNAME;
						extra = topint;
					}else{
						$("#core_entries > *").fadeOut(FADE_SPEED);
						obj = what;
						extra = null;
					}
				}
				finish_load(obj, extra, null);
			});
			
		}
	}
}

function finish_load(obj, extra, type){
			
	div =  $("<div/>");
	if(type && path){
		path = "?type="+ type +"&row=" + obj;
	}else if(type){
		path = "?type=" + type;
	}else if(obj){
		path = "?row=" + obj;
	} else {
		path = null;
	}
	
	div.load("/core/getentry.php" + path,null,function(){
		if(isnum(obj)){
			$("#core_content").html(div);
			$("#core_entries > *").fadeIn(FADE_SPEED);
			$("#core_body").fadeIn(FADE_SPEED, function(){
				if(loadthemall && obj != topint) entry_nav(obj);
				animating = false;				 
			});
		}else if(!extra){
			$("#core_body").html(div);
			$("#core_body").fadeIn(FADE_SPEED, function(){
				animating = false;									 
			});
		}else{
			$("#core_body").html(div)
			if(loadthemall) finish_load(extra,null,"all");
			else finish_load(extra, null, null);
		}
	});
}
