(function($) {
	$.log = function(a) {
		if (window.console && window.console.log) console.log(a);
	};
	$.fn.setAngle = function(a) {
		return this.each(function() {
			var data = v.store[$(this).getItemId()];
			var rot = parseInt(a) || 0;
			if (rot > 11) rot = 11;
			if (rot < 0) rot = 0;
			data.rot = rot;
			var img = '' + rot;
			if ($(this).hasClass('viewed') && !$(this).hasClass('active')) {
				img += 'r.gif';
			} else if ($(this).hasClass('active')) {
				img += '_.gif';
			} else {
				img += '.gif';
			}
			$(this).css('background', 'transparent url(/img/cam1/' + img + ') 50% 50% no-repeat');
		});
	};
	$.fn.setActive = function(b) {
		return this.each(function() {
			var data = v.store[$(this).getItemId()];
			if (b == false) {
				$(this).removeClass('active');
			} else {
				$('div.draw').removeClass('active');
				$(this).addClass('active');
			}
			$(this).setAngle(data.rot);
		});
	};
	$.fn.isShown = function() {
		var ret = true;
		this.each(function() {
			var $this = $(this);
			ret = (($this.css('display') != 'none') && ($this.css('visiblity') != 'hidden')) ? true : false;
		});
		return ret;
	};
	$.fn.Button = function(cb) {
		return this.each(function() {
			var $this = $this || $(this);
			var def = {
				enabled: false,
				carousel: null,
				counter: null,
				init: null,
				click: null
			};
			this.param = $.extend({}, def, cb || {});
			this.enable = this.enable ||
			function() {
				if (typeof arguments && arguments.length > 0) {
					var disable = !!/disable/.test(this.className);
					if (arguments[0] === true && disable) {
						$this.css('background-position', '0px').removeClass('disable');
					}
					if (arguments[0] === false && !disable) {
						$this.css('background-position', '-96px');
						this.className += ' disable';
					}
				}
				this.param.enabled = !/disable/.test(this.className);
				return this.param.enabled;
			};

			$this.mouseenter(function() {
				if (this.enable()) {
					$this.css('background-position', '-32px').css('cursor', 'pointer');
				} else {
					$this.css('cursor', 'default !important');
				}
			}).mouseleave(function() {
				if (this.enable()) {
					$this.css('background-position', '0px');
				}
			}).mousedown(function() {
				if (this.enable()) {
					$this.css('background-position', '-64px');
				}
			}).mouseup(function() {
				if (this.enable()) {
					$this.css('background-position', '-32px');
				}
			}).click(function() {
				if (this.enable() && this.param.counter !== null && this.param.carousel !== null) {
					var current = this.param.counter.current;// || 1;
					var carousel = this.param.carousel;
					var counter = this.param.counter;
					if (this.enable() && typeof this.param.click === 'function') {
						this.param.click.call(this, this.param.counter);
					}
				}
				return false;
			});
			this.enable(this.param.enabled);
			if (typeof this.param.init === 'function') {
				this.param.init.call(this, this.parentNode);
			}
			$(this).bind('enable', function(event, state) {
				this.enable(state);
			});
		});
	};
	$.fn.Thumb = function() {
		return this.each(function() {
			var $this = $(this);
			$this.mouseenter(function() {
				$this.addClass('thumb-hover');
			}).mouseleave(function() {
				$this.removeClass('thumb-hover');
			});
		});
	};
	$.fn.Photo = function() {
		return this.each(function() {
			this.show = function() {
				$('img:visible', this.parentNode).hide();
				$(this).show();
			};
		});
	};
	$.getCurrentFloor = function() {
		var id = $('li.fp-item.ui-tabs-selected').attr('id').match(/(\d+)/i);
		return parseInt(id[1]);
	};
	$.fn.getItemId = function() {
		var id = 0;
		this.each(function() {
			id = (this.id.match(/\D+(\d+)/i))[1];
		});
		return parseInt(id);
	};
	$.fn.getFloorId = function() {
		var id = 0;
		this.each(function() {
			var m = $.trim(this.className).match(/floor-(\d+)/i);
			if (m && m.length > 1) {
				id = parseInt(m[1]);
			}
		});
		return id;
	};
})(jQuery);
$(function() {
	function moveCams() {
		$('div.draw').each(function() {
			if (v.store.length == 0) return false;
			$(this).hide();
			var pt = $('img.floorplan-image:visible').position();
			var store = eval(v.store[parseInt(this.id.substr(4))]);
			var x = (pt.left + store.x);
			var y = (pt.top + store.y);
			$(this).css({left:x,top:y}).show();
		});
		if ($('li.ui-tabs-selected > a').size() > 0) {
			var floor = 'floor-' + parseInt($('li.ui-tabs-selected > a').attr('href').substr(6));
			$('div.draw:not(.' + floor + ')').addClass('disabled').hide();
		};
	};
	var $tabs = $('div.fp-content').tabs({
		selected: 0
	});
	$('div.fp-content').bind('tabsselect', function(event, ui) {
		$('body').removeAttr('style');
		var fid = parseInt(ui.panel.id.substr(5));
		$('img.floorplan-image').hide();
		$('div.draw').hide();
		$('div.draw.floor-' + fid).removeClass('disabled').show();
		$('div.draw:not(.active)').setActive(false);
		$('img.floorplan-image.fpid-'+fid).show('fast', function() {
			var pt = $(this).position();
			$('div.draw.floor-'+fid+':visible').each(function() {
				var data = eval(v.store[parseInt(this.id.substr(4))]);
				var x = parseInt(pt.left+data.x);
				var y = parseInt(pt.top+data.y);
				$(this).css({left:x,top:y}).show();
			});
		});
	});
	$('#carousel').jcarousel({
		scroll: 5,
		size: $('img.thumb').size(),
		initCallback: function(carousel) {
			var $carousel = carousel;
			$('div.draw').each(function() {
				var $this = $(this);
				$this.mouseenter(function() {
					var id = $this.getItemId();
					if ($this.isShown()) {
						$carousel.scroll(id);
						$('img.thumb.thumb-item-' + id).toggleClass('thumb-hover');
						$('div.internal-class-photo:visible').hide();
						$('div.internal-element-' + id).show();
						$('.img-item-' + v.store[id].id).show();
						$('div.counter').trigger('refresh', [id]);
						// $ref 03.01.2011
						if (!$(this).hasClass('active') && $(this).attr('rot')) {
							$(this).css({backgroundImage:'url(/img/cam1/'+$(this).attr('rot')+'o.gif)'});
						};
					};
					return false;
				}).mouseleave(function() {
					// $ref 2010-05-17
					$('.thumb-active').each(function() {
						var id = $(this).getItemId();
						$('div.internal-class-photo:visible').hide();
						$('div.internal-element-' + $(this).attr('idx')).show();
						$('.img-item-' + id).each(function() {
							var x = parseInt(this.id.substr(4));
							$('div.counter').trigger('refresh', [x]);
						}).show();
						var m = /thumb-item-(\d+)/.exec(this.className);
						if (m && m.length > 0) {
							$carousel.scroll(m[1]);
						};
					});
					$('img.thumb').removeClass('thumb-hover');
					// $ref 03.01.2011
					if (!$(this).hasClass('active')) {
						$(this).css({backgroundImage:'url(/img/cam1/'+$(this).attr('rot')+($(this).hasClass('viewed') ? 'r.gif' : '.gif')+')'});
					};
					return false;
				}).click(function() {
					$('div.draw.active').addClass('viewed');
					$('div.draw').setActive(false);
					$this.setActive(true);
					$('img.thumb').removeClass('thumb-active thumb-hover');
					$('img.thumb-item-' + this.id.substr(4)).addClass('thumb-active');
					return false;
				});
			});
			$('img.thumb').Thumb().each(function() {
				var $this = $(this);
				$(this).mouseenter(function() {
					var id = $(this).getItemId();
					$(this).toggleClass('hover', !$(this).hasClass('active'));
					$('div.internal-class-photo:visible').hide();
					$('div.internal-element-' + $(this).attr('idx')).show();
					$('.img-item-' + id).each(function() {
						var x = parseInt(this.id.substr(4));
						$('div.counter').trigger('refresh', [x]);
					}).show();
					return false;
				}).mouseleave(function() {
					// $ref 2010-05-16
					$('.thumb-active').each(function() {
						var id = $(this).getItemId();
						$('div.internal-class-photo:visible').hide();
						$('div.internal-element-' + $(this).attr('idx')).show();
						$('.img-item-' + id).each(function() {
							var x = parseInt(this.id.substr(4));
							$('div.counter').trigger('refresh', [x]);
						}).show();
						var m = /thumb-item-(\d+)/.exec(this.className);
						if (m && m.length > 0) {
						//	$carousel.scroll(m[1]);
						};
					});
					$(this).removeClass('hover');
					return false;
				}).click(function() {
					if ($('div.draw').size() > 0) {
						$('div.draw.active').addClass('viewed');
						$('div.draw').setActive(false);
						$('div.draw.draw-item-' + this.id.substr(4)).setActive(true);
					};
					var idx = parseInt($(this).attr('idx')) || 0;
					if (v.store.length >= (idx + 1)) {
						if (v.store[idx].fid > 0) {
							var curr = $('li.fp-item.fpid-' + v.store[idx].fid).attr('fid');
							if (curr == parseInt(curr)) {
								$tabs.tabs('select', parseInt(curr));
							};
						};
						$('img.thumb').removeClass('thumb-hover thumb-active');
						$('img.thumb.thumb-item-' + idx).addClass('thumb-active');
					};
					return false;
				});
			});
			$('div.scroll').each(function() {
				var owner = this;
				this.counter = {};
				this.next = {};
				this.prev = {};
				this.collection = [];
				$('div.internal-class-photo').each(function() {
					owner.collection.push(this);
				});
				$('div.counter', owner).each(function() {
					owner.counter = this;
					this.count = $('div.internal-class-photo').size();
					this.current = (this.count > 0) ? 1 : 0;
					this.refresh = function() {
						this.innerHTML = '' + this.current + '&nbsp;/&nbsp;' + this.count;
						var i = Math.max((this.current - 1), 0);
						var p = (this.count > 0 && i > 0) ? true : false;
						var n = (this.count > 0 && this.current < this.count) ? true : false;
						$('div.prev').trigger('enable', [p]);
						$('div.next').trigger('enable', [n]);
					};
					this.inc = function() {
						if (this.count > this.current) {
							this.current += 1;
							this.refresh();
						};
						return !this.is_last();
					};
					this.dec = function() {
						if (this.current > 1) {
							this.current -= 1;
							this.refresh();
						};
						return !this.is_first();
					};
					this.is_first = function() {
						return (this.current === 1);
					};
					this.is_last = function() {
						return (this.count === this.current);
					};
					this.refresh();
					$(this).bind('refresh', function(event, id) {
						id = Math.max(id, 0);
						if (id <= this.count) {
							this.current = Math.min(++id, this.count);
							this.refresh();
						};
					}).bind('increment', function() {
						this.inc();
						var i = Math.max((this.current - 1), 0);
						$('img.thumb.thumb-item-' + i).trigger('click');
						$('div.internal-class-photo:visible').hide();
						$(owner.collection[i]).show();
						$carousel.scroll(i);
					}).bind('decrement', function() {
						this.dec();
						var i = Math.max((this.current - 1), 0);
						$('img.thumb.thumb-item-' + i).trigger('click');
						$('div.internal-class-photo:visible').hide();
						$(owner.collection[i]).show();
						$carousel.scroll(i);
					});
				});
				$('div.next', owner).Button({
					enabled: true,
					counter: owner.counter,
					carousel: carousel,
					init: function(parent) {
						if (typeof parent.next !== 'undefined') {
							parent.next = this;
						};
					},
					click: function(counter) {
						$(counter).trigger('increment');
					}
				});
				$('div.prev', owner).Button({
					enabled: false,
					counter: owner.counter,
					carousel: carousel,
					init: function(parent) {
						if (typeof parent.prev !== 'undefined') {
							parent.prev = this;
						};
					},
					click: function(counter) {
						$(counter).trigger('decrement');
					}
				});
			});
		}
	});
	$(window).resize(function() {
		try {
			var fpid = parseInt($('li.ui-tabs-selected > a').get(0).hash.substr(6));
			$('div.draw.floor-'+fpid+':visible').each(function() {
				var pt = $('img.floorplan-image:visible').position();
				var data = eval(v.store[parseInt(this.id.substr(4))]);
				var x = parseInt(pt.left + data.x);
				var y = parseInt(pt.top + data.y);
				$(this).css({left:x,top:y});
			});
		} catch (e) { }
	});
	$('div.draw').each(function() {
		var pt = $('img.floorplan-image:visible').position();
		var data = eval(v.store[parseInt(this.id.substr(4))]);
		var x = parseInt(pt.left+data.x);
		var y = parseInt(pt.top+data.y);
		$(this).css({left:x,top:y}).show();
	});
	$('li.ui-tabs-selected').each(function() {
		var flr = parseInt(this.id.substr(3));
		$('div.draw').hide();
		$('div.floor-' + flr).show();
	});
	$('img.thumb-item-0').click(); // $ref 2010-05-16
});
