$(function(){
	
	// FEJLÉC FLASH
	$('#header-flash').flash({
		swf: domain+'/img/header.swf',
		width: 980,
		height: 132,
		params: {
			allowScriptAccess: 'always'
		},
		flashvars: {
			hu_link : header_hu_link,
			hr_link : header_hr_link,
			cz_link : header_cz_link,
			ee_link : header_ee_link,
			pl_link : header_pl_link,
			at_link : header_at_link,
			lt_link : header_lt_link,
			lv_link : header_lv_link,
			sk_link : header_sk_link,
			ua_link : header_ua_link,
			si_link : header_si_link,
			ro_link : header_ro_link,
			logolink: '/',
			active_country: header_active_country
		}
	});
	
	// FOOTER FLASH BANNER
	$('#footer_banner').flash({
		swf: domain+'/swf/bpcard_940x120_'+lang+'.swf',
		width: 940,
		height: 120,
		params: {
			allowScriptAccess: 'always'
		}
	});
	
	// LIGHTBOX
	$("a[rel='lightbox']").lightBox();
	
	// NYELVVÁLTÓ
	$("#lang").change(function() {
		$("#languageForm").submit();
	});
	
	// DÁTUMVÁLASZTÓK
	var objDatepicker = new Object({
		showOn: 'both',
		showAnim: 'fadeIn',
		buttonImageOnly: true,
		hideIfNoPrevNext: true,
		altFormat: 'mm/dd/yy',
		onSelect: function(dateText, inst) {
			if ($(this).attr("id") == "car_datepicker_from") {
				syncDatepickers("car_datepicker_from", "car_datepicker_to");
				setMinDate("car_datepicker_from", "car_datepicker_to");
			} else if ($(this).attr("id") == "hotel_datepicker_from") {
				syncDatepickers("hotel_datepicker_from", "hotel_datepicker_to");
				setMinDate("hotel_datepicker_from", "hotel_datepicker_to");
			} else if ($(this).attr("id") == "flight_datepicker_from") {
				syncDatepickers("flight_datepicker_from", "flight_datepicker_to");
				setMinDate("flight_datepicker_from", "flight_datepicker_to");
			}
		}
	});
	objDatepicker.buttonImage = "img/datepicker_blue.png";
	$("#hotel_datepicker_from, #hotel_datepicker_to").datepicker(objDatepicker);
	$("#hotel_datepicker_from, #hotel_datepicker_to").datepicker("option", "minDate", "0");
	$("#hotel_datepicker_from").datepicker('option', 'altField', '#hotel_datepicker_from_alt');
	$("#hotel_datepicker_to").datepicker('option', 'altField', '#hotel_datepicker_to_alt');
	setMinDate("hotel_datepicker_from", "hotel_datepicker_to");
	objDatepicker.buttonImage = "img/datepicker_green.png";
	$("#flight_datepicker_from, #flight_datepicker_to").datepicker(objDatepicker);
	$("#flight_datepicker_from, #flight_datepicker_to").datepicker("option", "minDate", "0");
	$("#flight_datepicker_from").datepicker('option', 'altField', '#flight_datepicker_from_alt');
	$("#flight_datepicker_to").datepicker('option', 'altField', '#flight_datepicker_to_alt');
	setMinDate("flight_datepicker_from", "flight_datepicker_to");
	objDatepicker.buttonImage = "img/datepicker_orange.png";
	$("#car_datepicker_from, #car_datepicker_to").datepicker(objDatepicker);
	$("#car_datepicker_from, #car_datepicker_to").datepicker("option", "minDate", "0");
	$("#car_datepicker_from").datepicker('option', 'altField', '#car_datepicker_from_alt');
	$("#car_datepicker_to").datepicker('option', 'altField', '#car_datepicker_to_alt');
	setMinDate("car_datepicker_from", "car_datepicker_to");
	
	// ORSZÁGVÁLTÓ
	$("#country").change(function () {
		$("#countryForm").submit();
	});
	
	// TÉRKÉP FELUGRÓ
	$('.map').popupWindow({centerScreen:1, width:700, height:400});
	
	// HOTEL FORM
	$("#hotel_country").change(function() {
		if ($("#hotel_country").val()=='Hungary') {
			$("#hotel_minstarrating_label").text(rombrandt_hotel_category_text);
			$("#hotel_search_form").attr('action', rombrandt_link);
			$("#hotel_destinationid").attr('name', 'cityid');
			document.getElementById('hotel_minstarrating').options[document.getElementById('hotel_minstarrating').options.length] = new Option(hotel_category_other, 6);
		} else {
			$("#hotel_minstarrating_label").text(expedia_hotel_category_text);
			$("#hotel_search_form").attr('action', expedia_link);
			$("#hotel_destinationid").attr('name', 'hotel[destinationid]');
			if (document.getElementById('hotel_minstarrating').options.length == 7) {
				document.getElementById('hotel_minstarrating').options[document.getElementById('hotel_minstarrating').options.length-1] = null;
			}
		}
		getDestination("#hotel_country", "#hotel_destinationid");
	});
	$("#hotel_country").change();
	
	// AJÁNLATOK
	if ($("#hotel_offer_box").attr('id')=='hotel_offer_box') getOfferBox('hotel', offer_box_from_page, hotel_offers_params);
	if ($("#flight_offer_box").attr('id')=='flight_offer_box') getOfferBox('flight', offer_box_from_page, flight_offers_params);
	if ($("#car_offer_box").attr('id')=='car_offer_box') getOfferBox('car', offer_box_from_page, car_offers_params);
	
	// HOTEL SZOBAVÁLASZTÓ
	$("#hotel_rooms").change(function() {
		var num_of_rooms = $(this).val()*1;
		for (var i=1; i<=num_of_rooms; i++) $('#hotel_room_'+i+'_tr').show();
		for (var i=num_of_rooms+1; i<=8; i++) $('#hotel_room_'+i+'_tr').hide();
	});
	$("#hotel_rooms").change();
	$(".hotel_children").change(function() {
		var patt=/[0-9]/;
		var room_i=patt.exec($(this).attr('id'));
		var num_of_children = $(this).val()*1;
		if (num_of_children) {
			$('#hotel_room_'+room_i+'_age_label').show();
			for (var i=1; i<=num_of_children; i++) $('#hotel_room_'+room_i+'_age_'+i).show();
			for (var i=num_of_children+1; i<=3; i++) $('#hotel_room_'+room_i+'_age_'+i).hide();
		} else {
			$('#hotel_room_'+room_i+'_age_label').hide();
			for (var i=1; i<=3; i++) $('#hotel_room_'+room_i+'_age_'+i).hide();
		}
	});
	$(".hotel_children").change();
	
	// FLIGHT GYEREKEK
	$("#flight_infants, #flight_children").change(function() {
		var num_of_infants = $("#flight_infants").val()*1;
		var num_of_children = $("#flight_children").val()*1;
		if (num_of_infants || num_of_children) $('#children_tr, #children_ages_tr').show();
		else $('#children_tr, #children_ages_tr').hide();
		for (var i=1; i<=num_of_infants; i++) $('#flight_infants_ages_div_'+i).show();
		for (var i=num_of_infants+1; i<=6; i++) $('#flight_infants_ages_div_'+i).hide();
		for (var i=1; i<=num_of_children; i++) $('#flight_children_ages_div_'+i).show();
		for (var i=num_of_children+1; i<=6; i++) $('#flight_children_ages_div_'+i).hide();
	});
	$("#flight_infants, #flight_children").change();
	
	// LEMONDÁSI SZABÁLYZAT
	$(".policy-dialog").dialog({ autoOpen: false, bgiframe: true, width: 350, height: 200, modal: true, draggable: false, resizable: false });
	$('.policy').click(function() {
		var id = $(this).attr('id');
		var room_i=id.replace("policy-dialog-link-", "")
		$("#policy-dialog-"+room_i).dialog('open');
		return false;
	});
	
	// NON-REFUNDABLE
	$(".policy-dialog").dialog({ autoOpen: false, bgiframe: true, width: 350, height: 200, modal: true, draggable: false, resizable: false });
	$('.policy, .nonrefundable').click(function() {
		var id = $(this).attr('id');
		id=id.replace("policy-dialog-link-", "");
		var room_i=id.replace("nonrefundable-dialog-link-", "");
		$("#policy-dialog-"+room_i).data("title.dialog", $(this).attr('title')); 
		$("#policy-dialog-"+room_i).dialog('open');
		return false;
	});
	
	// SZOBATÍPUS LEÍRÁS
	$(".roomdescription-dialog").dialog({ autoOpen: false, bgiframe: true, width: 500, height: 400, modal: true, draggable: false, resizable: false });
	$('.roomdescription').click(function() {
		var id = $(this).attr('id');
		var room_i=id.replace("roomdescription-dialog-link-", "")
		$("#roomdescription-dialog-"+room_i).dialog('open');
		return false;
	});
	
	// AUTÓ ESTIMATED PRICE
	$("#estimated-dialog").dialog({ autoOpen: false, bgiframe: true, width: 350, height: 250, modal: true, draggable: false, resizable: false });
	$("#estimated-link").click(function() { $("#estimated-dialog").dialog('open'); return false; });
	
	// AUTÓ FOGLALÁSI SZABÁLYOK
	$("#rules-dialog").dialog({ autoOpen: false, bgiframe: true, width: 350, height: 250, modal: true, draggable: false, resizable: false });
	$("#rules-dialog-link").click(function() { $("#rules-dialog").dialog('open'); return false; });
	
	// FLIGHT NON-REFUNDABLE
	$(".flight-non-refundable-dialog").dialog({ autoOpen: false, bgiframe: true, width: 500, height: 150, modal: true, draggable: false, resizable: false });
	$('.flight-non-refundable').click(function() {
		var id = $(this).attr('id');
		var flight_i=id.replace("flight-non-refundable-link-", "")
		$("#flight-non-refundable-dialog-"+flight_i).dialog('open');
		return false;
	});
	
	// FLIGHT PAPER TICKET
	$("#flight-paper-ticket-dialog").dialog({ autoOpen: false, bgiframe: true, width: 500, height: 350, modal: true, draggable: false, resizable: false });
	$('#flight-paper-ticket-link').click(function() {
		$("#flight-paper-ticket-dialog").dialog('open');
		return false;
	});
	
	// FLIGHT FOGLALÁSI SZABÁLYOK
	$("#flight-rules-dialog").dialog({ autoOpen: false, bgiframe: true, width: 640, height: 480, modal: true, draggable: false, resizable: false });
	$("#flight-rules-dialog-link").click(function() { $("#flight-rules-dialog").dialog('open'); return false; });
	
	// FLIGHT SHIPPING SAME
	$('#shipping_same').change(function() {
		if ($('#shipping_same').attr('checked')) $('#shipping_address_div').hide();
		else $('#shipping_address_div').show();
	});
	$('#shipping_same').change();
	
	// PRINT LINK
	$("#print_link").click(function(){
		$(".printable").print();
		return false;
	});
	
	$("#car_reservation_rules, #flight_reservation_rules").focus(function() {
		this.blur();
	});


});

