// JavaScript Document
(function($){
	$(function(){
		$("#bgloader").html('<img src="http://www.knightstyled.com/blogTest/raw/images/loader.gif" width="31" height="31" alt="loading background" />');
		/*$.get(
				"getgamer.php?gamertag=Birthmark111",
				function(result){
					$(document.body).append('<img src="'+result.getElementsByTagName("TileUrl")[0].firstChild.nodeValue+'" />');
					console.log(result.getElementsByTagName("TileUrl")[0].firstChild.nodeValue);
				},
				"xml"
		 );*/
		$.getJSON(
			"http://www.knightstyled.com/blogTest/raw/bgs.json",
			function(result){
				var list = result.list;
				var rnd = Math.floor(Math.random()*(list.length));
				rnd=(rnd>list.length)?list.length:rnd;
				var url = list[rnd];
				$("<img />").attr({src:"http://www.knightstyled.com/blogTest/raw/"+url}).load(function(){
					$("#inner")
						.css({backgroundImage: "url(http://www.knightstyled.com/blogTest/raw/"+url+")"})
						.animate({opacity:1}, 800)
					;
					$("#bgloader").empty();
				});
			}
		);
		if($("#featuredslide").length>0){
		var featuredslide = $("#featuredslide").jSlideView({
			prev: ".featurePrev",
			next: ".featureNext",
			view: 1,
			loop: true,
			startPage: 0,
			speed: 900,
			width: 550,
			height: 160,
			direction: "horizontal"
		});
		}
		$("#featurednav a")
			.click(function(e){
				$("#featurednav a").removeClass("featurehilite");
				$(this).addClass("featurehilite");
				$("#featurearrow").css({marginTop:34+(26)*($.inArray(this, $("#featurednav a")))});
				featuredslide.goTo($.inArray(this, $("#featurednav a")));
				return false;
			})
		;
		$("#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.knightstyled.com/blogTest/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.knightstyled.com/blogTest/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.knightstyled.com/blogTest/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.knightstyled.com/blogTest/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);
