$(function() {
	// Разбить список на колонки
	if (jQuery().autocolumnlist) {
		$('ul.autocolumnlist').autocolumnlist();	 
    }
	
	// Карусель
	if (jQuery().jCarouselLite) {
		$("#carousel-outer").jCarouselLite({
			mouseWheel: true,
			btnNext: "#carousel-next",
			btnPrev: "#carousel-prev",
			visible: 6,
			speed: 500
		});		
    }
	
	// Галерея
	
    $('a.gallery-thumbnail').click(function(e) {
		var self = $(this);
		var path = self.attr('href');
		var id = self.attr('rel');
		
		$('a.gallery-thumbnail').removeClass('current');
		self.addClass('current');
        
        $('.big_img').css('z-index', '10');
        $('#'+id).css('z-index', '50');
		/*
		$('#gallery-photo').animate({opacity: 0},100,function() {
			$(this).html('<img src=' + path + ' width="522" class="photo" />').find('img').bind('load',function(){
				$(this).parent().animate({opacity: 1},100);
			});
		}).attr('href', rel);
        */
		e.preventDefault();
	});
    
	$('.colors a').click(function(e) {
		var self = $(this);
		var thumb = $('#thumbnail');
		thumb.attr('src', this);
		$('.colors a').removeClass('active_color');
		self.addClass('active_color');
		e.preventDefault();
	});
	// Выбрать первое фото в галереи
	$('a.gallery-thumbnail:first').click().addClass('current');
    
    //$('a.big_img:first').css('z-index', '50').
	
	$('ul#my-menu ul').each(function(i) {
					if ($.cookie('submenuMark-' + i)) {
						$(this).show().prev().removeClass('collapsed').addClass('expanded');
					} else {
						$(this).hide().prev().removeClass('expanded').addClass('collapsed');
					}
					$(this).prev().addClass('collapsible').click(function() {
						var this_i = $('ul#my-menu ul').index($(this).next());
						if ($(this).next().css('display') == 'none') {
							$(this).parent('li').parent('ul').find('ul').each(function(j) {
								if (j != this_i) {
									$(this).slideUp(200, function () {
										$(this).prev().removeClass('expanded').addClass('collapsed');
										cookieDel($('ul#my-menu ul').index($(this)));
									});
								}
							});
							$(this).next().slideDown(200, function () {
								$(this).prev().removeClass('collapsed').addClass('expanded');
								cookieSet(this_i);
							});
						} else {
							$(this).next().slideUp(200, function () {
								$(this).prev().removeClass('expanded').addClass('collapsed');
								cookieDel(this_i);
								$(this).find('ul').each(function() {
									$(this).hide(0, cookieDel($('ul#my-menu ul').index($(this)))).prev().removeClass('expanded').addClass('collapsed');
								});
							});
						}
						return false;
					});
	});
	
	// Всплвающая гелерея для фото
    if (jQuery().lightBox) {
		$('a[rel$=popup]').lightBox({
			overlayBgColor: '#000',
			overlayOpacity: 0.6,
			imageLoading: 'images/lytebox/lightbox-ico-loading.gif',
			imageBtnClose: 'images/lytebox/close.png',
			imageBtnPrev: 'images/lytebox/lightbox-btn-prev.gif',
			imageBtnNext: 'images/lytebox/lightbox-btn-next.gif',
			imageBlank: 'images/lytebox/lightbox-blank.gif',
			containerResizeSpeed: 350,
			containerBorderSize: 0,
			txtImage: 'Фото',
			txtOf: 'из'
		});
    }
    
    if (jQuery().prettyPhoto) {
        $("*[rel^='prettyPhoto']").prettyPhoto({
             slideshow: 5000
            ,social_tools: ''
            ,counter_separator_label: '/'
            ,deeplinking: false
        });
		$("a[rel^='prettyMap']").each(function() {
			
			var $this = $(this);
			var $latitude = $this.attr('data-latitude');
			var $longitude = $this.attr('data-longitude');
			var $name = $this.attr('data-name');
			var $address = $this.attr('data-address');
			
			$this.prettyPhoto({
				 custom_markup: '<div id="map_canvas" style="width:800px; height:600px"></div>'
				,changepicturecallback: function(){ initMap($latitude, $longitude, $name, $address);}
				,deeplinking: false
                ,allow_resize: false
			});
		});
    }

    if ($().tipTip) {
        $('[data-title]').tipTip({
            attribute: 'data-title'
            ,delay: 200
            ,fadeIn: 200
            ,fadeOut: 200
        });
    }

    if ($().metadata && $().validate) {
        //$.metadata.setType("html5");
        $('form').each(function(){
            $(this).validate({
                //meta: "validate"
                errorElement: "span"
                //errorPlacement: function (error, element) {
                //    $('#error_box').html(error);
                //}
                //errorContainer: $('#error_box')
                ,submitHandler: function(form) {
                    var self = form;
                    setTimeout(function(){self.submit();}, 1000);
                }
            });
        });
    }
	
});

/*
 * Инициализация карты Google.Map
 */

function initMap(latitude, longitude, name, address) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(latitude, longitude), 13);
		map.setMapType(G_NORMAL_MAP);
		map.setUIToDefault();
		var point = new GLatLng(latitude, longitude);
		var marker = createMarker(point,'<br /><b>'+name+'</b><br /><br />'+address);
		map.addOverlay(marker);
	}
}
/*
 * Поставить маркер на карту
 */
function createMarker(point, html) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}
function cookieSet(index) {
	$.cookie('submenuMark-' + index, 'opened', {expires: null, path: '/'});
}
function cookieDel(index) {
	$.cookie('submenuMark-' + index, null, {expires: null, path: '/'});
}
