teatras = {};

teatras.initialize = function() {
	
	teatras.initializeByNodeList($("ul.main-menu"), "teatras.mainmenu");
	teatras.initializeByNodeList($(".calendar-slider-small"), "teatras.calendar");
	teatras.initializeByNodeList($(".__slider"), "teatras.slider");
	teatras.initializeByNodeList($("*[tooltip]"), "teatras.tooltip");
	new teatras.cart();
	new teatras.toggle();
	new m2.autoscroll();
	new m2.ajax();
	new teatras.tabs({classActive : "active"});
	new teatras.checkboxes();	
	
	$(".lightbox").lightBox({
		imageBlank: 'assets/images/lightbox/lightbox-blank.gif',
		imageLoading: 'assets/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 'assets/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: 'assets/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: 'assets/images/lightbox/lightbox-btn-next.gif',
		txtImage : "Paveikslėlis",
		txtOf : "iš"
	});
	
}

teatras.initializeByNodeList = function(nodes, className) {
	for (var i = 0; i < nodes.length; i++) {
		var options = {container : nodes[i]};
		eval("new " + className + "(options)");
	}
}

teatras.mainmenu = function(options) {
	
	this.options = options;
	this.container = $(this.options.container);
	
	this.container.children("li.menu-item").mouseover($.proxy(this.mouseover, this));
	this.container.children("li.menu-item").mouseout($.proxy(this.mouseout, this));
	
}

teatras.mainmenu.prototype.findActive = function(event) {
	var el = $(event.target);
	if (!el.hasClass("menu-item")) {
		el = el.parents(".menu-item");
	}
	return el;
}

teatras.mainmenu.prototype.mouseover = function(event) {
	var el = this.findActive(event);
	el.find("div.sub-menu-container").show();
}

teatras.mainmenu.prototype.mouseout = function(event) {
	var el = this.findActive(event);
	el.find("div.sub-menu-container").hide();
}

teatras.calendar = function(options) {
	
	this.options = options;
	this.container = $(this.options.container);
	this.range = [this.container.attr("from"), this.container.attr("to")];
	
	this.container.find("a.next, a.prev").click($.proxy(this.gotoDeltaEvent, this));
	this.reset();
	
}

teatras.calendar.prototype.getActive = function() {
	return this.container.children("div:visible");
}

teatras.calendar.prototype.reset = function() {
	var el = this.getActive();	
	this.container.find("a.prev").css("display", el.attr("date") == this.range[0] ? "none" : "");
	this.container.find("a.next").css("display", el.attr("date") == this.range[1] ? "none" : "");
}

teatras.calendar.prototype.gotoDeltaEvent = function(event) {
	this.gotoDelta($(event.target).hasClass("next") ? 1 : -1);
}

teatras.calendar.prototype.gotoDelta = function(delta) {	
	var nodeActive = this.getActive();
	var nodeNewActive = null;
	if (delta > 0) {
		nodeNewActive = nodeActive.next("div");
	} else {
		nodeNewActive = nodeActive.prev("div");
	}
	if (nodeNewActive.length > 0) {
		nodeActive.hide();
		nodeNewActive.show();
	} else {
		var listener = {
			"preloader" : new teatras.preloader({container : this.container}), "delta" : delta,
			"parent" : this
		};
		listener.complete = function(reponse) {
			this.preloader.destroy();
			if (reponse) {
				if (this.delta > 0) {
					this.parent.getActive().after(reponse);
				} else {
					this.parent.getActive().before(reponse);
				}
				this.parent.gotoDelta(this.delta);
			}
		}
		$.get(
			this.container.attr("uri") + nodeActive.attr("date") + ",delta." + delta, 
			$.proxy(listener.complete, listener)
		);
	}
	this.reset();
}

teatras.preloader = function(options) {

	this.options = options;
	this.container = $(this.options.container);
	
	this.containerPreloader = document.createElement("DIV")	
	$(document.body).append(this.containerPreloader);
	this.containerPreloader = $(this.containerPreloader);
	this.containerPreloader.addClass("__preloader__");
	this.containerPreloader.html("<br />");
	this.containerPreloader
		.offset(this.container.offset())
		.width(this.container.width())
		.height(this.container.height());
	
}

teatras.preloader.prototype.destroy = function() {
	this.containerPreloader.remove();
}

