﻿
    /*
        Displays a generic popup dialog, centered horizontally and vertically.
        Usage: 
        <a href="#" class="simple_popup">[LINK OR BUTTON TO CAUSE POPUP]
        <span class="simple_popup_info">[CONTENTS OF POPUP]</span>
        
    */
   
        
    $(".simple_popup_info").each(function(){
        //Grab ID
        var dialogID = $(this).attr("id");
        $(this).css("display","none");
        //Attach popup generation to associated link
        $('[contentid="'+dialogID+'"]').click(function(){
            var ClosingCode = $(this).attr("closingcode"); //Get extra onclose code if injected by Dialog.ascx
            $(".simple_popup_div").remove();
            var strSimple = "<div class='simple_popup_div'><div class='simple_popup_inner'>";
            strSimple +=  "<div style='z-index:99999;position:absolute;top:12px;left:-15px;width:100%;text-align:right;' class='simple_close'>";
            strSimple +=  "<a href='#'><img src='/images/icons/myhome/close_button_black.jpg' border='0' width='18' height='18' /></a></div>";

            strSimple += $('#'+dialogID).html();
            strSimple += "</div></div>";
            $("body").append(strSimple);
            viewport.init(".simple_popup_div");
            $(".simple_close").click(function(){
                eval(ClosingCode); //Execute the onclose code;
                $(".simple_popup_div").remove();
                return false;
            });
            return false;
        });
    });
    
    /* Helper methods to manipulate dialogs */
    var Dialog = new Object();
        
        //Given the ID of a .NET Dialog user control, replaces its inner contents
        Dialog.ReplaceContent = function(dialogID, newContent) {
            $("#"+dialogID+"_content").html(newContent);
        }
        
        //Injects contents of the inputted string inside a specific container class, inside the active dialog
        Dialog.Inject = function(inputData, containerClass, dialogObject) {
            //alert(dialogObject.innerHTML);
            
            sn_getElementsByClassName(containerClass, dialogObject)[0].innerHTML = inputData;
            /*
            $(".simple_popup_div").contents("."+containerClass).each(function() {
                alert("Appending");
                alert($(this).html());
                $(this).append(inputData);
                alert("Done appending");
            });
            */
        }
        
        //Appends inputData to first instance of containerClass in the default dialog
        Dialog.AppendChild = function(inputData, containerClass)
        {
            sn_getElementsByClassName(containerClass, document.getElementById('activeDialog'))[0].innerHTML += inputData;
        }
        
        //Inserts the input HTML into the button region of the dialog
        Dialog.AddButtons = function(inputData, dialogObject) {
            Dialog.Inject(inputData, "dialog_buttons", dialogObject);
        }
        
        //Inserts the input HTML into the arguments region of the dialog
        Dialog.Arguments = function(inputData, dialogID) {
            document.getElementById(dialogID+'_arguments').innerHTML = inputData;
        }
        
        Dialog.GetArguments = function(dialogID) {
            return document.getElementById(dialogID+'_arguments').innerHTML;
        }
        
        //Allows a dialog to be closed programatically
        //Does not execute OnClose code
        Dialog.Close = function() { $(".simple_popup_div").remove(); }
        
        
        
        //Allows a dialog to be opened programatically
        //Will auto-close any other dialog if one is currently open 
        Dialog.Open = function(dialogID) {
            
            var ClosingCode = $('[contentid="'+dialogID+'"]').attr("closingcode"); //Get extra onclose code if injected by Dialog.ascx
            var DialogWidth = $('[contentid="'+dialogID+'"]').attr("dialogwidth");
            $(".simple_popup_div").remove();
            var strSimple = "<div id='activeDialog' class='simple_popup_div'><div class='simple_popup_inner'>";
            strSimple +=  "<div style='z-index:99999;position:absolute;top:12px;text-align:right;width:"+DialogWidth+"px'>";
            strSimple +=  "<a href='#'><img class='simple_close' src='/images/icons/myhome/close_button_black.jpg' border='0' width='18' height='18' /></a></div>";

            strSimple += $('#'+dialogID).html();
            strSimple += "</div></div>";
            $("body").append(strSimple);
            viewport.init(".simple_popup_div");
            $(".simple_close").click(function(){
                eval(ClosingCode); //Execute the onclose code;
                $(".simple_popup_div").remove();
                return false;
            });
            //alert ($("#activeDialog").attr("id"));
            
            return document.getElementById('activeDialog'); //Return the dialog as an object
            
        }
        
        //Searches for an element within the open dialog
        Dialog.getElementById = function(id, tagName)
        {
            var els = document.getElementById('activeDialog').getElementsByTagName(tagName);
            for (var i=0; i<els.length; i++)
                if (els[i].attributes["id"])
                    if (els[i].attributes["id"].value == id)
                        return els[i];
        }