
/* Created by	  : Kush Murod (me@eboyreal.com) */
/* Date created  :	09.05.2006 */

/* Handles Objects related requests */

var o = {
	
	isObject : function (ele) {
    	return (ele && typeof ele == 'object');
	},
	
	getObj : function(id, d) {
		if(!d) {
			var d = document;
		}
		obj = d.getElementById(id);
		if (!obj) {
			var obj = parent.document.getElementById(id);
		}
		if (this.isObject(obj)) {
			return obj;
		} else {
			return false;
		}
	},
	
	setHtml : function(id, content) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.innerHTML = content;
			return true;
		}
	},
	
	getHtml : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.innerHTML;
		} else {
			return false;
		}
	},
	
	clearHtml : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.innerHTML = '&nbsp;';
			return true;
		} else {
			return false;
		}
	},
	
	setVal : function(id, content) {
		var obj = this.getObj(id);
		if (this.isObject(obj) && content) {
			obj.value = content;
			return true;
		} else {
			return false;
		}
	},
	
	getVal : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.value;	
		} else {
			return false;
		}
	},
	
	clearVal : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.value = '';
			return true;
		} else {
			return false;
		}
	},
	
	setSrc : function(id, src) {
		var obj = this.getObj(id);
		if (this.isObject(obj) && src) {
			obj.src = src;
			return true;
		} else {
			return false;
		}
	},
	
	getSrc : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.src;
		} else {
			return false;
		}
	},
	
	clearSrc : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.src = '';
			return true;
		} else {
			return false;
		}
	},
	
	setBGImg : function(id, img) {
		var obj = this.getObj(id);
		if (this.isObject(obj) && img) {
			obj.style.backgroundImage = img;
			return true;
		} else {
			return false;
		}
	},
	
	getBGImg : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.style.backgroundImage;
		} else {
			return false;
		}
	},
	
	clearBGImg : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.style.backgroundImage = '';
			return true;
		} else {
			return false;
		}
	},
	
	setBGC : function(id, color) {
		var obj = this.getObj(id);
		if (this.isObject(obj) && color) {
			obj.style.backgroundColor = color;
			return true;
		} else {
			return false;
		}
	},
	
	getBGC : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.style.backgroundColor;
		} else {
			return false;
		}
	},
	
	clearBGC : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.style.backgroundColor = '';
			return true;
		} else {
			return false;
		}
	},
	
	displayObj : function() {
		for (i=0;i<arguments.length;i++) {
			var obj = this.getObj(arguments[i]);
			if (this.isObject(obj)) {
				obj.style.display = '';
			}
		}
	},
	
	hideObj : function() {
		for (i=0;i<arguments.length;i++) {
			var obj = this.getObj(arguments[i]);
			if (this.isObject(obj)) {
				obj.style.display = 'none';
			}
		}
	},
	
	focusOnObj : function() {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			obj.focus();
			return true;
		} else {
			return false;
		}
	},
	
	getDisplayStatus : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.style.display;
		} else {
			return false;
		}
	},
	
	getVisibilityStatus : function(id) {
		var obj = this.getObj(id);
		if (this.isObject(obj)) {
			return obj.style.visibility;
		} else {
			return false;
		}
	},
	
	setHtmlMultiple : function(arr) {
		for (var i in arr) {
			this.setHtml(i, arr[i]);
		}
	},
	
	hideUnhide : function (id) {
		if (this.getDisplayStatus(id)=='none') {
			this.displayObj(id);
		} else {
			this.hideObj(id);
		}
	},
	
	isBoolean : function (ele) {
    	return typeof ele == 'boolean';
	},
	
	isArray : function(ele) {
		if (this.isObject(ele)) {  
			return ele.constructor.toString().match(/array/i);
		}		 
		return false;
	},
	
	isNumber : function (ele) {
		/* The isFinite() [js native] function is used to check if a value is a finite number. */
    	return (typeof ele == 'number' && isFinite(ele));
	},
	
	isString : function (ele) {
		return typeof ele == 'string';
	},
	
	/* Handles String related requests start here */
	
	trim : function(str) {
		if (this.isString(str)) {
			return str.replace(/^\s*|\s*$/g,"");
		}
	},
	
	/* Handles Array related requests start here */
	
	inArray : function(niddle, haystack) {
		if (!this.isArray(haystack) || !niddle) {
			return false;
		}
		var i = haystack.length-1;
		do {
			if (haystack[i] == niddle) {
				return true;
			}
		} while (i--);
		return false;
	},
	
	arraySearch : function(niddle, haystack) {
		if (!this.isArray(haystack) || !niddle) {
			return false;
		}
		var i = haystack.length-1;
		do {
			if (haystack[i] == niddle) {
				return i;
			}
		} while (i--);
		return false;
	},
	
	/* js print_r alerts to the screen */
	
	print_r : function (obj) {
		var tmp = '';
		for (var i in obj) {
			tmp += i + '->' + obj[i] + "\n";
		}
		alert(tmp);
	}
	
}