/***               ***/
/***   FUNCTIONS   ***/
/***               ***/
function syncDatepickers(from_id, to_id) {
	var fromDate = new Date(Date.parse($("#"+from_id).datepicker("getDate")));
	var toDate = new Date(Date.parse($("#"+to_id).datepicker("getDate")));
	if (toDate<=fromDate) {
		toDate = fromDate;
		toDate.setDate(fromDate.getDate()+1);
		var dateFormatted = $.datepicker.formatDate("mm/dd/yy", toDate);
		$("#"+to_id).val(dateFormatted);
	}
}

function setMinDate(from_id, to_id) {
	if ($("#"+from_id).attr('id') && $("#"+to_id).attr('id')) {
		var fromDate = minDate = new Date(Date.parse($("#"+from_id).datepicker("getDate")));
		minDate.setDate(fromDate.getDate()+1);
		var fromDateFormatted = $.datepicker.formatDate("mm/dd/yy", fromDate);
		var minDateFormatted = $.datepicker.formatDate("mm/dd/yy", minDate);
		$("#"+to_id).datepicker("option", "minDate", dateDiff(minDate));
	}
}

function dateDiff(myDate) {
	today=new Date();
	var one_day=1000*60*60*24;
	return Math.ceil((myDate.getTime()-today.getTime())/(one_day));
}

function getDestination(source_id, target_id) {
	$.get("/ajax_getdestination.php", { country_name: $(source_id).val() },
		function(data){
			$(target_id).empty().append(data);
		},
		"json"
	);
}

function getOfferBox(offer_box_type, offer_box_from_page, offers_params) {

var params='';
$.each(offers_params, function(key, value) { 
  params = params+key+'='+value+'&'; 
});

	$.get("/ajax_offer_box.php", {
			offer_box_type: offer_box_type,
			offer_box_from_page: offer_box_from_page,
			params: params
		},
		function(data){
			$('#'+offer_box_type+'_offer_box').html(data);
		},
		"json"
	);
}

function submitFooterLinksForm(subdomain, country, destination_id) {
	if (country=='Hungary') {
		$('#footerLinksForm').attr('action', rombrandt_link);
		$('#footerlinks_destinationid').attr('name', 'cityid');
	} else {
		$('#footerLinksForm').attr('action', 'http://'+subdomain+'.centraleuropeanhotels.com'+expedia_link);
		$('#footerlinks_destinationid').attr('name', 'hotel[destinationid]');
	}
	$('#footerlinks_destinationid').val(destination_id);
	$('#footerLinksForm').submit();
}