// instantiate global variables
var map;
var panoLayer;
var wikiLayer;
var radioChecked;


function initialiseSchoolFilters()
{
	// bind function to the change events of sort order and user currency
	$("#results_info select.submit").change(function() {$(this).parents("form:first").submit();});

	// bind function to the change event of recently viewed
	$("#results_info select#recently_viewed").change(function() {
		if ($(this).val()) {
			window.location = $(this).val();
		}
	});

	// bind function to the click event of go button
	$("#results_filter #go").click(function() {
		var url = $("#results_filter form#filter_form").attr("action");
		var region = $("#results_filter select#region").val();
		$("#results_filter form#filter_form").attr("action", url+region+"/").submit();
	});
}


function initialiseSchoolResults()
{
	// bind function to the change event of the course weeks and accommodations dropdowns
	$("#school_results_list .courses .weeks select, #school_results_list .courses .accommodations select").change(function() {
		var weeks = $(this).parents("tr:first").find("td.weeks select").val();
		var prices = $(this).parents("tr:first").find("td.accommodations select option:selected").attr("class");
		prices = prices.split(",");
		$(this).parents("tr:first").find("td.price span").html(prices[weeks-1]);
	});

	// bind function to the click event of checkboxes
	$("#school_results_list .courses .select input").click(function() {
		// remove highlights from all other rows
		$(this).parents("table:first tr").removeClass("highlight");

		// highlight selected row
		$(this).parents("tr:first").addClass("highlight");
	});

	// bind function to the click event of book now button
	$("#school_results_list .book_now").click(function() {
		if (!$(this).parents("form:first").find("td.select input:checked").val()) {
			$(this).parents("form:first").find("input#weeks").remove();
			$(this).parents("form:first").find("input#accommodation").remove();
		}
		else {
			var weeks = $(this).parents("form:first").find("td.select input:checked").parents("tr:first").find("td.weeks select").val();
			var accommodation = $(this).parents("form:first").find("td.select input:checked").parents("tr:first").find("td.accommodations select").val();
			$(this).parents("form:first").find("input#weeks").val(weeks);
			$(this).parents("form:first").find("input#accommodation").val(accommodation);
		}

		$(this).parents("form:first").submit();
		return false;
	});

	// bind function to the click event of the more courses link
	$("#school_results_list .toggle_hidden_courses").click(function() {
		$(this).parent().find("tr.more").toggleClass("hidden");
		$(this).toggleClass("open");
		return false;
	});
}


function initialiseSchoolProfile()
{	
	// bind function to the click event of the course type
	$("#school_window .courses .type a").click(function() {
		$(this).parents("tr:first").next("tr.extra").toggleClass("hidden");
		return false;
	});

	// bind function to the change event of the course and accommodation week dropdowns
	$("#school_window .courses .weeks select").change(function() {updatePrices();});

	// bind function to the mousedown event of checkboxes
	$("#school_window .courses .select input").mousedown(function() {
		radioChecked = $(this).attr("checked");
	});

	// bind function to the click event of checkboxes
	$("#school_window .courses .select input").click(function() {
		if (radioChecked) {
			$(this).attr("checked", false);
		}
		updatePrices();
	});

	// bind function to the click event of book now button
	$("#school_window .book_now").click(function() {
		if (!$(this).parents("form:first").find("tr.course td.select input:checked").val()) {
			alert("You must select a course to book");			
		}
		else {
			var course_weeks = $(this).parents("form:first").find("tr.course td.select input:checked").parents("tr:first").find("td.weeks select").val();
			$(this).parents("form:first").find("input#course_weeks").val(course_weeks);

			if ($(this).parents("form:first").find("tr.accommodation td.select input:checked").val()) {
				var accommodation_weeks = $(this).parents("form:first").find("tr.accommodation td.select input:checked").parents("tr:first").find("td.weeks select").val();

				if (accommodation_weeks > course_weeks) {
					alert("The accommodation weeks cannot be longer than the course weeks");
					return false;
				}

				$(this).parents("form:first").find("input#accommodation_weeks").val(accommodation_weeks);
			}

			$(this).parents("form:first").submit();
		}

		return false;
	});

	// update prices
	updatePrices();
}


