Element.addMethods({
    'find': function(){
        var args = $A(arguments)
        var element = $(args.shift());
        return Selector.findChildElements(element, args);
    },

    'fireEvt': function(element, event){
        var evt;
        if(document.createEvent)
        {
            var type;
            switch(event)
            {
                case 'change':
                    type = 'HTMLEvents';
                    break;
                case 'keyup':
                case 'keydown':
                case 'keypress':
                    type = 'KeyboardEvent';
                    break;
                case 'hover':
                case 'dblclick':
                    type = 'MouseEvents';
                    break;
            }
            // dispatch for firefox + others
            evt = document.createEvent(type);
            // event type,bubbling,cancelable
            evt.initEvent(event, true, true );
            return !element.dispatchEvent(evt);
        }
        else
        {
            // dispatch for IE
            evt = document.createEventObject();
            return element.fireEvent('on'+event , evt)
        }
    },

    'popUp': function(element)
    {
        var height = element.getHeight();
        var style = element.readAttribute('style').replace('display: none;', '');
        element.makeClipping().setStyle({height: '0px', width:'0px', opacity:'0'}).show();
        element.morph('opacity:1;width:100%;height:'+height+'px', {
            duration:0.5,
            afterFinish:function(){
                element.setAttribute('style', style);
            }
        });
    },

    'popOut': function(element, after_finish)
    {
        element.morph('opacity:0;height:0px', {
            duration:0.5,
            afterFinish:function(){
                after_finish();
                element.hide();
            }
        });
    }
});

String.prototype.hash = function(separator){
    var out = new Hash();
    var array = this.split(separator);
    array.each(function(item){
        if (item) out.set(item, item);
    });
    return out;
};

var Client = Class.create({});
Object.extend(Client, {
    messagebox: null,
	get: function(path)
	{
		document.location = Client.getUrl(path);
	},

    open: function(path, windowName)
    {
        var url = Client.getUrl(path);
        window.open(url, windowName);
    },

    openFront: function(path, windowName)
    {
        var url = 'http://www.joshsfrogs.com/' + path;
        window.open(url, windowName);
    },

    ajax: function(url, options)
    {
        $$('.'+options.messagebox).invoke('update', '<div class="ajax_loader"></div>');
        var callback = options.onComplete;
        options.onComplete = function(transport){
            callback(Client.log(transport, options));
        };
        new Ajax.Request(url, options);
    },

    getUrl: function(path)
    {
        return 'http://www.joshsfrogs.com/' + path;
    },

    log: function(transport, options)
    {
        var response = transport.responseText.split('<!-- debug-info -->');

        if (response.length>1 && $$('.log').length>0)
            $$('.log').first().insert({before:response[1]});

        $$('.'+options.messagebox).invoke('update', response[0]);
        return response[0];
    }
});

function getViewportsize()
{
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } 
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } 
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    
    return {"width": myWidth, "height": myHeight};
}
