//alert(window.location.hash.length+"-"+window.location.pathname.length)
if(getQuerystring('fb')!=1)
{
	if(window.location.pathname.length>1 && window.location.hash.length==0 && window.location.pathname!="/author" && window.location.pathname!="/tags")
	{
		if(window.location.pathname.substr(1,6)=="author")
			window.location="/author#"+window.location.pathname;
		else if(window.location.pathname.substr(1,4)=="tags")
			window.location="/tags#"+window.location.pathname;
		else
			window.location="/#"+window.location.pathname;
	}
}

jQuery.fn.extend({
	inputHint: function()
	{
		var alt=this.attr("alt");
		if(alt)
		{
			var oldcolor=this.css("color");
			
			this.val(alt).css({color:"#AAAAAA"});

			this.mouseenter(function(){
				if($(this).val()==alt)
					$(this).val("").css({color:"#AAAAAA"});
				else
					$(this).css({color:oldcolor});
			}).mouseleave(function(){
				if($(this).val()=="")
					$(this).val(alt).css({color:"#AAAAAA"});
				else
					$(this).css({color:oldcolor});	
			}).blur(function(){
				if($(this).val()!="" && $(this).val()!=alt)
					$(this).css({color:oldcolor});
			}).focus(function(){
				if($(this).val()!="")
					$(this).css({color:oldcolor});
			}).keyup(function(){
				if($(this).val()!="")
					$(this).css({color:oldcolor});
			});;
		}
	}
});

