jQuery(document).ready(function(){
	var WebRoot = gdn.definition('WebRoot');
	
	var ReplaceLoading = function(){
		if($("span", this).length == 0) return;
		var img = document.createElement("img");
		$("span", this).each(function(){
			var attributeName = $(this).attr("class").toLowerCase();
			if(attributeName == 'onclick'){
				var location = $(this).text();
				if(location.substr(0, 7) != 'http://'){
					if(location.charAt(0) == '/') location = location.substr(1);
					location = WebRoot + location;
				}
				$(img).click(function(){ document.location = location; });
			} else {
				$(img).attr(attributeName, $(this).text());
			}
		});
		// remove loading class
		$(this).children().remove();
		$(this).removeAttr('class').append(img);
	}
	
	var SetTitle = function(){
		var $img = $("img", this);
		var Root = $(this).parents(".Slider");
		$(".Controls a.Title", Root).attr('href', $img.attr('longdesc')).html( $img.attr('title') );
	}
	
	var settings = { 
		fx: 'fade,scrollLeft,cover,uncover,scrollHorz', // fade,scrollLeft,cover,uncover,scrollHorz
		pause: true,
		randomizeEffects: false,
		timeout: 15000,
		speed:  800, 
		slideExpr: ".Items div",
		before: function(){
			$(this).parent(".Items").children(".Loading").slice(0, 1).each(ReplaceLoading);
			SetTitle.apply(this);
		}
	};
	
	
	if($.fn.cycle){
		$.fn.cycle.debug = false;
		$(".Slider").each(function(index, node){
			var id = "#" + $(node).attr('id');
			var options = $.extend(settings, {prev: id+' .Prev',next: id+' .Next'});
			options.delay = (index+1) * 5000;
			$(id).cycle(options);
		});
	}

	if($.fn.uBox) $("a[href$='.jpg'] img[src], a[href$='.png'] img[src]").uBox({showSpeed:800,hideSpeed:500,showImageAlt:false});
	
	// TODO: CHECK
	if ($.fn.autogrow) $('#Form_Comments, #Form_ExhibitionOrders').livequery(function() {
		$(this).autogrow();
	});
	
	if ($.fn.handleAjaxForm) $('.NotifyChangeForm').handleAjaxForm(
		{data: { 'DeliveryType' : 'BOOL', 'DeliveryMethod' : 'JSON' }}
	);
	
	// tabs
	$('ul.Tabs li a').click(function() {
		$('ul.Tabs li').removeAttr('class');
		$(this).parent('li').attr('class', 'Active');
		var tabs = $('ul.Tabs');
		tabs.nextAll().remove();
		tabs.after('<div class="Loading">&nbsp;</div>');
		$.get(this.href, {'DeliveryType': 'VIEW'}, function(data){
			tabs.nextAll().remove();
			tabs.after(data);
		});
		return false;
	});
	
	//$("#Head .Logo").children(":eq(0)").wrap('<a href='+definition("WebRoot")+' />');
	
	var Logo = '<a class="Logo" href="'+ gdn.definition('WebRoot') + '">'+ $("#Head").find('.Logo').html() + '</a>';
	$("#Head .Logo").replaceWith(Logo);
	
	/*$("#Head .Logo")
		.click(function(){ document.location = gdn.definition("WebRoot"); })
		.mouseenter(function(){ $(this).css("cursor", "pointer"); })
		.mouseleave(function(){ $(this).css("cursor", "auto"); });*/
	
	// external links
	$("a[rel~='external']").each(function(){ this.target = "_blank"; });

	// where to buy (clasic)
	$("#WhereToBuy select[name='Form/WhereToBuy']").change(function(){ // click
		var v = $(this).val();
		//var WebRoot = definition("WebRoot");
		//if(v.substr(0, 7) == "http://") WebRoot = '';
		// not using WebRoot, because same using in maryabath.ru
		if(v) document.location = "http://www.marya.ru/cities/" + v;
	});
	
	// callnow bubble (removed)
	/*$("#CallNow").hover(function(){
		$(this).css('position', 'relative');
		if($.browser.msie) $("#CallNowBubble").show();
		else $("#CallNowBubble").fadeIn('fast');
	}, function(){
		$("#CallNowBubble").hide();
		$(this).css('position', 'static');
	});*/
	
	// classic search box
	$('#ClassicSearchBox').data('SearchWord', $('#ClassicSearchBox').val());
	$('#ClassicSearchBox').focus(function(){
		$(this).val("");
	})
	.blur(function(){
		$(this).val( $(this).data('SearchWord') );
	});
	
/*	var $table = $("#factory_orders_index table");
	if($table.length > 0) {
		//$.getScript(WebRoot + 'applications/factory/js/jquery.tableSort.js');
		$("tr th:eq(0)", $table).click(function(){
			$("#factory_orders_index table").sortTable({onCol: 7,keepRelationships: true, sortType: 'numeric'});
		});
		$.getScript(WebRoot + 'applications/factory/js/jquery.tableSort.js', function(){
			$table.tableSort({onCol: 1});
			//$table.tableSort({onCol: 4, keepRelationships: true, sortType: 'numeric'});
			//$table.tableSort({onCol: 7, keepRelationships: true, sortType: 'numeric'});			
		});
	}*/



	/*
	$("ul.DetailsInfo a.Details").click(function(){
		var self = this;
		
		$("ul.DetailedInfo").remove();
		
		
		$(self).after('<div class="Loading">&nbsp;</div>');
		var $LoadingBox = $(self).next('div');
		$.ajax({
			type: "POST",
			url: $(self).attr('href'),
			data: 'DeliveryType=VIEW',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$.popup({}, textStatus);
			},
			success: function(html){
				$LoadingBox.remove();
				$box = $(html).hide();
				//$box.css('display', 'none');
				$(self).after($box);
				$box.fadeIn('fast');
				//$LoadingBox.html(html);
				//console.log(html);
				//if(json.ErrorMessage) return gdn.inform(json.ErrorMessage, false);
				//$(self).text(json.LinkText);
				//if(json.StatusMessage) gdn.inform(json.StatusMessage);
			}
			
		});
		return false;
	});
	*/

});