var timeoutHide=false;
var runDOMLoaded=true;
function DOMLoaded(){
	// sometimes this function doesn't run when the DOM is loaded, so it will run again at page load; but it shouln't run twice
	if(!runDOMLoaded){
		fixFooter();
		return;
	}
	else
		runDOMLoaded=false;
	$('img.msover').attr('rel', function(){
		if($(this).attr('src').match(/_on.png$/))
			return $(this).attr('src');
		else
			return $(this).attr('src').replace(/\.png$/, '_on.png');
	});
	$('img.msover').mouseover(
		function(){
			var tmp=$(this).attr('src');
			$(this).attr('src', $(this).attr('rel'));
			$(this).attr('rel', tmp);
		}
	);
	$('img.msover').mouseout(function(){ $(this).mouseover(); });

	// put the close buttons on shortcuts on homepage
	if($('#main_table_shortcuts').length){
		$('<img src="/i/mt_c_delete.png" id="mtClose" style="display: none; position: absolute; cursor: pointer" />').prependTo('body');
		$('#main_table_shortcuts td.txt').mouseover(function(){
				var o=$(this).offset();
				$('#mtClose').css({'top':(o.top+40)+'px', 'left':(o.left+89)+'px'}).show();
				$('#mtClose').attr('rel', $(this).attr('rel'));
		});
		$('#main_table_shortcuts td.txt').mouseout(function(){ $('#mtClose').hide(); });
		$('#mtClose').mouseover(function(){ $('#mtClose').show(); });
		$('#mtClose').click(function(){
			$.cookie('ss_saved_searches['+$(this).attr('rel')+']', null);
			location.href=location.href;
		});
	}

	fixFooter();

/*
	$('#main_table_shortcuts > div').draggable({
		revert:'invalid',
		snap:true,
		snapMode: 'inner',
		cursor: 'move'
	});
	$('#main_table_shortcuts').droppable({
		drop: function(event, ui){
			$(this).append(ui.draggable.css({'top':'0px', 'left':'0px'}));
			// trebuie sa adaug la cookie si faptul ca s-a pus poza asta in shortcuts
			$.cookie('ss_shortcuts', $.cookie('ss_shortcuts')+','+ui.draggable.attr('rel'), { expires: 30, path: '/' });
		}
	});
	$('#tp_games > tbody > tr > td > div').draggable({
		revert:'invalid',
		snap:true,
		snapMode: 'inner',
		cursor: 'move'
	});
	$('#tp_games > tbody > tr > td').droppable({
		drop: function(event, ui){
			$(this).append(ui.draggable.css({'top':'0px', 'left':'0px'}));
			// trebuie sa adaug la cookie si faptul ca s-a scos poza asta din shortcuts
			$.cookie('ss_shortcuts', $.cookie('ss_shortcuts').replace(','+ui.draggable.attr('rel'), ''), { expires: 30, path: '/' });
		}
	});
	if(!$.cookie('ss_shortcuts'))
		$.cookie('ss_shortcuts', '0', { expires: 30, path: '/' });
*/

}

function fixFooter(){
	$(document).ready(function(){
		if($('#footer').offset().top<$(window).height()-$('#footer').innerHeight()-170)
			$('#content').css('height', ($(window).height()-$('#footer').offset().top-$('#footer').innerHeight()+$('#content').height()-1)+'px');
	});
}

function ss_login(){
	if(!$('#loginForm').length)
		$.get('/account/login.html', {'ajax': 1, 'return':location.pathname+location.search}, function(data){
			$('html>body').append(data);
			$('#loginForm #roundBottom').remove();
			$('#loginForm img').remove();
			$('#loginForm').dialog({dialogClass: 'popup', resizable: false, autoOpen: true});
		});
	else
		$('#loginForm').dialog('open');
	return false;
}

function ss_register(){
	if(!$('#registerForm').length)
		$.get('/account/register.html', {'ajax': 1, 'return':location.pathname+location.search}, function(data){
			$('html>body').append(data);
			$('#registerForm').dialog({dialogClass: 'popup', resizable: false, autoOpen: true});
		});
	else
		$('#registerForm').dialog('open');
	return false;
}

