function addScript(data,cache){  
	//if (site.sid != null) data += '&'+site.sid;
	if (cache == true) {
		var cached = site.cache;
		var d = cached + 'site='+site.game +'&url='+encodeURIComponent(data);
		var jsn = new Request.JSON({url: d,
		 onComplete: function(response,text) {
			eval(text);   
		}
	  }).send();
	} else new Asset.javascript(data, {id: 'my_script'});
}
	var C = new Class({
	  Implements: Chain,
	  initialize: function(){
		this.chain.apply(this, arguments);
	  }
	});
	String.implement({
		tweetify: function() {
			return this.replace(/(https?:\/\/\S+)/gi,'<a href="$1">$1</a>').replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>').replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
		}
	});
	Number.implement({
		numberFormat : function(decimals, dec_point, thousands_sep) {
			decimals = Math.abs(decimals) + 1 ? decimals : 2;
			dec_point = dec_point || '.';
			thousands_sep = thousands_sep || ',';
		
			var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
			var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
			return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + 
					(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
		}
	});
	function create_url(mod,data) {
	if (data == '') return site.root + mod + site.ext;
	else {
		var param  = ''
		$H(data).each(function(i,k) {
			i = String.split(i,' ').join('');
			param += site.sep + i;

		});
		return site.root + mod + param + site.ext;
		}
	}
	Array.implement({
		shuffle:function() {
			this.sort(function (x,y) { return Math.floor(Math.random()*3)-1; });
			return this;		
		}
	});
	
var navtail =  function(data) {	$('navtail').set('html',' &gt; <strong>'+data+'</strong>'); }
//servercheck stuff
	function serverCheck2(data) {
		var x = data.data, time = data.date, zone = data.zone, total = data.total;
		
		last = new Date().parse(time);
		var now = new Date();
		var minutes = last.diff(now, 'minute');
		
		//adjusted 
			var localTime = now.get('ms').toInt();
			var localOffset = now.get('gmtoffset').toInt() * 60000;
			var utc = localTime + localOffset;
		
			var b = utc.toInt() + (3600000 * zone.toInt());
			//alert(b);
			//var stime = now.decrement('hour', 2);
			//var nd = new Date().set('timezoneoffset',zone.toInt());
			//alert("time is " + nd.get('date') + "<br>");
		//adjusted
		var t = $('muActions');
		if ($('serverCheck')) var tr = $('serverCheck').empty();
		else var tr = new Element('div',{'id':'serverCheck'}).injectBefore(t);
		
		
	
		
		var h1 = new Element('h5').set('html','Game Server Check').inject(tr);
		var total = new Element('div',{'class':'totalonline'}).set('html','<b>'+total+'</b><label>Total Online</label>').inject(tr);
		var ul = new Element('ul').inject(tr);
		var h2 = new Element('h4').set('html','Last Check: ' +minutes+' minutes').inject(tr);
		$H(x).each(function(v,k) {
			var li = new Element('li');
			var name = new Element('label').set('html',k).inject(li);
			if (v.status == 'up') var status = 'On' + '<dd>'+v.on+'</dd>';
			else var status = 'Offline';
			
			var stat = new Element('b',{'class':v.status}).set('html',status).injectAfter(name);
			
			li.inject(ul);
		
		});
		tr.addEvents({
			'click':function() {
			ul.set('html','Refreshing Data... Please wait a while');
			addScript(site.cache+site.location+'widgets.php?callback=servercheck&nocache=1');
			},
			'mouseenter': function() {
				h2.set('html',last +'<br/>Click on this to Refresh').setStyle('text-align','center');
			},
			'mouseleave':function() {
				h2.set('html','Last Check: ' +minutes+' minutes');		
			}
		});
	}
	//end servercheck
	function iScreen(data) {
		var tr = $('UserPanel'), base = data.base, x= data.images, count = 0,size = 0, ul = new Element('ul',{'id':'iScreen'}).injectAfter(tr);
			
		x.each(function(r) {
			if (count > 5)  return;
			count++;
			if (count > 2) size = 124;
			else size = 210;
			var li = new Element('li',{'class':'iPic'+count});
			var a = new Element('a',{'href':base+r,'target':'_new'}).inject(li);
			var img = new Element('img',{'src':base+'thumbs/'+size+'/'+r}).inject(a);
			var over = new Element('div').inject(li).fade('out');
			li.inject(ul);
			li.addEvents({
				'mouseenter': function() {
					this.tween('border-color','#ff0');
				},
				'mouseleave': function() {
					this.tween('border-color','#444');

				}
			});
		});
		
		var fx = function() {
			if ($('iScreen')) {
				var ul = $('iScreen');
					var timer = 0,size= 0;
					x.shuffle();
					ul.getElements('li').each(function(e,i) {
					(function() {        
							timer+=1;
							if (i > 2) size = 124;
							else size = 210;
						e.getElement('a').set('href',base+x[i]);	
						e.getElement('img').set('src',base+'thumbs/'+size+'/'+x[i]);	
						(function() {  e.tween('border-color','#ff0');
						e.getElement('div').fade('in');          
						}).delay(200); 
						(function() {  
							e.tween('border-color','#aaa');
							e.getElement('div').fade('out'); }).delay(500);        
					}).delay(1500*i);    
				});
			} 

		};
		 
		fx.periodical(18000); 
		
		
	}
	function serverCheck(data) {
		var tr = $('mainBanner');
		if ($$('.frStats')) $$('.frStats').destroy();
		var wrp = new Element('div',{'class':'frStats'}).injectInside(tr);
		var e = $H(data); var timer;
		e.each(function(z,k) {		   
		if (k == 'Server Time') { timer = z;
			z = '<div id="serverTimer">'+z+'</div>';
		}
		if (k == 'zone') return;
				var dv = new Element('div',{'class':k,'html':z}).inject(wrp);
				var labl = new Element('label',{'html':k}).inject(dv);
					
		});
		clock.init(timer,'serverTimer',1000);
	}
	
	
var clock = {
	weekDays : ["SUN","MON","TUE","WED","THU","FRI","SAT"],
	monthNames : ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
	serverDate : {}, // server date obj
	localDate : {}, // local date obj
	dateOffset: {}, // offset ammount
	nowDate : {}, // adjusted date
	dateString : {}, // formated
	el : {}, // element to update
	timeout : {}, // timeout handle
	init : function (date,id,interval) {
		this.calculateOffset(date);
		this.el = document.getElementById(id);
		this.updateClock(interval);
	},
	calculateOffset : function (serverDate) {
		this.serverDate = new Date(serverDate);
		this.localDate = new Date();
		this.dateOffset = this.serverDate - this.localDate;
	},
	updateClock : function (interval) {
		this.nowDate = new Date();
		this.nowDate.setTime(this.nowDate.getTime() + this.dateOffset);
		this.dateFormat(this.nowDate);
		this.el.innerHTML = this.dateString;
		var me = this; this.timeout = setTimeout(function(){me.updateClock(interval)},interval);
	},
	stopClock : function () {
		clearTimeout(this.timeout);
	},
	dateFormat : function (dateObj) {
		this.dateString = '<span>' + this.digit(dateObj.getHours()) + ':' + this.digit(dateObj.getMinutes()) + ':' + this.digit(dateObj.getSeconds()) + '</span>';
		this.dateString += ' ';
		this.dateString += this.monthNames[dateObj.getMonth()] + ' ';
		this.dateString += this.digit(dateObj.getDate()) + ', ';
		this.dateString += dateObj.getFullYear();
	},
	digit : function (str) {
		str = String(str);
		str = str.length == 1 ? "0" + str : str;
		return str;
	}
};
function VoteScript(data) {
	var x = data.data;
	var msg = data.msg;
	var go = data.go;
	if (msg) notify.show({title: 'Vote Rewards Message ',message: msg});
	if ($('vote_space')) {
		var tr = $('vote_space').empty();
		var ul = new Element('ul').inject(tr);
		x.each(function(v) {
			var li = new Element('li');
			var img = new Element('img', {'src':v[4]}).injectInside(li);
			var name = new Element('h2').set('html',v[0]).injectAfter(img);
			var points = new Element('div').set('html','<b>'+v[1]+ '</b> Cash').injectAfter(name);
			var avail =   (v[2].length > 4) ? '' : 'Vote Available';
			var rep = new Element('div').set('html','<b>'+v[2]+'</b> '+avail).injectAfter(points);
			
			li.inject(ul);
			li.addEvents({
				'click': function() {
					rep.set('html','Processing Vote...');
					addScript(site.location+'login.php?callback=vote&id='+v[0]);
				},
				'mouseenter': function() {
					//this.tween('background-color','#121212');			
					
				},
				'mouseleave': function() {
					//this.tween('background-color','#000');			
				},
			
			});
		});
	}
	if (go) window.location = go;

}	
window.addEvent('domready', function(){		
	if ($('MyVote')) {
		
		$$('#MyVote li a').addEvent('click',function(e) {
			if (site.login == 1) {
				e.stop();
				this.set('html','Processing Vote...');
			addScript(site.location+'login.php?callback=vote&id='+this.id);  
			}  
		});
	}
			
	Cufon.replace(['h1','h2','h3'], { fontFamily: 'Trahan Pro' });  
	
  /** tabs **/
$$('ul.tabs').each(function(tabList) {
	var tabContentList = tabList.getNext('ul.tabs-content'), 
		cookie = tabList.get('title'),
		startIndex = Cookie.read(cookie) || 0,
		tabs = tabList.set('title','').getElements('li'), 	
		activeTab = tabs[startIndex].addClass('active'), 
		activeContent = tabContentList.getElements('li')[startIndex].setStyle('height','auto');
	tabs.each(function(tab,i) {
		var content = tabContentList.getElements('li')[i], contentHeight = content.getScrollSize().y;
		tab.addEvent('click',function() {
			if(tab != activeTab) {
				activeTab.removeClass('active');
				(activeTab = tab).addClass('active');
				activeContent.set('tween',{
					onComplete:function() {
						activeContent = content.fade('in').set('tween',{ onComplete: $empty }).tween('height',contentHeight);
					}
				}).setStyles({
					height: contentHeight,
					overflow: 'hidden'
				}).fade('out').tween('height','0');
				Cookie.write(cookie,i);
			}
		});
	});
	activeTab.fireEvent('click');
});
// chains commands

				

var flip = $('page-flip');
	var flipImage = $('page-flip-image');
	var flipMessage = $('page-flip-message');
	
	flip.addEvents({
		mouseenter:function() {
			$$(flipImage,flipMessage).set('morph',{ duration: 500 }).morph({
				width: 307,
				height: 219
			});
		},
		mouseleave:function() {
			flipImage.set('morph',{ duration: 220 }).morph({
				width: 52,
				height: 52
			});
			flipMessage.set('morph',{ duration:200 }).morph({
				width: 50,
				height:50
			});
		}	
	});
	var c = new C(
	//function(){ addScript('http://cdn.wibiya.com/Toolbars/dir_0392/Toolbar_392959/Loader_392959.js'); },
	//function(){ addScript('http://www.mmowebs.com/iscreen/json/tag=ryl,screenshots/count=10/&'+$random(0, 10),true); },
	function(){ addScript(site.location+'widgets.php?callback=servercheck'); }
	  
	);
	c.callChain(); //c.callChain(); //c.callChain();	
	
	
});


function Microsoft_Live_Messenger_PresenceButton_onPresence(data) {
   var id = $(data.id.split('@')[0]).empty().show().removeEvents('click');;
if (data.status == 'Offline') id.hide();
else {  
  id.show();
   var img = new Element('img',{'src':data.icon.url,'align':'top'}).inject(id);
   var name = new Element('span',{'html':data.displayName}).inject(id);
   id.addEvent('click',function() {
         var url = 'http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee='+data.id+'&mkt=en-US' + "&buttonhost=" + document.location.hostname;
         window.open(url, '_blank', 'height=300px,width=300px');

    });
}
	//$('MySupportNote').set('html','Tip: To Reload Support Group / Sales Agent. Just Double-Click Here!');
};

window.addEvent('domready',function() {
	if ($('MySupport')) {
		$('MySupport').addEvents({
			'mouseenter': function() {
				
				var li = this.getElements('span.msnbox');			
				if (this.retrieve('done') != 1) {
					  li.each(function(i) {
							new Asset.javascript('http://messenger.services.live.com/users/'+i.id+'@apps.messenger.live.com/presence?dt=&mkt=en-US&cb=Microsoft_Live_Messenger_PresenceButton_onPresence', {
								
							});
						});
					this.store('done',1);	
					this.getElements('span.mySupportMSG').set('html','');
					li.getParent().show();					
				}
			},
			'dblclick': function() {
				 this.store('done',0);
				this.fireEvent('mouseenter');
			}

		});
	}
});
