// connects a link and a box (when click of a button of id link, activates box of id container)
// keepalive == 1 cause the box to not disappear when clicked in area of the box
// keepalive == 2 cause the box's visibility to only be toggled by the link,
var dropBox = Class.create({
	initialize: function(link, container, keepAlive) {
		this.link = link;
		this.container = container;
		this.keepAlive = keepAlive;
		this.isOpen = false;
	},
	
	hide: function() {
		$(this.container).style.display="none";
		this.isOpen=false;
	},
	
	show: function() {
		$(this.container).style.display="block";
		this.isOpen = true;
		if (this.link == "login") {
			$('login-username').focus();
		}
	},
	
	toggle: function() {
		var x = $(this.container).style.display;
		( x == "none" || x == "" ) ? this.show() : this.hide();
	}
});

var dropCtrls = Class.create({
	initialize: function(box) {
		this.box = Array();
		for (var i=0;i<box.length;i++) {
			var x = box[i];
			if ( $(x.link) != null && $(x.container) != null) {
				this.box.push(x);
			}
		}
		this.size = this.box.length;
		Event.observe(document, 'click', this.listen.bind(this));
	},
	
	listen: function(e) {
		var fired = Event.element(e).identify();
		//alert("fired: " + fired);
		for (var i=0;i<this.size;i++) {
			var x = this.box[i];
			if ( $(fired) == null) return;
			if ( $(fired).descendantOf(x.container) || fired == x.container) {
				if (x.keepAlive != 0) {
					if (!x.isOpen) {
						x.show();
					}
				}
				else {
					x.toggle();
				}
			}
			else if (fired == x.link) {
				x.toggle();
			}
			else {
				if (x.keepAlive != 2)
					x.hide();
			}
		}
	}
});

var osComAjax = Class.create({
	initialize: function(url, container, autograb, load, error) {
		if (load == "" || load == null) load = "ajax-loading";
		if (error == "" || error == null) error = "ajax-error";
		if (autograb == "" || autograb == null) autograb = false;
		this.container = $(container);
		this.url = url;
		this.loadText = $(load).innerHTML;
		this.errorText = $(error).innerHTML;
		
		if (autograb == true) {
			this.grab(url);
		}
	},
	
	
	
	recall: function() {
		ajaxRecall();
	}, 
	
	paintText: function(text) {
		this.container.innerHTML = text;
	},
	
	grab: function(url) {
		this.paintText(this.loadText);
		new Ajax.Request(url, { method: 'get', onSuccess: this.osSuccess.bind(this), onFailure: this.osFailure.bind(this)});
	},
	
	qgrab: function(qstr) {
		this.grab(this.url + qstr);
	},
	
	osSuccess: function(response) {
		this.paintText(response.responseText);
		this.recall();
	},
	
	osFailure: function(response) {
		this.paintText(this.errorText);
		//this.paintText(response.responseText);
	}
});

var ostdRate = Class.create(osComAjax, {
	initialize: function($super, url, container, themekey, rating, rootstyle, autograb, load, error ) {
		$super(url, container, autograb, load, error);
		// if rating in nan
		if(rating != rating) rating = 0;
		this.n=0;
		this.themekey = themekey;
		this.rating = Math.round(rating);
		this.rootstyle = rootstyle;



		this.showStars(this.rating);

		//alert(rating);
		//alert(this.themekey);
	},
	
	showStars: function(n) {
		clearTimeout(this.t);
		this.removeStars();
		for (var i=0;i<n;i++) {
			var j = i+1;
			var x = 'star__' + j;
			$(x).src = this.rootstyle + "img/star.gif";
		}
		for (var i=n;i<5;i++) {
			var j = i+1;
			var x = 'star__' + j;
			$(x).src = this.rootstyle + "img/star_bg.gif";
		}
	},
	
	clearStars: function() {
		this.t = setTimeout(this.removeStars.bind(this), 100);
	},
	
	removeStars: function() {
		for (var i=0;i<5;i++) {
			var j = i+1;
			var x = 'star__' + j;
			if (i >= this.rating)
				$(x).src = this.rootstyle + "img/star_bg.gif";
			else
				$(x).src = this.rootstyle + "img/star.gif";
		}
	},
	
	setStars: function(n) {
		this.grab(this.url + "?rating=" + n + "&key=" + this.themekey);
	}
});

