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

trapezeShare.socialShare = {
    ready: function() {
        var shares = trapezeShare.getElementsByClass('trapeze-social-share', document, 'div');
        var close_btn = p_share = share_link = social_sites = null;
        
        // finds all trapeze social share
        for (var i = 0; i < shares.length; i++) {

            // Sets the Close Button
            close_btn = trapezeShare.getElementsByClass('ts-close', shares[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();
            });
            
            // Set every link to close the box after it's clicked
            social_links = trapezeShare.getElementsByClass('ts-social-link', shares[i], 'a') || [];
            for (var j=0; j<social_links.length; j++) {
                trapezeShare.addHandler(social_links[j], "click", function(event){
                    href = event.target.getAttribute('href','');
                    window.open(href);
                    trapezeShare.hideAllShared();
                    event.stop();
                    return false;
                });
            }
            
            // 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://media.mycineplex.com/images/trans_space.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;');
                
                shares[i].appendChild(iframe_mask);
            }

            // Sets the Share Link (Show or Hide box on click)
            p_share = trapezeShare.get_position(shares[i]);
            share_link = shares[i].getElementsByTagName('a')[0];
            trapezeShare.addHandler(share_link, "click", function(event){
                social_sites = trapezeShare.getElementsByClass('ts-social-sites', event.target.parentNode.parentNode, 'div')[0];

                //Hack for ie
                if (document.all) {
                    iframe_mask = event.target.parentNode.parentNode.getElementsByTagName('iframe')[0];
                }
                
                if (social_sites.style.display == "none") {
                    trapezeShare.hideAllShared(); // Hides all share boxes that are oppened
                                      
                    social_sites.style.display = "block";
                    social_sites.style.left = p_share.left + 'px';
                    social_sites.style.top = (p_share.top + 22) + 'px'; // #todo change the 22 for the image obj height

                    //Hack for ie
                    if (document.all){
                        iframe_mask.style.left = social_sites.style.left;
                        iframe_mask.style.top = ((p_share.top + 22) - 4) + 'px';
                        //iframe_mask.style.zIndex = (parseInt(social_sites.style.zIndex) - 1).toString();
                        iframe_mask.style.width = social_sites.offsetWidth;
                        iframe_mask.style.height = social_sites.offsetHeight;
                    }

                } else social_sites.style.display = "none";
                
                //Hack for ie
                if (document.all) {
                    iframe_mask.style.display = social_sites.style.display;
                }
                
                event.stop();
            });
            
        }
    }
}