var gallery = {
	
	getUrl : function (get, value) {
	
		//To convert a string from URL-encoded form, unescape("It%27s%20me%21"), result: "It's me!"
		tmp = unescape(location.href);
		urls = tmp.split('/');
		if (o.inArray(get, urls)) {
			var key = o.arraySearch(get, urls);
			if (value) {
				if (o.inArray(urls[key+1], urls)) {
					return urls[key+1];
				} else {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
}


/*****************************************************/

_server = 'http://' + location.hostname + '/';
_img = _server + 'images/';

function dothis(){
	
	work = gallery.getUrl('work', true);
	photo = gallery.getUrl('photo', true);
	total = gallery.getUrl('total', true);
	
	photo = photo - 0;
	
	speed = o.getObj('speed');
	
	speed.disabled = false;
	
	if(gallery.getUrl('empty')){
		speed.disabled = true;
		form_prev_next("prev",0,0,false);
		o.getObj('img_play_pause', parent.document).setAttribute('src', _img + 'play_img_disabled.gif');
		form_prev_next("next",0,0,false);
		return false;
	}
	if(!photo||photo==1){
		if(total==1){
			speed.disabled = true;
			form_prev_next("prev",0,0,false);
			form_prev_next("next",0,0,false);
		} else{
			next = 2;
			form_prev_next("prev",0,0,false);
			form_prev_next("next",next,total,true);
		}
	}
	else if(total==photo){
		prev = photo - 1;
		form_prev_next('prev',prev,total,true);
		form_prev_next('next',0,0,false);
	} else {
		next = photo + 1;
		form_prev_next("next",next,total,true);
		prev = photo - 1;
		form_prev_next("prev",prev,total,true);
	}
	
	status = gallery.getUrl('slideshow', true);
	
	obj_link = o.getObj('a_play_pause', parent.document);
	obj_img = o.getObj('img_play_pause', parent.document);
	
	if(total==1){
		obj_link.removeAttribute("href");
		obj_img.setAttribute("src",_img + "play_img_disabled.gif");
		return;
	}
	
	if (status=='play') {
		if (total==photo) {
			do_ = "play";
			next = 1;
			obj_img.setAttribute("src",_img + "play_img.gif");
		} else {
			
			jumper = _server + "gallery/view/work/" + work + "/photo/" + next + "/total/" + total + "/slideshow/play/";
			
			speed_ = speed.options[speed.selectedIndex].value;
			speed.disabled = true;
			
			setTimeout('location.href="' + jumper + '"',1000 * speed_);
			do_ = "pause";
			next = next - 1;
			obj_img.setAttribute("src",_img + "pause_img.gif");
		}
	}
	else if(status=="pause"){
		do_ = "play";
		obj_img.setAttribute("src",_img + "play_img.gif");
		speed.disabled = false;
	}
	else{
		if (total==photo) {
			do_ = "play";
			next = 1;
			obj_img.setAttribute("src",_img + "play_img.gif");
		} else {
			do_ = "play";
			obj_img.setAttribute("src",_img + "play_img.gif");
		}
	}
	url = _server + "gallery/view/work/" + work + "/photo/" + next + "/total/" + total + "/slideshow/" + do_ + "/";
	obj_link.setAttribute("href",url);
	
}

function form_prev_next(type, number, total, enabled) {
	link_obj = o.getObj('a_' + type, parent.document);
	img_obj = o.getObj('img_' + type, parent.document);
	if (enabled) {
		var commonUrl = _server + "gallery/view/work/" + work + "/photo/";
		url = commonUrl + number + "/total/" + total + "/";
		link_obj.setAttribute("href",url);
		img_obj.setAttribute("src",_img + type + "_img.gif");
	}
	else{
		link_obj.removeAttribute("href");
		img_obj.setAttribute("src",_img + type + "_img_disabled.gif");
	}
}

function gallerySorter(f) {
	
	var catObj = o.getObj('category');
	var sldndex = catObj.selectedIndex;
	var sldVal = catObj.options[sldndex].value;
	var cat = catObj.id + '/' + sldVal + '/year/all/';
	
	url = _server + 'gallery/thumbnails/' + cat;
	frames['thumbnail'].location = url;
	url = _server + 'gallery/view/' + cat;
	frames['view'].location = url;
	
	return false;
}