function fillSelect(box, url){
	box.options.length=0;
	$.getJSON(url, function(tmp){
		for(var i in tmp)
			box.options[box.options.length]=new Option(tmp[i], i);
	});
}

function softScroll(top){
	var steps=Math.floor(($(document).scrollTop()-top)/10);
	if(steps>0){
		for(var i=steps;i>=0;i--)
			setTimeout('$(document).scrollTop('+(top+i*10)+')', 300);
	}
	else{
		for(var i=steps;i<=0;i++)
			setTimeout('$(document).scrollTop('+(top+i*10)+')', 300);
	}
}

function w(){
	if(typeof(window.innerWidth)=='number')
		var wh={'width': window.innerWidth, 'height': window.innerHeight}; //Non-IE
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		var wh={'width': document.documentElement.clientWidth, 'height': document.documentElement.clientHeight} //IE 6+ in 'standards compliant mode'
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		var wh={'width': document.body.clientWidth, 'height': document.body.clientHeight}; //IE 4 compatible
	else
		var wh={'width':0, 'height':0};

	if(document.body && (document.body.scrollLeft || document.body.scrollTop))
		var lt={'left': document.body.scrollLeft, 'top': document.body.scrollTop} //DOM compliant
	else if(typeof(window.pageYOffset)=='number')
		var lt={'left': window.pageXOffset, 'top': window.pageYOffset} //Netscape compliant
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
		var lt={'left': document.documentElement.scrollLeft, 'top': document.documentElement.scrollTop} //IE6 standards compliant mode
	else
		var lt={'left':0, 'top':0};
	return {'width': wh.width, 'height': wh.height, 'scrollLeft': lt.left, 'scrollTop': lt.top};
}


function popPic(src){
	if(document.getElementById('popPic'))
		document.getElementById('popPic').onclick();
	img=document.createElement('img');
	img.className='popPic';
	img.title='Click to close';
	img.id='popPic';
	img.onclick=function(){
		document.body.removeChild(this);
	}
	img.onerror=function(e){
		alert('The image could not be loaded');
	}
	img.onload=function(){
		var ww=w();
		this.style.top=parseInt((ww.height-this.height)/2+ww.scrollTop)+'px';
		this.style.left=parseInt((ww.width-this.width)/2+ww.scrollLeft)+'px';
		document.body.appendChild(this);
	}
	img.src=src;
}

var activeTradePic=0;
function tradePic(dir){
	if(dir==1)
		activeTradePic=activeTradePic==tradePics.length-1?0:activeTradePic+1;
	else if(dir==-1)
		activeTradePic=activeTradePic==0?tradePics.length-1:activeTradePic-1;
	$('#tradePic').attr('href', "JavaScript:popPic('/pics/f/"+tradePics[activeTradePic]+"')");
	$('#tradePic > img').attr('src', '/pics/'+tradePics[activeTradePic]);
}

function bidReply(id, a){
	a.style.display='none';
	document.getElementById('r'+id).innerHTML+='<form action="'+location.href+'" method="post"><input type="hidden" name="reply" value="'+id+'"><textarea name="comment"></textarea><br><input type="image" src="/i/add_comment.png"></form>';
}

function saveSearch(){
	if(!$('#searchSaved').length){
		$.cookie('ss_saved_searches['+(new Date()).getTime()+']', location.search.replace(/&[xy]=[0-9]+/g, '').substring(1), { expires: 30, path: '/' });
		$('<div id="searchSaved" title="Saved" style=""><small>This search has been saved.<br>You can find a shortcut to this search on the homepage.</small></div>').prependTo('body').dialog({
		buttons:{
			"Ok": function(){
				$(this).dialog("close");
			}
		},
		closeOnEscape: true,
		resizable: false,
		autoOpen: true,
		dialogClass: 'popup'
	});
	}
	else
		$('#searchSaved').dialog('open');

	return false;
}