document.observe("dom:loaded", function() {
	initAddToCart();
	fixBackGroundPNG();
});

// This looks for the shade select menu on a spp page to determine if shaded product page.  If it finds it, it will add the event observe to make sure a shade is selected.  Could be expanded in the future to include non shaded products.
function initAddToCart () {
	var btnNode = $('addtocart');
	var formNode = $('content_chooser_select');
	var isShadeSelect = $$('.shade_selector')[0];
	var warningNode = $('shade_warning');
	if (isShadeSelect) {
		btnNode.observe('click', function(evt) {
			if($F(formNode)) {
				addtocart($F(formNode));
				warningNode.setStyle({'display':'none'});
			} else {
				evt.preventDefault();
				warningNode.setStyle({'display':'block'});
			}
		});
	}
}

function showShade(color, image) {
    var shade_block = document.getElementById('shade_popup');
    shade_block.style.backgroundImage = "url('" + image + "')";
    shade_block.style.backgroundColor = color;
    shade_block.style.display = 'block';
}

function hideShade() {
    document.getElementById('shade_popup').style.display='none';
}

function mailFriend(product_id, category_id) {
    var your_name    = document.getElementById('FULL_NAME_REQUIRED').value;
    var your_email   = document.getElementById('EMAIL_REQUIRED').value;
    var friend_email = document.getElementById('FRIEND_EMAIL_REQUIRED').value;
    var comment      = document.getElementById('COMMENT').value;
    var cc_me        = document.getElementById('CC_ME').checked;

    comment = comment.replace(/\n/g,"<br>").replace(/\r/g,"");

    var url = encodeURI('/templates/includes/fragments/email_a_friend_send.tmpl?product_id=' + product_id + '&category_id=' + category_id + '&your_name=' + your_name + '&your_email=' + your_email + '&friend_email=' + friend_email + '&comment=' + comment + '&cc_me=' + cc_me);

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(yay) {
            if (yay.responseText.match(/success/)) {
                javascript:s_div(this, 'new_add', '0');
            }
            else {
                var form_errors = yay.responseText;
                document.getElementById('error_msg').innerHTML = form_errors;
            }
        }
    });
}

// since the .htc fix doesn't work on prod, this is used to go around the delay issue.
function fixBackGroundPNG() {
	var shadeNodes = $$('#product_left_content .menuoff');	
    if (shadeNodes) {
    	shadeNodes.each(function(ele) {
    		var swatchNode = ele.select('div')[0];    				
    		IEPNGFixINLINE.process(swatchNode, 1);
    				
    		ele.observe('mouseover', function() {
    			var shadePop = $('shade_popup');    		IEPNGFixINLINE.process(shadePop, 1);
    		});
    	});    		
    };
}