function updatePrices()
{
	var total = 0;
	var deposit = 0;

	// update all prices
	$("#school_window .courses .weeks select").each(function() {
		var price = $(this).children("option:selected").attr("title");
		$(this).parents("tr:first").find("td.price span").html(price);
	});

	// remove highlights from all rows
	$("#school_window .courses tr").removeClass("highlight");

	// calculate total and add highlights to appropriate rows
	$("#school_window .courses td.select input:checked").each(function() {
		total += parseFloat($(this).parents("tr:first").find("td.price span").html());
		total += $(this).parents("tr:first").next("tr.extra").find("span.fee").html() ? parseFloat($(this).parents("tr:first").next("tr.extra").find("span.fee").html()) : 0;

		// highlight selected rows and their descriptions
		$(this).parents("tr:first").addClass("highlight");
		$(this).parents("tr:first").next("tr.extra").addClass("highlight");
	});

	// calculate deposit
	$("#school_window .courses tr.course td.select input:checked").each(function() {
		deposit += parseFloat($(this).parents("tr:first").find("td.price span").html());
	});

	deposit = deposit * 0.2;

	total = total.toFixed(2);
	deposit = deposit.toFixed(2);
	$("#school_window .courses #total_price").html(total);
	$("#school_window .courses #deposit").html(deposit);
}


function showSchoolMap(latitude, longitude, icon, school_name, street_address, city, country)
{
    Shadowbox.open({
        player:     "html",
        content:    "",
        height:     450,
        width:      600,
        options:    {
            onFinish: function(item) {
		if (GBrowserIsCompatible()) 
		{
			// create map and add controls
			map = new GMap2(document.getElementById("sb-body-inner"));
			map.addControl(new GLargeMapControl());
			map.addControl(new MapControls());
			//map.addControl(new GMapTypeControl());

			// set centre of map to coordinates of school and add zoom and map type controls
			var center = new GLatLng(latitude, longitude);
			map.setCenter(center, 15);	
	
			// create marker icon
			var schoolIcon = new GIcon();
			schoolIcon.image = "http://www.eurolanguages.com/images/school_map_icon.png";
			schoolIcon.shadow = "http://www.eurolanguages.com/images/school_map_icon_shadow.png";
			schoolIcon.iconSize = new GSize(32, 32);
			schoolIcon.shadowSize = new GSize(56, 32);
			schoolIcon.iconAnchor = new GPoint(16, 32);
			schoolIcon.infoWindowAnchor = new GPoint(16, 0);

			// set html for marker
			var html = new Array('<div id="map_marker"><div id="icon"><img src="http://www.eurolanguages.com/images/school_photos/');
			html.push(icon);
			html.push('" width="82" align="left" /></div><div><div id="title">');
			html.push(school_name);
			html.push('</div><div id="address">');
			html.push(street_address);
			html.push('<br/>');
			html.push(city);
			html.push('<br/>');
			html.push(country);
			html = html.join("");
	
			// create a marker with an info window
			var point = new GLatLng(latitude, longitude);
			var marker = new GMarker(center, {icon: schoolIcon, title: school_name});
			GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});

			// add marker to map
			map.addOverlay(marker);

			// open html window
			marker.openInfoWindowHtml(html);

			// define layers
			panoLayer = new GLayer("com.panoramio.all");
			wikiLayer = new GLayer("org.wikipedia.es");
                }
	  }
	}
    });
}


function showPanoLayer()
{
	if ($("#pano_checkbox").attr("checked")) {map.addOverlay(panoLayer);}
	else {map.removeOverlay(panoLayer);}
}


function showWikiLayer()
{
	if ($("#wiki_checkbox").attr("checked")) {map.addOverlay(wikiLayer);}
	else {map.removeOverlay(wikiLayer);}
}


$(document).unload(function()
{
	// prevents memory leaks
	GUnload();
});

