/****************************************

	GamesRogue Javascript Library
	Author: Shaun Shull
	Contact: shaunshull #at# gmail dot com

 ***************************************/
 
 /* HOME PAGE */
 
 function buildPromo(arr,page) {
 	page = typeof(page) != 'undefined' ? page : 1;
 	window.promo = page;
 	if (typeof(arr) != 'undefined') {
		var pdiv = document.getElementById('promo');
		var promo = arr[page-1];
		var str = '';
		str += '<div class="promoimg"><a href="'+promo['link']+'" onmouseover="clearInterval(promocycle)" onmouseout="resetPromoCycle()"><img src="'+promo['img']+'" alt="" /></a></div>';
		str += '<div><table><tr><td class="promotxt">'+promo['title']+'</td><td class="promonav">';
		for (var i=0;i<arr.length;i++) {
			(i==(page-1)) ? str += '<img width="18" height="20" src="/img/mton.gif" alt="" />' : str += '<a href="javascript:promoClick('+(i+1)+')"><img width="18" height="20" src="/img/mtoff.gif" alt="" onmouseover="this.src=\'/img/mtover.gif\'" onmouseout="this.src=\'/img/mtoff.gif\'" /></a>';
		}
		str += '</td></tr></table></div>';
		pdiv.innerHTML = str;
 	}
 }
 
 function updatePromo() {
 	if (typeof(window.promo) != 'undefined') {
 		var page = window.promo;
 		(page!=promos.length) ? buildPromo(promos,page+1) : buildPromo(promos,1);
 	}
 }
 
 function resetPromoCycle() {
 	if (promocycle) clearInterval(promocycle);
 	promocycle = setInterval('updatePromo()', 5000);
 }
 
 function promoClick(page) {
 	resetPromoCycle();
 	buildPromo(promos,page);
 }
 
 /* MULTI-USE */
 
 function createTbox(menu, chosen, container, mcount) {
 	if (!mcount) mcount = '';
 	var str  = '<div class="tbox">';
 	str += '<ul>';
 	for (var i=0; i<menu.length;i++) {
 		if (i == chosen) { var mystyle = ' class="on"'; } else { var mystyle = ''; };
 		str += '<li'+mystyle+'><a href="javascript:createTbox(menu'+mcount+','+i+',\''+container+'\',\''+mcount+'\')">'+menu[i]['nav']+'</a></li>';
 	}
 	str += '</ul>';
 	str += '<div class="tboxcontent" id="tboxcontent">'+menu[chosen]['content']+'</div>';
 	str += '</div>';
 	var div = document.getElementById(container);
 	div.innerHTML = str;
 }
 
 function createTboxAjax(menu, chosen, container, mcount) {
 	if (!mcount) mcount = '';
 	var str  = '<div class="tbox">';
 	str += '<ul>';
 	for (var i=0; i<menu.length;i++) {
 		if (i == chosen) { var mystyle = ' class="on"'; } else { var mystyle = ''; };
 		str += '<li'+mystyle+'><a href="javascript:createTboxAjax(menu'+mcount+','+i+',\''+container+'\',\''+mcount+'\')">'+menu[i]['nav']+'</a></li>';
 	}
 	str += '</ul>';
 	str += '<div class="tboxcontent" id="tboxcontent"></div>';
 	str += '</div>';
 	
 	if(menu[chosen]['id'])
 	{
	 	// use id given
 		new Ajax.Updater('tboxcontent', menu[chosen]['ajax_url'], { method: 'get', parameters: {id: menu[chosen]['id'], section: menu[chosen]['ajax_section']} });
	}
	else if(menu[chosen]['platform_id'])
 	{
	 	// use id given
 		new Ajax.Updater('tboxcontent', menu[chosen]['ajax_url'], { method: 'get', parameters: {platform_id: menu[chosen]['platform_id'], section: menu[chosen]['ajax_section']} });
	}
	else
	{
		new Ajax.Updater('tboxcontent', menu[chosen]['ajax_url'], { method: 'get', parameters: {section: menu[chosen]['ajax_section']} });
	}
 	
 	var div = document.getElementById(container);
 	div.innerHTML = str;
 }
 
 
	// resizeBlockTo( int cur_width, int cur_height, int orig_width, int orig_height, int to_size ) */
	// usage: resizeBlockTo( this, 480, 600, 300 )
	function resizeBlockTo( myblock, orig_width, orig_height, to_size )
	{
		if ( myblock.width < orig_width )
		{
			// resize back to default
			myblock.width = orig_width;
			myblock.height = orig_height;
		}
		else
		{
			// resize smaller
			if ( orig_width >= orig_height )
			{
				myblock.width = to_size;
				myblock.height = Math.floor( ( to_size * ( orig_height / orig_width ) ) );
			}
			else
			{
				myblock.height = to_size;
				myblock.width = Math.floor( ( to_size * ( orig_width / orig_height ) ) );
			}
		}
		
		return;
	}
	
	function addFavorite( type, myid )
	{
		new Ajax.Request('/ajax/favorite.php', { method: 'get', parameters: {type: type, add: myid}, onCreate: function(){ $('add_favorite_link').hide(); $('add_favorite_loading').show(); }, onSuccess: function(){ $('add_favorite_link').hide(); $('add_favorite_loading').hide(); $('add_favorite_added').show(); }, onFailure: function(){ $('add_favorite_link').hide(); $('add_favorite_loading').hide(); $('add_favorite_added').hide(); $('add_favorite_fail').show(); } } );
	}
	
	function removeFavorite( type, myid, blockid )
	{
		new Ajax.Request('/ajax/favorite.php', { method: 'get', parameters: {type: type, remove: myid}, onCreate: function(){ $('remove_favorite_link_'+blockid).hide(); $('remove_favorite_loading_'+blockid).show(); }, onSuccess: function(){ $('remove_favorite_link_'+blockid).hide(); $('remove_favorite_loading_'+blockid).hide(); $('remove_favorite_added_'+blockid).show(); }, onFailure: function(){ $('remove_favorite_link_'+blockid).hide(); $('remove_favorite_loading_'+blockid).hide(); $('remove_favorite_added_'+blockid).hide(); $('remove_favorite_fail_'+blockid).show(); } } );
	}
	
	function addLike( type, typeid )
	{
		new Ajax.Request('/ajax/like.php', { method: 'get', parameters: {type: type, add: typeid}, onSuccess: function(){ $('content_like').update("Added!"); } } );
	}
	
	function removeLike( type, typeid )
	{
		new Ajax.Request('/ajax/like.php', { method: 'get', parameters: {type: type, remove: typeid}, onSuccess: function(){ $('content_like').update("Removed!"); } } );
	}
	
	function rateComment( comment_id, type )
	{
		new Ajax.Request('/ajax/comment-rate.php', { method: 'get', parameters: {id: comment_id, type: type}, onCreate: function(){ $('comment_rate_'+comment_id).replace("<small>Thanks!</small>"); }, onSuccess: function(){ $('comment_rate_'+comment_id).replace("<small>Thanks!</small>"); }, onFailure: function(){ $('comment_rate_'+comment_id).replace("<small>Error!</small>"); } } );
	}
	
	
	
	var pics=new Array();
