if (typeof trapezeShare == 'undefined') trapezeShare = new Object();
if (typeof trapezeShare.emailShare == 'undefined') trapezeShare.emailShare = new Object();

trapezeShare.emailShare = {
    ready: function() {
        var emails = trapezeShare.getElementsByClass('trapeze-email-share', document, 'div');
        var close_btn = send_button = p_email = email_link = email_form = null;
        
        // finds all trapeze email share
        for (var i = 0; i < emails.length; i++) {

            // Sets the Close Button
            close_btn = trapezeShare.getElementsByClass('te-close', emails[i], 'a')[0];
            trapezeShare.addHandler(close_btn, "click", function(event){
                event.target.parentNode.style.display = "none";

                //Hack for ie
                if (document.all) {
                    event.target.parentNode.parentNode.getElementsByTagName('iframe')[0].style.display = event.target.parentNode.style.display;
                }

                event.stop();
            });

            // Hack for ie //Create an iframe mask behind the share box to fix select and flash problems
            if (document.all) {
                iframe_mask = document.createElement('iframe');
                iframe_mask.setAttribute('frameborder','0');
                iframe_mask.setAttribute('src','http://g-ecx.images-amazon.com/images/G/16/nav/spacer._V42753720_.gif');
                iframe_mask.style.position = 'absolute';
                iframe_mask.style.display = 'none';
                //iframe_mask.setAttribute('style','position:absolute;left:-3000;top:-3000;z-index:0;display: none;');
                
                emails[i].appendChild(iframe_mask);
            }

            // Sets the Send Button
            send_button =  trapezeShare.getElementsByClass('te-form-button', emails[i], 'a')[0];
            trapezeShare.addHandler(send_button, "click", function(event){
                form = trapezeShare.getElementsByClass('te-email-form', event.target.parentNode.parentNode.parentNode.parentNode, 'form')[0];
                form.submit();
                event.stop();
                return false;
            });

            // Sets the Email Link (Show or Hide box on click)
            p_email = trapezeShare.get_position(emails[i]);
            email_link = emails[i].getElementsByTagName('a')[0];
            trapezeShare.addHandler(email_link, "click", function(event){
                email_form = trapezeShare.getElementsByClass('te-email-share', event.target.parentNode.parentNode, 'div')[0];

                //Hack for ie
                if (document.all) {
                    iframe_mask = event.target.parentNode.parentNode.getElementsByTagName('iframe')[0];
                }

                if (email_form.style.display == "none") {
                    trapezeShare.hideAllShared(); // Hides all share boxes that are oppened

                    email_form.style.display = "block";
                    email_form.style.left = p_email.left + 'px';
                    email_form.style.top = (p_email.top + 22) + 'px'; // #todo change the 22 for the image obj height
    
                    //Hack for ie
                    if (document.all){
                        iframe_mask.style.left = email_form.style.left;
                        iframe_mask.style.top = ((p_email.top + 22) - 4) + 'px';
                        //iframe_mask.style.zIndex = (parseInt(email_form.style.zIndex) - 1).toString();
                        iframe_mask.style.width = email_form.offsetWidth;
                        iframe_mask.style.height = email_form.offsetHeight;
                    }

                } else email_form.style.display = "none";

                //Hack for ie
                if (document.all) {
                    iframe_mask.style.display = email_form.style.display;
                }

                event.stop();
            });
            
        }
    }
}
