/*
	One person behind the tons of coding
	person is DesignCode
	dc-interactive.net
*/

var animateEffects = true;
var animationDurationDef = 0.5;

var E_INVALID_NAME = 'Your Name must be of 4 characters minimum and 20 characters maximum.';
var E_INVALID_USERNAME = 'Username must be of 4 characters minimum and 15 characters maximum and can only have Aphanumeric values (A-Z,a-z,0-9).';
var E_INVALID_PASSWORD = 'Password must be of 5 characters minimum and 15 characters maximum.';
var E_INVALID_PASSWORD2 = 'Both password must be same.';
var E_INVALID_EMAIL = 'Please enter your valid email address.';
var E_INVALID_REQUEST = 'Oops, something went wrong, please refresh the page and retry.';
var E_INVALID_CAPTCHA = 'Please enter the correct verfication string.';



// Effect Engine Wrapper
function effect(effect, obj, callBack, callBackOnHide, forceEffect, callBack2, callBackOnHide2, animationSpeed)
{
	if(empty(callBack))
		callBack = '';
	if(empty(callBack2))
		callBack2 = '';
	if(empty(callBackOnHide))
		callBackOnHide = '';
	if(empty(callBackOnHide2))
		callBackOnHide2 = '';
	if(empty(forceEffect))
		forceEffect = false;
	if(!empty(animationSpeed))
		animationDuration = animationSpeed;
	else
		animationDuration = animationDurationDef;
	
	var effectScope = 'myscope_'+obj.id;
	
	switch(effect)
	{
		
		case 'blind':
			if(empty(animationSpeed))
				animationDuration = 0.4;
			if(obj.visible() || forceEffect == true)
				new Effect.BlindUp(obj, {duration:animationDuration, beforeStart: function(){eval(callBackOnHide)}, queue:{scope:effectScope, position:'end', limit: 1}});
			else
				new Effect.BlindDown(obj, {duration:animationDuration, afterFinish: function(){eval(callBack)}, queue:{scope:effectScope, position:'start', limit: 1}});
		break;
		
		case 'grow':
			if(obj.visible())
				new Effect.Shrink(obj,{duration:animationDuration, beforeStart: function(){eval(callBackOnHide)}, afterFinish: function(){eval(callBackOnHide2)}, direction:'top-left', queue:{scope:effectScope, position:'end', limit: 1}});
			else
				new Effect.Grow(obj,{duration:animationDuration, afterFinish: function(){eval(callBack)}, direction:'top-left', queue:{scope:effectScope, position:'start', limit: 1}});
		break;
		
		case 'grow2':
			if(obj.visible())
				new Effect.Shrink(obj,{duration:animationDuration, beforeStart: function(){eval(callBackOnHide)}, afterFinish: function(){eval(callBackOnHide2)}, queue:{scope:effectScope, position:'end', limit: 1}});
			else
				new Effect.Grow(obj,{duration:animationDuration, afterFinish: function(){eval(callBack)}, queue:{scope:effectScope, position:'start', limit: 1}});
		break;
		
		case 'appear':
			if(obj.visible())
				new Effect.Fade(obj,{duration:animationDuration, beforeStart: function(){eval(callBackOnHide)}, afterFinish: function(){eval(callBackOnHide2)}, direction:'top-left', queue:{scope:effectScope, position:'end', limit: 1}});
			else
				new Effect.Appear(obj,{duration:animationDuration, afterFinish: function(){eval(callBack)}, direction:'top-left', queue:{scope:effectScope, position:'start', limit: 1}});
		break;
		
		case 'highlight':
			new Effect.Highlight(obj,{duration:animationDuration, startcolor:'#ff0000' ,queue:{scope:effectScope, position:'start', limit: 1}});
		break;
		
	}
	animationSpeed = null;
}



function isIE6()
{
	version = navigator.appVersion.match(/MSIE (\d)/);
	if(version)
		return (version[1] == 6);
	else
		return false;
}

