﻿var pullDownTimeout;
var displayDelay;
$().ready(function () {

	if (browserSniffer.isIE6 === true) {
		return;
	}

	$('.transparent').fadeTo(0, 0.8);
	$('div#header div.subMenu').fadeTo(0, 0.85);

	wireUpTopNav();

	// Secondary menus
	$('.subMenu ul li.node').each(function () {
		if ($(this).find('ul').length > 0) {
			$(this).prepend('<a class="opener" href="#"><span>Opener</span></a>')
				    .filter(':not(.open)')
				    .addClass('closed');
		}
	});

	$('a.opener').click(function () {
		$(this).parents('li.node:first').toggleClass('closed');
		return false;
	});

	$('#main-nav ul ul a').live('mouseover', function () {
		$(this).parent('li').css('background-color', '#875885');
	});

	$('ul#main-nav li .subMenu li h2 a').live('mouseover', function () {
		$(this).parent('h2').parent('li').css('background-color', '#875885');
	});

	$('#main-nav ul ul a').live('mouseout', function () {
		$(this).parent('li').css('background-color', '#5B144B');
	});

	$('ul#main-nav li .subMenu li h2 a').live('mouseout', function () {
		$(this).parent('h2').parent('li').css('background-color', '#5B144B');
	});


	$('input.numeric').keypress(function (e) {
		if (e.which == 46)   //period
			return true;
		if (e.which < 32)
			return true;
		if (e.which < 48 || e.which > 57)
			return false;
	});
	//Image Rollovers
	$('div.rollover a img,li.rollover a img,input.rollover,a.rollover img').each(function () {
		makeRollover(this);
	});

	//ZD-3346 JS/CMS popup gallery now disabled. All galleries redirect to /photos-and-videos/photos

	// Image galleries
	/*
	$('.gallery').each(function (i, e) {
		e = $('a', e).get(0) || e;
		prepareGalleryDialog(950, 700,
			'Close galleries',
			e,
			$(e).attr('href'));
	});
	*/
	alignSubmenus();
});

function alignSubmenus() {

	//force cufon to run
	Cufon.now();

	//find the width of the anchor in the menu and use that to position the submenu
	$('ul#main-nav > li').each(function (e) {
		var width = 0;

		//these grandchildren will be the cufon elements
		$(this).children('a').children().each(function (xe) {
			width += $(this).width();
		});

		$(this).children('div.subMenu').css('left', width + 'px');
	});
}

function wireUpTopNav() {

	var menuImageOriginalPath;

	//top menu li elements
	$('#header > ul li').hover(function () {
		window.clearTimeout(pullDownTimeout);

		//This fixes a z-index bug in IE7 - the li elements were being ordered above the submenus
		//and any z-index css rules were ignored
		if (browserSniffer.isIE7 === true) {
			$('li#about').css('z-index', '-1');
			$('li#courses').css('z-index', '-2');
			$('li#colleges').css('z-index', '-3');
			$('li#prices').css('z-index', '-4');
			$('li#advice').css('z-index', '-5');
			$('li#galleries').css('z-index', '-6');
		}

		//sub menu div in this menu item
		var submenu = $('.subMenu', this);

		if (submenu.length == 0)
			return;

		//hide all sub menus containers
		$('.subMenu', $(this).parents('ul')).hide();

		//var menuImage = $('img', $(this));
		//menuImageOriginalPath = menuImage.attr('src');
		//var rolloverImagePath = generateRolloverImageName(menuImageOriginalPath);

		//display delay stops the submenu poping out if the cursor is just travelling
		//across it to another area of the page - timeout is cleared further down when mouse leaves...
		displayDelay = window.setTimeout(function () {
			//fade in selected submenu container
			if (submenu.is(':hidden')) {

				submenu.show().fadeTo(250, 0.9);

				//do rollover
				//menuImage.attr("src", rolloverImagePath);
			}
		}, 300);

	}, function () {

		window.clearTimeout(displayDelay);

		//sub menu container for this menu item
		var submenu = $('.subMenu', this);

		//phase out submenu
		if (submenu.length > 0) {
			$('img', $(this)).attr("src", menuImageOriginalPath);

			pullDownTimeout = window.setTimeout(function () {
				submenu.fadeTo(250, 0, function () {
					submenu.hide();
				});
			}, 500);
		}
	});
}

function basicImagePreloader(path) {
	var body = $('body');
	var image = $('<img/>');
	$(image).css('display', 'none');
	$(image).attr('src', path);
	$(body).append($(image));
}

// Uses convention to find rollover image (image.ext > image-rollover.ext)
function makeRollover(element) {
	if (element.length == 0) return;

	var originalImage = $(element).attr("src");

	var rolloverImage = generateRolloverImageName(originalImage);

	//check if a rollover image exists on the server
	$.ajax({
		url: rolloverImage,
		success: function (data) {
			//preload image
			basicImagePreloader(rolloverImage);

			//hook up rollover event
			$(element).hover(function () {
				$(element).attr("src", rolloverImage);
			}, function () {
				$(element).attr("src", originalImage);
			});
		},
		error: function (request, status, error) {
			//IE7 fails to do the AJAX request

			//preload image
			basicImagePreloader(rolloverImage);

			//hook up rollover event
			$(element).hover(function () {
				$(element).attr("src", rolloverImage);
			}, function () {
				$(element).attr("src", originalImage);
			});
		}
	});
}

function generateRolloverImageName(originalImage) {
	var fileExtension = originalImage.substring(originalImage.length - 4, originalImage.length)

	var pathWithoutFileExtension = originalImage.substring(0, originalImage.length - 4);

	var rolloverImage = pathWithoutFileExtension + "-rollover" + fileExtension;

	return rolloverImage;
}
// For the home page map rollovers
function mapRollover(element, rolloverImage) {
	var mapContainer = $('#home-map');
	var originalImage = $(mapContainer).css("backgroundImage");

	basicImagePreloader(rolloverImage);

	//hook up rollover event
	$(element).hover(function () {
		$(mapContainer).css("backgroundImage", 'url(' + rolloverImage + ')');
	}, function () {
		$(mapContainer).css("backgroundImage", originalImage);
	});
}

function launchSWFObject(video, containerId, width, height) {
	var flashvars = {
		file: video,
		autostart: "true"
	}

	var params = {
		allowfullscreen: "true",
		allowscriptaccess: "always"
	}

	var attributes = {
		id: "player1",
		name: "player1"
	}

	swfobject.embedSWF("/assets/JWFlashPlayer/player-licensed.swf", containerId, width, height, "9.0.115", false, flashvars, params, attributes);
}



//Script for Photo Gallery Dialog content items
function prepareGalleryDialog(width, height, title, anchor, galleryUrl) {
	var dialogHtml = '<div class="photoGalleryDialog" id="photoGalleryDialog" title="' + title + '"><iframe src ="' + galleryUrl + '" width="100%" height="100%" border="0"><p>Your browser does not support iframes.</p></iframe></div>';
	var dialog = $(dialogHtml);

	$(dialog).dialog({
		bgiframe: true,
		autoOpen: false,
		width: width,
		height: height,
		modal: true,
		resizable: false
	});

	$(anchor).click(function () {
		$(dialog).dialog('open');

		return false; //prevent link click navigating away from this page
	});
}
//End script for Photo Gallery Dialog content items
