/*
###########################################
	My Screenshots functionality
###########################################
*/

var function_lock 	=  0;
var current_sid 	= -1;
var current_id  	= -1;

function renameScreenshot(i) 
{
	
	if (function_lock == 1)
		return;
		
		current_id	= i;
		current_sid	= screens_list[i][0];
	var name 		= screens_list[i][1];
	
	document.getElementById('h'+i).className = 'thumbName2';
	document.getElementById('n'+i).innerHTML = '<input onBlur="renameScreenshot2();" type="text" value="'+name+'" id="rename" '+
	'style="padding: 0px; border: 0px; background-color: transparent; text-align: center; font-weight: bold; width:118px; font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;">';
	document.getElementById('rename').focus();
	document.getElementById('rename').select();
	document.getElementById('rename').onkeypress = renameScreenshot3;

}

function renameScreenshot2()
{
	
	var new_name 	= document.getElementById('rename').value;

	if ( new_name == '' )
		new_name = 'Untitled';
		
	document.getElementById('h'+current_id).className = 'thumbName';
	
	if ( screens_list[current_id][1] == new_name ) 
	{
		document.getElementById('n'+current_id).innerHTML = new_name;
		return;
	}
	
	screens_list[current_id][1] = new_name;
	document.getElementById('n'+current_id).innerHTML = 'Renaming...';
	function_lock = 1;
	
		
	activeUrl('/myscreens/?a=rename&sid='+current_sid+'&newname='+escape(new_name)+'&session='+g_session);

}

function renameScreenshot3(e)
{
	
	if (!window.event)
        Key = e.which;
    else
        Key = window.event.keyCode;
    if (Key == 13) 
		renameScreenshot2();
     
}

function deleteScreenshot(i)
{
	
	if (function_lock == 1)
		return;
	
	current_id = i;
	var conf = confirm('Are you sure you want to delete this Screenshot ('+screens_list[current_id][1]+')?\nNote: This operation CANNOT be undone.');
	if (conf) 
	{
		document.getElementById('n'+current_id).innerHTML = 'Deleting...';
		if (current_id == i)
			closeModifyBox();
		activeUrl('/myscreens/?a=delete&sid='+screens_list[current_id][0]+'&session='+g_session);
	}

}

var mod_box_height = 0;
var mod_box_max_height = 162;
var mod_box;
var mod_box_timeout;
var mod_box_timeout2;
var mod_box_catdata = '';

function modifyAreaSetup() 
{
	
	var mod_area = document.getElementById('mod_area');
	
	if(mod_box_catdata=='')
	{	
	
		mod_box_catdata += '<option value="0">No Category</option>';
		
		for(i=0;i<categories.length;i++) 
		{
			indent = '';
			for(k=0;k<categories[i][2];k++) indent+='&nbsp; ';
			mod_box_catdata += '<option value="'+(categories[i][3]==1?categories[i][1]:0)+'" style="background-color:'+categories[i][4]+'">'+indent+categories[i][0]+'</option>';
		}
	
		mod_area.innerHTML = 
			'<div id="mod_box" style="overflow:hidden; height:0px"><div id="header">'+
				'<span>Modify Screenshot</span>'+
			'</div>'+
			'<div id="content">'+
				'<table>'+
				
					'<tr><td nowrap>Name:</td>'+
					'<td colspan="2"><input id="mod_name2" '+
					'style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; width:150px;" type="text" name="name" value="" />'+
					'</td></tr>'+
					
					'<tr><td nowrap>Category:</td><td>'+
					'<select id="mod_cat2" style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; width:154px;">'+
					mod_box_catdata+
					'</select></td><td width="100%" style="vertical-align:bottom; padding:0 0 2px 3px;">'+
					'<a href="/ctcode/" target="_blank">'+
						'Enhance your message with CT Code!' +
					'</a>' +
					'</td></tr>'+
				
				'<tr><td nowrap>Description:</td><td colspan="2">'+
				'<textarea id="mod_desc2" style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; width:400px; height:65px"></textarea></td></tr>'+
				'<tr><td></td><td colspan="2" nowrap>'+
				'<input id="mod_submit" type="button" style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; " value="Modify Screenshot" '+
				'onClick="modifyScreenshot2(); closeModifyBox()">'+
				'<input type="button" style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; " value="Cancel" '+
				'onClick="closeModifyBox()"></td></tr>'+
				'</table></div>'+
			'</div>';
			
	}	
	
}

