/* Display a slide from the gallery of an individual product.
 *
 * Requirements: 
 *    - globally set a JS array gallerySlides[] containing the URLs and parents of all the slides (construct JS via ditto call)
 * 								i.e. gallerySlide[modx_docid_of_slide] = array('url.png', modx_parent_id)
 *    - moonbox.js (must be loaded *after* this script)
 */

jQuery(document).ready(function() 
	{
	if (!jQuery('a[rel^="lightbox"]').length)
		// Ensure moonbox loads when it checks for links
		jQuery('body').append('<div style="display: none"><a rel="lightbox" href="#"></a></div>');
	});

function restartFlash()
	{
	(jQuery.browser.msie ? window["main"] : document["main"]).asFunc("close");
	}

function displayGallerySlide(id)
	{
	// Check for Moonbox and non-empty slides array
	if (!jQuery.Moonbox.loaded || !gallerySlides.length) return false;
	// Prepare the image set
	jQuery.Moonbox.images = new Array();
	var cnt = 0;
	var parent = gallerySlides[id][1];
	for (key in gallerySlides)
		{
		// Set image to start at
		if (key == id) jQuery.Moonbox.current = cnt;
		// Only pick slides with the same parent as the current slide
		if (gallerySlides[key][1] == parent) jQuery.Moonbox.images[cnt++] = new Array(gallerySlides[key][0], '');
		}

	// Prepare Moonbox - THE FOLLOWING CODE IS moonbox.open() re-written so that Flash is only hidden for linux

	jQuery.Moonbox.position();

	// Hide flash in linux
	if (navigator.platform.indexOf('Linux') != -1)
		{
		jQuery('object#main').css('visibility', 'hidden');
		function Moonbox_showFlash ()
			{
			jQuery('object#main').css('visibility', 'visible');
			}
		jQuery.Moonbox.box.closelink.click(Moonbox_showFlash);
		jQuery.Moonbox.box.overlay.click(Moonbox_showFlash);
		}

	// Ensure flash gets restart signal
	jQuery.Moonbox.box.closelink.click(restartFlash);
	jQuery.Moonbox.box.overlay.click(restartFlash);

	// Set the top of the wrapper
	jQuery.Moonbox.box.wrapper.css('top', jQuery(window).scrollTop() + (jQuery(window).height() / 15));

	// Show Moonbox
	if (jQuery.Moonbox.step == -1)
		{
		// Show overlay, then wrapper
		jQuery.Moonbox.box.overlay.css({'opacity': 0, 'display': 'block'}).fadeTo(500, 0.9, function(){
			jQuery.Moonbox.box.wrapper.show();
			});
		}

	jQuery.Moonbox.step = 0;

	// Display that image!
	jQuery.Moonbox.change();
	}

/* Menu scripts
 *
 * a) drop downs
 * b) fading language menu
 */

jQuery(document).ready(function() {

	/*** Drop down menus ***/
	jQuery('#nav li:not(.active) ul').css('display', 'none').css('left', '0px'); // Undo CSS for screen readers
	jQuery('#nav .parent').hoverIntent({
		over: function ()	// **** show ****
			{
			var menuSecondLevel = jQuery(this).find('ul:hidden');
			if (menuSecondLevel.length)
				{
				menuSecondLevel.height(1).slideDown(300); // height=1 makes IE set the correct width to the dropdown 
				jQuery(this).siblings().find('ul').fadeOut(200, function ()
					{ // IE7 - remove filter attribute to enable cleartype when menu re-displayed
					if (jQuery.browser.msie) this.style.removeAttribute('filter');
					});
				}
			},
		out: function ()	// **** hide ****
			{
			if (this.className.indexOf('active') == -1)
				{
				jQuery(this).find('ul').slideUp(200);
				jQuery(this).siblings().filter('.active:has(ul)').find('ul').fadeIn(200, function ()
					{ // IE7 - remove filter attribute to enable cleartype
					if (jQuery.browser.msie) this.style.removeAttribute('filter');
					});
				}
			}
		}
		).filter(':not(#nav-26)').children('a').click(function () { return false; }).css('cursor', 'default');	// block top level click

	/*** Fade to language menu ***/
	jQuery('#nav-35 a').click(function()
		{
		jQuery('#nav').fadeOut(1000, function ()
			{
			jQuery('#language-menu').fadeIn(1000, function () 
				{ // IE7 - remove filter attribute to enable cleartype
				if (jQuery.browser.msie) this.style.removeAttribute('filter');
				});
			});	
		return false;
		});

	/*** Fade back to normal menu; reload if required ***/
	jQuery('#language-menu a').click(function ()
		{
		if (this.parentNode.className.indexOf('active') == -1) 
			// different language selected - reload
			window.location = this.href;
		else
			{
			// Fade back to navigation
			jQuery('#language-menu').fadeOut(1000, function () 
				{
				jQuery('#nav').fadeIn(1000, function ()
					{ // IE7 - remove filter attribute to enable cleartype
					if (jQuery.browser.msie) this.style.removeAttribute('filter');
				
					});
				});
			return false;
			}
		});
	});

/* 
 * Non-HTML pages content reveal
 */
jQuery(document).ready(function()
	{
	if (!jQuery('.flash-page').length)
		{
		document.getElementById('background').style.display = 'none';
		document.getElementById('content').style.display = 'none';
		if (document.getElementById('watch-img')) document.getElementById('watch-img').style.display = 'none';

		jQuery(window).load(function () //wait for images to load
			{
			jQuery('#background, #watch-img').slideDown(1000);
			setTimeout(function ()
				{
				jQuery('#content').slideDown(800);
				}, 200);
			});
		}
	});