var oindexTabs = Class.create(osComAjax, {
	initialize: function($super, url, container, header, autograb, load, error) {
		$super(url, container, autograb, load, error);
		this.header = header;
		this.url = url;
		
		$(this.header[0]).addClassName("btn-active");
		$(this.header[0]).removeClassName("btn-inactive");
		this.qurl = this.url + "?m=" + this.header[0];
		this.total = 20;
		this.prev = this.total;
		this.next = 2
		this.now = 1;
		this.grab(this.qurl);
	},
	
	listen: function(e) {
		var fired = Event.element(e).identify();
		var firedid=-1;
		for (var i=0;i<this.header.length;i++) {
			if (this.header[i] == fired) {
				firedid=i;
				this.qurl = this.url + "?m=" + this.header[i];
				this.total = 20;
				this.prev = this.total;
				this.next = 2
				this.now = 1;
				this.grab(this.qurl);
			}
		}
		
		if (firedid == -1) return false;
				
		for (var i=0;i<this.header.length;i++) {
			if (firedid == i) {
				$(this.header[i]).addClassName("btn-active");
				$(this.header[i]).removeClassName("btn-inactive");
			}
			else {
				$(this.header[i]).addClassName("btn-inactive");
				$(this.header[i]).removeClassName("btn-active");
			}
		}
	},
	
	nextPage: function(maxpage) {
	
		if (maxpage < this.total) {
			this.total = maxpage;
		}
	
		this.grab(this.qurl + "&p=" + this.next);
		this.prev = this.now;
		this.now = this.next;
		this.next = (this.next+1 > this.total) ? 1 : this.next+1;
		//alert("next: " + this.next);
	},
	
	prevPage: function(maxpage) {
		if (maxpage < this.total) {
			this.total = maxpage;
		}
		
		this.grab(this.qurl + "&p=" + this.prev);
		this.next = this.now;
		this.now = this.prev;
		this.prev = (this.prev-1 == 0) ? this.total : this.prev-1;
		//alert("prev: " + this.prev);
	}
});

var osComments = Class.create(osComAjax, {
	initialize: function($super, url, container, autograb, load, error) {
		$super(url, container, autograb, load, error);
	},
	
	post: function(form) {
		var qstr = "?" + $(form).serialize();
		$(form).reset();
		$(form).disable();
		
		new PeriodicalExecuter(function(pe) {
		  $(form).enable();
		  pe.stop();
		}, 5);
		this.grab(this.url + qstr);
	},
	
	deletecomment: function(cid, u, id) {
		this.grab(this.url + "?m=del&comid=" + cid + "&u=" + u + "&id=" + id);
	},
	
	block: function(poster) {
		this.grab(this.url + "?m=block&poster=" + poster);
	}
});

var osSupModels = Class.create(osComAjax, {
	initialize: function($super, url, container, autograb, load, error) {
		$super(url, container, autograb, load, error);
	},
	
	showBrand: function(brand) {
		this.grab(this.url + "?b=" + brand);
	}
});

var osPopTT = Class.create(osComAjax, {
	initialize: function($super, url, container, autograb, load, error) {
		$super(url, container, false, load, error);
		this.total = 20;
		this.prev = this.total;
		this.next = 2
		this.now = 1;
		this.qurl = this.url + "?t=1";
		this.grab(this.url);
	},
	
	nextPage: function() {
		this.grab(this.qurl + "&p=" + this.next);
		this.prev = this.now;
		this.now = this.next;
		this.next = (this.next+1 > this.total) ? 1 : this.next+1;
		//alert("next: " + this.next);
	},
	
	prevPage: function() {
		this.grab(this.qurl + "&p=" + this.prev);
		this.next = this.now;
		this.now = this.prev;
		this.prev = (this.prev-1 == 0) ? this.total : this.prev-1;
		//alert("prev: " + this.prev);
	}
});

var cropImg = Class.create({
	initialize: function(ratio) {
		this.ratio = ratio;
		this.top = this.left = this.width = this.height = "";
	},

	getSelectedArea: function( coords, dimensions ) {
		this.top = coords.y1;
		this.left = coords.x1;
		this.width = dimensions.width;
		this.height = dimensions.height;
	},
	crop: function() {
		//alert("scaled dimensions\n\ntop: " + this.top + "\nleft: " + this.left + "\n\nwidth: " + this.width + "\nheight: " + this.height + "\n\nratio: " + this.ratio);
		this.top *= this.ratio;
		this.left *= this.ratio;
		this.width *= this.ratio;
		this.height *= this.ratio;
		
		this.top = Math.ceil(this.top);
		this.left = Math.ceil(this.left);
		this.width = Math.ceil(this.width);
		this.height = Math.ceil(this.height);
		
		//alert("real dimensions\n\ntop: " + this.top + "\nleft: " + this.left + "\n\nwidth: " + this.width + "\nheight: " + this.height + "\n\nratio: " + this.ratio);
		return {y: this.top, x: this.left, w: this.width, h: this.height};
	}

});

