﻿var $j = jQuery.noConflict();

jQuery(document).ready(function() { //Start code ******************************************
	// Tabs - used in EUKeyProducts
	$j(".priceTabContainer").tabs();

	var prefix = window.location.protocol
	if (prefix == "https:") {
		var imageServer = "https://images.golfbreaks.com/customimage.aspx?format="
	}
	else {
		var imageServer = "http://images.golfbreaks.com/customimage.aspx?format="
	};

	var elements = $j(".customBackground");

	$j(elements).each(function() {
		var element = $j(this);
		var classNames = $j(element).attr("class").split(" ");

		var customBackground = "";
		for (j = 0; j < classNames.length; j++) {
			if (classNames[j] == "customBackground" && (j + 1) < classNames.length) {
				customBackground = classNames[j + 1];
				break;
			}
		};

		if (customBackground != "") {
			// Get the height and width of the element
			var width = $j(this).attr("offsetWidth");
			var height = $j(this).attr("offsetHeight");

			// Set the custom background image of the element
			var backgroundImage = imageServer + customBackground + "&width=" + width + "&height=" + height;
			$j(element).css("backgroundImage", "url(" + backgroundImage + ")");
		};

	});

	// Tabs - used in membership area
	$j(".tabContainer").tabs({ cookie: { expires: 30} });

	// Various forms don't fire when you use the enter key to submit & usual .net hack doesn't fix it
	$j("#[id*=VenueSearch] input[type=text]").enterKeySubmit($j("#[id*=VenueSearch] input[type=image]"));
	$j("#[id*=googleSearchTextBox]").enterKeySubmit($j("#[id*=Header] div.googleSiteSearch input[type=image]"));

	$j("input.datepicker").datepicker({
		dateFormat: 'dd/mm/yy',
		minDate: 0,
		showOn: 'button',
		buttonImage: '/Themes/Golfbreaks/Images/calendar-dropdown.gif',
		buttonImageOnly: true,
		beforeShow: function() {
			if ($j(this).parent("#[id*=QuickSearch]") != null) {
				$j("#[id*=QuickSearch].searchPanel").addClass("datepickerHack");
			}
		},
		onClose: function() {
			if ($j(this).parent("#[id*=QuickSearch]") != null) {
				$j("#[id*=QuickSearch].searchPanel").removeClass("datepickerHack");
			}
		}
	});

	$j(".ui-datepicker-trigger")
        .attr("alt", "Calendar")
        .attr("title", "Open calendar");

	// Venue images - swap thumbs / main image
	$j("#[id*=VenueImageThumbnails] img").click(function(event) {

		// Construct the new main image URL and set
		$j("#[id*=mainImageContainer] img")
			.attr("src", $j(this).attr("src").replace("100/72", "230/123"))
    		.attr("alt", this.alt);

		// Sort the thumbnail image class
		$j("#[id*=VenueImageThumbnails] img").removeClass("selected");
		$j(this).addClass("selected");
	});

	$j("#[id$=crossSellingVenues] li:last, #[id*=SpecialOffersList] li:last-child, #[id*=CategorySpecialOfferList] li:last-child").addClass("last");
});