/* =Load Functions
-----------------------------------------------------------------------------*/
var URL;
jQuery(document).ready(function(){
	URL = getURL();
	psutoClass();
	archiveList();
	directory_tree();
	$("a.lightbox").click(function(){return false;}).lightBox({
		fixedNavigation:true,
		overlayOpacity: 0.6,
		imageLoading: '/images/lightbox-ico-loading.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif',
		containerResizeSpeed: 250
	});
	// add class to current page link in nav
	$("#nav li a[href^='"+URL.file+"']").parent().addClass("curPage");
});

/* =Add class "hover to all li's and buttons and add focus class
-----------------------------------------------------------------------------*/
function psutoClass(){
	$("li, button").hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")});
	$("input, textarea, select")
		.focus(function(){$(this).addClass("focus");})
		.blur(function(){$(this).removeClass("focus")});
	$("input:disabled, select:disabled, button:disabled, textarea:disabled").addClass("disabled");
};

/* Pull a Variable wraped in "[]" from a string. 
 * EG: 
 * 		var string = "this is a string with a variable[value] wraped in brackets".
 * 		var variable = stringVar(string,'variable');
 * 		// variable == 'value';
 *
 * Works Well with Class Names
 *---------------------------------------------------------------------------*/
function stringVar(string,Var){
	var exp = new RegExp(Var+'(\\[.*?\\])',["i"]);
	var m = exp.exec(string);
	if (m != null){ return m[1].substring(1,m[1].length-1); }
	return false;
};
function getClassVar(el,Var){
	return stringVar($(el).attr("class"),Var);
};
function removeClassVar(el,Var){
	var string = getClassVar(el,Var);
	$(el).removeClass(Var+"["+string+"]");
};
function changeClassVar(el,Var,value){
	var string = getClassVar(el,Var);
	$(el)
		.removeClass(Var+"["+string+"]")
		.addClass(Var+"["+value+"]");
};

/* =Archive List
-----------------------------------------------------------------------------*/
function archiveList(){
	$(".archiveList").each(function(){
		var list = $(this);
		// hide all ul's
		list.find("li ul").hide();
		
		// Make Toggles
		list.find("li span").each(function(){
			if($(this).parent("li").find("ul").length>0){
				$(this).before('<span class="open"><a href="#" style="font-weight:bold; text-decoration:none; padding:0 5px;">+</a></span> ');
				$(this).parent("li").find("span.open a").toggle(
					function(){
						$(this).html("-");
						$(this).parents("li:first").find("ul:first").slideDown(300);
						return false;
					},
					function(){
						$(this).html("+");
						$(this).parents("li:first").find("ul:first").slideUp(300);
						return false;
					}
				);
			}
		});
		
		// show latest
		var link = list.find("a[href$='"+((URL.file!='index.php') ? URL.file : URL.path)+"']");
		if(link.length>0){
			if(link.parent("span").length>0){
				link.parents("ul:first").parent("li").find(".open a").click()
					.parents("ul:first").parent("li").find(".open a").click()
					.parents("ul:first").parent("li").find(".open a").click()
					.parents("ul:first").parent("li").find(".open a").click();
			}else{
				link.parents("li:first").find(".open a").click()
					.parents("ul:first").parent("li").find(".open a").click()
					.parents("ul:first").parent("li").find(".open a").click();
			}
		}
	});
};


function directory_tree(){
	var trees = $(".directory_tree");
	if(trees.length==0) return false;
	trees.each(function(){
		var tree = $(this);
		var root = parseInt(tree.attr("id").split('-')[1]);
		
		tree.fileSystem({
				'root'			: '['+root+']', 
				'script'		: '/includes/functions/directory_tree.php', 
				'folderEvent'	: 'click', 
				'expandSpeed'	: 200, 
				'collapseSpeed'	: 100, 
				'loadMessage'	: 'Loading...' ,
				'multiFolder'	: true,
				'addclicks'		: true,
				'emptydir'		: ''
			}, 
			function(link,is_file) {
				// file click
				if(is_file){
					window.open(link.href);
				}
			},
			function($this){
				if($this.is(".expanded")){
					$this.find('.content:first').show();
				}
			}
		);
	});

}