function modifyScreenshot(i)
{
	
	if (function_lock == 1)
		return;
	
	if (mod_box_height > 0) 
	{
		closeModifyBox();
		setTimeout('modifyScreenshot('+i+')',48);
		return;
	}
	
	if (current_id == i) 
	{ 
		current_id = -1; 
		return;
	}
	
	window.location='#top';
	
	current_id = i;
	var desc = screens_list[current_id][4];
	
	mod_box 	= document.getElementById('mod_box');
	mod_cat 	= document.getElementById('mod_cat2');
	mod_name 	= document.getElementById('mod_name2');
	mod_desc 	= document.getElementById('mod_desc2');
	
	mod_name.value 	= screens_list[current_id][1];
	mod_desc.value 	= desc.replace(/<br>/g,"\r\n");
	
	if(mod_cat.selectedIndex)
		mod_cat.selectedIndex=0;
	
	for (i=0;i<mod_cat.length;i++)
	{
		if (mod_cat.options[i].value == screens_list[current_id][3])
		{
			mod_cat.selectedIndex = i; 
			break;
		}
	}
	
	openModifyBox();

}

function modifyScreenshot2() 
{

	function_lock = 1;
	activeUrl('/myscreens/?a=modify&sid='+screens_list[current_id][0]+
			  '&newname='+escape(document.getElementById('mod_name2').value)+
			  '&cid='+escape(document.getElementById('mod_cat2').value)+
			  '&desc='+escape(document.getElementById('mod_desc2').value)+
			  '&session='+g_session);
	
}

function openModifyBox()
{
	
	if (mod_box_height < mod_box_max_height)
	{
		mod_box_height = mod_box_height + (parseInt((mod_box_max_height-mod_box_height)>>2)+2);
		mod_box.style.height = mod_box_height + 'px';
		clearTimeout(mod_box_timeout);
		mod_box_timeout = setTimeout('openModifyBox()',52);
	}

}

function closeModifyBox()
{
	
	if (mod_box_height > 0)
	{
		mod_box_height = mod_box_height - (parseInt((mod_box_height)>>1)+2);
		if(mod_box_height<0) mod_box_height=0;
		mod_box.style.height = mod_box_height + 'px';
		clearTimeout(mod_box_timeout);
		mod_box_timeout = setTimeout('closeModifyBox()',52);
	}

}

/*
###########################################
	Screenshot listing functionality
###########################################
*/

var g_myscreens 			=  0;
var g_width 				=  0;
var g_cid	 				= -1;
var g_last_row_size 		= -1;
var g_screens_searchbox 	= '';
var g_screens_sorttype	 	= '';
var g_screens_navigation 	= '';
var g_screens_array_index 	=  0;
var g_screens_array 		= Array(0);

var g_ranking = Array('Not Yet Ranked','1 out of 5 Stars','2 out of 5 Stars','3 out of 5 Stars','4 out of 5 Stars','5 out of 5 Stars!');