var stat=new Array();
var pic=new Array();

function changepic(i,code)
{ var loop=0;

	if (stat[code])
	{
		while ((pics[code][i]==0 || i>=7) && loop<100)
		{ if (i>=7) { i=0; }
			else
			{ i++;
			}		
			loop++;
		}
		
		if (pic[code][i].complete)
		{	document.getElementById(code).src=pic[code][i].src;
			setTimeout("changepic("+(i+1)+",'"+code+"')",500);
		}
		else
		{	setTimeout("changepic("+i+",'"+code+"')",20);
		}
	}
	
	
}

function loadpic(url,code,j)
{	if (stat[code]) { pic[code][j].src=url; }
}

function startm(code,ta,te)
{	stat[code]=1;
	var jj, jjj;
	var first=1;
	
	for(var j=0;j<7;j++)
	{ if (pics[code][j]==1)
		{ pic[code][j]=new Image();
			jj=j+1;
			//if (jj>=100) { jjj=""+jj; }
			//if (jj<100 && jj>=10) { jjj="0"+jj; }
			//if (jj<10) { jjj="00"+jj; }
			//if (first) { first=0; loadpic(ta+jjj+te,code,j); }
			//else { setTimeout("loadpic('"+ta+jjj+te+"','"+code+"',"+j+")",j*50); }
			if (first) { first=0; loadpic(ta+jj+te,code,j); }
			else { setTimeout("loadpic('"+ta+jj+te+"','"+code+"',"+j+")",j*50); }
		}
	}
	changepic(0,code);
}

function endm(code)
{	stat[code]=0;
}
