//////////////////////////////////////////////////////////////////
// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com
//
// Multi-tag support by James Crooke
// http://www.cj-design.com
//
// Inspired by code from Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
//////////////////////////////////////////////////////////////////

var qTipTag = "div,img,label"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipX = 0; //This is qTip's X offset//
var qTipY = 15; //This is qTip's Y offset//

var qClass = "qtip";

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) { document.onmousemove = function (evt) {tooltip.move (evt)}; }

	var a, sTitle, elements;
	var elementList = qTipTag.split(",");
	for(var j = 0; j < elementList.length; j++)
	{	
		elements = document.getElementsByTagName(elementList[j]);
		if(elements)
		{
			for (var i = 0; i < elements.length; i ++)
			{
				a = elements[i];
				sTitle = a.getAttribute("title");
				
				if (a.className) elemClass = a.className;
				else if (a.getAttribute("class")) elemClass=a.getAttribute("class");
				else elemClass = null;
				
				if (elemClass == null) {
					continue;
				}
				
				if(sTitle && elemClass.indexOf(qClass) != -1)
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function() {tooltip.hide()};
				}
			}
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

// 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.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.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");
			}
		}
	}
});

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, cp) {
		this.grab(this.url + "?m=del&comid=" + cid + "&u=" + u + "&id=" + id + "&cp=" + cp);
	},
	
	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 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 themes_changeModel2(uri, key, value, time, quality, categoryid, region, showflag) {
	var x = $('themes-selectmodel');
	var y = uri + '?rg=' +region+ '&tk=' + key + '&tv=' + value + '&tt=' + time + '&tq=' + quality + '&tp=1&tc=' + categoryid + '&tm=' +showflag;
	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 = "skn";
	
	if (type == "skn") {
		location.href='theme?tm=&tv=' + k;
	}
	
	else if (type == "fla") {
		location.href='flash_lite_slide?ts=' + k;
	}

	else if(type == "gif") {
		location.href='gif?tk=ms&tv=' + k;
	}
	
	else if (type == "cfd") {
		location.href='classified_svalue_listings.oss?tk=mc&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;
	}
}

function clickOsChat(event) {
	if ( $('oschat-wrap').style.display == "none" || $('oschat-wrap').style.display == "") {
		$('oschat-wrap').style.display = "block";
		//startOsChat(arguments[1],arguments[2]);
	}
	
	else if ( $('oschat-wrap').style.display == "block" ) {
		$('oschat-wrap').style.display = "none";
		//stopOsChat();
	}
}

var poller = null;
var pollerad = null;

var hintidx = 0;
var hintTxt = new Array(3);
hintTxt[0] = "<small style='color: red;'>Hint: type <b>*username message</b> to whisper to someone!</small>";
hintTxt[1] = "<small style='color: green;'>Hint: type <b>-username</b> to ignore someone!</small>";
hintTxt[2] = "<small style='color: blue;'>Hint: type <b>+username</b> to un-ignore someone!</small>";

function startOsChat2direct(l, u, k) {
	$('oschat-wrap').style.display = "block";
	startOsChat2("1", l, u, k);
}
function startOsChat2(stage, l, u, k) {

	if(stage=="0") { stopOsChat(); }
	$('oschatfix').style.display = "none";
	$('oschat-wrap').style.width = "670px";
	$('oschat-window').style.height = "590px";
	$('oschat-input').style.display = "none";
	$('chat2btn').style.display = "none";
	$('chatxitbtn').onclick = new Function("OsChat2exit();");

	$('oschat-window').innerHTML = "<iframe frameborder='0'  width='670px' height='590px' marginwidth='2px' marginheight='2px' src='chat2_start.oss?l="+l+"&u="+u+"&k="+k+"'></iframe>";
	
}
function OsChat2exit() {
	$('oschat-window').innerHTML = "";
	$('oschat-wrap').style.width = "";
	$('oschat-window').style.height = "";
	$('oschat-input').style.display = "";
	$('chat2btn').style.display = "";
	$('chatxitbtn').onclick = new Function("clickOsChat();return false;");
	$('oschat-wrap').style.display = "none";
}


function startOsChat(langtype) {
	startOsChat(langtype,3);
}

function startOsChat(langtype,refreshtimer) {

	$('oschat-window').scrollTop = $('oschat-window').scrollHeight;
	$('oschat-message').focus();
	if ( poller == null) {
		//pollerad = new Ajax.PeriodicalUpdater('oschat-ad', 'aj-chat.oss?ad=1', { method: 'get', frequency: 30});
		poller = new Ajax.PeriodicalUpdater('oschat-window', 'aj-chat.oss?lang='+langtype, 
				{ method: 'get', frequency: refreshtimer,
					onCreate: function(transport) {
						$('oschat-load').style.visibility="visible";
						$('oschat-load').innerHTML = "<img src='img/ajax-small.gif' />";
					}, 
					onSuccess: function(transport) {
						//$('oschat-load').style.visibility="hidden";
						$('oschat-load').style.visibility="visible";
						$('oschat-load').innerHTML = hintTxt[(hintidx++)%3];
						//if(transport.responseText.length() > 0) {
							$('oschat-window').innerHTML = transport.responseText;
						//}
						$('oschat-window').scrollTop = $('oschat-window').scrollHeight;
					},
					onFailure: function(transport) {
						//$('oschat-load').style.visibility="hidden";
						$('oschat-load').style.visibility="visible";
						$('oschat-load').innerHTML = hintTxt[(hintidx++)%3];
						$('oschat-window').innerHTML = $('ajax-error').innerHTML;
						poller.stop();
						//pollerad.stop();
					}
				});
		
	} else {
		poller.start();
		//pollerad.start();
	}
}

