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);
		}
	};
	
	var SliderIndex = 0;
	
	if ($.fn.cycle) {
		$.fn.cycle.debug = false;
		$(".Slider").livequery(function(){
			var id = "#" + $(this).attr('id');
			var options = $.extend(settings, {prev: id+' .Prev',next: id+' .Next'});
			options.delay = (++SliderIndex) * 5000;
			$(id).cycle(options);
		});
/*		$(".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], a[href$='.gif'] img[src]").uBox({showSpeed:800,hideSpeed:500,showImageAlt:false});
	
	if ($.fn.autogrow) $('#Form_Comments, #Form_ExhibitionOrders, #Form_Comment').livequery(function() {
		$(this).autogrow();
	});
	
	if ($.fn.handleAjaxForm) $('.NotifyChangeForm').handleAjaxForm(
		{data: { 'DeliveryType' : 'BOOL', 'DeliveryMethod' : 'JSON' }}
	);
		
	if ($.fn.colorbox) {
		$('a[rel~=colorbox]').colorbox({
			maxWidth: ($(window).width() - 100),
			current: gdn.definition("colorbox-current", 'current'),
			previous: gdn.definition("colorbox-previous", 'previous'),
			next: gdn.definition("colorbox-next", 'next'),
			close: gdn.definition("colorbox-close", 'close'),
			opacity: 0.3,
			preloading: false,
			speed: 'fast',
			dummy: null
		});
	}
	
	$("#ShowCityShops").css('cursor', 'pointer');
	$("#ShowCityShops").click(function(e){
		var link = $(this).find('a').attr('href');
		e.stopPropagation();
		document.location = gdn.url(link);
		return false;
	});
		
	// 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();
		// not using WebRoot, because same using in maryabath.ru
		if(v) document.location = "http://www.marya.ru/cities/" + v;
	});*/
	
	// callnow bubble (removed)
	
	// classic search box

	
	if ($.fn.accordion) {
		$('ul.Accordion.Auto').accordion({
                canToggle: true,
                canOpenMultiple: true,
				handle: "strong",
				panel: "ul"
		});
	}
	
		$('#CityListData > ul').hide();
		$('#CityListData').hide();
	$('a#Russia').click(function(){
			$('ul#Russia').toggle();
			if ( (!$("ul#Russia").is(":visible")) && $('#CityListData').is(":visible"))$('#CityListData').hide(); else $('#CityListData').show();
			$('#CityListData > ul').not('ul#Russia').hide();
			$('li.Active').removeClass();
			$('a#Russia').parent().addClass('Active')});
	$('a#Ukraine').click(function(){
			$('ul#Ukraine').toggle();
			if ( (!$("ul#Ukraine").is(":visible")) && $('#CityListData').is(":visible"))$('#CityListData').hide(); else $('#CityListData').show();
			$('#CityListData > ul').not('ul#Ukraine').hide();
			$('li.Active').removeClass();
			$('a#Ukraine').parent().addClass('Active')});
	$('a#Kazahstan').click(function(){
			$('ul#Kazakhstan').toggle();
			if ( (!$("ul#Kazakhstan").is(":visible")) && $('#CityListData').is(":visible"))$('#CityListData').hide(); else $('#CityListData').show();
			$('#CityListData > ul').not('ul#Kazakhstan').hide();
			$('li.Active').removeClass();
			$('a#Kazahstan').parent().addClass('Active')});
	$('a#Belarus').click(function(){
			$('ul#Belarus').toggle();
			if ( (!$("ul#Belarus").is(":visible")) && $('#CityListData').is(":visible"))$('#CityListData').hide(); else $('#CityListData').show();
			$('#CityListData > ul').not('ul#Belarus').hide();
			$('li.Active').removeClass();
			$('a#Belarus').parent().addClass('Active')});

});
