function hideComic()	{
	$("#comic").height(50);
}

function showComic()	{
	$("#comic").height(220);
}

// E-MAIL ELLENŐRZÉS
function isEmail(v) { 
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(v); 
}

$(function(){
		   
	/*** png fix ***/
	$(document).pngFix({ blankgif:'/img/blank.gif' });

   $('#flash_menu').flash({
		src: domain+'/Images/BudapestAdventure/flash/menu980x140.swf',
		width: 900,
		height: 129,
		wmode: 'transparent',
		allowscriptaccess: 'always',
		flashvars: { 
			adventure_id: island_id, 
			language_id: lang,
			clickTag1: island1_virtlink,
			clickTag2: island2_virtlink,
			clickTag3: island3_virtlink,
			clickTag4: island4_virtlink,
			clickTag5: island5_virtlink,
			clickTag6: island6_virtlink,
			valaszto: valaszto_virtlink
		}		
	});
		   
	$.datepicker.regional[lang];
	//initialize datepickers
	$("#begin").datepicker({
		minDate: min_time,
		maxDate: max_time,
		dateFormat: 'yy.mm.dd',
		showOn: 'both',
		buttonImage: '/img/datepicker.gif',
		buttonImageOnly: true,
		duration: 'normal',
		showAnim: 'fadeIn'
	});
	
	$("#end").datepicker({
		minDate: min_time,
		maxDate: max_time,
		dateFormat: 'yy.mm.dd',
		showOn: 'both',
		buttonImage: '/img/datepicker.gif',
		buttonImageOnly: true,
		duration: 'normal',
		showAnim: 'fadeIn'
	});
	
	$("#searchtoggle").click(function(){
		$("#search").fadeIn("fast");
		$("#searchtext").focus();
		return false;
	});
	
	$("#search a").click(function(){
		$("#search").fadeOut("fast");						  
		return false;
	});
	
	$(".toptips li:first").addClass("active");
	$(".toptips li:first img").fadeIn("fast");

	var arrTop = $(".toptips li");
	var arrTopImp  = $(".toptips li img");
	arrTop.mouseover(function(){
		arrTopImp.hide();  
		$(this).children("img").show();
		arrTop.removeClass("active");
		$(this).addClass("active");
		
	});

	/*** vote ***/
	jQuery.each($(".rate"), function() {
		$("#"+$(this).attr("id")).rater({
			url: '/vote.php',
			mediapath: '/img/',
			value: 0
		});
	});
	
	/*** video váltás a videó oldalon ***/
	$('#videolist > ul li a').click(function(){ // When link is clicked
		$('#videolist > ul li a').removeClass('active'); // Remove active class from links
		$(this).addClass('active'); //Set parent of clicked link class to active
		$.get("/getvideo.php", { id: $(this).attr('id') },
			function(data){
				$('#videocontent').html('<h1>'+data.title+'</h1>'+
				'<object width="430" height="385">'+
				'	<param name="movie" id="movie" value="'+data.youtube_link+'"></param>'+
				'	<param name="allowFullScreen" value="true"></param>'+
				'	<param name="allowscriptaccess" value="always"></param>'+
				'	<embed src="'+data.youtube_link+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="430" height="385"></embed>'+
				'</object>'+
				'<p>'+data.description+'</p>');
				$('.rate').attr('id', 'video-'+data.id);
				if (data.avarage_vote==0) data.avarage_vote = '';
				$('.avarage_vote').html(data.avarage_vote);
				$('#video-'+data.id).rater({
					url: '/vote.php',
					mediapath: '/img/',
					value: 0
				});
			},
			"json"
		);
	});
	
	/*** video váltás az élménysziget oldalon ***/
	$('#videobox a.next, #videobox a.prev').click(function(){ // When link is clicked
		$.get("/ajax_getislandvideo.php", { ord_index: $(this).attr('id'), island_id: island_id },
			function(data){
				$('#videobox div.item').html(
				'<object width="220" height="145">'+
				'	<param name="movie" id="movie" value="'+data.youtube_link+'"></param>'+
				'	<param name="allowFullScreen" value="true"></param>'+
				'	<param name="allowscriptaccess" value="always"></param>'+
				'	<embed src="'+data.youtube_link+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="220" height="145"></embed>'+
				'</object>');
				if (data.prev_ord_index!=0) {
					$('#videobox a.prev').show();
					$('#videobox a.prev').attr('id', data.prev_ord_index);
				} else $('#videobox a.prev').hide();
				if (data.next_ord_index!=0) {
					$('#videobox a.next').show();
					$('#videobox a.next').attr('id', data.next_ord_index);
				} else $('#videobox a.next').hide();
			},
			"json"
		);
	});
	
	/*** TABS ***/
	$('#tabs > div').hide(); // Hide all divs
	$('#tabs > ul li a').click(function(){ // When link is clicked
		$('#tabs > ul li').removeClass('active'); // Remove active class from links
		$(this).parent().addClass('active'); //Set parent of clicked link class to active
		var currentTab = $(this).attr('href'); // Set currentTab to value of href attribute
		$('#tabs > div').hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
	});
	
	/*** file style ***/
	$("input[type=file]").each(function(){ 
		var fileinput = $(this);
		fileinput.removeClass("data");
		fileinput.addClass("fileinput");
		fileinput.wrap('<span class="filecontainer"></span>');
		fileinput.wrap('<span class="filebutton"></span>');
		fileinput.before('<input class="button" type="button" id="'+$(this).attr("id")+'_img" value="'+browse+'" />');
		fileinput.parent().before('<input id="'+$(this).attr("id")+'_input" type="text" class="data" readonly="readonly" />');
		var input = $("#"+$(this).attr("id")+"_input");
		var img = $("#"+$(this).attr("id")+"_img");
	
		img.mousemove(function(e){
		  fileinput.css({
			right: (img.outerWidth()-(e.pageX-img.offset().left)-10)+"px",
			bottom: (img.outerHeight()-(e.pageY-img.offset().top)-10)+"px"
		  });
		});

		fileinput.bind('change', function() {
			input.val(fileinput.val());
			$(this).blur();		  
		});
	});
	
	/*** lightbox ***/
	//$("div.gallery a.pic").lightBox();
	//$("div.ecard a.pic").lightBox();
	$("a.pic").lightBox();

	
	/*** ALERT DIALOG ***/
	showAlert = function (jForm, title, message) {
		$(".errorMessage").remove();
		var htmlFragment = '';
		htmlFragment += '<div class="errorMessage">';
		htmlFragment += '<div class="errorMask"></div>';
		htmlFragment += '<div class="errorDialog">';
		htmlFragment += '<h2>'+title+'</h2>';
		htmlFragment += '<a href="javascript:void(0);" class="close">x</a>';
		htmlFragment += '<p>'+message+'</p>';
		htmlFragment += '</div>';
		htmlFragment += '</div>';
		jForm.after(htmlFragment);
		var errDlg = $(".errorMessage");
		if (jForm.attr("id")=="dialog") {
			errDlg.height(jForm.height()-40);
			errDlg.width(jForm.width()+10);
		} else {
			if (jForm.attr("id")=="ecardfields") {
				errDlg.css("top", jForm.position().top);
				errDlg.height(jForm.height()+20);
				errDlg.width(jForm.width());
			} else {
				errDlg.height(jForm.height()+70+40);
				errDlg.width(jForm.width()+60);
			}
		}
		errDlg.find(".errorDialog a").click(function(){
			errDlg.remove();
			if (jForm.attr("id")=="dialog") $("#dialog .submit").show();
		});
		$(document).pngFix({ blankgif:'/img/blank.gif' });
	}
	
	/*** ÉLMÉNY BEKÜLDŐ FORM ELLENŐRZÉSE ***/
	$("form#experienceform").submit(function(){
		var messages = new Array();
		var require_error = false;
		if ($("input#name").val() == "") require_error = true;
		if ($("input#email").val() == "") require_error = true;
		if ($("input#country").val() == "") require_error = true;
		if ($("input#address").val() == "") require_error = true;
		if ($("textarea#msg").val() == "") require_error = true;
		if ($("input#captcha").val() == "") require_error = true;
		if (!$("input#terms").attr("checked")) require_error = true;
		if (require_error) messages.push(error_must_fill);
		if (!isEmail($("input#email").val())) messages.push(error_not_email);
	  if (messages.length>0) {
	  	showAlert($("#contact"), error_title, messages.join("<br />"));
			return false;
		} else return true;
	});
	
	/*** KÉP BEKÜLDŐ FORM ELLENŐRZÉSE ***/
	$("form#imageform").submit(function(){
		var messages = new Array();
		var require_error = false;
		if ($("input#picpath").val() == "") require_error = true;
		if ($("input#pictitle").val() == "") require_error = true;
		if ($("input#picname").val() == "") require_error = true;
		if ($("input#picemail").val() == "") require_error = true;
		if ($("input#piccountry").val() == "") require_error = true;
		if ($("input#picaddress").val() == "") require_error = true;
		if ($("textarea#picmsg").val() == "") require_error = true;
		if ($("input#piccaptcha").val() == "") require_error = true;
		if (!$("input#picterms").attr("checked")) require_error = true;
		if (require_error) messages.push(error_must_fill);
		if (!isEmail($("input#picemail").val())) messages.push(error_not_email);
	  if (messages.length>0) {
	  	showAlert($("#uploadimg"), error_title, messages.join("<br />"));
			return false;
		} else return true;
	});
	
	/*** VIDEÓ BEKÜLDŐ FORM ELLENŐRZÉSE ***/
	$("form#videoform").submit(function(){
		var messages = new Array();
		var require_error = false;
		if ($("input#videopath").val() == "") require_error = true;
		if ($("input#videotitle").val() == "") require_error = true;
		if ($("input#videoname").val() == "") require_error = true;
		if ($("input#videoemail").val() == "") require_error = true;
		if ($("input#videocountry").val() == "") require_error = true;
		if ($("input#videoaddress").val() == "") require_error = true;
		if ($("textarea#videomsg").val() == "") require_error = true;
		if ($("input#videocaptcha").val() == "") require_error = true;
		if (!$("input#videoterms").attr("checked")) require_error = true;
		if (require_error) messages.push(error_must_fill);
		if (!isEmail($("input#videoemail").val())) messages.push(error_not_email);
	  if (messages.length>0) {
	  	showAlert($("#uploadvideo"), error_title, messages.join("<br />"));
			return false;
		} else return true;
	});
	
	/*** E-CARD BEKÜLDŐ FORM ELLENŐRZÉSE ***/
	$("form#ecardform").submit(function(){
		var messages = new Array();
		var require_error = false;
		if ($("input#yourname").val() == "") require_error = true;
		if ($("input#targetname").val() == "") require_error = true;
		if ($("input#email").val() == "") require_error = true;
		if ($("input#targetmail").val() == "") require_error = true;
		if (require_error) messages.push(error_must_fill);
		if (!isEmail($("input#email").val()) || !isEmail($("input#targetmail").val())) messages.push(error_not_email);
	  if (messages.length>0) {
	  	showAlert($("div.ecard"), error_title, messages.join("<br />"));
			return false;
		} else return true;
	});
	
	/*** OLDAL AJÁNLÓ MŰKÖDÉSE ***/
	var recommendStatus = 0;
	$("#recommend a").click(function(){
		recommendStatus = 1;
		$("#dialogmask").css({
			"height": window.top.document.body.scrollHeight
		});
		var windowWidth = document.documentElement.clientWidth;  
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popupContact").height();
		var popupWidth = $("#popupContact").width();
		var scrollTop = $(document).scrollTop();
		$("#dialog").css({
			"position": "absolute",
			"top": (windowHeight/2-popupHeight/2) + scrollTop,
			"left": windowWidth/2-popupWidth/2
		});
		$("#dialog").show();
		$("#dialogmask").show();
		return false;
	});
	$("#dialogmask").click(function(){
		$("#dialog").hide();
		$("#dialogmask").hide();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && recommendStatus==1){
			$("#dialog").hide();
			$("#dialogmask").hide();
		}
	});
	
	/*** OLDAL AJÁNLÓ FORM ELLENŐRZÉSE ***/
	$("form#recommendform").submit(function(){
		var messages = new Array();
		var require_error = false;
		if ($("input#dlgyourname").val() == "") require_error = true;
		if ($("input#dlgtargetname").val() == "") require_error = true;
		if ($("input#dlgemail").val() == "") require_error = true;
		if ($("input#dlgtargetmail").val() == "") require_error = true;
		if (require_error) messages.push(error_must_fill);
		if (!isEmail($("input#dlgemail").val()) || !isEmail($("input#dlgtargetmail").val())) messages.push(error_not_email);
	  if (messages.length>0) {
	  	showAlert($("#dialog"), error_title, messages.join("<br />"));
			var position = $("#dialog").position();
			$(".errorMessage").css({
				"top": (position.top+30)+"px",
				"left": (position.left+20)+"px",
				"margin-left": $("#dialog").css("margin-left"),
				"margin-top": $("#dialog").css("margin-top")
			});
			$("#dialog .submit").hide();
			return false;
		} else {
			$.post("/ajax_recommend.php",
				{ 
					send_recommend_message: $("#send_recommend_message").val(),
					page_url: $("#page_url").val(),
					from_person: $("#dlgyourname").val(),
					to_person: $("#dlgtargetname").val(),
					from_email: $("#dlgemail").val(),
					to_email: $("#dlgtargetmail").val(),
					recommend_message: $("#dlgmessage").val()
				},
				function(data){
					showAlert($("#dialog"), recommend_thank_you_title, recommend_thank_you_text);
					var position = $("#dialog").position();
					$(".errorMessage").css({
						"top": (position.top+30)+"px",
						"left": (position.left+20)+"px",
						"margin-left": $("#dialog").css("margin-left"),
						"margin-top": $("#dialog").css("margin-top")
					});
					$("#dialog .submit").hide();
					$("#dlgyourname").val("");
					$("#dlgtargetname").val("");
					$("#dlgemail").val("");
					$("#dlgtargetmail").val("");
					$("#dlgmessage").val("");
				},
				"json"
			);
			return false;
		}
	});
	
	/*** Szavazás ***/
	$('#poll_submit').click(function() {
		var q_id = $("#question_id").val();
		var a_id = $("input[name='vote']:checked").val();
		if (q_id && a_id){
			$.post("/ajax_vote.php", { 
					question_id: q_id, 
					answer_id: a_id
				},
				function(data) {
					$('#pollvote').hide();
					$('#poll_submit').hide();
					$.each(data, function(id, percent){
						$('#pollresult'+id).css('width', percent+'%');
						$('#pollresult'+id).text(percent+'%');
					});
					$('.pollresults').show();
				},
				"json"
			);
		}
	});
	
	/*** Ecard váltás ***/
	$('div.ecard a.next, div.ecard a.prev').click(function() {
		$.get("/ajax_getecard.php", { ord_index: $(this).attr('id') },
			function(data){
				$('div.ecard img').attr('src', data.src);
				$('div.ecard a.pic').attr('href', data.src);
				$('#ecard_id').val(data.id);
				if (data.prev_ord_index!=0) {
					$('div.ecard a.prev').show();
					$('div.ecard a.prev').attr('id', data.prev_ord_index);
				} else $('div.ecard a.prev').hide();
				if (data.next_ord_index!=0) {
					$('div.ecard a.next').show();
					$('div.ecard a.next').attr('id', data.next_ord_index);
				} else $('div.ecard a.next').hide();
			},
			"json"
		);
	});
});
