/*
* Copyright (c) 2008 Vinter (www.vinterwebb.se)
* Date: 2008-11-11
* Version 1.0
*/

var Vinter = {

	SubMenu: function (settings) {

		var settings = $.extend({
			nav: "ul#nav > li",
			main_link: ".topnav-main-link a",
			menu_trigger: ".topnav-submenu-trigger",
			menu_active_class: "active-submenu",
			menu_fade_speed: "fast"
		}, settings)

		/* Add hover & click events events */
		$(settings.nav)
			.click(function () {
				hideSub();
				//alert(1);
				document.location.href = $(settings.main_link, this).attr("href");
			})
			.hover(function () {
				$(this).addClass("hover");
				hideSub();
			},
			function () {
				$(this).removeClass("hover");
			});


		$(".leftcol")
            .hover(function () {
            	hideSub();
            });

		$("img")
            .hover(function () {
            	hideSub();
            });
		$(".cycle")
            .hover(function () {
            	hideSub();
            });

		/* Add menu toggle */
		$(settings.menu_trigger).hover(function (e) {
			$("ul", this.parentNode)
				.css({ opacity: 0.9 })
				.fadeIn(settings.menu_fade_speed);
			$(this.parentNode).addClass(settings.menu_active_class);
			// $(document).click(hideSubMenu);
		});


		$("footer").blur(function () {
			alert(123);
		});

		function hideSub() {
			var d = $("." + settings.menu_active_class);
			$("ul", d).fadeOut(settings.menu_fade_speed);
			d.removeClass(settings.menu_active_class);
			$(document).unbind("click");
		}

		function hideSubMenu(e) {
			var d = $("." + settings.menu_active_class);
			$("ul", d).fadeOut(settings.menu_fade_speed);
			d.removeClass(settings.menu_active_class);
			$(document).unbind("click");
		}
	},

	CurrencyConverter: function () {
		if ($("#currencyConverter").length == 0)
			return;

		$("#convertSubmit").click(function () {
			var fromCurrency = $("#fromCurrency").val();
			var toCurrency = $("#toCurrency").val();
			var amount = $("#convertAmount").val();

			$.getJSON(Vinter.baseUrl + "handlers/currencyconverter.ashx", { t: toCurrency, f: fromCurrency, a: amount }, function (json) {
				$("#conversionResult").html(json.result).show();
			});
			return false;
		});
	},

	Init: function () {
		// Append class to body if IE < 7, Do this so we don't need conditional comments 
		if ($.browser.msie === true && parseInt($.browser.version) <= 6) {
			$("body").addClass("png8");
			document.execCommand('BackgroundImageCache', false, true);
		}

		Vinter.SubMenu({
			nav: "ul#nav > li"
		});

		Vinter.CurrencyConverter();

		$("#bookingMainIframe").load(function () {
			// only for development
			$("bookingLoader").hide();

			// use the line below in production
			//$("#bookingMainIframe").css("height", 0);
		});


		if (typeof $().formHints == "function") {
			$("form").formHints({
				css_class: "preview-value",
				elements: ".text-hint"
			});
		}

		$("#searchForm .text").bind("focus", function () {
			$(this).css({
				width: "181px"
			});
		});

		$("#searchForm .text").bind("blur", function () {
			$(this).css({
				width: "181px"
			});
		});

		$("#searchBottomForm .text").bind("focus", function () {
			$(this).css({
				width: "181px"
			});
		});

		$("#searchBottomForm .text").bind("blur", function () {
			$(this).css({
				width: "181px"
			});
		});


		$('#selectBookmark').bookmark({ hint: '{s}', sites:
            ['facebook', 'delicious', 'digg', 'google', 'stumbleupon', 'twitter']
		});

		$(".cycle").cycle({
				pager: "#pager",
				pagerEvent: 'click',
				timeout: 5000,
				pause: 1,
				next: "#nextslide",
				after: function () {
					var next = $(this).next();

					if (next.length == 0)
						next = $(this.parentNode).children().eq(0);

					$("#nextslide").html($("h2", next).text());

				}
			});

		$('#pauseButton').click(function () {

			$('.cycle').cycle('pause');
			$('#pauseButton').hide();
			$('#playButton').show();
		});

		$('#playButton').click(function () {
			$('.cycle').cycle('resume');
			$('#playButton').hide();
			$('#pauseButton').show();
		});

		$('#playButton').hide();
		$('#pauseButton').show();

		/* language dropdown */

		$("#showLanguages").click(function () {
			$("#tools").slideToggle("fast");
		});

	}

}

// Add load events
$().ready(Vinter.Init);
