/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

(function($) {
    $.fn.extend({
        modalPanel: function() {

            //Create our overlay object
            overlay = $("<div id='modal-overlay'></div>");
            modalWindow = $("<div id='modal-window'></div>");
            var modalClose = $("<div id='modal-close'></div>");
            return this.each(function() {

                //Listen for clicks on objects passed to the plugin
                $(this).click(function(e) {
                    $.getJSON("/webim/b.php?i=webim&amp;lang=en", {

                        }, function (data) {
                            STATUS = data.status;

                        });
                    if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
                        $("body","html").css({
                            height: "100%",
                            width: "100%"
                        });
                    }
                    //Append the overlay to the document body
                    $("body").append(overlay.click(function() {
                        modalHide();
                    }));
                 
                    //Set the css and fade in our overlay
                    overlay.css("opacity", 0.7);
                   
                    overlay.fadeIn(500,function() {
                        $("body").append("<div id='modal-load'></div>");
                    });
                
                    // overlay.animate({ opacity:0.7,
                    //backgroundColor: "#f78080"
                    //                   }, 1700);
                    //Prevent the anchor link from loading
                    e.preventDefault();
  hEscape = handleEscape;
                    //Activate a listener
                    $(document).keydown(handleEscape);
                    $("body").append(modalWindow);
                 
                    modalWindow.append(modalClose.click(function() {
                        modalHide();
                    }));
                    modalWindow.append('<IFRAME id="chatFrame" scrolling="no"  src="/webim/client.php?locale=en" style="width:660px; height:442px">');
                    $('iframe#chatFrame').attr('src', "/webim/client.php?locale=en");

                    $('iframe#chatFrame').load(function(){
                        //   window.frames[0].document.getElementById('daLink')
                        $("#modal-load").remove();
                        setTimeout('$("#modal-load").remove();',600);
                       modalWindow.css('display','block');
                       
                        // var imageWidth =$(this).width;
                        //              var imageHeight =$(this).height;// img.height / 2;
                        modalWindow.css({
                            "margin-left": -300,
                            "margin-top": -200
                        });
          
                    });
                         
                         
                /*  $(img).css({
                            height: "0px",
                            width: "0px"
                        });
                        //        img.width = '0';
                        //        img.height = '0';
                        var imageWidth =150;// img.width / 2 ;
                        var imageHeight =150;// img.height / 2;
                        modalWindow.css({
                            "margin-left": -imageWidth,
                            "margin-top": -imageHeight
                        });
                        modalWindow.append(img);
                    
                        $(this).addClass("modal-image");
                        $("#modal-load").remove();
                        $("body").append(modalWindow);
    //  modalWindow.effect("bounce");
      $(img).animate({
                            height:[300,"easeOutBounce"],
                            width:[300,"easeOutBounce"]
                        }, 1700);
                                          
                      
                        setTimeout(function(){
      
                            // modalWindow.fadeIn(550);
                            }, 300);
                */
                       

                });
            });

            //Our function for hiding the modalbox
          

            //Our function that listens for escape key.
            function modalHide() {
                $("#modal-load").remove();
                setTimeout('$("#modal-load").remove();',600);
           
               closeChat();
               
                if(STATUS !="on")
                {
                     $(document).unbind("keydown", handleEscape);
                    var remove = function() {
                        $(this).remove();
                    };
                 
                    overlay.fadeOut(remove);
                    modalWindow
                    .fadeOut(remove)
                    .empty();
                }
            }

         function handleEscape(e) {
                if (e.keyCode == 27) {
                    modalHide();
                }
            }

        }
    });
})(jQuery);
function setAlert()
{
    alert('juhuhu');
}

function closeChat()
{
    if(STATUS =="on")
        {
           
        
        document.getElementById('chatFrame').contentWindow.st.fh.fu();
     endClose1();
    }
//document.getElementById('chatFrame').contentWindow.close();
}
function endClose1()
{
    $(document).unbind("keydown", hEscape)
    var remove = function() {
        $(this).remove();
    };
    $("#modal-load").remove();
    overlay.fadeOut(remove);
    modalWindow
    .fadeOut(remove)
    .empty();
}
function endClose()
{

}
