
function PhotoListingClass(Gallery) { this.init(Gallery); }
PhotoListingClass.prototype = {
	selector : '#shop_photos_viewer',
	description_selector : '#shop_photos_description',
	photo_link : null,
	photo_img : null,
	Gallery : null,
	ajax_indicator : null,
	lock : false,
	description : '',
	photo_id : 0,
	css_fade : {opacity: 0.85},
	css_unfade : {opacity: 1},
	init : function(Gallery) {
		this.Gallery = Gallery;
		this.photo_link = $(this.selector).find('a.thephoto');
		this.photo_link.click(this.click_photo.bind(this));
		this.photo_img = this.photo_link.find('img.photo');
		this.photo_img.load(this.onload_photo.bind(this));
		this.photo_img.show();
		this.ajax_indicator = this.photo_link.find('img.ajax_indicator');
		this.ajax_indicator.hide();
		var current_id = this.photo_link.attr('photo_id');
		this.photo_id = current_id;
		var index = this.Gallery.Popup.IdCycle.get_index(current_id);
		this.Gallery.Popup.IdCycle.set_iterator(index);
		$(this.selector).find('.gallery_shop_left')
			.css(this.css_fade)
			.hover(this.unfade_nav.bind(this), this.fade_nav.bind(this))
			.click(this.click_prev.bind(this));
		$(this.selector).find('.gallery_shop_right')
			.css(this.css_fade)
			.hover(this.unfade_nav.bind(this), this.fade_nav.bind(this))
			.click(this.click_next.bind(this));
		$(this.selector).show();
	},
	
	reset_iterator : function() {
		var index = this.Gallery.Popup.IdCycle.get_index(this.photo_id);
		this.Gallery.Popup.IdCycle.set_iterator(index);
	},
	
	fade_nav : function(event) { $(event.currentTarget).css({opacity: 0.7}); },
	
	unfade_nav : function(event) { $(event.currentTarget).css({opacity: 1}); },
	
	onload_photo : function(event) {
		this.ajax_indicator.hide();
		this.photo_img.show();
		this.lock = false;
		$(this.description_selector).html(this.description.description);
	},
	
	click_next : function(event) {
		event.preventDefault();
		this.reset_iterator();
		this.photo_id = this.Gallery.Popup.IdCycle.next();
		this.show_photo();
	},
	
	click_prev : function(event) {
		event.preventDefault();
		this.reset_iterator();
		this.photo_id = this.Gallery.Popup.IdCycle.prev();
		this.show_photo();
	},
	
	click_photo : function(event) {
		event.preventDefault();
		this.Gallery.Popup.load_photo(this.photo_id);
	},
	
	show_photo : function() {
		if (this.lock)
			return;
		this.lock = true;
		this.ajax_indicator.show();
		this.photo_img.hide();
		$.getJSON(this.Gallery.Popup.url+'/'+this.photo_id, function(json, textStatus) {
			if ('success' == textStatus && json.status=='ok')
			{
				this.description = json.photo;
				this.photo_img.attr('src', json.src);
			}
		}.bind(this));
	}
};

var GalleryArea = null;
var PhotoListing = null;

$(document).ready(function () {
	GalleryArea = new GalleryAreaClass('#hidden_shop_gallery', '#gallery_popup');
	GalleryArea.Popup.url = js_options.base_url + 'shop/get_photo';
	GalleryArea.Popup.find('.popup_inside').css({width : 'auto', 'padding-right': '30px'});
	GalleryArea.Popup.window.css({height:'auto'});
	GalleryArea.Popup.photo_cont.css({height: '310px'});
	GalleryArea.Popup.link_prev.addClass('horizontal');
	GalleryArea.Popup.link_next.addClass('horizontal');
	GalleryArea.Popup.link_prev.find('img').width(130);
	GalleryArea.Popup.link_prev.find('img').height(80);
	GalleryArea.Popup.link_next.find('img').width(130);
	GalleryArea.Popup.link_next.find('img').height(80);
	GalleryArea.Popup.show_description = false;
	GalleryArea.Popup.auto_width = true;
	GalleryArea.Popup.min_width = '400px';
	PhotoListing = new PhotoListingClass(GalleryArea);
	
	try {
		if (!coord_x || !coord_y) {
			
			var geocoder = new YMaps.Geocoder("Россия, Санкт-Петербурге, " +addr, {results: 1});
			YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
				if (this.length()) {
					add_balloon(this.get(0).getGeoPoint());
				}else {
					alert("Ничего не найдено");
				}
			});
		}
		else {
			var point = new YMaps.GeoPoint(coord_x, coord_y);
			add_balloon(point);
		}
	}
	catch(e) {}
	 h1 = $('#shop_description').outerHeight();
	 h2 = $('#shop_photos_description').outerHeight();
	if (h1 > h2)
		$('#shop_photos_description').height(h1);
	else
		$('#shop_description').height(h2);
	$('#left_columb_wrap').show();
	$('#assortiment .selection_container a').click(switch_assortiment);
});

function add_balloon(point) {
	var sampleBalloonTemplate = new YMaps.LayoutTemplate(SampleBalloonLayout);
	var map = new YMaps.Map(document.getElementById("themap"));
	map.setType(YMaps.MapType.MAP);
	map.setCenter(point, 14);
	var style = new YMaps.Style();
	style.balloonStyle = {template: sampleBalloonTemplate };
	balloon = new YMaps.Placemark(point, {style: style });
	map.addControl(new YMaps.Zoom(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_LEFT));
	map.addOverlay(balloon);
	balloon.openBalloon();
}

function switch_assortiment() {
	
	$('#assortiment_list > div:visible').hide();
	$(this).closest('div').find('a.selected').removeClass('selected');
	$(this).addClass('selected');
	var cat = this.id.replace(/^asrt_switch_/, '');
	$('#asrt_'+cat).show();
}


function SampleBalloonLayout() {

	this.element = YMaps.jQuery('<div id="ymballoon" class="content"></div>');
	this.close = this.element.find(".close");
	this.content = this.element.find(".content");
	 
	this.disableClose = function(){
		this.close.unbind("click").css("display", "none");
	};
	this.enableClose = function(callback){
	this.close.bind("click", callback).css("display", "");
		return false;
	};
	 
	this.onAddToParent = function (parentNode) {
		YMaps.jQuery(parentNode).append(this.element);
	};
	this.onRemoveFromParent = function () {
		this.element.remove();
	};
	 
	this.setContent = function (content) {
		content.onAddToParent(this.content[0]);
	};
	this.update = function() {
		this.element.css("margin-top", "-" + (this.content.height() + 20 ) + "px");
		this.element.css('left', -7);
	};
};
