//basically, all widget initialization files

// INITIALIZE PRESETS
// -----------------------------------------------------------------------------

	//autocomplete presets
	var fcities = [{"cc": "NZ", "c": "AKL", "fn": "Auckland, New Zealand (AKL)", "id": 81},{"cc": "NZ", "c": "CHC", "fn": "Christchurch, New Zealand (CHC)", "id": 825},{"cc": "NZ", "c": "DUD", "fn": "Dunedin, New Zealand (DUD)", "id": 1409},{"cc": "NZ", "c": "HLZ", "fn": "Hamilton, New Zealand (HLZ)", "id": 1146},{"cc": "NZ", "c": "NPE", "fn": "Napier-Hastings, New Zealand (NPE)", "id": 1847},{"cc": "NZ", "c": "PMR", "fn": "Palmerston North, New Zealand (PMR)", "id": 5440},{"cc": "NZ", "c": "ZQN", "fn": "Queenstown, New Zealand (ZQN)", "id": 2723},{"cc": "NZ", "c": "WLG", "fn": "Wellington, New Zealand (WLG)", "id": 4456}];
	var hcities = [{"cc": "NZ", "c": "AKL", "fn": "Auckland, New Zealand", "id": 283},{"cc": "NZ", "c": "CHC", "fn": "Christchurch, New Zealand", "id": 1344},{"cc": "NZ", "c": "DUD", "fn": "Dunedin, New Zealand", "id": 1949},{"cc": "NZ", "c": "HLZ", "fn": "Hamilton, New Zealand", "id": 2913},{"cc": "NZ", "c": "NPE", "fn": "Napier-Hastings, New Zealand", "id": 5254},{"cc": "NZ", "c": "PMR", "fn": "Palmerston North, New Zealand", "id": 5895},{"cc": "NZ", "c": "ZQN", "fn": "Queenstown, New Zealand", "id": 8792},{"cc": "NZ", "c": "WLG", "fn": "Wellington, New Zealand", "id": 8020}];

	//datepicker presets
	var today = new Date();
	var outboundDaysOffset = 21; // flights
	var inboundDaysOffset = 25;
	var checkInDaysOffset = 7; //hotels
	var checkOutDaysOffset = 9;

	//dealspreview preset
  var progressErrs = null;
  var fill_from_human = "";
  var fill_from_code = "";
  var fill_to_human = "";
  var fill_to_code = "";
  var to_js_parse_fromdate = "";
  var to_js_parse_todate = "";
  var user_country_code = "SIN";
  perPage = 8;

	//iFrame preset
	var iFrameContent = true;

// ON DOM READY
// -----------------------------------------------------------------------------

