jQuery(document).ready(function(){
	
    if (jQuery.fn.jDoubleSelect && jQuery("#SelectCity").length > 0) {
		jQuery('#SelectCity').jDoubleSelect({text: ""});
		jQuery("#Form_Go").click(function(){
			var loc = WebRoot + 'cityhome/' + jQuery("#SelectCity_jDS_2").val();
			//console.log(loc);
			document.location = loc;
			return false;
		});
	}
	
	
	var ReplaceLoading = function(){
		if(jQuery("span", this).length == 0) return;
		var img = document.createElement("img");
		jQuery("span", this).each(function(){
			var attributeName = jQuery(this).attr("class").toLowerCase();
			if(attributeName == 'onclick'){
				var location = jQuery(this).text();
				if(location.substr(0, 7) != 'http://'){
					if(location.charAt(0) == 'default.htm') location = location.substr(1);
					location = WebRoot + location;
				}
				jQuery(img).click(function(){ document.location = location; });
			} else {
				jQuery(img).attr(attributeName, jQuery(this).text());
			}
		});
		// remove loading class
		jQuery(this).children().remove();
		jQuery(this).removeAttr('class').append(img);
	}
	
	var SetTitle = function(){
		var jQueryimg = jQuery("img", this);
		var Root = jQuery(this).parents(".Slider");
		jQuery(".Controls a.Title", Root).attr('href', jQueryimg.attr('longdesc')).html( jQueryimg.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(){
			jQuery(this).parent(".Items").children(".Loading").slice(0, 1).each(ReplaceLoading);
			SetTitle.apply(this);
		}
	};
	
	
	if(jQuery.fn.cycle){
		jQuery.fn.cycle.debug = false;
		jQuery(".Slider").each(function(index, node){
			var id = "#" + jQuery(node).attr('id');
			var options = jQuery.extend(settings, {prev: id+' .Prev',next: id+' .Next'});
			options.delay = (index+1) * 5000;
			jQuery(id).cycle(options);
		});
	}

	if(jQuery.fn.uBox) jQuery("a[hrefjQuery='.jpg'] img[src], a[hrefjQuery='.png'] img[src]").uBox({showSpeed:800,hideSpeed:500,showImageAlt:false});
	
	if (jQuery.fn.autogrow) jQuery('#Form_Comments, #Form_ExhibitionOrders, #Form_Comment').livequery(function() {
		jQuery(this).autogrow();
	});
	
	if (jQuery.fn.handleAjaxForm) jQuery('.NotifyChangeForm').handleAjaxForm(
		{data: { 'DeliveryType' : 'BOOL', 'DeliveryMethod' : 'JSON' }}
	);
	
	// external links
	jQuery("a[rel~='external']").each(function(){ this.target = "_blank"; });

	// where to buy (clasic)
	jQuery("#WhereToBuy select[name='Form/WhereToBuy']").change(function(){ // click
		var v = jQuery(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 =  v;
	});
	
	
	// classic search box
	jQuery('#ClassicSearchBox').data('SearchWord', jQuery('#ClassicSearchBox').val());
	jQuery('#ClassicSearchBox').focus(function(){
		jQuery(this).val("");
	})
	.blur(function(){
		jQuery(this).val( jQuery(this).data('SearchWord') );
	});

	
	// 8. Reclamation 
	if (jQuery.fn.popup) jQuery('td.ReclamationScore a').popup({
		afterSuccess: function(settings, json) {
			if (json.Targets) gdn.processTargets(json.Targets);
		}
	});
/*	jQuery('#Form_Comment').livequery(function() {
		jQuery(this).setMaxChars(600);
	});*/

});
