function CycleClass () {}
CycleClass.prototype = {
	list : [],
	iterator : 0,
	set_list : function(list) { this.list = list; return this; },
	set_iterator : function(index) { this.iterator = index; return this; },
	get_current : function() { return this.list[this.iterator]; },
	get_index : function(value) {
		for(var i=0; i<this.list.length; i++) if (this.list[i] == value) return i;
		return false;
	},
	get_next : function () {
		var _iter = this.iterator;
		var value = this.next();
		this.iterator = _iter;
		return value;
	},
	get_prev : function() {
		var _iter = this.iterator;
		var value = this.prev();
		this.iterator = _iter;
		return value;
	},
	prev : function() {
		if (--this.iterator<0) this.iterator=this.list.length-1;
		return this.get_current();
	},
	next : function () {
		if (++this.iterator>=this.list.length) this.iterator=0;
		return this.get_current();
	}
};

Function.prototype.bind = function (context) {
	var fun = this;
	return function() {
		fun.apply(context, arguments);
	};
};

function is_ie6() {	return $.browser.msie && $.browser.version=='6.0'; }

var init_pagewrap_width = null;

function restore_pagewrap()
{
	$('#page_wrap').css({width: init_pagewrap_width, height:'auto', overflow: 'visible'});
}

function GalleryAreaClass(selector, popup_window_selector) { this.init(selector, popup_window_selector); }
GalleryAreaClass.prototype = {
	
	selector : null,
	border : null,
	Popup : null,
	click_photo_handler : null,
	hover_photo_handler : null,
	hide_border_nandler : null,
	
	init : function(selector, popup_window_selector) {
		this.selector = selector;
		this.Popup = new GalleryPopupClass(popup_window_selector);
		var photos = $(selector).find('div.gallery_photo');
		this.click_photo_handler = this.click_photo.bind(this);
		this.hover_photo_handler = this.hover_photo.bind(this);
		this.hide_border_nandler = this.hide_border.bind(this);
		var photo, id = [], src = [];
		for(var i=0; i<photos.length; ++i) {
			photo = photos.get(i);
			$(photo)
				.mouseenter(this.hover_photo_handler)
				.mouseleave(this.hide_border_nandler)
				.find('a.border')
					.click(this.click_photo_handler);
			
			id.push(photo.getAttribute('photo_id'));
			src.push($(photo).find('img').get(0).src);
		}
		this.Popup.IdCycle.set_list(id);
		this.Popup.SrcCycle.set_list(src);
	},
	
	
	unhide : function() {
		$(this.selector).show();
	},
	
	hover_photo : function(event) {
		$(event.currentTarget).children('.border').show();
	},
	
	hide_border : function(event) {
		$(event.currentTarget).children('.border').hide();
	},
	
	click_photo : function(event) {
		event.preventDefault();
		var id = $(event.target).closest('div.gallery_photo').attr('photo_id');
		this.Popup.load_photo(id);
	}
	
};

var advice_load_timer = null;

$(document).ready(function() {
	// ie7 bug fix - incorrect positioning in relatively positioned container being inside table
	$('div.fixme').each(function() {
		var w=$(this).siblings('img').width();
		if (w === null)
			w = $(this).parent().find('img').width();
		$(this).width(w);
		$(this).show();
	});
	
	$([
		['#gall_link', $('#gall_link').parent(), 'gall_hover'],
		['#more_foto_link', $('#more_foto_link').parent(), 'more_foto_hover'],
		['#developer_link', $('#developer_link').parent(), 'ears']
	]).each(function() {
		var el = this;
		$(el[1]).addClass(el[2]);
		$(el[0]).mouseenter(function() { $(el[1]).addClass(el[2]); });
		$(el[0]).mouseleave(function() { $(el[1]).removeClass(el[2]); });
		$(el[1]).removeClass(el[2]);
	});

	$('#another_advice_link').click(function() {
		clearTimeout(advice_load_timer);
		current_id = $('#current_advice_id').val();
		$('#advice_container').hide();
		$('#advice_ajax_loader').fadeIn(50);
		$.getJSON(js_options.base_url+'ajax/load_advice/'+current_id, function(json) {
			$('#advice_ajax_loader').hide();//fadeOut(200);
			$('#advice_container').html(json.advice).show();
			//advice_load_timer = setTimeout(function() {$('#advice_container').fadeIn(100);}, 260);
			$('#current_advice_id').val(json.id);
		});
	});
});