// Create the list data
function createScreenList(screens_list, div_id, option) {
	
	var length 			= (screens_list.length - 1);
	var screens_data 	= Array(0);
	
	if ( g_cid != -1 ) 
	{
		
		var hrefstart = '?cid='+g_cid+(g_search!=''?'&search='+g_search:'');
		var out = '<table width="100%"><tr><td width="50%" nowrap><b>Page ' + g_current_page + ' of ' + (g_max_page==0?1:g_max_page) + '</b> ';
		
		if ( g_current_page - 3 > 0 ) out += '<a href="'+hrefstart+'&page=0">&laquo; First</a> ... ';
		if ( g_current_page - 1 > 0 ) out += '<a href="'+hrefstart+'&page='+(g_current_page-1)+'">&laquo;</a> ';
		if ( g_current_page - 2 > 0 ) out += '<a href="'+hrefstart+'&page='+(g_current_page-2)+'">' + (g_current_page - 2) + '</a> ';
		if ( g_current_page - 1 > 0 ) out += '<a href="'+hrefstart+'&page='+(g_current_page-1)+'">' + (g_current_page - 1) + '</a> ';
		
		out += '<b>[' + g_current_page + ']</b> ';
		
		if ( g_current_page + 1 <= g_max_page ) out += '<a href="'+hrefstart+'&page='+(g_current_page+1)+'">' + (g_current_page + 1) + '</a> ';
		if ( g_current_page + 2 <= g_max_page ) out += '<a href="'+hrefstart+'&page='+(g_current_page+2)+'">' + (g_current_page + 2) + '</a> ';
		if ( g_current_page + 1 <= g_max_page ) out += '<a href="'+hrefstart+'&page='+(g_current_page+1)+'">&raquo;</a> ';
		if ( g_current_page + 3 <= g_max_page ) out += '... <a href="'+hrefstart+'&page='+g_max_page+'">Last &raquo;</a> ';
		
		g_screens_navigation = out;
		
		g_screens_sorttype = 
			'</td><td width="50%" align="right" nowrap><b>Sort by: </b>'+
			( g_sort_by == 0 ?
			 '[Date] <a href="'+hrefstart+'&sort=1">Name</a>' :
			 '<a href="'+hrefstart+'&sort=0">Date</a> [Name]'
			 );
			
		g_screens_searchbox = 
			'<table width="100%"><tr><td>' +
			'<b>Search:</b> ' +
			'<input type="text" value="'+g_search+'" id="search" style="font-size:14px"/ onFocus="this.onkeypress = checkSearch;" /> ' +
			'<input type="button" value="Search" style="font-size:14px;" onClick="window.location=\'?cid='+g_cid+'&search=\'+document.getElementById(\'search\').value;" />' +
			'</td></tr></table>';
		
			
		
		
	}
	
	if ( g_myscreens == 0 )
	{ 
		for ( var i = 0; i < length; i++ ) 
		{
			rating_rounded = Math.round(screens_list[i][2]);
			screens_data[i] = '<table><tr><td style="padding:3px 0 7px 0;"><div class="thumbView">' + 
			'<table cellpadding=0 cellspacing=0 width="152" height="150"><tr>'+
			'<td background="/t/'+screens_list[i][0]+'.jpg" style="background-position:center; background-repeat: no-repeat" nowrap>' + 
			'<div class="thumbName" nowrap title="'+screens_list[i][1]+'">' +
			'<div class="iefix">'+ 
			screens_list[i][1]+
			'</div></div>'+
			'<a href="/'+screens_list[i][0]+'"><div class="middleRegion" title="View Screenshot"></div></a>'+
			'<div class="ratingbg" title="'+g_ranking[rating_rounded]+'"><div style="padding: 5px 0 0 8px;">'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>0?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>1?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>2?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>3?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>4?'':'2')+'.gif" width=18 height=21/>'+
			'</div></div>'+
			'</td></tr></table>'+
			'</div></td></tr></table>';
		}
	}
	else
	{
		for ( var i = 0; i < length; i++ ) 
		{
			rating_rounded = Math.round(screens_list[i][2]);
			screens_data[i] = '<table><tr><td style="padding:3px 0 7px 0;"><div class="thumbView">' + 
			'<table cellpadding=0 cellspacing=0 width="152" height="150"><tr>'+
			'<td background="/t/'+screens_list[i][0]+'.jpg" style="background-position:center; background-repeat: no-repeat" nowrap>' + 
			'<div id="h'+i+'" class="thumbName" nowrap title="'+screens_list[i][1]+'">' +
			'<div id="n'+i+'" class="iefix" onDblClick="renameScreenshot('+i+');">'+ 
			screens_list[i][1]+
			'</div></div>'+
			'<a href="/'+screens_list[i][0]+'"><div class="middleRegion" style="height: 83px" title="View Screenshot"></div></a>'+
			'<div style="float: left; background-position: top left; background-image:url(/skins/default/images/layout/thumb_view_options.gif); background-repeat: no-repeat; width:43px; height:40px"><div title="Delete Screenshot" onClick="deleteScreenshot('+i+');" style="width:16px; height:16px; position: relative; cursor: pointer; left:6px; top:5px;"></div><div style="width:18px; height:18px; cursor:pointer; position: relative; left:21px; top:0px;" title="Modify Screenshot" onClick="modifyScreenshot('+i+');"></div></div>'+
			'<div><div style="height:6px; font-size:1px; cursor: pointer;" title="View Screenshot" onClick="window.location=\'/i/'+screens_list[i][0]+'\'"></div>'+
			'<div class="ratingbg" style="float:right; width: 107px; left:0px; " title="'+g_ranking[rating_rounded]+'"><div style="padding: 5px 0 0 8px;">'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>0?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>1?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>2?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>3?'':'2')+'.gif" width=18 height=21/>'+
			'<img src="/skins/default/images/layout/thumb_view_star'+(rating_rounded>4?'':'2')+'.gif" width=18 height=21/>'+
			'</div></div>'+
			'</td></tr></table></div>'+
			'<div align="center" style="font-size:10px; padding:4px 4px 0px 4px;"><div align="center" style="padding: 1px 2px 1px 2px; border:1px; border-color:#666666; background-color:#CCCCCC">http://ctscreens.net/'+screens_list[i][0]+'</div></div>'+
			'</td></tr></table>';
		}	
		modifyAreaSetup() ;
	}
	
	g_screens_array[g_screens_array_index] = Array(screens_data,div_id,parseInt(option));
	g_screens_array_index++;
	
	// enable the updates since we now have something to do
	window.onresize = refreshScreenList;
	
}

