var $j = jQuery.noConflict();

$j(window).load(function() { //Start code ******************************************

	var prefix = window.location.protocol;
	var imageServer = "http://images.golfbreaks.com/customimage.aspx?format=";
	if (prefix == "https:") {
		imageServer = "https://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} });
	// Handles error message on page when switching between tabs
	$j(".tabContainer").bind('tabsselect', function(event, ui) { $j(".pageMessage").attr('style', 'display: none'); });

	// 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("#directionsFieldset input[type=text]").enterKeySubmit($j("#directionsFieldset input[type=image]"));

	// Date picker
	$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) {

		// Get the thumbnail image ID and the banner ID
		var $imageId = this.id.split("-")[1];
		var $bannerId = $j.getQueryString({ ID: "id", URL: $j("#[id*=mainImageContainer] img").attr("src") });

		// Construct the new main image URL and set
		$j("#[id*=mainImageContainer] img")
				.attr("src", "http://images.golfbreaks.com/banner.aspx?id=" + $bannerId + "&imageid=" + $imageId)
				.attr("alt", this.alt);

		// Sort the thumbnail image class
		$j("#[id*=VenueImageThumbnails] img").removeClass("selected");
		$j(this).addClass("selected");
	});

	// Decorative .last class
	$j("#[id$=crossSellingVenues] li:last, #[id*=SpecialOffersList] li:last-child, #[id*=CategorySpecialOfferList] li:last-child").addClass("last");

	// Continue browsing link for enquiry and callback forms. Tmp until next rls
	if ($j('#rmCbLink123').length > 0 && document.referrer.indexOf('golfbreaks') != -1) {
		$j('#rmCbLink123').attr('href', document.referrer)
	};
});