// Adds onclick events to product Images
function productImageHandler()
{
	var tag_array = document.getElementsByTagName("a");
	
	// Do we have an object
	if(tag_array)
	{
		for (i=0; i<tag_array.length; i++)
		{
			// Loop around div
			// Product Minus
			if(tag_array[i].className == "jsProductImage")
			{
				// Add the on click behaviour
				tag_array[i].onmouseover = function()
				{
					// Get the image url
					var imageUrl = this.getAttribute("href");
					
					// Change the /images/huge/ to /images/large/
					imageUrl = imageUrl.replace("huge", "large");
					
					// Get the product Image
					var productImage = document.getElementById("product-image");

					// Change the product image			
					//productImage.setAttribute('src', imageUrl);	
					productImage.innerHTML = "<span></span><img src=\"" + imageUrl +"\" >";
					
					// Don't follow the link
					return false;
				}
			}	
		}
	}
}

addLoadEvent(productImageHandler); 


function productMovieHandler()
{
	var tag_array = document.getElementsByTagName("a");
	
	// Do we have an object
	if(tag_array)
	{
		for (i=0; i<tag_array.length; i++)
		{
			// Loop around div
			// Product Minus
			if(tag_array[i].className == "jsProductMovie")
			{
				// Add the on click behaviour
				tag_array[i].onclick = function()
				{
					// Get the movie url
					var movieUrl = this.getAttribute("href");
					
					// Get the current product Image
					var productMovie = document.getElementById("product-image");
					
					// Change the product image to the movie
					
					var html = "<object id=\"FlashID\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"300\" height=\"300\" style=\"visibility: visible;\">";
					html = html + "<param name=\"movie\" value=\"" + movieUrl + "\" />";
					html = html + "<param name=\"quality\" value=\"high\" />";
					html = html + "<param name=\"wmode\" value=\"opaque\" />";
					html = html + "<param name=\"swfversion\" value=\"6.0.65.0\" />";
					html = html + "<!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you donŐt want users to see the prompt. -->";
					html = html + "<param name=\"expressinstall\" value=\"/Scripts/expressInstall.swf\" />";
					html = html + "<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->";
					html = html + "<!--[if !IE]>-->";
					html = html + "<object type=\"application/x-shockwave-flash\" data=\"" + movieUrl + "\" width=\"300\" height=\"300\">";
					html = html + "<!--<![endif]-->";
					html = html + "<param name=\"quality\" value=\"high\" />";
					html = html + "<param name=\"wmode\" value=\"opaque\" />";
					html = html + "<param name=\"swfversion\" value=\"6.0.65.0\" />";
					html = html + "<param name=\"expressinstall\" value=\"/Scripts/expressInstall.swf\" />";
					html = html + "<!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->";
					html = html + "<div>";
					html = html + "<h4>Content on this page requires a newer version of Adobe Flash Player.</h4>";
					html = html + "<p><a href=\"http://www.adobe.com/go/getflashplayer\"><img src=\"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif\" alt=\"Get Adobe Flash player\" width=\"112\" height=\"33\" /></a></p>";
					html = html + "</div>";
					html = html + "<!--[if !IE]>-->";
					html = html + "</object>";
					html = html + "<!--<![endif]-->";
					html = html + "</object>";
					html = html + "<script type=\"text/javascript\">";
					html = html + "<!--";
					html = html + "swfobject.registerObject(\"FlashID\");";
					html = html + "//-->";
					html = html + "</script>";
					
					//productImage.setAttribute('src', imageUrl);	
					productMovie.innerHTML = html;
					
					// Don't follow the link
					return false;
				}
			}	
		}
	}
}

addLoadEvent(productMovieHandler);

// New functions to create modal window and ajaxForm below here

// Firstly lets put jQuery into noConflict mode since we have other javascript libraries
jQuery.noConflict();


// Lets perform a ready function
jQuery(document).ready(function()
{
	// Now lets add a click handler to the reserve button
	jQuery('#reserve').click(function()
	{
		// Scroll to top of page
		jQuery('html, body').animate({scrollTop: 0}, 200);
		
		// Lets open the overlay and popup
		// jQuery('#modal-overlay').fadeIn('100', loadModalWindow);
		
		loadModalWindow();
		
		// Intercept the link
		return false;
		
	});
		
});


function loadModalWindow()
{
	// Load in the modal window then run prepareform
	jQuery('#modal').fadeIn('100', prepareForm);
}

/**
 * This function gets the product id and brings in the ajax form
 * 
 * @return
 */
function prepareForm()
{
	// Get the productId
	var productId = jQuery('#reserve').attr('rel');
	
	// Now run an ajax call and stick this into modal
	jQuery('#modal').load("/pages/ajax-reserve.php", {productId: productId}, function()
	{
		// Create the ajaxform options
		var options = 
		{
				target:	'#modal',
				beforeSubmit: showRequest,
				success: showResponse,
				clearForm: true,
				url: "/pages/ajax-reserve.php"
		};
		
		jQuery('#name').focus();
		
		// Assign this to the form
		jQuery('#reservation-form').ajaxForm(options);
		
		// Add a click handler to the overlay so it will clear
		jQuery('#close-button').click(function()
		{
			// Add a remove in fast mode to this
			removeModal('fast');
		});
		
		jQuery('input').each(function()
		{
			jQuery(this).click(function()
			{
				jQuery(this).focus();
			});
		});
		
		
	});
	
}

/**
 * This will run before the form submits (add validation)
 * @param formData
 * @param jqForm
 * @param options
 * @return
 */
function showRequest(formData, jqForm, options)
{
	var queryString = jQuery.param(formData);
	
	// Run some validation before we submit the form
	
	if(jQuery('#name').val() == "")
	{
		alert('Please enter your name.');
		
		return false;
	}
	
	if(jQuery('#email').val() == "")
	{
		alert('Please enter your email address.');
		
		return false;
	}
	
	if(validateEmail(jQuery('#email').val()) == false)
	{
		alert('Please enter a valid email address.');
		
		return false;
	}
	
	// Debug code // Remember to turn this off
	// alert ('About to submit: \n\n' + queryString);

	// If we haven't failed out, then submit by returning true!
	return true;
}

/**
 * Performs actions after the form has responded
 * @param responseText
 * @param statusText
 * @return
 */
function showResponse(responseText, statusText)
{
	// Run the slow remove window code
	removeModal('slow');
}

function removeModal(speed)
{
	// This will fade the windows out depending on the speed
	if(speed == 'slow')
	{
		jQuery('#modal').delay('3000').fadeOut('200');
		jQuery('#modal-overlay').delay('3000').fadeOut('200');
	}
	else if(speed == 'fast')
	{
		jQuery('#modal').fadeOut('100');
		jQuery('#modal-overlay').fadeOut('100');
	}
}

/**
 * Email validator
 * 
 * @param str
 * @return
 */
function validateEmail(str) 
{
	// Something here is broken so turning this off
	
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}

	if (str.indexOf(" ")!=-1)
	{
		return false;
	}
	
	return true;		
}