jQuery.fn.extend({

	galleryActivate: function()
	{
		moveGallery=function()
		{
			$(".gallery img[id!='gallery-arrow']").fadeTo(500, .2);
			$.galleryWidth=$(window).width();
			$.im=$(".gallery .wid div.fl:eq("+$.s+") img");
			$.offsetWidth=0;

			$(".gallery .wid div.fl:lt("+$.s+") img").each(function(){$.offsetWidth+=$(this).attr("width");});
			$.left=$.offsetWidth;
			$.leftFixed=($.left-($.galleryWidth/2-$.im.attr("width")/2));
			$.im.fadeTo(500, 1);
			$.gallery.animate({scrollLeft:$.leftFixed}, 500);
		}

		$.totalWidth=0;
		$(".gallery .wid img").each(function(){$.totalWidth+=$(this).attr("width");})
		$(".gallery .wid").width($.totalWidth);

		$.s=0;
		$.gallery=$(".gallery");
		$.max=$(".gallery .wid div.fl").size()-1;

		//--------------------------------
		$.startX=-1;
		$(".gallery .wid img, #gallery-arrow").css({"cursor":"none"}).bind("mousedown",function(e){
			e.preventDefault();
		})

/*
		$("#gallery-arrow").bind("mousedown",function(e){
			e.preventDefault();
			//$(this).hide().css({"top":"-1000px", "left":"-1000px"});
			$.gallery.trigger("mousedown");
		});
*/
		//$("#gallery-arrow, .gallery").mousedown(function() { targetElement = this; });
		
		//$("#gallery-arrow").css({"cursor":"none"}).mousemove( function(e){
		//	$("#gallery-arrow").css({"cursor":"none","left":e.pageX-$("#gallery-arrow").width()/2/* , "top":e.pageY-$("#gallery-arrow").height()/2 */});
		//});

		$.clicked=false;
		//$.gallery.css({"cursor":"none"}).mousemove( function(e){
		//	$("#gallery-arrow").css({"cursor":"none","left":e.pageX-$("#gallery-arrow").width()/2, "top":e.pageY-$("#gallery-arrow").height()/2});
		//});

/*
		$.gallery.mouseover(function(e){
			$("#gallery-arrow").show();
			e.stopPropagation();
		}).mouseout(function(){
			$("#gallery-arrow").hide();
		});
*/

		$.gallery.bind("mouseenter", function(e){		
			e.stopPropagation();		
			$("#gallery-arrow").show().fadeTo(300, .5);
		}).bind("mouseleave", function(e){		
			e.stopPropagation();
			$("#gallery-arrow").fadeTo(300, 0);
		}).bind("mousemove", function(e){
			e.stopPropagation();
			$("#gallery-arrow").css({"left":$.gallery.scrollLeft()+e.pageX-$("#gallery-arrow").width()/2 , "top":e.pageY-$.gallery.offset().top-$("#gallery-arrow").height()/2});
			
			if($.clicked==true)
			{
				if($.startX<0) $.startX=e.pageX+$.gallery.scrollLeft();
				$.gallery.scrollLeft($.startX-e.pageX);

				checkCenter();
			}
			if(e.pageX<0 || e.pageX>$(window).width())
				$.gallery.trigger("mouseup");

		}).bind("mousedown", function(e){
			e.preventDefault();
			e.stopPropagation();
			$("#gallery-arrow").fadeTo(300, .05);
			$.clicked=true;
		}).bind("mouseup", function(e){
			e.preventDefault();
			e.stopPropagation();
			$.clicked=false;
			$.startX=-1;
			$("#gallery-arrow").fadeTo(300, .5);
		});

		checkCenter=function()
		{
			$.galleryWidth=$(window).width();
			$.min=100000000;
			$(".gallery img[id!='gallery-arrow']").each(function()
			{
				$(".gallery img[id!='gallery-arrow']").fadeTo(0, .2);
				$.mymin=Math.abs($.galleryWidth/2-($(this).offset().left+$(this).width()/2));

				if($.mymin<$.min)
				{
					$.min=$.mymin;
					$.sMc=$(this);
				}
			})
			$.sMc.fadeTo(0, 1);
			$.s=$(".gallery img").index($.sMc);
		}
		//--------------------------
		
		$(".gallery_left").fadeTo(0, .5).click(function(e){
			e.preventDefault();
			if($.s>0)
			{
				$.s--;
				moveGallery();
			}
		}).mouseenter(function(){$(this).fadeTo(0, 1)}).mouseleave(function(){$(this).fadeTo(0, .5)});
		
		$(".gallery img[id!='gallery-arrow']").fadeTo(0, .2);
		$(".gallery img:first").fadeTo(0, 1);
		
		$(".gallery_right").fadeTo(0, .5).click(function(e){
			e.preventDefault();
			if($.s<$.max)
			{
				$.s++;
				moveGallery();
			}
		}).mouseenter(function(){$(this).fadeTo(0, 1)}).mouseleave(function(){$(this).fadeTo(0, .5)});
		
		/*
		$(".gallery").mousemove(function(e){
			if($.totalWidth==0)
			{
				$(".gallery .wid img").each(function(){$.totalWidth+=$(this).width();})
				$(".gallery .wid").width($.totalWidth);
				$.gallery=$(".gallery");
				$.galleryWidth=$.gallery.width();
				$.imagesWidth=$(".gallery .wid").width();					
			}
			$.gsw=($.galleryWidth/2)-e.pageX;
			if($.gsw>1)
				$.gallery.css("cursor","w-resize");
			else
				$.gallery.css("cursor","e-resize");

			console.log($.galleryWidth+", "+$.gsw)
		});
		*/
		/*
		$(".gallery .wid .pic img").fadeTo(0, .3);
		$.totalWidth=0;
		$(".gallery .wid .pic img").mouseenter(function(){
			$(this).stop().fadeTo("normal", 1);
		}).mouseleave(function(){
			$(this).stop().fadeTo("normal", .3);
		});
	
		$(".gallery").mousemove(function(e){
			if($.totalWidth==0)
			{
				$(".gallery .wid img").each(function(){$.totalWidth+=$(this).width();})
				$(".gallery .wid").width($.totalWidth);
				$.gallery=$(".gallery");
				$.imagesWidth=$(".gallery .wid").width();					
			}
			$.galleryWidth=$.gallery.width();
			var left = (e.pageX - $.gallery.offset().left) * ($.imagesWidth-$.galleryWidth) / $.galleryWidth;
			$.gallery.scrollLeft(left);
		});
		*/
	}
});

