// JavaScript Document
(function($){
	$(function(){

		$("#topmenu-inside").css({"display":"none"});
		$("#topmenu-dock").click(function(e){
			if($("#topmenu-inside").is(":visible")){
				$("#topmenu-inside").slideUp();
			}else{
				$("#topmenu-inside").slideDown();
			}
			e.stopPropagation();
			return false;
		});
		$("#topmenu-bg").click(function(e){$("#topmenu-inside").slideUp(); return false;});
		$("#topmenu-inside *, #topmenu-dock-inside *").click(function(e){e.stopPropagation();});
		
		$("#dock-hideui").toggle(
			function(e){
				$(this).text("show ui");
				$("#uiwrap").stop(true).animate({opacity:0},800,function(){$(this).hide();});
				e.stopPropagation();
				return false;
			},
			function(e){
				$(this).text("hide ui");
				$("#uiwrap").show();
				$("#uiwrap").stop(true).animate({opacity:1},800);
				e.stopPropagation();
				return false;
			}
		);
		
		var docklogin=function(){
			//$("#topmenu-dock").trigger("click");
			if($("#dock-login").html()=="login"){
				$("#topmenu-dock").trigger("click");
			}else{
				$.post(
					"http://www.gamingtruth.com/gtlogin.php",
					{action: "logout"},
					function(result){
						if(result.out){
							$("#dock-username").html("");
							$("#topmenu-inside").slideUp();
							$("#w-gamercard").html("");
							$("#dock-settings").html("");
							$("#dock-login").html("login");
						}
					},
					"json"
				);
				return false;
			}
			return false;
		}
		$("#dock-login").click(function(e){
			docklogin();
			e.stopPropagation();
			return false;
		});
		$("#searchfield").focus(function(e){
			if($(this).val()=="search..."){$(this).val("");}
		});
		$("#searchfield").blur(function(e){
			if($(this).val()==""){$(this).val("search...");}
		});
		$("#searchsubmit img").click(function(e){
			document.searchform.submit();
			return false;
		});
		
		var ajaxclicker = function(e){
			$("#content-main").html('<img src="http://www.gamingtruth.com/raw/images/loader.gif" width="31" height="31" alt="loading content..." style="position:relative; margin:0 auto;" />');
			$("#content-main-w").load($(this).attr("href") + " #content-main", function(){
				$(".navigation a").click(ajaxclicker);
				$("#content-main script").remove();
			});
			return false;
		}
		
		$("#content-main-nav > a, .navigation a").click(ajaxclicker);
		
		$("#loginform").submit(function(e){
			$.post(
				"http://www.gamingtruth.com/gtlogin.php",
				{action: "login", log: $(this.log).val(), pwd: $(this.pwd).val()},
				function(result){
					if(result.data){
						var result=result.data;
						$("#dock-username").html(result["display_name"]);
						$("#topmenu-inside").slideUp();
						$("#w-gamercard").html('<img src="http://www.gamingtruth.com/raw/images/loader-dock.gif" />');
						$("<img />").attr({src:"http://card.mygamercard.net/gbar/grill/"+encodeURI(result["display_name"])+".gif"}).load(function(){
							$(this).replaceAll("#w-gamercard img");
						});
						$("#dock-login").html("logout");
						$("#dock-settings").html("settings");
					}else if(result.errors){
						//for(var b in result.errors){if(console){console.log(b);}}
					}
				},
				"json"
			);
			return false;
		});
		
		var changeDock = function(targ){
		}
		
	});
})(jQuery);