/*-----------------------------------------------------------------------------
author:    I-iN-Studio
email:     info@i-in-studio.com
website:   http://www.i-in-studio.com/
-----------------------------------------------------------------------------*/

$(document).ready( function () {

	//return;
/*	if ( ($("#Content .Item .Gallery .Photos a").length > 0) && !($.browser.msie && $.browser.version < 7) ) {
	
	    $("#Content .Item .Photo img").addClass("Ready");
	    $("#Content .Item .Photo img").attr("alt", "Открыть галерею");
		
	    $("#Content .Item .Photo img").click( function () {

	    	gallery_open($("#Content .Item .Gallery .Photos a").attr("href"), $("#Content .Item .Gallery .Photos a img").attr("alt"));
		
		    return false;
		
	    });
	
	    $("#Content .Item .Gallery .Photos a").click( function () {

	    	gallery_open($(this).attr("href"), $("img", this).attr("alt"));
		
		    return false;
		
	    });
	
	}*/
	
	if (document.domain != "www.lestnicatmp.ru") {
		$("a").click(function(){
			_gaq.push(["_link", "http://www.lestnicatmp.ru/"+$(this).attr("href")]); return false;
		});
	}
	
	if ( ($("#Content .Table table thead tr").length > 0) ) {
		
		$("#Content .Table table thead tr").css("cursor", "pointer");
		
		var buffer = $("#Content .Table table tbody ").html();
		
		$("#Content .Table table thead tr th").click( function () {

			var asc = $(this).hasClass("ASC");
			var desc = $(this).hasClass("DESC");
			var index = this.cellIndex;

			$("#Content .Table table thead tr th").removeClass("ASC DESC");

			if ( desc ) {
			
				$("#Content .Table table tbody").html(buffer);
				
			} else {

				if ( asc ) {
					
					$("#Content .Table table tbody tr").tsort("td:eq("+index+")", {order:"desc"});
					
					$(this).addClass("DESC");
			
				} else {
					
					$("#Content .Table table tbody tr").tsort("td:eq("+index+")", {order:"asc"});
					
					$(this).addClass("ASC");
					
				}
				
			}

		});
		
	}
	
	if ( ($("#Content .Flip a").length > 0) ) {
		
		$("#Content .Flip a").click( function () {

			$(".Photo img", $(this).parents(".Element")).fadeOut(0);
			
			$(".Photo img", $(this).parents(".Element")).attr("src", $(this).attr("href"));
			$(".Flip a", $(this).parents(".Element")).toggleClass("Selected");
			$(".Flip span", $(this).parents(".Element")).toggleClass("Selected");
			
			$(".Photo img", $(this).parents()).fadeIn("slow");
			
		    return false;
		    
		});
		
	}
	
	$("#Toplink").show();
	$("#Toplink a").click( function () {
	
		$.scrollTo(0, 500);
		
		return false;
	
	});
	
});

function gallery_open (href, alt) {

	//TODO :: скрыть изначально
	
	$("body").append("<div id='Gallery'><div class='Heading'></div><div class='Display'></div><div class='Text'></div><div class='Navigation'></div><div class='Close'></div></div>");
	
	$("#Gallery .Heading").html($("#Content .Item .Heading").text());
	
	$("#Gallery .Navigation").html($("#Content .Item .Gallery .Photos").html());

	$("#Gallery .Navigation a").click( function () {

		gallery_show($(this).attr("href"), $("img", this).attr("alt"));
		
		return false;
		
	});
	
	$("#Gallery .Close").click(gallery_close);
	
	gallery_show(href, alt);
	
	$("#Gallery").fadeIn("slow");

}

function gallery_show (href, alt) {

	$("#Gallery .Text").html(alt);
	
	$("#Gallery .Display").html("<img src='"+href+"' alt='"+alt+"' />");

}

function gallery_close () {

	$("#Gallery").fadeOut("slow", function () {
		
		$(this).remove();
		
	});

}
