var popupid = 0;

// if use domready IE will give Operation Aborted error at iframe.
//window.addEvent('domready', function()
window.addEvent('load', function()
{
    if($$('a'))
    {
        $$('a').addEvent('click', function(e){
            // Add handler to all popup_windows
            if("popup_window" == this.name || "popup_windows" == this.name)
            {
                new Event(e).stop();
                var arg = new Hash();
                if(this.getProperty('rel'))
                    arg = new Hash(JSON.decode(this.getProperty('rel')));
                arg.title = (this.title) ? this.title : arg.title;
                if(arg.contentURL == "" && this.getProperty('href') != "#" && this.getProperty('href').indexOf('void(0)') == -1 && this.getProperty('href') != "")
                {
                    arg.contentURL = this.getProperty('href');
                }
                MochaUI.popup_window({title: arg.title,
                                      content: arg.content,
                                      contentURL: arg.contentURL,
                                      type: arg.type,
                                      width: arg.width,
                                      height: arg.height,
                                      scrollbars: arg.scrollbars,
                                      draggable: arg.draggable,
                                      loadMethod: arg.loadMethod
                                    });
            }
        });
    }
    MochaUI.Modal = new MochaUI.Modal();
});

// This method is use to popup directly, requires hash parameters
MochaUI.popup_window = function(arg)
{
    if(!arg.id) arg.id = "popupwindow" + popupid++;
    if(!arg.title) arg.title = "Windows";
    if(!arg.type) arg.type = 'modal';
    if(!arg.width) arg.width = 400;
    if(!arg.height) arg.height = 200;
    if(!arg.scrollbars) arg.scrollbars = false;
    if(!arg.draggable) arg.draggable = true;

    if(arg.contentURL)
    {
        if(!arg.loadMethod) arg.loadMethod = "xhr";
    }

    //calculate correct center position for IE and mozilla
    var winWidth = 0, winHeight = 0, yOffset = 0;
    if(typeof(window.innerWidth ) == 'number')
    {
        //Non-IE
        yOffset = window.pageYOffset;
        winWidth = window.innerWidth;
        winHeight = window.innerHeight;
    }
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        yOffset = document.documentElement.scrollTop;
        winWidth = document.documentElement.clientWidth;
        winHeight = document.documentElement.clientHeight;
    }
    else if(document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        yOffset = document.body.scrollTop;
        winWidth = document.body.clientWidth;
        winHeight = document.body.clientHeight;
    }
    arg.y = (yOffset + (winHeight-arg.height)/2);
    arg.x = (winWidth-arg.width)/2;

/*
    arg.headerStartColor = [255, 153, 00];
    arg.headerStopColor = [85, 85, 85];
    arg.bodyBgColor = [255, 153, 00];
*/
    // disable canvas otherwise we can't use css to control colors.
    arg.useCanvas = false;

    new MochaUI.Window(arg);
}