$().ready(function() {
  //deals init
	var fromTxt = $('#wg_origin').val();
	var from = $('#wg_origin_code').val();
	var toTxt = $('#wg_destination').val();
	var to = $('#wg_destination_code').val();
	
	// iFrame initialization
	// -----------------------------------------------------------------------------
	if(iFrameContent){
		
		$('body form, body a').attr({'target':'_parent'}).click(function(){
						
			var myString = $(this).attr('href');;
			var mySplitResult = myString.split("?");

			if(mySplitResult[1] == 'undefined') {
				$(this).attr({'target':'_parent', 'href':$(this).attr('href')+'&ts_code=c62a1'});				
			} else {
				$(this).attr({'target':'_parent', 'href':$(this).attr('href')+'?ts_code=c62a1'});				
			}
			

			$('#wg_navigation a').attr({'target':'', 'href':'#'});
			$('#wg_trip_type input').attr({'target':'', 'href':'#'});
			$('#wg_filter_flights ul li label').attr({'target':'', 'href':'#'});
			$('#wg_packages_nav').attr({'target':'_parent', 'href':'http://www.wego.com/packages/'+'?ts_code=c62a1'});
			$('#wg_deals_nav').attr({'target':'_parent', 'href':'http://www.wego.com/deals/'+'?ts_code=c62a1'});
		});
	}
	
	// intialize tabswitcher
	// -----------------------------------------------------------------------------
	tabswitcher('#wg_navigation a','#wg_container','',{exclude:['#wg_packages_nav','#wg_deals_nav']}); // for main navigation
	tabswitcher('#wg_trip_type input','#wg_flights_search'); // for flights navigation option
	tabswitcher('#wg_filter_flights ul li label','#wg_filter_flights ul li', true); // for flights filter
	$('#wg_flights_nav').click();
//	tabInit();
	
	// initialize datepicker
	// -----------------------------------------------------------------------------
	datepickerInit();
	
	// assign element to a datepicker
	$('#wg_outbound_date').datepicker();
	$('#wg_inbound_date').datepicker();
	$('#wg_checkIn_date').datepicker();
	$('#wg_checkOut_date').datepicker();

	//synchronize chosen dates
	synchDates('#wg_outbound_date','#wg_inbound_date');
	synchDates('#wg_checkIn_date','#wg_checkOut_date');

	// format dates and assign default value
	var givenFormat = 'dd/mm/yy';
	formatDatepicker('#wg_outbound_date', givenFormat, outboundDaysOffset);
	formatDatepicker('#wg_inbound_date', givenFormat, inboundDaysOffset);
	formatDatepicker('#wg_checkIn_date', givenFormat, checkInDaysOffset);
	formatDatepicker('#wg_checkOut_date', givenFormat, checkOutDaysOffset);
	
	// initialize form
	// -----------------------------------------------------------------------------
	initForm();	
	initField(".wg_location #wg_origin",		"Auckland, New Zealand (AKL)",		".wg_location #wg_origin_code",		"AKL");
	validateForm();

	// deals
	// -----------------------------------------------------------------------------
	var perPage = 8;
	var page = new Page(perPage, $('#wg_deals_info_panel #wg_deals_module ul#listing'), 'li',  $('#wg_dealsprev a'), $('#wg_dealsnext a'));
  var module = new Module(page,
    $('#wg_deals_info_panel #wg_deals_module'), 
    $('#wg_deals_info_panel dl'), 
    {
      originCode: '', //Cookie.get("from_code")
      originBlurb: '' //Cookie.get("from_str")
    }
  );
	dealsWhatsNew(); 
  page.init();

  // initialize autocomplete
	// -----------------------------------------------------------------------------
	var defaultFlightsAjaxUrl = 'http://www.wego.com/p/flights/locations/search'; // fcities ~ flights
	var defaultHotelsAjaxUrl = 'http://www.wego.com/hotels/locations/search'; 		// hcities ~ hotels
	autocompleteInit('#wg_origin', 						fcities, defaultFlightsAjaxUrl, '#wg_origin_code',							'flights', '', 252);
	autocompleteInit('#wg_destination',			  fcities, defaultFlightsAjaxUrl,	'#wg_destination_code',					'flights', '', 252);
	autocompleteInit('#wg_origin_activities', fcities, defaultFlightsAjaxUrl,	'#wg_location_code_activities',	'flights', '', 518);
	autocompleteInit('#wg_origin_hotels', 		hcities, defaultHotelsAjaxUrl,  '#wg_location_id_hotels',			'hotels', '#wg_location_code_hotels', 518); 
	$('.wg_location input').click(function(){this.select();});

  var callRouteHandler = function() {  module.handleRouteChange(fromTxt,from,toTxt,to); };

	$('#wg_origin, #wg_destination').change(function(){
		setDealsInp('#wg_origin','#wg_origin_code', '#wg_destination', '#wg_destination_code');
		setTimeout(callRouteHandler, 10);	
	});
	$('#wg_origin_hotels').change(function(){
		setDealsInp('#wg_origin_hotels','#wg_location_code_hotels', '', '');
		setTimeout(callRouteHandler, 10);	
	});
	$('#wg_origin_activities').change(function(){
		setDealsInp('#wg_origin_activities','#wg_location_code_activities', '', '');
		setTimeout(callRouteHandler, 10);	
	});

	function setDealsInp(frmTxt, frm, tTxt, t){
			 fromTxt = $(frmTxt).val().length > 0 ? $(frmTxt).val() : '';
			 from = $(frm).val().length > 0 ? $(frm).val() : '';
			 toTxt = $(tTxt).val().length > 0 ? $(tTxt).val() : '';
			 to = $(t).val().length > 0 ? $(t).val() : '';
	}
	$('#wg_hotels_form li input[name=city]').click(function(){
		$('#wg_origin_hotels').val($(this).parent().find('label').text());
		$('#wg_location_id_hotels').val($(this).val());
		$('#wg_location_code_hotels').val($(this).attr('id'));
		$('#wg_origin_hotels').change();
	});
	var actFilter = '#wg_research_form li.wg_destination h3 a';
	$(actFilter).hide();	
	$('#wg_research_form input[type=radio]').click(function(){
		$(actFilter).show();
	});
	$(actFilter).click(function(){
		$('#wg_research_form input[type=radio]').attr('checked',false);
		$(this).hide();
	});
});