// Create a category list 
function createCatList(cat_list, div_id) 
{
	
	var length 		= (cat_list.length - 1);
	var cat_data 	= Array(0);
	
	if (length <= 0) return;
	
	for ( var i = 0; i < length; i++ ) 
	{

		cat_data[i] = '<div style="padding:10px;"><a style="text-decoration: none;" href="/listings/?cid='+cat_list[i][1]+(g_search!=''?'&search='+g_search:'')+'">'+
		'<img border=0 width="20" height="20" align="absmiddle" src="/images/siteimages/cat_icon_'+cat_list[i][4]+'.gif"> '+
		cat_list[i][0]+' ('+cat_list[i][3]+')</a></div>';
		
	}
	 
	g_screens_array[g_screens_array_index] = Array(cat_data,div_id,2);
	g_screens_array_index++;
	
	// enable the updates since we now have something to do
	window.onresize = refreshScreenList;
	
}

// Reprint the list based on browser window size
function refreshScreenList() {

	var myWidth = 0
	var myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }
	
	width = myWidth - 320;
	if( width > 520 ) {} else width = 520;
	var screens_per_row = parseInt( width / 220 );
		
	if( screens_per_row == g_last_row_size ) 
		return;		
		
	g_last_row_size = screens_per_row;	
	

	for (var k = 0; k <  g_screens_array.length; k++)
	{

		if (g_screens_array[k][2]==3)
		{
			screens_per_row = 3;
			g_last_row_size = 3;
		}
		
		var row 	= 0;
		var col 	= 0;
		var rowp 	= Math.round(100/screens_per_row);
		var length 	= g_screens_array[k][0].length;
		
		var output = '<table id="mainTable" border=0 width="100%" cellpadding=10 cellspacing=0>';
		
		for ( var i = 0; i < length; i++ ) 
		{
			
			if ( g_screens_array[k][0][i] == 0 ) continue;
			
			output += '<td align="center" width="'+rowp+'%">' + g_screens_array[k][0][i] + '</td>';
			col++;
				
			if( col%screens_per_row == 0 ) 
			{
				output += '</tr><tr>';
				col = 0;
				if (g_screens_array[k][2]==1) break;
				row++;
			}	
				
		}
	
		if ( col > 0 ) {
			while (++col <= screens_per_row) {
				output += '<td width="'+rowp+'%"></td>';
			}
			row++;
		}
					
		output += '</tr></table>';
	
		if ( g_screens_array[k][2] == 0 )
			output =  g_screens_searchbox + g_screens_navigation + g_screens_sorttype + '</td></tr></table>' + output + g_screens_navigation + '</td></tr></table>';
			
		document.getElementById(g_screens_array[k][1]).innerHTML = output;
	
	}
	
	

}

function checkSearch(e)
{
	
	if (!window.event)
        Key = e.which;
    else
        Key = window.event.keyCode;
    if (Key == 13) 
		window.location='?cid='+g_cid+'&search='+document.getElementById('search').value;
     
}

function displaycatlist(total) {
	
	var category_data  = '<table width="100%" cellspacing=0 style="padding-top:5px;"><tr><td><a style="font-weight: bold; text-decoration: none;" href="/listings/?cid=-1">View All ('+total+')</a></td></tr>';
	var indent;
	
	for(i=0;i<categories.length-1;i++) {
		
		indent = '';
		for(k=0;k<categories[i][2];k++) indent+='&nbsp; &nbsp;  &nbsp; ';
		category_data += '<tr><td style="padding:2px;">'+indent+'<a href="/listings/?cid='+categories[i][1]+'" style="text-decoration: none;'+(categories[i+1][2]>categories[i][2]?'font-weight: bold;">':'"><img border=0 width="20" height="20" align="absmiddle" src="/images/siteimages/cat_icon_'+categories[i][4]+'.gif">')+' '+categories[i][0]+' ('+categories[i][3]+')</a></td></tr>';
		
	}
	
	document.getElementById('lbox').innerHTML = category_data + '</table>';
	
}

