var NewsTicker = {

	/**
	 * set some properties...
	 */
	"ticking": null, 
	"current_id": null, 
	"current_index": null, 
	"available": [], 
	"timeout_animation": 500, 
	"timeout_tick": 5000, 

	/**
	 * function move()
	 *
	 *	manually move to next strip
	 *
	 * @param int $id (target strip ID)
	 */
	"move": function(id) {
	
		/**
		 * disable automatic ticking if running
		 */
		if (this.ticking != null) {
		 	clearTimeout(this.ticking);
		 	this.ticking = null;
		}

		/**
		 * tick
		 */
		this.tick(id);
	}, 

	/**
	 * function tick()
	 *
	 *	switch stripes with opacity animation
	 *
	 * @param int $id (target strip ID)
	 */
	"tick": function(id) {

		/**
		 * mark all nt_link items as not selected, select current one
		 */
		$(".nl_link").removeClass("selected");
		$("#nl_" + id).addClass("selected");

		/**
		 * if something is displayed, hide it (we will set display to none for
		 * MSIE, in all other browsers we can use opacity animation)
		 */
		if (this.current_id != null && id != this.current_id) {
			if ("\v" == "v") {
				$("#nt_" + this.current_id).css("display", "none");
			} else {
				$("#nt_" + this.current_id).animate({opacity: 0}, this.timeout_animation);
			}
		}

		/**
		 * display selected strip, animate for non-MSIE browsers
		 */
		$("#nt_" + id).css("display", "block");
		if ("\v" != "v") $("#nt_" + id).animate({opacity: 1}, this.timeout_animation);

		/**
		 * set currently displayed strip to selected one
		 */
		this.current_id = id;
	}, 

	/**
	 * function autotick()
	 *
	 *	determine next item to display and automatically update
	 */
	"autotick": function() {

		/**
		 * determine key of next strip from array
		 */
		if (this.current_id != null) {
			this.current_index++;
			if (!this.available[this.current_index]) this.current_index = 1;
		} else {
			this.current_index = 1;
		}

		/**
		 * move to next strip
		 */
		this.move(this.available[this.current_index]);

		/**
		 * call self, set autotick to true
		 */
		clearTimeout(this.ticking);
		this.ticking = setTimeout("NewsTicker.autotick();", this.timeout_tick);
	}
}


Slider = {

	/**
	 * set some properties
	 */
	"moving": false,
	"automove_direction": "left",
	"automove_timeout": 3000,
	"automove_action": null,

	/**
	 * function move()
	 */
	"move": function (direction) {

		/**
		 * get step width, get current list left position, get ammount of list items
		 */
		this.step_width = parseInt($("#homepage_action_products_content li").css("width")) + parseInt($("#homepage_action_products_content li").css("margin-right"));
		this.list_left = parseInt($("#homepage_action_products_content").css("left"));
		this.list_items = $("#homepage_action_products_content").children().length;

		/**
		 * count minimum left position
		 */
		this.min_left = 0 - (this.step_width * (this.list_items - 6) + parseInt($("#homepage_action_products_content li").css("margin-right")));

		/**
		 * switch by direction, count next left position, move if permitted and
		 * not moving right now
		 */
		switch (direction) {
			case "left":
				this.next_left = this.list_left - this.step_width;
				if (this.next_left >= this.min_left && this.moving == false) {
					this.moving = true;
					$("#homepage_action_products_content").animate({left: this.next_left}, 400, function() { Slider.moving = false; })
				}
				break;

			case "right":
				this.next_left = this.list_left + this.step_width;
				if (this.next_left <= 0 && this.moving == false) {
					this.moving = true;
					$("#homepage_action_products_content").animate({left: this.next_left}, 400, function() { Slider.moving = false; })
				}
				break;
		}
	},

	/**
	 * function automove()
	 */
	"automove": function() {

		/**
		 * continue only if there is more than 6 childs in list
		 */
		if ($("#homepage_action_products_content").children().length > 6) {

			/**
			 * determine next automove direction or keep current
			 */
			switch (this.automove_direction) {
				case "left":
					if ((this.list_left - this.step_width) < this.min_left) {
						this.automove_direction = "right";
						this.move(this.automove_direction);
					}
					break;

				case "right":
					if ((this.list_left + this.step_width) > 0) {
						this.automove_direction = "left";
						this.move(this.automove_direction);
					}
					break;
			}

			/**
			 * move item and set timeout
			 */
			this.automove_action = setTimeout("Slider.move('" + this.automove_direction + "'); Slider.automove();", this.automove_timeout);
		}
	}, 

	/**
	 * function stop()
	 */
	"stop": function() {
		clearTimeout(this.automove_action);
		this.automove_action = null;
	}
}


