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

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

				/* submit button */
				$('input#btn_submit_query').click(function(event){
					// load result
					repopulateCourseVenue(jQuery("#weekendCountrySelect").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);

					/* repopulate country cities onload */
					repopulateCountries();
					repopulateCountryCities(jQuery("#user_country_code").val());

					// repopulate course result
					repopulateCourseVenue(jQuery("#user_country_code").val());

					// manipulation!!
					$("#weekendCitySelect").show();
					var productCode = $("#product_code").val();
					if ((productCode === "STAFFMWKT") || (productCode === "STAFFMPTP") || (productCode === "ADMINMPTP") || (productCode === "STAFFCWKT") || (productCode === "STAFFCPTP")) {
						switch (productCode) {
							case "STAFFMWKT":
								boxtitle = "Moodle Weekend TEFL";
							break;
							case "ADMINMWKT":
								boxtitle = "Moodle Weekend TEFL Admin";
								break;
							case "STAFFMPTP":
								boxtitle = "Moodle Job Placements";
								$("#weekendCitySelect").hide();
							break;
							case "ADMINMPTP":
								boxtitle = "Moodle Job Placements Admin";
								$("#weekendCitySelect").hide();
								break;
							case "STAFFCWKT":
								boxtitle = "CLOOP Weekend TEFL";
								$("#weekendCitySelect").hide();
								break;
							case "STAFFCPTP":
								boxtitle = "CLOOP Job Placements";
								$("#weekendCitySelect").hide();
								break;
						}
						$("#ui-dialog-title-dialog").html("Staff View: "+boxtitle);
					} else {
						$("#ui-dialog-title-dialog").html("Choose your classroom course location from the options below:");
					}


				});

			});

		})(jQuery);

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

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

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

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

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