function stopOsChat() {
	if(poller != null) 
		poller.stop();
	//pollerad.stop();
}

function sendOsChat(langtype) {
	sendOsChat(langtype,10);
}

function sendOsChat(langtype, cooltimer) {
	var msg = $('oschat-message').value;
		
	if (isArabic(msg)) {
		// stop all the polling, reset them, and start polling for arabic
		// this is purely cosmetic, as we have server side checking too.
		langtype = "ar";
		stopOsChat();
		pollerad = null;
		poller = null;
		startOsChat(langtype);
	}
	
	if (trim(msg) == "") {
		return;
	}
	
	var url = "aj-chat.oss?lang="+langtype;

	$('oschat-load').style.visibility="visible";
	new Ajax.Request(url,
					{ method: 'post', parameters: { msg: msg},
						onSuccess: function(transport) {
							new Ajax.Updater('oschat-window', url, 
										{method: 'get',
											onSuccess: function(transport) {
												$('oschat-load').style.visibility="hidden";
												$('oschat-message').value="";
												$('oschat-window').value = transport.responseText;
												$('oschat-window').scrollTop = $('oschat-window').scrollHeight;
												$('oschat-form').disable();
												coolDown(cooltimer);
											}
										});
						}
					});
}
function trim(s) {
	return s.replace(/^\s+|\s+$/g,"");
}

function coolDown(i) {

	if (i == 0) {
		$('oschat-message').value = "";
		$('oschat-form').enable();
		$('oschat-message').focus();
	}
	else if (i > 0) {
		$('oschat-message').value = i;
		i--;
		setTimeout("coolDown("+i+")", 1000);
	}
}

function isArabic(s) {
	for(i=0; i<s.length; i++) {
		var c = s.charCodeAt(i);
		if((c >= 1536 && c <= 1791) || (c >= 1872 && c <= 1919)) {
			return true;
		}
	}
	return false;
}

function gif_changeModel(uri, key, value, time, quality, categoryid) {
	var x = $('gif-selectmodel');
	var y = uri + '?tk=' + key + '&tv=' + value + '&tt=' + time + '&tq=' + quality + '&tp=1&tc=' + categoryid + "&tb=" + gifbrand + "&tr=";

	if (x.value == "") {
		return false;
	}

	location.href= y + x.value + "&tm=" + x.options[x.selectedIndex].innerHTML;
}

gifmodels = new Hash();
gifbrand = "";
gifmodel = "";
function gif_changeBrand(b, m){
	gifbrand = b;
	
	if (gifmodels.get(b) != null) {
		$('gif-selectmodel').innerHTML = gifmodels.get(b);
		return;
	}
	
	if (m == null) m = "";
	
	$('gif-model-load').innerHTML = $('ajax-loading').innerHTML;
	$('gif-model-load').style.display = "block";
	$('gif-selectmodel').style.display = "none";
	
	new Ajax.Request('aj-gifmodel.oss', {
		parameters: { brand: b, model: m },
		method: 'get',
		onSuccess: function(transport) {
			gifmodels.set(b, transport.responseText);
			$('gif-model-load').style.display="none";
			$('gif-selectmodel').style.display="block";
			$('gif-selectmodel').innerHTML = transport.responseText;
		},
		onFailure: function(transport) {
			$('gif-model-load').innerHTML = $('ajax-error').innerHTML;
		}
	});
}


function games_changeModel(uri, key, categoryid, value) {
	var x = $('themes-selectmodel');
	var y = uri + '?tk=' + key + '&tp=1&tc=' + categoryid + '&tm=' + x.value + '&tv=' + value;
	location.href= y;
}

function games_detailchangeModel(uri, apid) {
	var x = $('themes-selectmodel');
	var y = uri + '?t=' + apid + '&tm=' + x.value + '&cm=y';
	location.href= y;
}

function ConfirmDelete(msg){
    var ans = confirm(msg);
    if (ans == true || ans == 1) { 
        return true;
    }
    return false; 
}

var state = 'none'; 
function ShowHideContent(layer_ref) { 
	if (state == 'block') { state = 'none'; } else { state = 'block'; } 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval( "document.all." + layer_ref + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[layer_ref].display = state; 
	} 
	if (document.getElementById &&!document.all) { 
		hza = document.getElementById(layer_ref); 
		hza.style.display = state; 
	}

	if(state == 'block') {
		document.getElementById("limit-skininfo").style.display = 'none';
	}
	if(state == 'none') {
		document.getElementById("limit-skininfo").style.display = '';
	}
} 