var TP = {
	"language_code": "", 
	"url_more_info": "", 
	"url_payment": "", 
	"text_price_free": "", 
	"text_more_info": "", 
	"text_more_info_title": "", 
	"text_from_order_price": "", 
	"error_loading_payments": "", 
	"language_id": 0, 
	"currency_id": 0, 
	"country_id": 0, 
	"transport_id": 0, 
	"payments": null, 
	"payment_id": 0, 
	"price_vat_string": "", 


	/**
	 * function get_payments()
	 *
	 *	set transport ID, call ajax request for payments
	 *
	 *	@param int $transport_id "selected transport's ID"
	 */
	"get_payments": function(transport_id) {

		/**
		 * set transport ID if found
		 */
		if (typeof(transport_id) != "undefined") this.transport_id = transport_id;

		/**
		 * call for JSON object with payments
		 */
		$.ajax({url: "/get_payments.php?language_id=" + this.language_id + "&currency_id=" + this.currency_id + "&country_id=" + this.country_id + "&transport_id=" + this.transport_id, success: function(data) { TP.payments = $.parseJSON(data); TP.process_payments(); }, error: function() { TP.error("payments_loading"); }, cache: false});
	}, 


	/**
	 *	function process_payments()
	 *
	 * traverse payments object, write into page
	 */
	"process_payments": function() {
		if (this.payments != null) {

			/**
			 * remove all current content
			 */
			$("#payment_types").empty();

			/**
			 * insert all payments
			 */
			for (var i in this.payments) {
				var payment = "<p class=\"transport_payment_row\">";
				payment += "<input type=\"radio\" name=\"payment_id\" id=\"payment_id_" + this.payments[i].id + "\" value=\"" + this.payments[i].id + "\"" + (this.payment_id == this.payments[i].id ? " checked=\"checked\"" : "") + " />";
				payment += "<label class=\"pointer\" for=\"payment_id_" + this.payments[i].id + "\">&nbsp;" + this.payments[i].name + "</label>";

				if (this.payments[i].price_free == "yes") {
					payment += " - " + this.text_price_free;
				} else {
					payment += " - " + (this.payments[i].price_type == "fixed" ? this.payments[i].price_vat_formatted : this.payments[i].price_percent_formatted + "% " + this.text_from_order_price);
				}

				if (this.payments[i].description == "yes") payment += " (<a href=\"/" + this.language_code + "/" + this.url_more_info + "/" + this.url_payment + "/" + this.payments[i].id + "\" title=\"" + this.text_more_info_title + "\" class=\"fancyframe\">" + this.text_more_info + "</a>)";

				payment += "</p>";

				$("#payment_types").append(payment);
			}

			/**
			 * because fancybox searches for element when DOMready is fired, we must
			 * recall it here to be able to display descriptions in fancybox
			 */
			$("a.fancyframe").fancybox({overlayColor: "#dcdcdc", titleShow: false, type: "iframe", width: 666, height: 350});
		}
	}, 


	/**
	 * function error()
	 */
	"error": function(error_type) {
		switch (error_type) {
			case "payments_loading":
				$("#payment_types").html("<p class=\"msg_error\">" + this.error_loading_payments + "</p>");
				break;
		}
	}
}