var os2uibox = Class.create({
	initialize: function(text) {
		this.text = text;
	},
	
	alert: function(str, ok) {
		str = "<div id='os2alert-text'>"+str+"</div>";
		str += "<div id='os2alert-ctrls'><button onclick=\""+ok+";Shadowbox.close();\">" + this.text.ok + "</button></div>";
		this.box(str);
	},
	
	confirm: function(str, yes, no) {
		str = "<div id='os2alert-text'>"+str+"</div>";
		str += "<div id='os2alert-ctrls'><button onclick=\""+yes+";Shadowbox.close();\">" + this.text.yes + "</button>&nbsp;&nbsp;<button onclick=\""+no+";Shadowbox.close();\">" + this.text.no + "</button></div>";
		this.box(str);
	},
	
	okcancel: function(str, ok, cancel) {
		str = "<div id='os2alert-text'>"+str+"</div>";
		str += "<div id='os2alert-ctrls'><button onclick=\""+ok+";Shadowbox.close();\">" + this.text.ok + "</button>&nbsp;&nbsp;<button onclick=\""+cancel+";Shadowbox.close();\">" + this.text.cancel + "</button></div>";
		this.box(str);
	},
	
	box: function(str) {
		Shadowbox.open({
				type: 'html',
				content: str,
				width: 300,
				height: 100
			},
			{
				enableKeys: false,
				listenOverlay: false,
				animate: false, 
				resizeDuration: 0,
				initialHeight: 100,
				initialWidth: 300

			});
		
	}
});

function themes_changeModel(uri, key, value, time, quality, categoryid) {
	var x = $('themes-selectmodel');
	var y = uri + '?tk=' + key + '&tv=' + value + '&tt=' + time + '&tq=' + quality + '&tp=1&tc=' + categoryid + '&tm=';
	location.href= y + x.value;
}

function gif_changeDim(uri, key, value, time, quality, categoryid) {
	var x = $('gif-selectdim');
	var y = uri + '?tk=' + key + '&tv=' + value + '&tt=' + time + '&tq=' + quality + '&tp=1&tc=' + categoryid + '&tm=';
	if(x.selectedIndex == 0)
			  location.href = y;
	else
			  location.href= y + x.value;
}

function tdform_changeModel() {
	var x = document.downloadForm.c;
	if (x.selectedIndex == 0) {
		document.downloadForm.s.value = "";
		document.downloadForm.submitbtn.disabled = true;
	}
	else {
		document.downloadForm.s.value = x.selectedIndex;
		document.downloadForm.submitbtn.disabled = false;
	}
}

function showElem(element) {
	$(element).style.display="block";
}
function hideElem(element) {
	$(element).style.display="none";
}
function searchPop(k, type) {
	if (type == null) type = "theme";
	
	if (type == "theme") {
		location.href='theme?tm=&tv=' + k;
	}
	
	else if (type == "tape") {
		location.href='tape?tv=' + k;
	}
}


function searchUserTags(s,c,p) {
	location.hash = 'searchAnchor';
	$('home-search').style.display = 'block';
	$('seek-member').innerHTML = $('ajax-loading').innerHTML;
	
	new osComAjax('aj-homeseekmember.oss?s='+s+'&c='+c+'&p='+p, 'seek-member', true);
	//new Ajax.Updater('seek-member', 'aj-homeseekmember.oss', {asynchronous:true, method: 'post', parameters: 's='+s+'&c='+c+'&p='+p});
}

function img_resize(id, xlimit, skinRatio) {
	id = $(id);
	//alert(id.width/xlimit);
	var ratio = (id.width/xlimit);
	//alert(ratio);
	
	h = id.height;
	//alert("orig height: " + id.height + "\norig width: " + id.width + "\nratio: " + ratio);
	id.width /= ratio;
	//alert("new height: " + id.height + "\nnew width: " + id.width);
	
	return ratio;
}

function clear_entities(string) {
	var y=document.createElement('span');
	y.innerHTML=string;
	return y.innerHTML;
}

function overlay(url) {
	if (!$('shadowbox_overlay')) {
		// kanchiong
		return true;
	} else {
		Shadowbox.open({
			type: 'iframe',
			content: url,
			width: 600,
			height: 440
		});
		return false;
	}
}