function isIEShit()
{
	version = navigator.appVersion.match(/MSIE (\d)/);
	if(version)
		return version[1];
	else
		return false;
}

function isAlphaNumeric(str)
{
	if(empty(str))
		return false;
	
	for(var j=0; j<str.length; j++)
	{
		var alpha = str.charAt(j);
		var hh = alpha.charCodeAt(0);
		if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
			return true;
		else
			return false;
	}
	return true;
}

function empty(str)
{
	if(str == undefined || str == '')
		return true;
	return false;
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}

function validateEmail(str)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(str) == false) {
		return false;
	}
	return true;
}

function validateURL(str)
{
	var http = /http(s)?:\/\/[A-Za-z0-9\.-]{1,}\.[A-Za-z]{2}/;
	if(!http.test(str))
		return false;
	return true;
}

function selectAll(obj)
{
	obj = $(obj);
	obj.focus();
	obj.select();
}

function markInputField(field, text)
{
	if(text)
		alert(text);
	field.focus();
	effect('highlight', field);
	return false;
}

function validateName(str)
{
	if(str.length > 20 || str.length < 4)
		return false;
	
	return true;
}

function validateUsername(str)
{
	if(!isAlphaNumeric(str))
		return false;
	if(str.length > 15 || str.length < 4)
		return false;
	
	return true;
}

function validatePassword(str)
{
	if(str.length < 5 || str.length > 15)
		return false;
	
	return true;
}



function handleGarbage(hide, force) {
	var garbage = new Array();
	if(isIE6())
		garbage = ['SELECT', 'EMBED', 'OBJECT']
	else if(Prototype.Browser.Opera)
		garbage = ['EMBED', 'OBJECT']
	else if(isIEShit() == 8)
		garbage = []
	else if(force)
		garbage = ['SELECT', 'EMBED', 'OBJECT']
	
	if(garbage.length > 0) {
		garbage.each(function (tag) {
			ele = document.getElementsByTagName(tag);
			$A(ele).each(function (item) {
				if(hide)
					$(item).setStyle({'visibility':'hidden'});
				else
					$(item).setStyle({'visibility':'visible'});
			});
		});
	}
}

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		if (cookie_name == name)
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function deleteCookie(name, path, domain) {
	if(getCookie(name))
		document.cookie = name + "=" + ((path) ? ";path=" + path : "") + (( domain ) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function showFav()
{
	if(!isLoggedIn())
		return true;
	
	new myWindow({title: 'My Favorites', width: 660, url: '/ajax/fav.php', params: 'action=getFavListBig&skip=true&uid=&type=1&page=1&ele=GB_content&mp=true'});
	return false;
}

var lastFavPage = new Array();
function loadFav(uid, type, page, ele, mp)
{
	if(page == lastFavPage[type])
		return;
	
	if(empty(mp))
		mp = '';
	
	try {
		$(ele+'_holder').startWaiting('bigWaiting');
	} catch (e) {
		$(ele).startWaiting('bigWaiting');
	}
	
	new Ajax.Request("/ajax/fav.php", {
		onSuccess : function(r) {
			$(ele).update(r.responseText);
			try { $(ele+'_holder').stopWaiting(); } catch (e) {}
			$(ele).stopWaiting();
		},
		method: 'post',
		parameters: 'action=getFavListBig&skip=true&uid='+uid+'&type='+type+'&page='+page+'&ele='+ele+'&mp='+mp
	});
	lastFavPage[type] = page;
}

function showRSSBox(ele)
{
	var rssDiv = $('global_rss_div');
	effect('blind', rssDiv);
	//rssDiv.toggle();
	return false;
}

function isLoggedIn()
{
	return IS_USER_LOGGED_IN;
}

function askLogin(simple)
{
	if(!empty(simple)) {
		str = 'In order to perform this operation, Please login first.';
		alert(str);
		return false;
	}
	
	var ALF = $('ALF_container');
	
	var pageSize = getPageSize();
	var pageScroll = getPageScroll();
	
	var ALF_left = parseInt(((pageSize[0] / 2) + pageScroll[0]) - (ALF.getWidth() / 2));
	var ALF_top = (pageScroll[1] + (pageSize[3] / 2)) - (ALF.getHeight() / 2);
	
	if(ALF_top < 10)
		ALF_top = 10;
	
	ALF.setStyle({'left': ALF_left+'px', 'top': ALF_top+'px'});
	
	
	if(ALF.visible()) {
		effect('highlight', ALF);
		return false;
	}
	
	effect('grow2', ALF, '', '', '', '', '', 0.3);
	
	new Draggable('ALF_container',{
		handle:'ALF_header',
		starteffect:function(){
			return;
		},
		endeffect:function(){
			return;
		}
	});
	
	return false;
}

function hideLogin()
{
	effect('grow2', $('ALF_container'), '', '', '', '', '', 0.3);
}

function go(url)
{
	window.location = url;
}

function reloadCaptcha(obj, name)
{
	$(obj).ancestors().first().descendants().first().src = '/captcha/reload/'+name+'.png?rand='+Math.random();
	$(obj).ancestors().first().descendants().last().value = '';
}





function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.bclientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	var pageWidth;
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll);
	return arrayPageScroll;
}