var TabSwitcher = {
	"target_id": null, 

	/**
	 * function init()
	 *
	 *	register clicks, select tab stored in cookie / first tab in list
	 */
	"init": function() {

		/**
		 * register events for clicks
		 */
		$(".product_detail_tab_switcher li a").click(function(event) { TabSwitcher.switch_tab(event); return false; });	

		/**
		 * if tab stored in cookie exists, switch to it. else trigger click on
		 * first tab switcher link
		 */
		if (document.getElementById("tab_" + $.cookie("eshop_tab"))) {
			this.target_id = $.cookie("eshop_tab");
			this.switch_tab(false);
		} else {
			$(".product_detail_tab_switcher li:first-child a").trigger("click");
		}
	}, 

	/**
	 * function switch_tab()
	 *
	 *	switch tab in area
	 *
	 *	@param object|boolean $event (jQuery event object)
	 */
	"switch_tab": function(event) {

		/**
		 * get target tab's ID
		 */
		if (event != false) {
			this.target_id = new String(event.target);
			this.target_id = this.target_id.split("#tab_")[1];
		}

		/**
		 * hide all tabs, unset all selected classes in switcher
		 */
		$(".product_detail_tabarea .product_detail_tab").each(function() { this.style.display = "none"; });
		$(".product_detail_tab_switcher li a").each(function() { this.className = ""; });

		/**
		 * show desired tab, select link in switcher
		 */
		$("#tab_" + this.target_id).css("display", "block");
		$("#link_" + this.target_id).addClass("selected");

		/**
		 * set cookie to remember tab
		 */
		$.cookie("eshop_tab", this.target_id, { path: "/", expires: 365 });
	}
}

var As = {
	"list_as": [], 
	"list_options": [], 

	/**
	 * define basic options as constructor with simple return
	 */
	"options": function() {
		return {
			"varname": "pattern", // Name of variable passed to script holding current input.
			"script": null, // Either: A string containing the path to the script that returns the results in XML format. (eg, "myscript.php?") Or: A function that accepts on attribute, the autosuggest field input as a string, and returns the path to the result script.
			"callback": null, // A function taking one argument: an object {id:"1", value:"Foobar", info:"Cheshire"}
			"minchars": 1, // Length of input required before AutoSuggest is triggered.
			"delay": 250, // Number of milliseconds before an AutoSuggest AJAX request is fired.
			"timeout": 2500, // Number of milliseconds before an AutoSuggest list closes itself.
			"cache": false, // Whether or not a results list should be cached during typing.
			"shownoresults": true, // Whether to display a message when no results are returned.
			"noresults": "Žádné výsledky.", // No results message.
			"json": true, // Whether or not a results are returned in JSON format. If not, script assumes results are in XML.
			"maxentries": 10 // The maximum number of entries being returned by the script.
		}
	}, 

	/**
	 * function add()
	 *
	 *	add new AutoSuggest to the list
	 *
	 *	@param string $element "ID of target input element"
	 *	@param string $language_id "language ID to use"
	 *	@param function $callback "callback function which will handle return"
	 *	@param object $options "custom options, will be used instead of defaults"
	 */
	"add": function(element, language_id, callback, options) {
		this.list_options[this.list_as.length] = typeof(options) == "undefined" ? new this.options : options;
		this.list_options[this.list_as.length].script = "/json.php?type=simple_search&language_id=" + language_id + "&";
		this.list_options[this.list_as.length].callback = callback;

		this.list_as[this.list_as.length] = new bsn.AutoSuggest(element, this.list_options[this.list_as.length]);
	}
}


var Paging = {
	"prompt_question": null, 
	"url": null,
	"url_page_number": null, 
	"page_number": null, 
	"total_pages": null, 

	/**
	 * function set()
	 *
	 *	check/set page number eneterd via javascript prompt
	 */
	"set": function() {
		var target_page = parseInt(prompt(this.prompt_question, this.page_number));
		if (target_page > 0 && target_page <= this.total_pages && this.url != null) window.parent.window.location.href = this.url + "/" + this.url_page_number + target_page;
	},

	/**
	 * function move()
	 *
	 *	move to next / previous page depending on keycode
	 *
	 *	@param int $code (keycode of pressed key)
	 */
	"move": function(code) {
		if (code == 37 && this.page_number > 1) window.parent.window.location.href = this.url + "/" + this.url_page_number + (this.page_number - 1);
		if (code == 39 && this.page_number < this.total_pages) window.parent.window.location.href = this.url + "/" + this.url_page_number + (this.page_number + 1);
	}
}