$(document).ready(function ()
{
	/*
	jQuery.ifixpng('/i/s.gif');
	jQuery('img, .bgPng').ifixpng();
	jQuery('td, .bgPng').ifixpng();
	jQuery('div, .bgPng').ifixpng();
	jQuery('a, .bgPng').ifixpng();
	jQuery('li, .bgPng').ifixpng();
	jQuery('body, .bgPng').ifixpng();
	*/
	
	Cufon.replace('.c_light', {fontFamily:"spectacularlight"});
	Cufon.replace('.c_regular', {fontFamily:"spectacular"});

	$('html, body').scrollTop(0);
	
	$('.article').prepend('<div class="over"></div><div class="over_move"></div>');
	$('.over').fadeTo(0, 0);

	$('.article, .article2').bind("mouseover",function(){
		$(this).find('.over').stop().fadeTo("fast", .15)
	}).bind("mouseout",function(){
		if($(this).find(".over_move").width()==0)
			$(this).find('.over').stop().fadeTo("fast", 0)
	});
	
	$('.article .title a').live('click', function(e){
		e.preventDefault();
		var title=$(this).parent().parent().parent();
		var loadbar=title.find(".over_move");
		var url=$(this).attr("href");
		var text=$(this).attr("title");//find("cufontext").html();
		
		if(text==null)
			text=$(this).html();

		if($.selectedGroup!=url)
		{
			$.selectedArticle="";
			$.selectedGroup=url;
			$(".subdcontent").slideUp("fast", function(){$(this).remove()});
			
			$(".over_move").each(function(){
				if($(this).width()>400)
					$(this).animate({width:0}, 1000, function(){
						$(this).prev().fadeTo("fast", 0);
					});
			});
	
			//$(this).find(".number").addClass("w");
			loadbar.show().width(1).animate({width:800}, 1500)
	
			title.after("<div class=\"subdcontent\"></div>");
			var content=title.next();		
			content.hide();
			content.load(
				url,
				function()
				{
					document.title=text+" • spectacularpractices.com";
					window.location.hash="#"+url;
					$.selectedGroupText=text;

					Cufon.replace('.subdcontent .c_light', {fontFamily:"spectacularlight"});
					Cufon.replace('.subdcontent .c_regular', {fontFamily:"spectacular"});
	
					$('.article2').prepend('<div class="over"></div><div class="over_move"></div>');
					$('.article2 .over').fadeTo(0, 0);
		
					$('.article, .article2').bind("mouseover",function(){
						$(this).find('.over').stop().fadeTo("fast", .15)
					}).bind("mouseout",function(){
						if($(this).find(".over_move").width()==0)
							$(this).find('.over').stop().fadeTo("fast", 0)
					});
					
					loadbar.animate({width:970}, 500, function()
					{
						if($.PreOpening)
							$('.article2 .title a[href=/'+$.PreOpening+']').click();
						
						content.slideDown(1000);
						$('html, body').animate({
							scrollTop: title.offset().top
						}, 1000, function(){
						});
					});
				}
			);
		}
		else
		{
			//collapse
			$(".subdcontent").slideUp("fast", function(){$(this).remove()});
			loadbar.animate({width:0}, 500);
			$.selectedArticle="";
			$.selectedGroup="";
			window.location.hash="#/";
			document.title="spectacularpractices.com";
		}
	});
	
	$('.article2 .title a').live('click', function(e){
		e.preventDefault();
		var title=$(this).parent().parent().parent();
		var loadbar=title.find(".over_move");
		var url=$(this).attr("href");
		var text=$(this).attr("title");
		if(text==null)
			text=$(this).html();
		
		if($.selectedArticle!=url)
		{
			$.selectedArticle=url;
			$('.article2').removeClass("wht");
			$('.article2').addClass("black");
			title.animate({"delay": "true"}, 350, function(){
				$(this).addClass("wht")
				Cufon.refresh('.subdcontent .c_light');
				Cufon.refresh('.subdcontent .c_regular');	
			});
			
			$(".articlecontent").slideUp(0, function(){$(this).remove()});
			$('html, body').animate({"delay": "true"}, 1).animate({
				scrollTop: title.offset().top
			}, 1000);
			
			
			$(".subdcontent .over_move").each(function(){
				if($(this).width()>400)
					$(this).animate({width:0}, 1000, function(){
						$(this).prev().fadeTo("fast", 0);
					});
			});
	
			loadbar.show().width(1).animate({width:800}, 1500)
	
			title.after("<div class=\"articlecontent\"></div>");
			var content=title.next();
			content.hide();
			content.load(
				url,
				function()
				{
					document.title=text+" • spectacularpractices.com";
					$.selectedArticleText=text;
					//window.location.hash="#"+$.selectedGroup+url;

					if(window.location.pathname=="/author")
					{
						$.tmp=window.location.hash.split("/");
						//alert($.tmp[0]+"==="+$.tmp[1]+"--"+$.tmp.length);
						window.location.hash=$.tmp[0]+"/"+$.tmp[1]+url;
					}
					else
						window.location.hash="#"+url;
					
					Cufon.replace('.articlecontent .c_light', {fontFamily:"spectacularlight"});
					Cufon.replace('.articlecontent .c_regular', {fontFamily:"spectacular"});

					//galleryActivate();
	
					loadbar.animate({width:970}, 500, function(){
						content.slideDown(2000);
						$('html, body').animate({
							scrollTop: title.offset().top
						}, 1000, function(){
						});
					});
				}
			);
		}
		else
		{
			//collapse
			$(".articlecontent").slideUp("fast", function(){$(this).remove()});
			loadbar.animate({width:0}, 500);
			$.selectedArticle="";
			$('.article2').removeClass("wht");
			$('.article2').addClass("black");
			Cufon.refresh('.subdcontent .c_light');
			Cufon.refresh('.subdcontent .c_regular');
			window.location.hash="#"+$.selectedGroup;
			$.selectedArticleText="";
			
			document.title=$.selectedGroupText+" • spectacularpractices.com";
		}		
	});


	//comments action
	$("#savecomment").live("submit", function(e){e.preventDefault();return false;})
	$("#addcomment").live("click", function(e){
		e.preventDefault();
		if($("#comments_body").val().length>0 && $("#comments_caption").val().length>0 && $("#comments_body").val()!=$("#comments_body").attr("alt") && $("#comments_caption").val()!=$("#comments_caption").attr("alt"))
		{
			$(".nocomments").fadeOut();
			$.serialized=$("#savecomment").serialize();
			$(this).fadeTo("fast", .3);
			$(".onecomment:last").show().after("<div class=\"onecomment\">"+$(".onecomment:last").html()+"</div>");
			$.last=$(".onecomment:last");
			$.last.hide();
			
			$("#comments_body").attr("disabled", true);
			$("#comments_caption").attr("disabled", true);
			$.ajax({
				type: "POST",
				url: $("#savecomment").attr("action"),
				data: $.serialized,
				success: function(msg)
				{
					var d = new Date();
					$.mn=d.getMonth()+1;
					
					$.last.find(".comdate div").html((d.getDate()<10?"0":"")+d.getDate()+"."+($.mn<10?"0":"")+$.mn);
					$.last.find(".comment .name").html($("#comments_caption").val());
					$.last.find(".comment .descr").html($("#comments_body").val());
					$.last.slideDown();
	
					$("#comments_body").removeAttr("disabled").val("");
					$("#comments_caption").removeAttr("disabled").val("");

					$("#addcomment").fadeTo("fast", 1);
				}
			});
		}
		else
		{
			
		}
	})

	//--------
	$("#more").live("click", function(e){
		e.preventDefault();

		$(this).parent().parent().after('<div></div>');
		$d=$(this).parent().parent().next();
		$(this).html("<img src='/i/loader.gif'>");

		$b=$(this).parent();
		$d.load(
			$(this).attr("rel"),
			function()
			{
				$d.slideUp(0);
				$d.slideDown();

				Cufon.replace('.subdcontent .c_light', {fontFamily:"spectacularlight"});
				Cufon.replace('.subdcontent .c_regular', {fontFamily:"spectacular"});

				$d.find('.article2').prepend('<div class="over"></div><div class="over_move"></div>');
				$d.find('.article2 .over').fadeTo(0, 0);
	
				$d.find('.article, .article2').bind("mouseover",function(){
					$(this).find('.over').stop().fadeTo("fast", .15)
				}).bind("mouseout",function(){
					if($(this).find(".over_move").width()==0)
						$(this).find('.over').stop().fadeTo("fast", 0)
				});

				$b.slideUp(500, function(){
					$b.remove();
					$d.slideDown(2000);
					$('html, body').animate({
						scrollTop: $d.offset().top
					}, 1000, function(){
					});
				});				
			}
		);
	})
	//--------------
	if(window.location.hash)
	{
		$.url0=window.location.hash.substr(2);
		if(window.location.pathname=="/author")
			if($.url0.indexOf("/")>0)
			{
				$.PreOpening=$.url0.substr($.url0.indexOf("/")+1);
				$.url=$.url0.substr(0, $.url0.indexOf("/"));
			}
			else
				$.url=$.url0;
		else if(window.location.pathname=="/tags")
			if($.url0.indexOf("/")>0)
			{
				$.PreOpening=$.url0.substr($.url0.indexOf("/")+1);
				$.url=$.url0.substr(0, $.url0.indexOf("/"));
			}
			else
				$.url=$.url0;
		else
			if($.url0.indexOf("-")>0)
			{
				$.PreOpening=$.url0;
				$.url=$.url0.substr(0, $.url0.indexOf("-"));
			}
			else
				$.url=$.url0;

		$(".article .title a").each(function(){
			if($(this).attr("href")=="/"+$.url || $(this).attr("href")=="/"+$.PreOpening)
			{
				if($.url0.indexOf("-")>0)
					$(this).attr("href", $(this).attr("href")+"?full");
				$(this).click();
			}
		})
	}
});

function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);

  if(qs == null)
    return default_;
  else
    return qs[1];
}
