$(document).ready(function(){
	// custom select
	$(".select").selectbox({className: 'selectbox', animationSpeed: 200 });
	
	// Main menu hover
	$("#mainmenu > li").hover(
	function(){
		$(this).children("ul").show();
	},
	function(){
		$(this).children("ul").hide();
	});

	/*** PROMO FLASH ***/
	$('#shirazflashpromo').flash({
		src: '/swf/shiraz_promo.swf',
		width: 590,
		height: 296,
		wmode: 'transparent',
		allowScriptAccess: 'always',
		flashvars: { lang: lang }
	});

	/*** PROMO JS ***/
	$("#promoslider").each(function(){
		var $sliderContainer = $(this);
		var xmlURL = $sliderContainer.attr("data-xml");
		$.ajax({
			type: "GET",
			url: xmlURL,
			dataType: "xml",
			success: function(xml) {
				
				var htmlPromoNav = '<ul>';
				var htmlPromoTitles = '<div id="promotitles">';
				var htmlPromoPix = '<div id="promopix">';
				$(xml).find('promo').each(function(){
					var $this = $(this);
					var navtitle = $this.attr("title");
					var navurl = $this.attr("url");
					var valign = $this.attr("valign");
					var imgurl = $this.children("image").attr("src");
					var txt1 = $this.children("text1").text();
					var txt2 = $this.children("text2").text();
					
					htmlPromoNav += '<li><a href="'+navurl+'"><span class="customfont">'+navtitle+'</span><em></em><b></b></a></li>';
					htmlPromoTitles += '<div class="promotitle '+valign+'"><h3 class="customfont">'+txt1+'</h3><h2 class="customfont">'+txt2+'</h2></div>'; 
					htmlPromoPix += '<img class="promopic" src="'+imgurl+'" alt="" />'; 
												   
				});
				
				htmlPromoNav += '</ul>';
				htmlPromoTitles += '</div>';
				htmlPromoPix += '</div>';

				$sliderContainer.append(htmlPromoNav + htmlPromoTitles + htmlPromoPix);
				Cufon.refresh();
				
				var $promoNav = $("li > a",$sliderContainer);
				var $promoTitles = $(".promotitle",$sliderContainer);
				var $promoPix = $(".promopic",$sliderContainer);
				var $promoLink = $("#promolink");
				var lastPromo = -1;

				$promoTitles.each(function(){
					$(this).css({ "left": -$(this).outerWidth(), "display": "block" });						   
				});

				function showPromo(idx) {
				
					var trans = 1000;
					
					if (lastPromo != -1) {

						$promoTitles.eq(lastPromo).stop(true,true).animate({"left":-$promoTitles.eq(lastPromo).outerWidth()},trans/2, function(){
							$promoTitles.eq(idx).animate({"left": 0}, trans/2);
						});
						$promoPix.eq(lastPromo).fadeOut(trans);
						//$promoPix.eq(idx).fadeIn(trans);
						$promoPix.eq(idx).css({
							left: -57,
							top: -23,
							width: 683,
							height: 284,
							opacity: 0
						});
						$promoPix.eq(idx).animate({
							left:0,
							top:0,
							width: 569,
							height: 237,
							opacity: 1
						},trans);
						$promoLink.attr("href",$promoNav.eq(idx).attr("href"));
						$promoNav.eq(lastPromo).children("em").fadeTo(trans,0);
						$promoNav.eq(lastPromo).removeClass("active");
						$promoNav.eq(lastPromo).children("b").stop(true,false).css("width",0);
						$promoNav.eq(idx).children("em").stop(true,true).fadeTo(trans,1);
						$promoNav.eq(idx).addClass("active");
						$promoNav.eq(idx).children("b").stop(true,true).animate({width: 138}, 10000, 'linear', function() {
							setTimeout(function(){showPromo((idx+1)%$promoNav.length)},10);						
						});						
						
					} else {
						
						$promoNav.eq(idx).addClass("active");
						$promoNav.eq(idx).children("em").stop(true,true).fadeTo(trans,1);
						$promoTitles.eq(idx).stop(true,true).animate({"left": 0}, trans);
						$promoPix.eq(idx).fadeIn(trans);
						$promoNav.eq(idx).children("b").stop(true,true).animate({width: 138}, 10000, 'linear', function() {
							setTimeout(function(){showPromo((idx+1)%$promoNav.length)},10);
						});
					}
					lastPromo = idx;

				}

				$promoNav.click(function(e) {
					var $this = $(this);
					var idx = $promoNav.index(this);
					e.preventDefault();
					
					if (lastPromo != -1 && lastPromo != idx && !$promoNav.eq(lastPromo).children("em").is(":animated")) showPromo(idx);

				});
				$promoNav.hover(function(){ 
					 if (!$(this).hasClass("active")) $("em",this).stop(true,true).fadeTo(750,0.3);
				}, function() {
					 if (!$(this).hasClass("active")) $("em",this).stop(true,true).fadeTo(750,0);	
				});
				
				$promoLink.attr("href",$promoNav.eq(0).attr("href"));
				showPromo(0);

			},
			error: function() { $sliderContainer.append("<p>XML LOADING ERROR!</p>"); }
		});

	});

	/*** set equal height for offers at the index page ***/
	if ($("#indexpage").length > 0) {
		var maxh = 0;
		var offers = $("#indexpage .offer");
		offers.each(function(){
			var h = $(this).height();
			if (h > maxh) maxh = h;
		});
		offers.height(maxh);
	}

	/*** RÉSZLETEK + BEZÁRÁS GOMBOK ***/
	$(".offerbox").each(function(){
		var $this = $(this);
		var $offerpanel = $(".offercontent",this);
		var $detailspanel = $(".offerdetails",this);
		var $offerbottom = $(".offerbottom",this);
		var $detailsbtn = $(".details",this);
		$(".details",this).click(function(){
			var height = $offerpanel.height();
			$detailsbtn.hide();
			$offerpanel.height(height);
			$detailspanel.slideDown(300, function(){
				$('html,body').animate({scrollTop: $this.offset().top + 'px'}, 600);
			});
			$offerbottom.css('background-image', 'url(/img/offerboxbottom_light.png)');
		});
		$(".close",this).click(function(){
			$detailspanel.slideUp(300, function(){ $detailsbtn.show(); });
			$offerbottom.css('background-image', 'url(/img/offerboxbottom.png)');
		});
		$(".details",this).filter(".click").click();
	});
	$(function() {
		if ($(".scrollhere").length>0) {
			$('html,body').animate({scrollTop: $(".scrollhere").offset().top + 'px'}, 600);
		}
		var myFile = document.location.toString();
		if (myFile.match('#')) {
			var myAnchor = myFile.split('#')[1];
			$("#details_button_"+myAnchor).click();
		}
	});

	/*** VENDÉGKÖNYV FORM ***/
	$("form#form_guestbook").submit(function() {
		$.post("/ajax_guestbook.php",
			{
				lang: $("#guestbook_lang").val(),
				name: $("#guestbook_name").val(),
				email: $("#guestbook_email").val(),
				message: $("#guestbook_message").val()
			},
			function(data){
				if (data.error) alertError(error_title, data.error, 'Ok');
				else if (data.message) {
					$("div#rightguestbookdiv").html(data.message);
					Cufon.replace('#mainmenu > li > a > span > em, .customfont');
					Cufon.now();
				}
			},
			"json"
		);
		return false;
	});

	/*** MESSAGE FORM ***/
	$("form#form_message").submit(function() {
		$.post("/ajax_message.php",
			{
				lang: $("#message_lang").val(),
				name: $("#message_name").val(),
				email: $("#message_email").val(),
				message: $("#message_message").val()
			},
			function(data){
				if (data.error) alertError(error_title, data.error, 'Ok');
				else if (data.message) {
					$("div#rightcontactdiv").html(data.message);
					Cufon.replace('#mainmenu > li > a > span > em, .customfont');
					Cufon.now();
				}
			},
			"json"
		);
		return false;
	});

	/*** NEWSLETTER FORM ***/
	$("form#form_newsletter").submit(function() {
		$.post("/ajax_newsletter.php",
			{
				lang: $("#newsletter_lang").val(),
				name: $("#newsletter_name").val(),
				email: $("#newsletter_email").val()
			},
			function(data){
				if (data.error) alertError(error_title, data.error, 'Ok');
				else if (data.message) {
					$("div#rightnewsletterdiv").html(data.message);
					Cufon.replace('#mainmenu > li > a > span > em, .customfont');
					Cufon.now();
				}
			},
			"json"
		);
		return false;
	});

	/*** DATEPICKER ***/
	$("#arrive").datepicker({
		altField: '#alt_arrive',
		altFormat: 'yy-mm-dd',
		maxDate: '+1y',
		minDate:'-0d',
		hideIfNoPrevNext: true,
		showOn: 'both',
		buttonImage: '/img/calendar.png',
		buttonImageOnly: true,
		duration: 'normal',
		showAnim: 'fadeIn'
	});
	if ($("#alt_arrive").val()) {
		var queryDate = $("#alt_arrive").val();
		var dateParts = queryDate.match(/(\d+)/g);
		var realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
		$('#arrive').datepicker('setDate', realDate);
	}
	$("#departure").datepicker({
		altField: '#alt_departure',
		altFormat: 'yy-mm-dd',
		maxDate: '+1y',
		minDate:'-0d',
		hideIfNoPrevNext: true,
		showOn: 'both',
		buttonImage: '/img/calendar.png',
		buttonImageOnly: true,
		duration: 'normal',
		showAnim: 'fadeIn'
	});
	if ($("#alt_departure").val()) {
		var queryDate = $("#alt_departure").val();
		var dateParts = queryDate.match(/(\d+)/g);
		var realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
		$('#departure').datepicker('setDate', realDate);
	}
	/*** CUSTOM TEXT ***/
	Cufon.replace('#mainmenu > li > a > span > em, .customfont');
	Cufon.now();
	
	/*** Jobb oldali menu ***/
	var total_width = 0;
	for (var i=4; i<=6; i++) {
		var span = $("li.pos"+i+" > a:first > span");
		total_width += span.width();
	}
	total_width += 2*14;
	var padding = 0;
	if (total_width<400) padding = Math.floor((400-total_width)/10);
	$("li.pos4, li.pos5").css("padding-left", (padding+14)+'px');
	$("li.pos5, li.pos6").css("padding-right", padding+'px');
	for (var i=4; i<=6; i++) {
		var span = $("li.pos"+i+" > a:first > span");
		span.css("margin-left", padding+'px');
		span.css("padding-right", padding+'px');
	}
	for (var i=4; i<=6; i++) {
		var mainmenu = $("li.pos"+i);
		var submenu = mainmenu.find("ul");
		var left = mainmenu.width()+parseInt(mainmenu.css("padding-left").replace("px", ""))-submenu.width();
		submenu.css("left", left+'px');
	}
	
	/*** Feliratkozás, leiratkozás ***/
	if (newsletter_message!='') alertError(newsletter_title, newsletter_message, newsletter_button);

	/*** PrettyPhoto ***/
	$(function() {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			theme: 'facebook',
			opacity: 0.50
		});
	});
	
	/*** NYEREMÉNYJÁTÉK FORM ***/
	$("form#form_game").submit(function() {
		// paraméterek összeállítása
		var params = {};
		params["game_id"] = $("#game_id").val();
		params["name"] = $("#game_name").val();
		params["email"] = $("#game_email").val();
		if ($("#game_newsletter").is(':checked')) params["newsletter"] = 1;
		var question_name_arr = new Array;
		jQuery.each($("#game").find(":radio"), function(i, answer) { question_name_arr.push($(answer).attr("name")); });
		question_name_arr = jQuery.unique(question_name_arr);
		jQuery.each(question_name_arr, function(i, name) { params[name] = $("input[name=\""+name+"\"]:checked").val(); });
		// post
		$.post("/ajax_game.php", params,
			function(data){
				alertError(data.title, data.fbml_message, 'Ok');
				if (!data.error) {
					$("form#form_game")[0].reset();
				}
			},
			"json"
		);
		return false;
	});
	
	// right booking form
	$(".booking_type").click(function(){
		if ($(".booking_type:checked").val()=="room") {
			$("#booking_offer_id").parent().hide();
			$("#booking_room_id").parent().show();
		} else {
			$("#booking_offer_id").parent().show();
			$("#booking_room_id").parent().hide();
		}
	});
	$(".booking_type:checked").click();
});
