
addLoadEvent(initLightbox);
addLoadEvent(prepareLinks);
addLoadEvent(shopnavResize);


function shopnavResize()
{
	if($('shopnav'))
	{
		var shopnav = $('shopnav');
		var content = document.getElementsByClassName("content")
		var shopnav_height = shopnav.offsetHeight;
		var content_height = content[0].offsetHeight;
		if(shopnav_height < content_height)
		{
			shopnav.style.height = (content_height) + 'px';
		}
	}
}
 

function prepareLinks()
{
	
	var address_fields = document.getElementsByClassName("address_field")
	var sameas = $('sameas');
	for(i=0; i<address_fields.length; i++)
    {
		address_fields[i].onfocus = function()
		{
			sameas.checked = false;
		}
	}
	
    var forgotten = $("forgotten")
    if (forgotten) {    
        forgotten.onclick = function()
        {
            popup(this.href,'forgotten',396,194,'scrollbars=0,resizable=0');
            return false;
        }
    }

    var bookmarklink = document.getElementById("bookmarklink")
    bookmarklink.onclick = function()
        { 
            bookmarksite(document.title, window.location);
            return false;
        } 
        
	var links = document.getElementsByClassName("popup");
	for(i=0; i<links.length; i++)
    {
	    links[i].onclick = function()
        {
            popup(this.href,'lapopup',410,550,'scrollbars=1,resizable=1');
            return false;  
        }
    }
 
	var links = document.getElementsByClassName("designerpop");
	for(i=0; i<links.length; i++)
	{ 
		links[i].onclick = function()
		{ 
            popup(this.href,'ladesignpopup',640,306,'scrollbars=0,resizable=0');
            return false;
		}
	}
	
	if($('emaillist_checkbox'))
	{
		var emaillist_checkbox = $('emaillist_checkbox');
		emaillist_checkbox.onchange = function()
		{
			if(this.checked){ s=1; } else { s=0; }
			var vars = '';
			var myAjax = new Ajax.Request(
				'/checkout/emaillist/'+s,
				{method: 'post', parameters: vars, onComplete: emailListResponse}
			);
		}
	}
	
	if($('go_newsletter'))
	{
		var joinlink = document.getElementById('go_newsletter');
		joinlink.onclick = function(){
			var emailaddress = document.getElementById("email"); 
			valid = false;
			valid = validate_email(emailaddress.value);
			if(valid && emailaddress.value != "e-mail address" && emailaddress.value != "")
			{
				var vars = "email=" + emailaddress.value;
				var myAjax = new Ajax.Request(
					'/newsletter/subscribe',
					{method: 'post', parameters: vars, onComplete: emailedResponse}
				);
			}
			else
			{
				alert('Please enter a valid e-mail address');
				//emailaddress.style.background = "#E02F6E";
			}
			
			return false;
		}
	}
		
	
	var links = document.getElementsByClassName("joinlistemail");
	for(i=0; i<links.length; i++)
	{
		links[i].onfocus = function()
		{
			this.value='';
		}
	}
	
	var links = document.getElementsByClassName("shipping");
	for(i=0; i<links.length; i++){
		links[i].onclick = function(){
			popup(this.href,'shipping',600,500,'scrollbars=1');
			return false;
		}
	}
}

function validate_email(address)
{
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(address));
}

function emailListResponse(originalRequest)
{
	var emaillist_response = $('emaillist_response');
	var response = originalRequest.responseText;
	emaillist_response.style.display = 'block';
	emaillist_response.innerHTML = response;
}

function emailedResponse(originalRequest)
{
	var response = originalRequest.responseText;
	//alert(response);
	var newsletter = $("newsletter_form");
	newsletter.innerHTML = "<div id=\"response\">" + response + "</div>";
}


function prodthumb_display(image)
{
	var mainimage = $('main_image');
	var mainimagelink = $('main_image_link');
	mainimage.src = "/product_images/image/" + image + "/248" // The product detail image size is 248px wide by whatever tall
	mainimagelink.href = "/product_images/image/" + image + "/344/416"; // The zoom image size is 344px wide x 416px tall
	//mainimagelink.onclick = function()
	//{
	//	prod_zoom($image);
	//	return false;
	//}
}

function popup(url,windowname,width,height,vars)
{
    var pInfo='status=no,';
        pInfo+=vars+',';
        pInfo+='width='+width+','; 
        pInfo+='height='+height;
    enlargeWindow=window.open(url,windowname, pInfo); 
    enlargeWindow.focus(); 
    return false;
}
/* Adds a bookmark.  Grabs the present URL/Title*/
function BookmarkPage( strProduct ) {

}

function bookmarksite(_title, _url)
{	try { 
		
		if( window.external ) {
			window.external.AddFavorite( _url, _title  );
		}
		else if( window.sidebar ) {
			window.sidebar.addPanel( _title, _url, "" );
		}
		else {
			alert( "Sorry.  Your browser does not support this functionality." );
		}
	}
	catch( e ) {
		alert( "Sorry.  Your browser does not support this functionality." );
	}
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}