/**
* File: 		chair.js
* Author: 		Matt Skelton
* Company: 		Broda Seating LTD.
* Date:		January 13, 2009		: Created
*			January 15, 2009 	: Updated
* Description:	This file contains mutliple functions/methods that assist in Ajax implementations of the seatingisbelieving.com website.
* License:		You are free to use any code within this file as you see fit, as long as you include the header of this file. Cheers!
*/

/**
* Makes an Ajax call to retrieve an image for a chair. Updated the DOM to replace the old image with the new image
*/
switchImage = function(chairId, imageId)
{
	var productImageContainer = document.getElementById("productImageContainer");
	
	if (productImageContainer)
	{	
		var xmlHttp 	= getTransport();
		
		if (xmlHttp == null)
		{
			return;
		}
		
		var url = "/image/getByIdAjax/" + imageId;
		
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
			{			
				var imageProperties = xmlHttp.responseText.split("\n");
				
				var imageId 		= imageProperties[0];
				var imageSrc 		= imageProperties[1];
				var imageAltText 	= imageProperties[2];
				
				productImageContainer.innerHTML = "<img src=\"/system/application/images/" + imageSrc + "\" alt=\"" + imageAltText + "\" />";
				
				var largeImageLink = document.getElementById("largeImageLink");
				
				if (largeImageLink)
				{
					largeImageLink.innerHTML = "<p><a href=\"/chair/largeImage/" + imageId + "/" + chairId + "\" onclick=\"window.open('/chair/largeImage/" + imageId + "/" + chairId + "', '_blank', 'width=400,height=400,scrollbars=no,status=no,resizable=yes,screenx=0,screeny=0'); return false;\" target=\"_blank\">Larger Image</a></p>";
				}
			}
		}

		xmlHttp.send(null);
		
		return false;
	}
}

/**
* Resizes the Chair's Lage Image window to the size of the image
*
* Counrtesy: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23426877.html
*/
resizeByImg = function() 
{
	var oImg 	= document.getElementById("largeProductImage");
    var width  	= oImg.offsetWidth + 10;
	var height 	= (oImg.offsetHeight) + 70;
	
    var LeftPosition 	= (screen.width) ? (screen.width-width)/2 : 0;
    var TopPosition 	= (screen.height) ? (screen.height-height)/2 : 0;
	
    window.moveTo(LeftPosition, TopPosition);
    window.resizeTo(width, height);
}