function getURL(){
	var http = (("https:" == document.location.protocol) ? "https://" : "http://");
	var wloc = window.location+'';
	// if there are parameters
	var rawP = null;
	if(wloc.indexOf("?") != -1){
		var larr = wloc.split('?');
		wloc = larr[0];
		rawP = larr[1];
	}
	// if there is an anchor
	var anch = null;
	if(wloc.indexOf("#") != -1) {
		var ancharr = wloc.split('#');
		anch = ancharr[1];
		wloc = ancharr[0];
	}else if(rawP && rawP.indexOf("#") != -1) {
		var ancharr = rawP.split('#');
		anch = ancharr[1];
		rawP = ancharr[0];
	}
	// create object from params
	var param = [];
	if(rawP){
		var parArr = rawP.split('&');
		for(var i=0; i<parArr.length; i++){
			var nvarr = parArr[i].split("=");
			param[i] = {'name':nvarr[0],'value':nvarr[1]};
		}
	}
	// find path and file
	var locarr = wloc.split(document.domain);
	var suff = locarr[locarr.length-1];
	var file = 'index.php';
	var path = '/';
	var ind = suff.lastIndexOf('/');
	if(ind != -1){
		var i = suff.substr(ind+1);
		if(!empty(i)){
			file = i;
			path = suff.split(file)[0];
		}else path = suff;
	}

	return {
		'http'		: http,
		'domain'	: document.domain,
		'base'		: http+document.domain,
		'path'		: path,
		'file'		: file,
		'cur'		: http+document.domain+path+file,
		'rawparam'	: rawP,
		'param'		: param,
		'anch'		: anch
	}
};

/* Helper functions
---------------------------------------------------------*/
function empty(mixed_var){var key;if(mixed_var===""||mixed_var===0||mixed_var==="0"||mixed_var===null||mixed_var===false||typeof mixed_var==='undefined'){return true;}if(typeof mixed_var=='object'){for(key in mixed_var){return false;}return true;}return false;}
function intval(mixed_var,base){var tmp;if(typeof(mixed_var)=='string'){tmp=parseInt(mixed_var*1);if(isNaN(tmp)||!isFinite(tmp)){return 0;}else{return tmp.toString(base||10);}}else if(typeof(mixed_var)=='number'&&isFinite(mixed_var)){return Math.floor(mixed_var);}else{return 0;}};
function stripslashes( str ) {return str.replace('/\0/g', '0').replace('/\(.)/g', '$1');};
function urlencode(str){var ret=str;ret=ret.toString();ret=encodeURIComponent(ret);ret=ret.replace(/%20/g,'+');return ret;};
function urldecode(str){var ret=str;ret=ret.replace(/\+/g,'%20');ret=decodeURIComponent(ret);ret=ret.toString();return ret;};
function changeStringVar(string,Var,newVar){
	var strV = stringVar(string,Var);
	string = string.split("["+strV+"]");
	return string[0]+"["+newVar+"]"+string[1];
};
function makeName(filename){
	var ext = fileExt(filename);
	filename = filename.split('.'+ext)[0];
	return filename.replace(/_/g, " ").replace(/-/g, " ");
};
function fileExt(filename){var x=false;if(filename.match(".")){x=filename.replace(/^.*\./,'');}return x;};
function isImage(filename){var ext=fileExt(filename);if(!ext)return false;var img=['jpg','jpeg','png','gif'];return($.inArray(ext.toLowerCase(),img)>=0);};
function imgver(filename,ver){var ext=fileExt(filename);var file=filename.split("."+ext)[0];return file+ver+"."+ext;};
function FormatFileSize($size){if($size<1024)return roundNum(($size*100)/100,2)+" bytes";if($size<1048576)return roundNum((($size/1024)*100)/100,2)+"KB";if($size<1073741824)return roundNum((($size/1048576)*100)/100,2)+"MB";return roundNum((($size/1073741824)*100)/100,2)+"GB";};
function roundNum(num,dec){return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);};
function file_exists(url){var req=this.window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();if(!req){throw new Error('XMLHttpRequest not supported');}req.open('HEAD',url,false);req.send(null);if(req.status==200){return true;}return false;};

