/* 
 * CentralTravel application
 * @author Łukasz Kieres
 * @company eSKY.pl
 * @date 2009-08-07
 * @modified 2010-09-02
 */

I18N_ChildAge = 'wiek dziecka';
I18N_ChildrenAge = 'wiek dzieci';

var I18N_datepicker = {
	dateFormat : 'yy-mm-dd', //'ISO_8601'
	firstDay: 1,
	yearRange: '-0:+1',
	numberOfMonths: 1,
	closeText: 'Zamknij',
	prevText: '&#x3c;Poprzedni',
	nextText: 'Następny&#x3e;',
	currentText: 'Dziś',
	monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
	monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze','Lip','Sie','Wrz','Pa','Lis','Gru'],
	dayNames: ['Niedziela','Poniedzialek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
	dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
	dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
	isRTL: false,
	showOn:'both',
	duration:'',
	buttonText: 'Wybierz datę'
};

/**
 * Show modal window for phone booking form
 * @author lkieres
 */
$.fn.showPhoneBookingForm = function(){
	$('#overlay').remove();
	// open modal
	$('<div></div>').attr('id','overlay').appendTo('body');
	$('#pb-progress,#pb-status').remove();
	$('#phone-booking-popup form').show();
	$('#phone-booking-popup').fadeIn();
	
	$('#overlay').one('click',function() {
		$('#phone-booking-popup').hide();
		$('#overlay').remove();
		$('select.hiddenv').removeClass('hiddenv');
		return false;
	});

	if ($.browser.msie && parseInt($.browser.version) <= 6) {
		$('body').scrollTop(0);
	}

	$('#phone-booking-popup').delegate('#cancel','click',function(e) {
		$('#overlay').remove();
		$('#phone-booking-popup').hide();
		$('select.hiddenv').removeClass('hiddenv');
		e.preventDefault();
	});

	return this;
};

$(document).ready(function(){
	
	$.datepicker.setDefaults(I18N_datepicker);
	
	// podmiana przycisku Szukaj w QSF	
	$('#wizard tfoot img:last, #wizard_small img:last').attr('src','/images/btn-search.gif');
	
	if (jQuery.browser.msie && jQuery.browser.version == 6) { 
		$('<iframe></iframe>').appendTo($('#ep3_asg_hint'));
	}
	
	/* filtrowanie artykułów w Poradnikach */
	if ($('#guide-filter').size()>0) { 
		
		/* wg typu */
		$('#filter-on,#show-articles').change(function() {
			var type = $('#filter-on').val();
			var num = $('#show-articles').val();
			
			if (num == '1000') { //wszystkie
				var url = type
			} else {
				
				if (type == '/przewodniki/') {
					var url = type+'top/' + num;
				} else {
					var url = type+'/top/' + num;
				};
				
			};
			
			window.location=url;
			return false;
		});
	};
	
	/* Walidacja formularza kontaktowego */
	if ($('#travel-contact-form').size()>0) { 
		var travel = $('form#travel-contact-form');
		travel.validate(travel_options);
	};
	
	if ($('#phone-booking-popup').length) {
		var pb_validation = $('#phone-booking-popup form').validate(travel_phone_booking);
		$('[rel=phone-booking-form]').click(function(e) {
			$('#qsfform select, #qsfform-narrow select').addClass('hiddenv');
			$('body').showPhoneBookingForm();
			e.preventDefault();
			return false;
		});
	}

	// selecting number of children
	$('[name$=Travellers[Child]]').change(function() {
		$age = $(this).closest('fieldset').find('.children-age');
		// no children
		if ($(this).val() == '0') {
			$(this).closest('label').removeClass('selected');
			$age.addClass('hidden');
		// some children
		} else {
			$(this).closest('label').addClass('selected');
			$age.addClass('hidden');
			$(this).closest('fieldset').find('.children-age select').addClass('hidden');
			$(this).closest('fieldset').find('.children-age select:lt('+$(this).val()+')').removeClass('hidden');
			$age.find('small').text(($(this).val() == '1')?I18N_ChildAge:I18N_ChildrenAge);
			$age.removeClass('hidden');
		}
	}).change();

	// calendars
	$('[name=DepartureDateFrom]').datepicker({
		minDate: '+0d',
		maxDate: '+365d',
		// auto-fill date on init and after selecting checkout > (checkin - 5)
		onSelect: function(dateText, instance) {
			
			checkin = $.datepicker.formatDate("@", $('[name=DepartureDateFrom]').datepicker('getDate'));
			checkout = $.datepicker.formatDate("@", $('[name=DepartureDateTo]').datepicker('getDate'));
			
			if((checkout - 5*24*60*60*1000) < checkin) {
				$('[name=DepartureDateTo]').datepicker("setDate", new Date(Date.parse($(this).datepicker('getDate')) + 14*24*60*60*1000));
				$('[name=DepartureDateTo]').trigger('change');
			}
			$('[name=DepartureDateFrom]').trigger('change');
			
		}
	});
	$('[name=DepartureDateTo]').datepicker({
		minDate: '+0d',
		maxDate: '+365d',
		beforeShow: function(){
			var checkin = $('[name=DepartureDateFrom]',$(this).closest('form')).datepicker('getDate');
			return (this.name == 'DepartureDateTo') ? {
				minDate: new Date(Date.parse(checkin) + 5*24*60*60*1000),	//Add 5 days to departure date
				maxDate: new Date(Date.parse(checkin) + 365*24*60*60*1000)	//Add 365 days to departure date
			} : {};
		}
	});

	// ukrywanie pola do podania numeru telefonu pod którym jest dostepny klient - aby go nie straszyc
	if ($('h3:contains(Oferta) + table tbody tr td:contains(Organizator)').size()>0) {
		$('input[name^=payer_note]').val('032123456'); // bogus phone :)
		$('b:contains(nr telefonu)').parent().parent().parent().parent().prev().hide();
		$('b:contains(nr telefonu)').parent().parent().parent().parent().hide();
	};

		
	/* WIDGETS TABS */
	$('.widget ul.tabs a').click(function(){
		var index = $(this).parent().parent().find('a').index(this);
		var selected = $(this).parent().hasClass('active');
		var widget = $(this).parents().filter('div.widget');
		
		if (!selected) {
			// wylaczamy zakladki
			$(this).parents().filter('ul.tabs').find('li').removeClass('active');
			// właczamy kliknietą
			$(this).parent().addClass('active');
			// ukrywamy wszystkie tresci tabow
			widget.find('div[class^=tab]').not('hidden').addClass('hidden');
			// pokazujemy tresc taba związanego z indeksem kliknietej zakładki
			widget.find('div[class^=tab]:eq(' + index + ')').removeClass('hidden');
		}
		return false;
	});
	
	$('input,select','#widget-search').change(function() {

		if ($(this).attr('name') == 'skin')
			$('#widget-search img').attr('src', '/images/widgets/thumb-search-' + $(this).val() + '.png');

		$('li.igoogle a','#widget-search').attr('href','http://fusion.google.com/ig/add?synd=open&source=ggyp&moduleurl=http://hosting.gmodules.com/ig/gadgets/file/111147196189267155876/centraltravel.xml&up_type=search&' +
		 'up_skin=' +  $('#widget-search select[name=skin]').val() + '&' +
		 'up_w=' +  $('#widget-search input[name=width]').val() + '&' +
		 'up_h=' +  $('#widget-search input[name=height]').val()		 
		);
	
		$('textarea','#widget-search').text('<script src="http://www.gmodules.com/ig/ifr?url=http://hosting.gmodules.com/ig/gadgets/file/111147196189267155876/centraltravel.xml&amp;up_type=search&amp;synd=open&amp;w='+$('#widget-search input[name=width]').val()+'&amp;h='+$('#widget-search input[name=height]').val()	+'&amp;up_skin='+$('#widget-search select[name=skin]').val()+'&amp;title=&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"></script>')

	});
	
	$('input,select','#widget-offers').change(function() {

		if ($(this).attr('name') == 'skin')
			$('#widget-offers img').attr('src', '/images/widgets/thumb-offers-' + $(this).val() + '.png');

		$('li.igoogle a','#widget-offers').attr('href','http://fusion.google.com/ig/add?synd=open&source=ggyp&moduleurl=http://hosting.gmodules.com/ig/gadgets/file/111147196189267155876/centraltravel.xml&' +
		 'up_type=' +  $('#widget-offers select[name=type]').val() + '&' +
		 'up_skin=' +  $('#widget-offers select[name=skin]').val() + '&' +
		 'up_count=' +  $('#widget-offers select[name=count]').val() + '&' +
		 'up_w=' +  $('#widget-offers input[name=width]').val() + '&' +
		 'up_h=' +  $('#widget-offers input[name=height]').val()		 
		);
		
		$('textarea','#widget-offers').text('<script src="http://www.gmodules.com/ig/ifr?url=http://hosting.gmodules.com/ig/gadgets/file/111147196189267155876/centraltravel.xml&amp;synd=open&amp;w='+$('#widget-offers input[name=width]').val()+'&amp;h='+$('#widget-offers input[name=height]').val()+'&amp;up_type='+$('#widget-offers select[name=type]').val()+'&amp;up_skin='+$('#widget-offers select[name=skin]').val()+'&amp;up_count='+$('#widget-offers select[name=count]').val()+'&amp;title=&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"></script>')

	});
	
});
