$(function(){
	
	// Bookings on events
	var parent_inactive_class = "booking_parent_inactive";
	var rotator_speed = 7000;
	var $bookalt = $("div.booking_alternative");
	var $bookcont = $("div.booking_content");
	var $choose_dropdown = $("select[name='choose_an_event']");

	// Add listeners	
	$("> div", $bookalt)
		.bind("mouseover", alt_mouseover)
		.bind("mouseout", alt_mouseout)
		.bind("click", alt_mouseclick)
		.addClass("booking_alt_btn")
	
	$("> div > div", $bookcont).hide();
	
	$("#boka_bokanu_btn").bind("click", boka_bokanu_btn_click);

	$choose_dropdown
		.bind("change", drop_change)
		.trigger("change");
		
	$("img[src$='/event_boka_knapp.png']").bind("click", event_book_click);

	// Init	
	//if(!$choose_dropdown.attr("selectedIndex") > -1)
	//	$bookalt.parent().addClass(parent_inactive_class);
	
	//$bookcont.addClass(parent_inactive_class);
	
	//
	if($("#startpage_itunes_holder").length > 0) {
		var iteration = 1;
		$("#startpage_event_holder > .startpage_list_item").each(function(){
			var $item = $(this).clone();			
			if(iteration == 1) {
				$item.css("display","none");
			}
			$("#startpage_itunes_holder").prepend($item);
			
			if(iteration != 1) {
				$(this).hide();
			}
						
			iteration++;
		});

		setTimeout(function(){ startpage_rotate(); }, rotator_speed);
	}
	
	if($("div.startpage_rotator_left").length > 0) {
		var $left = $("div.startpage_rotator_left");
		var $right = $("div.startpage_rotator_right");
		var $rotator = $left.parent().find("div").eq(2);
		var max_len = $("div.startpage_rotator_item").length;
		var current_ix = 0;

		function scrollNext(e) {
			current_ix += 3;
			if(current_ix >= max_len) { current_ix -= 3; return }
			var $next = $("div.startpage_rotator_item:eq("+ current_ix +")", $rotator);			
			$rotator.scrollTo($next, 500, { easing:'linear', offset: 1 });
		}
		
		function scrollPrev(e) {
			current_ix -= 3;
			if(current_ix < 0) current_ix = 0;		
			var $prev = $("div.startpage_rotator_item:eq("+ current_ix +")", $rotator);
			$rotator.scrollTo($prev, 500, { easing:'linear', offset: 0 });
		}
		
		$left
			.bind("click", scrollPrev)
			.css("cursor","pointer");
		$right
			.bind("click", scrollNext)
			.css("cursor","pointer");
		
	}
	
	// Functions
	function event_book_click() {
		var $boka = $("#Bokamodul");
		$.scrollTo($boka, 800);
	}
	
	function drop_change() {
		var selected = this.selectedIndex; //$(this).attr("selectedIndex");
		if(selected > -1) {
			$bookalt.parent().removeClass(parent_inactive_class);
			$bookcont.removeClass(parent_inactive_class);

			var $activeOption = $("select[name='choose_an_event']").children(":eq("+ selected +")");
			var franDatum = $activeOption.attr("title");
			var title = $activeOption.text();
			var jsDate = new Date(franDatum.substring(0,4),(franDatum.substring(5,7)-1),franDatum.substring(8,10),franDatum.substring(11,13),franDatum.substring(14,16));
			var val = $(this).val();
			
			$("#boka_datumtid").html(jsDate.getDate() +"/"+ (jsDate.getMonth()+1) +"<br/>"+ jsDate.getHours() +":"+ ((jsDate.getMinutes() < 10) ? jsDate.getMinutes()+"0" : jsDate.getMinutes()));
			$("#boka_evenemang").html(title);
			
			if(val > '') {
				$("> div:first", $bookalt).show().trigger("click");
			} else {
				$("> div:first", $bookalt).hide();
				$("> div:nth-child(2)", $bookalt).trigger("click");
			}
		} else {
			$bookalt.parent().addClass(parent_inactive_class);
			$bookcont.addClass(parent_inactive_class);
			
			$("#boka_datumtid").html("");
			$("#boka_evenemang").html("");
		}
	}
	
	function alt_mouseover() {
		if($bookalt.parent().hasClass(parent_inactive_class))
			return;
			
		$(this).addClass("booking_alt_btn_over");
	}
	
	function alt_mouseout() {
		if($bookalt.parent().hasClass(parent_inactive_class))
			return;
			
		$(this).removeClass("booking_alt_btn_over");
	}
	
	function alt_mouseclick() {
		if($bookalt.parent().hasClass(parent_inactive_class))
			return;
		// Inits
		$(" > div", $bookalt).removeClass("booking_alt_active");
		$(this).addClass("booking_alt_active");
		$("> div > div", $bookcont).hide();
		// Get current
		var ix = $(" > div", $bookalt).index($(this));
		var $active = $("> div > div:eq("+ ix +")", $bookcont);

		$active.show();
		
		$bookcont.removeClass(parent_inactive_class);
	}
	
	var current_bigimage = 0;
	function startpage_rotate() {
		var $bigimage = $("#startpage_event_holder");
		var $smallimage = $("#startpage_itunes_holder");
		var num_items = $(".startpage_list_item", $bigimage).length;
		
		var $current = $(".startpage_list_item:eq("+ current_bigimage +")", $bigimage);
		var $current_small = $(".startpage_list_item:last", $smallimage);
		
		current_bigimage++;
		if(current_bigimage >= num_items)
			current_bigimage = 0;
			
		var $next = $(".startpage_list_item:eq("+ current_bigimage +")", $bigimage);
		var $next_small = $(".startpage_list_item:last", $smallimage);
		
		$next_small.hide().remove();
		$next_small.prependTo($smallimage);
		$current_small.slideDown("slow");
		
		$current.fadeOut("slow");
		$next.fadeIn("slow");
		
		setTimeout(function(){ startpage_rotate(); }, rotator_speed);
	}
	
	function boka_bokanu_btn_click() {
		// var $activeOption = $("option:eq("+ $choose_dropdown.selectedIndex +")", $choose_dropdown);
		var url =$choose_dropdown.val();
		if (url=='')return;
		url = url.replace("http://","");
		window.open("http://"+ url);
	}
	
	
	
	//
	// abg.
	var abg_abb = $('.abg_abonemang');
	var abg_botten = $('.abg_botten');
	var colors = ['gul', 'gron', 'bla', 'rosa'];
	
	if (abg_abb.length > 0) {
		var hiddHeight = abg_abb.height();
		var visHeight = hiddHeight + 99;
		abg_abb.css('overflow', 'hidden');
		abg_botten.show();
		
		for (var i = 0; i < colors.length; i++) {
			var color = colors[i];
			var btn = $('.abg_' + color);
			var txt = $('.abg_' + color + '_text');
			
			(function(btn, txt) {
				txt.hide();
				btn.hover(function() {
					abg_abb.stop().animate({height: hiddHeight + txt.outerHeight()}, 400, function() {
						txt.fadeIn('fast');
					});
				}, function() {
					txt.hide();
					abg_abb.stop().animate({height: hiddHeight}, 200);
				});
			}).call(this, btn, txt);
		};
	}
	
	$(".footer_map").bind("click", show_footer_map).css("cursor","pointer");
	
	function show_footer_map() {
		var url = "/4.54e1ee96130f5a8bfbe80002459.Karta.portlet";
		var $thisstate = $(this).data("state");
		
		
		if($("#footer_google_map").data("is-loaded") === true) {
			if($thisstate == "open") {
				$("#footer_google_map").slideUp("fast");
				$(this).css("backgroundPosition","left top");
				$(this).data("state", "closed");
			} else {
				$("#footer_google_map").slideDown("slow");
				$(this).css("backgroundPosition","left bottom");
				$(this).data("state", "open");
			}
			return;
		} else {
	
			$.get(url, function(cb){
				$("#footer_google_map")
					.html(cb)
					.slideDown("fast", function(){ $.scrollTo($("#footer_google_map"), "slow"); })
					.data("is-loaded", true);
			});
			
			$(this).data("state", "open").css("backgroundPosition","left bottom");
		
		}
	}
});