teatras.slider = function(options) {
	
	this.options = options;
	this.container = $(this.options.container);
	
	this.vertical = this.container.hasClass("__slider_vertical");
	this.positionProperty = this.vertical ? "top" : "left";
	
	this.next = this.container.find(".__slider_next");
	this.prev = this.container.find(".__slider_prev");
	this.containerSlides = this.container.find(".__slider_container");
	this.slides = this.container.find(".__slider_slide");
	this.slideSize = 100;
	if (this.slides.length > 0) {
		var slide = $(this.slides.get(0));		
		if (this.vertical) {
			this.slideSize = slide.height();
		} else {
			this.slideSize = slide.width() + 
				parseInt(slide.css("paddingLeft")) + parseInt(slide.css("paddingRight"));
		}
	}
	this.containerSlides.css(this.vertical ? "height" : "width", (this.slideSize * this.slides.length) + "px");
	this.next.click($.proxy(this.gotoDeltaEvent, this));
	this.prev.click($.proxy(this.gotoDeltaEvent, this));
	
	this.reset();
	
}

teatras.slider.prototype.getPosition = function() {
	return Math.floor(-this.containerSlides.position()[this.positionProperty] / this.slideSize);
}

teatras.slider.prototype.reset = function() {
	var position = this.getPosition();
	this.prev.css("display", position > 0 ? "" : "none");
	this.next.css("display", position < this.slides.length - 1 ? "" : "none");
}

teatras.slider.prototype.gotoDeltaEvent = function(event) {
	this.gotoDelta($(event.target).hasClass("__slider_next") ? 1 : -1);
}

teatras.slider.prototype.gotoDelta = function(delta) {
	this.gotoPosition(this.getPosition() + delta);
}

teatras.slider.prototype.gotoPosition = function(position) {
	position = Math.min(this.slides.length - 1, Math.max(0, position));
	var css = {};
	css[this.positionProperty] = (-position * this.slideSize) + "px";
	this.containerSlides.animate(css, 250, $.proxy(this.reset, this));	
}

teatras.tabs = function(options) {	
	this.options = options;
	this.reset();	
}

teatras.tabs.prototype.reset = function() {
	$("*[tab_handle]").click($.proxy(this.click, this));
}

teatras.tabs.prototype.click = function(event) {
	var el = $(event.target);	
	if (!el.attr("tab_handle")) {
		el = el.parents("*[tab_handle]");
	}
	var tab_group = "[tab_group='" + el.attr("tab_group") + "']";
	$("*[tab]" + tab_group).hide();
	$("*[tab=" + el.attr("tab_handle") + "]" + tab_group).show();
	if (this.options.classActive) {
		$("*[tab_handle]" + tab_group).removeClass(this.options.classActive);
		el.addClass(this.options.classActive);
	}
}

teatras.toggle = function() {
	$("*[toggle]").click($.proxy(this.click, this));
}

teatras.toggle.prototype.click = function(event) {
	var el = $(event.target);
	var elTarget = $("*[toggle_target='" + el.attr("toggle") + "']");
	var className = el.attr("toggle_class_hide");	
	if (elTarget.hasClass(className)) {
		$("*[toggle_target]").addClass(className);
		elTarget.removeClass(className);
	} else {		
		elTarget.addClass(className);
	}
}

teatras.tooltip = function(options) {
	
	this.options = options;
	this.container = $(this.options.container);
	this.containerContent = $(".__tooltip__");
	this.container.mouseover($.proxy(this.show, this));	
	
}

teatras.tooltip.prototype.show = function(event) {
	var uri_buy = this.container.attr("tooltip_uribuy");
	this.containerContent.find(".name a")
		.html(this.container.attr("tooltip")).attr("href", this.container.attr("tooltip_uri"));
	this.containerContent.find(".fr a").css("display", uri_buy ? "" : "none").attr("href", uri_buy);
	var hall = this.container.attr("tooltip_hall");
	var elHall = this.containerContent.find(".fl a").css("display", hall ? "" : "none");
	elHall.attr("href", this.container.attr("tooltip_halluri"));
	elHall.html(hall);
	this.containerContent.css("display", "block");
	var position = this.container.offset();
	position.top = Math.round(position.top + this.container.height() - 10);
	position.left = Math.round(position.left + this.container.width() - 10);
	this.containerContent.css({left : position.left + "px", top : position.top + "px"});
	var handle = $.proxy(this.mousemove, this);
	$(document).unbind("mousemove", handle).mousemove(handle);
}

