$(document).ready(function(){
		function status_message(sort, message)
		{
			selector = $("#status_message");
			text_field = $("#status_message .status_message_text");
			text_field.text(message);
			if (sort == "succes")
				selector.css("background-color","rgb(105, 139, 34)").css("background-color","rgba(105, 139, 34,0.9)");
			if (sort == "error")
				selector.css("background-color","rgb(255, 106, 106)").css("background-color"," rgba(255, 106, 106,0.9)");
			
			selector.stop(true,true).slideDown(600);
			selector.delay(5000).slideUp();
		}
		
		function reload_partofPage(file, element, argument)
		{	
			var dataString = argument;
			$.get(file+"?"+dataString, 
			function(data){
			element.html(data);
			});
		}
		
		//SHOP
		//character tools
		$("#panel form").submit(function(e){
		e.preventDefault();
		var class_name = $(this).attr('class');
		var section = $(".tab_small.selected").attr('id');
		var donation = 0;
			if (section == "donate")
				donation = 1;
		if (class_name == "rename_section" || class_name == "customize_section" || class_name == "faction_change_section" || class_name == "race_change_section"){
			var character = $(this).find(".char_select").val();
			var r=confirm("¿Estás seguro que quieres hacer esto?"); // Are you sure you want to do this?
					if (r !=true)
						return;
					
			var dataString = "type=character_modify&character="+character+"&section="+class_name+"&donation="+donation;
			
			$.post("account_panel/shop_handler.php",dataString,
			function(data){
				var text = $(data).filter("aa").text();
				if (text == "")
					status_message("succes", "!La acción ha sido un éxito!"); // Action is succesful!
				else
					status_message("error", text);
				
				reload_partofPage("login.php", $("#login_wrapper .reload_part"), "refresh=1");
			});
		}
		});
		
		//unstuck
		$("#unstuck").submit(function(e) {
			e.preventDefault();
			var character = $("#unstuck .char_select").val();
			var r=confirm("¿Estás seguro que quieres desbloquear tu personaje?"); // Are you sure you want unstuck your character?
				if (r !=true)
					return;
			var dataString = "type=unstuck&character="+character;	
			$.post("account_panel/shop_handler.php",dataString,
			function(data){
				var text = $(data).filter("aa").text();
				if (text == ""){
					status_message("succes", "!Tu personaje está desbloqueado satisfactoriamente!"); // Your character is succesful unstucked!
					$.cookie("Unstuck", "unstuck", { expires: 60*30 });
					}
				else
					status_message("error", text);
			});
		});
		
		
		//char selection
		//$(".char_select").focus(function (e){
		function get_chars(){	
			var dataString = "type=characters";
			$.post("account_panel/shop_handler.php",dataString,
			function(data){
			$(".char_select option").next("option").remove();
			$(".char_select").append(data);
			
			});
			
		}/*);*/
		
		//switch realm
		$(".tab_realm").click(function (){		
		
		$(".search_shop .char_select").hide();
		$(".item").remove();
		
		var realm = $(this).attr('id');
		var section = $(".tab_small.selected").attr('id');
				
			var dataString = "type=select&realm="+realm;
			$.post("account_panel/shop_handler.php",dataString,
			function(data){
				$(".tab_realm").removeClass('selected');
				$("#"+realm).addClass('selected');
				get_chars();
			});
		});
		// SEND ITEM
		$("#panel .send").live("click" , function() {
			var section = $(".tab_small.selected").attr('id');
			
			var item_id = $(this).attr('id');
			var donation = $('#panel .item_'+item_id).val();
			var character = $("#"+section+"_content .char_select").val();
			var section = $(".tab_small.selected").attr('id');
			
			var r=confirm("¿Estás seguro que quieres adquirir este item?"); // Are you sure you want to buy this item?
			if (r !=true)
				return;
			
			var dataString = "type=send&item_id="+item_id+"&donation="+donation+"&character="+character;
			
			$.post("account_panel/shop_handler.php",dataString,
			function(data){
			var text = $(data).filter("aa").text();
			
			if (text == "")
					status_message("succes", "!El item ha sido enviado a tu correo!"); // The item is send to your mailbox!
				else
					status_message("error", text);
			
			reload_partofPage("login.php", $("#login_wrapper .reload_part"), "refresh=1");
			});
			
		});
		
		
		// SEARCH IN SHOP
		$(".search_shop").submit(function (e){
		e.preventDefault();
		//LITTLE RESET
		$(".item").remove();
			
		var item_name = $(this).find(".item_name").val();
		
		var cost = $(this).find(".cost").val();
		var quality = $(this).find(".quality").val();
		var armor_type = $(this).find(".armor_type").val();
		var equipment_slot = $(this).find(".equipment_slot").val();
		var itemlevel = $(this).find(".itemlevel").val();
		var character = $(this).find(".character").val();
		
		//get section
		var section = $(".tab_small.selected").attr('id');
		
		var character = $("#"+section+"_content .char_select").val();
		var donation = 1;
		if (section == "vote")
			donation = 0;
		
		var dataString = "type=search&code="+item_name+":"+cost+":"+armor_type+":"+quality+":"+equipment_slot+":"+itemlevel+":"+donation;
		$.post("account_panel/shop_handler.php",dataString,
		function(data){
			var text = $(data).filter("aa").text();
			if (text != ""){
				status_message("error", text);
				return;
				}
			
			$("#"+section+"_content .search_shop").after(data);
			$(".search_shop .char_select").css("display","block");
			});
		});
		
		//TAB
		$(".tab_small").click(function (){
	
		var tab_id = $(this).attr('id');
		$(".search_shop .char_select").hide();
		$(".item").remove();		
		  
			$(".tab_small").removeClass('selected');
			$("#panel .invisible").hide();
			$("#"+tab_id).addClass('selected');
			$("#"+tab_id+"_content").fadeIn();
			});
		
		
		
		
		//popup
		$("#popup_close").click(function (){
		$("#popup").fadeOut("500");
		$.cookie("Popup", "popup", { expires: 60*60*12 });
		});
	  
	
		//premium
		$("#premium_account .submit").live("click", function (e){
		e.preventDefault();

			var time = $("#premium_account #date").val();
			
			var dataString = 'date='+time;
			$.post("account_panel/premium.php",dataString, 
			
			function(data){
			var text = $(data).filter("aa").text();
			if (text == "")
					status_message("succes", "!Ahora eres un miembro V.I.P. Disfruta de tu tiempo!"); // Your account is premium now!
				else
					status_message("error", text);

			});
		});
		
		//recruit friend
		
		$("#recruit_friend .submit").live("click", function (e){
		e.preventDefault();
		
			var username = $("#recruit_friend .username").val();
			var your_name = $("#recruit_friend .your_name").val();
			var email = $("#recruit_friend .email").val();
			
			var dataString = 'username='+username+'&email='+email+'&yourname='+your_name;
			$.post("account_panel/recruit.php",dataString, 
			
			function(data){
			var text = $(data).filter("aa").text();
			var status = $(data).filter("status").text();
			if (text == "")
					status_message("succes", "!Has reclutado a tu amigo!"); // You recruit your friend!
				else
					status_message("error", text);
			
			
			
			if (status == "1")
				$.cookie("TestCookie", "foo", { expires: 60 });
			});
		});
		
		//CHANGE PASS
		$("#change_password .submit").click( function (e){
		e.preventDefault();

			var old_pass = $("#change_password .old_password").val();
			var pass1 = $("#change_password .new_pass1").val();
			var pass2 = $("#change_password .new_pass2").val();
		
			var dataString = 'old_password='+old_pass+'&pass1='+pass1+'&pass2='+pass2;
			$.post("account_panel/change_pass.php",dataString, 
		
			function(data){
			var text = $(data).filter("aa").text();
			
			if (text == "")
				status_message("succes", "!Tu contraseña ha sido cambiada!"); // Your password has been changed!
			else
				status_message("error", text);
			});
		});


		//HELP
		
		$("#retrieve_password .submit").click( function (e){
			e.preventDefault();
			
			var username = $("#retrieve_password .username").val();
			var email = $("#retrieve_password .email").val();
		
			var dataString = 'username='+username+'&email='+email;
			$.post("retrieve.php",dataString, 
		
			function(data){
			var text = $(data).filter("aa").text();
			if (text == "")
				status_message("succes", "!Un E-mail te ha sido enviado!"); // An email has been send to you!
			else
				status_message("error", text);
			
			});
		});
	 
	 //REGistration
	$("#reg_submit").click( function (e){
	e.preventDefault();
	
		var username = $("#reg_username").val();
		var email = $("#reg_email").val();
		var pass1 = $("#reg_pass1").val();
		var pass2 = $("#reg_pass2").val();
		var recruiter = $("#reg_recruiter").val();
		var captcha_code = $(".captcha_code").val();
		
		var dataString = 'username='+username+'&email1='+email+'&pass1='+pass1+'&pass2='+pass2+'&recruiter='+recruiter+'&captcha_code='+captcha_code;
		$.post("register.php",dataString, 
			function(data){
			var text = $(data).filter("aa").text();
			var status = $(data).filter("status").text();
			if (status == "1")
			{
				//$("#content_wrapper #reg_form").submit();
				window.location = "index.php";
			}	
			status_message("error", text);
			//captcha
			document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random();
			$(".captcha_code").val("");
			});
	});
	
	//NEWS
	$("#new_post .close").click( function (){
		$("#new_post #new_post_title").val("");
		//$("#new_post #new_post_text").val("");
		nicEditors.findEditor('new_post_text').setContent('');
		$("#new_post #news_id").val(0);
		$("#new_post").slideUp(500);
	});
	
	$(".post_new_button").click( function (){
		$("#new_post").slideDown(1000);
	});
	
	$(".delete_post").click( function (){
		var news_id = $(this).attr("id");
		var parent = $(this).parent();
		
		
		var r=confirm("¿Estás seguro que quieres eliminar este post?"); // Are you sure you want to delete this post?
		if (r !=true)
			return;
		
		var dataString = 'news_id='+news_id;
		
		$.post("includes/delete_post.php",dataString, 
	
		function(data){
		var text = $(data).filter("aa").text();
			if(text == "Deleted"){
				parent.remove();
			}
		});
		
	});
	
	$(".edit_post").click( function (){
		var news_id = $(this).attr("id");
		
		var title = $("#"+news_id+"_title").text();
		
		var text_content = $("#"+news_id+"_content").html();
		$("#new_post #new_post_title").val(title);
		$("#new_post #new_post_text").html(text_content);
		nicEditors.findEditor('new_post_text').setContent(text_content);
		$("#new_post #news_id").val(news_id);
		
		$("#new_post").slideDown(1000);
		
	});
	
});

jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setTime(t.getTime() + (days*1000));
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
