//just a quick note, there is a slight mod to the mootools toquerystring function to support empty values for text fields

function slide_content(){
	
	$$('img.hideshow').each(function(item){
		
		item.addEvent('click',function(){
			
			id = (this.id).substring(0,(this.id.length-2));
			
			var myFx = new Fx.Slide($(id), {
			  duration: 'long',
			   transition: Fx.Transitions.Bounce.easeOut
			});
			
			myFx.toggle();
			
		});
		
	});
	
}

function ajaxsearch(){
	
	search_value = $('search_name').value;
	
	new Request.JSON({

		url: url+'/ajax.php?action=search&Name='+search_value,
		onComplete: function(jsonObj) {
			
			$('search_results').empty();
			
			results = new Element('div', {'class':'menu'});
			
			jsonObj.each(function(item){
				
				new Element('a', {'href':item.Url,'title':item.Name,'text':item.Name}).inject(results);
				new Element('br',{}).inject(results);
				
			});
			
			results.inject($('search_results'));
			
			

			$('search_results').style.display = '';
			
		}
		
	}).send();

}

	function user_area(data){

		user_area_var = $('user_area').getChildren()[0].innerHTML;
			
		if(!data.status && data.action == 'user'){
			
			new Element('span',{'html':'Incorrect username or password!<br /><br />'}).inject($('user_area').getChildren()[0], 'top')
		
		}else{
			
			logged_in = true;
			
			$('user_area').empty();
			
			var wrapper = new Element('div',{'class':'content'});
			
			new Element('span',{'html':'Welcome to your user area '+data.data.Username+'<br /><br />'}).inject(wrapper);
			
			new Element('a',{'href':data.data.Profile_url,'html':'your profile ','title':data.data.Username}).inject(wrapper);
			new Element('a',{'href':data.data.Edit_url,'html':'(edit)','title':'edit profile'}).inject(wrapper);
			new Element('br',{}).inject(wrapper);
			new Element('a',{'href':data.data.Inbox_url,'html':data.data.Messages+' unread messages','title':'messages'}).inject(wrapper);
			new Element('br',{}).inject(wrapper);
			new Element('a',{'href':data.data.Friends_url,'html':'friends','title':'friends'}).inject(wrapper);
			new Element('br',{}).inject(wrapper);
			new Element('a',{'href':data.data.Logout_url,'html':'logout','title':'logout'}).inject(wrapper);
			
			new Element('span',{'html':'<br /><br />Favorite games:'}).inject(wrapper);
			
			var favorite_games = new Element('ol').inject(wrapper);
			
			(data.data.Favorites).each(function(item){
				
				var li = new Element('li').inject(favorite_games);
				new Element('a',{'href':item.Url,'html':item.Name,'title':item.Name}).inject(li);
				
			});
			
			new Element('span',{'html':'Recent games:'}).inject(wrapper);
			
			var recent_games = new Element('ol').inject(wrapper);
			
			(data.data.Last_played).each(function(item){
				
				var li = new Element('li').inject(recent_games);
				new Element('a',{'href':item.Url,'html':item.Name,'title':item.Name}).inject(li);
				
			});
			
			new Element('span',{'html':'Top rated:'}).inject(wrapper);
			
			var top_rated = new Element('ol').inject(wrapper);
			
			(data.data.Top_rated).each(function(item){
				
				var li = new Element('li').inject(top_rated);
				new Element('a',{'href':item.Url,'html':item.Name+' ('+item.Rating_f+')','title':item.Name}).inject(li);
				
			});
			
			wrapper.inject($('user_area'));

		} 
		
	}

function add_favorite(id){
	
	new Request.JSON({
		url: url+'/ajax.php?action=add_favorite&id='+id,
		onComplete: function(jsonObj) {
			user_area(jsonObj);
			$('fave_del').setStyle('display','');
			$('fave_add').setStyle('display','none');
		}				
	}).send();
	
}

function remove_favorite(id){
	
	new Request.JSON({
		url: url+'/ajax.php?action=remove_favorite&id='+id,
		onComplete: function(jsonObj) {
			user_area(jsonObj);
			$('fave_del').setStyle('display','none');
			$('fave_add').setStyle('display','');
		}				
	}).send();
	
}

