/**
 * @category  Application
 * @package   reisen.de
 * @author    Unister GmbH <teamleitung-dev@unister-gmbh.de>
 * @version   $Id: popup.js 8756 2010-07-01 07:45:00Z volker_andres $
 * @copyright Copyright (c) 2006-2008, Unister GmbH
 * @license   this code is property of company Unister GmbH and unterlays internal rules of company.
 * @link      http://www.reisen.de
 */

(function($){
    var popup   = {};
    var options = '';
    var config  = {
        height:     -1,
        width:      -1,
        left:       -1,
        top:        -1,
        screenX:    -1,
        screenY:    -1,
        url:        '',
        name:       '',
        location:   false,
        menubar:    false,
        resizable:  true,
        status:     false,
        scrollbars: true,
        toolbar:    false
    };
    var integerItem = [
       'height',
       'width',
       'left',
       'top',
       'screenX',
       'screenY'
    ];
    var booleanItem = [
       'location',
       'menubar',
       'resizable',
       'status',
       'scrollbars',
       'toolbar'
    ];

    var init = function(params) {
        config = $.extend(config, params);

        if (config.name === '') {
            config.name = 'default';
        }

        buildOptions();
    };

    var buildOptions = function() {
        options = '';

        for(i=0;i<booleanItem.length;i++) {
            var name = booleanItem[i];
            if(options !== '') {
                options += ',';
            }
            if (config[name] === true) {
                options += name + '=yes';
                continue;
            }
            if (config[name] === false) {
                options += name + '=no';
            }
        }

        for(i=0;i<integerItem.length;i++) {
            var name = integerItem[i];
            if(options !== '') {
                options += ',';
            }
            if (config[name] === -1) {
                var value = config[name];
                switch(name) {
                    case 'left':
                    case 'screenX':
                        value = (screen.availWidth - config.width) / 2;
                        break;
                    case 'top':
                    case 'screenY':
                        value = (screen.availHeight - config.height) / 2;
                        break;
                    default:
                        value = 0;
                }

                options += name + '='+value+'';
                continue;
            }
            if (config[name] >= 0) {
                options += name + '='+config[name]+'';
            }
        }
    };

    var create = function(params) {
        init(params);
        popup[config.name] = window.open(config.url, config.name, options);
        popup[config.name].focus();
    };

    var close = function(name) {
        if (popup[name] === null) {
            return;
        }
        popup[name].close();
    };

    $.popup = {
        create : function(params) {
            create(params);
        },
        close : function(name) {
            close(name);
        }
    };
})(jQuery);