var Category = Class.create();

Category.prototype = {
	
	initialize: function() {
		this.request = false;
		this.id = null;
		this.lastid = null;
		this.holder = $('module_categories');
		this.duration = 0.3;
	},
	
	load: function(id) {
		//alert(this.lastid);
		this.id = id;
		
		if(!this.request) {
			if(this.lastid != id) {
				if(this.holder.visible()) {
					this._load();
				}
				else {
					new Effect.BlindDown(this.holder, {
						duration: this.duration,
						queue:{scope:'load_category_'+this.id, position:'start', limit: 1},
						afterFinish: function() {
							this._load();
						}.bind(this)
					});
				}
				this.lastid = id;
			}
			else {
				this.request = true;
				new Effect.BlindUp(this.holder, {
					duration: this.duration,
					queue:{scope:'load_category_'+this.id, position:'start', limit: 1},
					afterFinish: function() {
						this.request = false;
					}.bind(this)
				});
				this.lastid = null;
			}
		}
		
		return false;
	},
	
	_load: function() {
		this.request = true;
		this.holder.update('');
		this.holder.startWaiting();
		new Ajax.Request('/ajax/categorylist.php', {
			method: 'post',
			parameters: 'moduleId='+this.id,
			onSuccess : function(r) {
				this.request = false;
				this.holder.update(r.responseText);
				this.holder.stopWaiting();
			}.bind(this)
		});
	},
	
	shut: function() {
		new Effect.BlindUp(this.holder, {
			duration: this.duration,
			queue:{scope:'load_category_'+this.id, position:'start', limit: 1},
			afterFinish: function() {
				this.request = false;
				this.lastid = null;
			}.bind(this)
		});
	}
}



var currentCategory;
var categoryRequest = false;
function showCatgory(i)
{
	var holder = $('module_categories');
	
	if(!holder.visible() || i == currentCategory)
		effect('blind', holder, "$('module_categories').startWaiting()", "$('module_categories').stopWaiting()");
	
	if(!categoryRequest) {
		categoryRequest = true;
		if(i != currentCategory) {
			holder.update('');
			if(holder.visible())
				$('module_categories').startWaiting()
			new Ajax.Request("/ajax/categorylist.php", {
								onSuccess : function(resp) {
									holder.update(resp.responseText);
									holder.stopWaiting();
									categoryRequest = false;
								},
								method: 'post',
								postBody: 'moduleId='+i
								});
		}
		currentCategory = i;
	}
	return false;
}

function showCatgoryCB()
{
	
}