var current	=	'details';

function change(element){
	
	new_el =	$(element+'_c');
	
	new_el.setStyle('display','');
	
	nav_el = $(element+'_w');
	nav_el.setStyle('background-color','#252525');
	nav_el.getParent().setStyle('background-color','#252525');
	nav_el.getParent().getChildren()[2].setStyle('color','white');
	
	current_el =	$(current+'_c');
	current_el.setStyle('display','none');
	nav_el = $(current+'_w');
	nav_el.setStyle('background-color','white');
	nav_el.getParent().setStyle('background-color','white');
	nav_el.getParent().getChildren()[2].setStyle('color','#252525');
	
	current	=	element;
	
}

function post_comment(form_el){

	new Request.JSON({
		url: url+'/ajax.php?action=comments&'+form_el.toQueryString(),
		onComplete: function(jsonObj) {
			
			div_errors = new Element('ul');
			$('errors').empty();

			if(!jsonObj.status){
				
				json_errors	=	jsonObj.errors;
				
				json_errors.each(function(item) {  
				    new Element('li',{'html':"<span class='sprite-error'></span>"+item}).inject(div_errors);
				}.bind(this));
				
				if($('captcha')){
				
					$('captcha').src = 'captcha.php?id='+Math.random();
					$('captcha_value').value='';
					
				}
					
			}else{
				
				if(logged_in){
						
					user_area(jsonObj);
						
				}

				new Element('li',{'html':"<span class='sprite-error'></span>Thanks, your comment will appear on the site shortly!"}).inject(div_errors);
				
				$('postcomment').setStyle('display','none');
				
			}
			
			div_errors.inject($('errors'));

		}				
	}).send();
	
}

function delete_comment(id){

	$('comment_'+id).setStyle('display','none');
	$('comment_'+id).getNext().setStyle('display','none');
	
	new Request.JSON({
		url: url+'/ajax.php?action=delete_comment&id='+id
	}).send();
	
}

function load_comment(id){
	
	new Request.JSON({
		url: url+'/ajax.php?action=load_comment&id='+id,
		onComplete: function(jsonObj) {
			$('comment_'+id+'_c').empty();
			
			new Element('textarea',{'html':jsonObj.Comment,'styles':{'width':'300px'}}).inject($('comment_'+id+'_c'));
			new Element('input',{'type':'button','value':'save','styles':{'height':'20px','font-size:':'12px'},'events':{'click':function(){save_comment(id)} }}).inject($('comment_'+id+'_c'));
			
			$('comment_'+id+'_m').setStyle('display','none');
			
		}
	}).send();
	
}

function save_comment(id){

	edited = $('comment_'+id+'_c').getChildren()[0].innerHTML;

	new Request.JSON({
		url: url+'/ajax.php?action=save_comment&id='+id+'&comment='+edited,
		onComplete: function(jsonObj) {
			
			$('comment_'+id+'_c').empty();
			$('comment_'+id+'_c').innerHTML = 'saved';
			$('comment_'+id+'_m').setStyle('display','');
			
		}
	}).send();
	
}

function popup(content){
    params  = 'width='+screen.width;
    params += ', height='+screen.height;
    params += ', top=0, left=0'
    params += ', fullscreen=yes';

    new_window  =   window.open('','windowname4', params);
    
    if (window.focus){
        new_window.focus()
    }
    
    new_window.document.open()
    new_window.document.write(content)
    new_window.document.close()
    
    return false;
}

var logged_in = false;

window.addEvent('domready', function() {
	
	slide_content();
	
	if(ajax_search){
	
		$('search_name').addEvent('keyup',function(item){		
			if (this.zid){
				clearTimeout(this.zid);
			}	
			this.zid = setTimeout(function(){
				ajaxsearch();
			},1000);	
		});
		
	}
	
	if(ajax_user && $('login_form') ){
	
			$('login_form').addEvent('submit',function(){
				
				new Request.JSON({
					url: url+'/ajax.php?action=user&'+$('login_form').toQueryString(),
					onComplete: function(jsonObj) {
						user_area(jsonObj);
					}				
				}).send();
				
				return false;

			});
		
	}

});