var setSelectOptions = function(el, options, selected){
	var innerHtml = "";
	$.each(options, function(value, text){
		innerHtml = innerHtml + '<option value="' + value + ( (selected && selected==value)?' selected="selected"':'' ) + '">' + text + '</option>';
	});
	$(el).html(innerHtml);
}
$(function(){
	// changing destination options on current location select 
	lm_renew = function(el){
		var form = $(el).parents('.booking')[0];
		var selectBoxSpanInner = $(form).find( '.destination-region-block .customStyleSelectBoxInner' );
		var currentSelectedItem = $(form).find( '.destination-region :selected' );
		selectBoxSpanInner.text(currentSelectedItem.text()).parent().addClass('changed');
		selectBoxSpanInner = $(form).find( '.destination-city-block .customStyleSelectBoxInner' );
		currentSelectedItem = $(form).find( '.destination-city :selected' );
		selectBoxSpanInner.text(currentSelectedItem.text()).parent().addClass('changed');
	}
	
	// fom submit
	$('.booking .search_txt_btn').click(function(e){
		//e.preventDefault();
		
		var booking_id = $(".tabs-selected a").attr("id");
		if ((booking_id != null) && (booking_id != undefined)) {
			if (booking_id == "hotel_reservations") {
				omnitureBookingWidgetForms('Hotels');
			} else if (booking_id == "airline_reservations") {
				omnitureBookingWidgetForms('Flights');
			} else if (booking_id == "car_reservations") {
				omnitureBookingWidgetForms('Cars');
			} else if (booking_id == "last_minute_deals") {
				omnitureBookingWidgetForms('Deals');
			} else if (booking_id == "book_activities") {
				omnitureBookingWidgetForms('Activities');
			} else {
				omnitureBookingForms();
			}
		} else {
			omnitureBookingForms();
		}
		
		var form = $(this).parents('.booking')[0];
		$(form).attr('target', '_blank');
		if($(form).find( '.book_start_date' ).length >0 ){
			var arrival_date			= $(form).find( '.book_start_date' ).attr('value').split('/');
			var departure_date	= $(form).find( '.book_end_date' ).attr('value').split('/');
//if(window && window.console) console.log(arrival_date);
//if(window && window.console) console.log(departure_date);
			$(form).find( '.doa_mm' ).attr( 'value', arrival_date[0] );
			$(form).find( '.doa_dd' ).attr( 'value', arrival_date[1] );
			$(form).find( '.doa_yy' ).attr( 'value', arrival_date[2] );
			$(form).find( '.dod_mm' ).attr( 'value', departure_date[0] );
			$(form).find( '.dod_dd' ).attr( 'value', departure_date[1] );
			$(form).find( '.dod_yy' ).attr( 'value', departure_date[2] );
		}
		if($(form).find( '.sel-area' ).length > 0) {
			var area_location		= $(form).find( '.sel-area' ).attr('value').split('/-#-/');
//if(window && window.console) console.log(area_location);
			$(form).find( '.inp-city' ).attr( 'value', area_location[0] );
			if(area_location.length>1){
				$(form).find( '.inp-country' ).attr( 'value', area_location[1] );
				if(area_location.length>2) $(form).find( '.inp-state' ).attr( 'value', area_location[2] );
			}
		}
	});
	
	//Redirects on select
	$('.booking .redirect-on-select').change(function(){
		var selected = $(this).find(":selected")[0];
		if( $(selected).hasClass('redirect') ) window.location.href = $(selected).attr('value');
		return false;
	});
	var showBlock = function(el){
		var form	= $(el).parents('.booking')[0];
		var id_arr = $(el).attr('id').split('-');
		var operation = id_arr.shift();
		operation = operation.split('_')[0];
		var block = "." + id_arr.join('-') + '-block';
		if( $(el).hasClass('radio-hide') ) $(form).find( '.'+ operation + '-' + id_arr.join('-') ).css('display', 'none');
		if(operation=="show") $(form).find(block).css('display', 'block');
		else $(form).find(block).css('display', 'none');
	}
	$('.booking .show-option:checked').each(function(index, item){
		showBlock($(item));
	});
	$('.booking .show-option').click(function(){
		showBlock($(this));
	});
	$('.booking .radion-checkbox').each(function(index, el){
		var form	= $(el).parents('.booking')[0];
		$(form).find('.' + $(el).attr('id')).click(function(){
			$(el).removeAttr('checked');
		});
	});
	$('.booking .radion-checkbox').click(function(){
		var form	= $(this).parents('.booking')[0];
		$(form).find('.' + $(this).attr('id')).removeAttr('checked');
	});

	//adding tabs
	$("#booking-tabs").tabs();

	//adding datapicker
	$(".book-datapicker").datepicker({
		minDate: +0
	});


	//submit button stylising
	$('.booking input[type="image"]')
		.mouseenter(function(){ $(this).attr('src', '/extension/nxc_tiglou/design/tiglou/images/structural/search_txt_btn_over.gif') })
		.mouseleave(function(){ $(this).attr('src', '/extension/nxc_tiglou/design/tiglou/images/structural/search_txt_btn.gif') });

	
});