teatras.tooltip.prototype.mousemove = function(event) {
	var position2 = this.containerContent.offset();
	var position = this.container.offset();
	position.left -= 10;
	position.top -= 5;	
	if (
		(
			position.left > event.pageX || position.top > event.pageY ||
			position.left + this.container.width() + 30 < event.pageX || 
			position.top + this.container.height() + 10 < event.pageY
		) &&
		(
			position2.left > event.pageX || position.top > event.pageY ||
			position2.left + this.containerContent.width() + 10 < event.pageX || 
			position2.top + this.containerContent.height() + 10 < event.pageY
		)
	) {
		this.containerContent.css("display", "none");
		$(document).unbind("mousemove", $.proxy(this.mousemove, this));
	}	
}

teatras.cart = function() {
	this.reset();
}

teatras.cart.prototype.reset = function() {
	var handle = $.proxy(this.tocart, this);
	var handleMouse = $.proxy(this.rowMouse, this);
	var handleRemove = $.proxy(this.remove, this);
	var handleChange = $.proxy(this.change, this);
	var handleToggleCompany = $.proxy(this.toggleCompany, this);
	$("*[tocart]").unbind("click", handle).click(handle);
	$("*[uricartremove]").unbind("click", handleRemove).click(handleRemove);
	$("*[uricartchange]").unbind("keyup", handleChange).keyup(handleChange);
	$("table.shopcart-list tr")
		.unbind("mouseover", handleMouse).mouseover(handleMouse)
		.unbind("mouseout", handleMouse).mouseout(handleMouse);
		
	$("*[name='company']").unbind("change", handleToggleCompany).change(handleToggleCompany)
		.unbind("click", handleToggleCompany).click(handleToggleCompany)
	this.toggleCompany(null);
}

teatras.cart.prototype.load = function(uri) {
	var listener = {container : $(".cart-block"), parent : this}
	listener.preloader = new teatras.preloader({container : listener.container});
	listener.complete = function(response) {
		this.container.replaceWith(response);
		this.parent.reset();
		this.preloader.destroy();		
	}
	$.get(uri, $.proxy(listener.complete, listener));
}

teatras.cart.prototype.tocart = function(event) {
	var el = $(event.target);
	if (!el.attr("tocart")) {
		el = el.parents("*[tocart]");
	}
	this.load(el.attr("tocart"));
}

teatras.cart.prototype.rowMouse = function(event) {	
	var el = $(event.target);
	if (el.get(0).nodeName !== "TR") {
		el = el.parents("TR");
	}
	el.find("*[tocart],*[uricartremove]").css("visibility", event.type === "mouseover" ? "visible" : "hidden");
}

teatras.cart.prototype.remove = function(event) {
	var el = $(event.target);
	if (el.get(0).nodeName !== "A") {
		el = el.parents("a");
	}
	var elTr = el.parents("tr");
	if (elTr.hasClass("no-border")) {
		elTr.prev().addClass("no-border");
	}
	elTr.remove();
	var elTable = $("table.shopcart-list");
	if (elTable.find("tr").length <= 3) {
		elTable.find("tr").show();
	}
	var listener = {}
	listener.complete = function(response) {
		$(".total-sum").html(response);
	}
	$.get(el.attr("uricartremove"), $.proxy(listener.complete, listener));
}

teatras.cart.prototype.change = function(event) {
	var el = $(event.target);
	var listener = {el : el}
	listener.complete = function(response) {
		var parts = response.split(";");
		$(".total-sum").html(parts[1]);
		this.el.parent().parent().find("td.total_price").html(parts[0]);
	}
	var value = parseInt(el.val());
	if (isNaN(value)) {
		value = 1;
	}
	el.val(Math.max(1, value));
	$.post(el.attr("uricartchange"), {amount : el.val()}, $.proxy(listener.complete, listener));
}

teatras.cart.prototype.toggleCompany = function(event) {
	var nodes = $("*[name='company']");
	if (nodes.length > 0) {
		$(".company-subform").css("display", nodes.get(0).checked ? "" : "none");
	}
}

teatras.checkboxes = function() {
	this.nodes = $('div.donations label');
	this.nodes.click($.proxy(this.toggle, this));
}

teatras.checkboxes.prototype.toggle = function(event) {
	this.nodes.removeClass("checked");
	$(event.target).toggleClass("checked");
}

$(document).ready(teatras.initialize);
