/*-----------------------------------------------------------------------------
author:    I-iN-Studio
email:     info@i-in-studio.com
website:   http://www.i-in-studio.com/
-----------------------------------------------------------------------------*/

$(document).ready( function () {

	//return;
	if ( $("#Content .List .Listing a").length > 0  ) {
		
		var buffer = $("#Content .List .Listing").html();
		
		$("#Content .List .Sorting").show();
		
		//TODO :: инкапсулировать функции
		
		$("#Content .List .Sorting .Name").click ( function () {
			
			var asc = $(this).hasClass("ASC");
			var desc = $(this).hasClass("DESC");
			
			$("#Content .List .Listing").hide();

			$("#Content .List .Sorting a").removeClass("ASC DESC");

			if ( desc ) {

				$("#Content .List .Listing").html(buffer);

			} else {

				if ( asc ) {

					$("#Content .List .Listing a").tsort(".Title", {order:"desc"});
					
					$(this).addClass("DESC");
			
				} else {
					
					$("#Content .List .Listing a").tsort(".Title", {order:"asc"});
					
					$(this).addClass("ASC");

				}

			}
			
			$("#Content .List .Listing").fadeIn("fast");

			return false;
			
		});
		
		$("#Content .List .Sorting .Price").click ( function () {
			
			var asc = $(this).hasClass("ASC");
			var desc = $(this).hasClass("DESC");
			
			$("#Content .List .Listing").hide();

			$("#Content .List .Sorting a").removeClass("ASC DESC");

			if ( desc ) {

				$("#Content .List .Listing").html(buffer);

			} else {

				if ( asc ) {

					$("#Content .List .Listing a").tsort(".Price .Value", {order:"desc"});
					
					$(this).addClass("DESC");
			
				} else {
					
					$("#Content .List .Listing a").tsort(".Price .Value", {order:"asc"});
					
					$(this).addClass("ASC");

				}

			}
			
			$("#Content .List .Listing").fadeIn("fast");

			return false;

		});
		
	}	

	if ( ($("#Content .Item .Gallery .Photos a").length > 0) && !($.browser.msie && $.browser.version < 7) ) {
	
	    $("#Content .Item .Photo img").addClass("Ready");
		
	    $("#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;
		
	    });
	
	}
	
	$("#Toplink").show();
	$("#Toplink a").click( function () {
	
		$.scrollTo(0, 500);
		
		return false;
	
	});
	
	//IE6,7 fix
	if ( $.browser.msie && $.browser.version < 8 ) {

		$("#Content .List .Listing .Element .Photo img").click( function() {

			window.location.href = $(this).parent().parent().attr("href");

		});
		
	}
	
});

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();
		
	});

}