/**
* this is for venue finder v3 jQuery version
* lazy to update all pages related to
*
* @version 3.1 $2009-08-08 AHO$
* @modified $2009-10-23 AHO$
*
**/
	(function($) {
		$(document).ready(function(){

			/* country select */
			$('#weekendCountrySelect').change(function() {
				repopulateCountryCities($("#weekendCountrySelect").val());
			});

			/* submit button */
			$('input#btn_submit_query').click(function(event){
				// load result
				repopulateCourseVenue($("#weekendCountrySelect").val(), $("#weekendCitySelect").val());
			});

			// -------- modal dailog setup ------------ //
			// setup modal dialog
			$("#dialog").dialog({
				bgiframe: true,
				autoOpen: false,
				width: 820,
				height: 510,
				resizable: false,
				modal: true
			});

			// open dialog
			$('a.pop-venue').click(function(event){

				$('#dialog').dialog('open');
				event.preventDefault();
				// product code
				var product_code = this.id.replace('find_venue-', "");

			    // course code in hidden value
			    $("#product_code").val(product_code);

			    // default in page
			    var countrycode = $("#user_country_code").val();
			    if ($("#page_city_name").length > 0) {
			    	var pagecityname = $("#page_city_name").val();	// if we have city name
			    } else {
			    	var pagecityname = "-1";	// default - no city name given
			    }

				/* repopulate country cities onload */
				repopulateCountries();
				repopulateCountryCities(countrycode,pagecityname);

				// repopulate course result
				repopulateCourseVenue(countrycode,pagecityname);

				$("#weekendCitySelect").show();
				var productCode = $("#product_code").val();
				$("#ui-dialog-title-dialog").html("Choose your classroom course location from the options below:");

			});

		});

		/* repopulate countries  */
		var repopulateCountries = function() {
			var countrycode = $("#user_country_code").val();
			var productcode = $("#product_code").val();
			$('#weekendCountrySelect').html('<option value="">loading...</option>');
			$("#weekendCountrySelect").load("/ajax/courseVenueFinder.ajax.php?getcv=countries&countrycode="+countrycode+"&productcode="+productcode);
		}

		/* repopulate country cities */
		var repopulateCountryCities = function(countrycode,cityname) {
			$('#weekendCitySelect').html('<option value="">loading...</option>');
			$("#weekendCitySelect").load('/ajax/courseVenueFinder.ajax.php?getcv=country_cities',
				{'countrycode':countrycode, 'cityname':cityname}
			);
		}

		/* repopulate course venue result */
		var repopulateCourseVenue = function(countrycode,cityname) {
			$('#venueResultsBox').html('<div class="loading" id="loading"> Loading <img alt="loading" src="/images/common/loading.gif" /> </div>');
			$('#venueResultsBox').load('/ajax/courseVenueFinder.ajax.php?getcv=filter_result',
				{'productcode':$("#product_code").val(), 'countrycode':countrycode,
				'cityname':cityname, 'daterange':$("#weekendDateRangeSelect").val()}
			);
			return false;
		}

		/* city info - temp disabled */
		var getCityCopy = function(city_name) {
			$('#venueInfoPanel').html('<p>Click on a course name in the search results above for further information.</p>');
			$('#venueInfoPanel').html('<div class="loading" id="loading"> Loading <img alt="loading" src="/images/common/loading.gif" /> </div>');
			$('#venueInfoPanel').load('/ajax/courseVenueFinder.ajax.php',
				{'getcv':'city_info', 'cityname': city_name}
			);
		}

	})(jQuery);

	/* booking redirect */
	function doBookNow(url) {
		window.open(url);
		jQuery("#dialog").dialog("close");
	}