/// <reference path="../core/mootools.core.js" />

/************************************************************************
/
/	BROWSER EXTENSIONS
/	-------------------------------------------------------
/	* Easy access to URLs and query strings
/	* Checks config to see if a browser should run JS
/	* Simple browser detection
/	* Simple 'Growl' implementation (http://growl.info/)
/	* Custom 'alert()' and 'confirm()' notifications
/	* Can turn JS off completely
/	* Handles popups in a nice way
/
/***********************************************************************/
$extend(Browser, {
	
	shouldRun: function(item)	{
		if(Config.get('js') == 'disabled')	{
			$$('BODY').addClass('js-disabled');
			return false;
		}
		else	{
			var config = Config.get(item) || {};
			var disable = (config.disableIn || '').split(' ').some(function(browser)	{
				return Browser.is(browser);									  
			});
			if(disable)	{
				$$('BODY').addClass(item + '-disabled');
				return false;	
			}
		}
		return true;
	},
	
	is: function(browser)	{
		var browser = browser.clean().toLowerCase();
		if(browser == 'all')	return true;
		else if(Browser.Engine.trident && browser == 'ie')	return true;
		else if(Browser.Engine.trident && Browser.Engine.version == 4 && browser == 'ie6')	return true;
		else if(Browser.Engine.trident && Browser.Engine.version == 5 && browser == 'ie7')	return true;
		else if(Browser.Engine.gecko && browser == 'firefox')	return true;
		else if(Browser.Engine.gecko && Browser.Engine.version == 18 && browser == 'firefox2')	return true;
		else if(Browser.Engine.gecko && Browser.Engine.version == 19 && browser == 'firefox3')	return true;
		else if(Browser.Engine.webkit && browser == 'safari')	return true;
		else if(Browser.Engine.webkit && Browser.Engine.version < 525 && browser == 'safari2')	return true;
		else if(Browser.Engine.webkit && Browser.Engine.version == 525 && browser == 'safari3')	return true;
		return false;
	},
	
	createGrowlElement: function() {
	    return new Element('DIV', {
	        styles: {
	            'background': '#000',
	            'width': 280,
	            'height': 55,
	            'position': 'fixed',
	            'top': 20,
	            'right': 20,
	            'color': '#FFF',
	            'font': 'bold 13px Arial',
	            'padding': 15,
	            '-moz-border-radius': '15px',
	            '-webkit-border-radius': '15px'
	        }
	    }).fade('hide').inject($(document.body));
	},
	
	growl: function(msg, type, delay)   {
		var div = Browser.createGrowlElement().set('text', msg).fade(0.8);
        (function() {
            div.fade('out');
            (function() {
                div.destroy()
            }).delay(1000);
        }).delay($defined(delay) ? delay : 3000);
	},
	
	createPopupWindow: function()	{
	
		var div = new Element('DIV', {
			'id': 'alertboxDiv'					  
		}).set('html', '<h2></h2><div><div>');
		
		var overlay = new Element('DIV', {
			'id': 'alertboxOverlay'					  
		}).set('opacity', .75);
		
		var closebutton = new Element('IMG', {
		    'id': 'alertboxClose',
		    'src': '/magazine/graphics/buttons/close.png'
		}).addEvent('click', function() {
		    div.dispose();
		    overlay.dispose();
		}).inject(div, 'top').makeButton();
		
		var confirmbutton = new Element('IMG', {
		    'id': 'alertboxConfirm',
		    'src': '/magazine/graphics/buttons/confirm.confirm.png'
		}).addEvent('click', function() {
		    div.dispose();
		    overlay.dispose();
		    return true;
		}).inject(div).makeButton();
		
		var cancelbutton = new Element('IMG', {
		    'id': 'alertboxCancel',
		    'src': '/magazine/graphics/buttons/confirm.cancel.png'
		}).addEvent('click', function() {
		    div.dispose();
		    overlay.dispose();
		    return false;
		}).inject(div).makeButton();
		
		$(document).store('alert:popup', div);
		$(document).store('alert:overlay', overlay);
		$(document).store('alert:close', closebutton);
		$(document).store('alert:cancel', cancelbutton);
		$(document).store('alert:confirm', confirmbutton);
		return div;
	},
	
	alert: function(title, text, callback, type)	{
	    if($type(callback) == 'string') {
	        var type = callback;
	    }
		var div = $(document).retrieve('alert:popup');
		
		if(!$defined(div))  div = Browser.createPopupWindow();
		    
		var overlay = $(document).retrieve('alert:overlay');
		var close = $(document).retrieve('alert:close');
		var cancel = $(document).retrieve('alert:cancel');
		var confirm = $(document).retrieve('alert:confirm');
		
		close.show();
		cancel.hide();
		confirm.hide();
		
		close.removeEvents('mousedown');
	    if($type(callback) == 'function') {
	        close.addEvent('mousedown', function()  {
	            callback();
	        });
	    }
		
		div.getElement('H2').set('text', title).setProperty('class', type || '');
		div.getElement('DIV').set('html', text);
		div.setProperty('class', '');
		
		div.inject($(document.body));
		overlay.inject($(document.body));
	},
	
	confirm: function(title, text, confirmed, cancelled, type)	{
		var div = $(document).retrieve('alert:popup');
		
		if(!$defined(div))  div = Browser.createPopupWindow();
		    
		var overlay = $(document).retrieve('alert:overlay');
		var close = $(document).retrieve('alert:close');
		var cancel = $(document).retrieve('alert:cancel');
		var confirm = $(document).retrieve('alert:confirm');
		
		cancel.removeEvents('mouseup');
		cancel.addEvent('mouseup', function()    {
		    cancelled();
		});
		confirm.removeEvents('mouseup');
		confirm.addEvent('mouseup', function()    {
		    confirmed();
		});
		close.hide();
		cancel.show();
		confirm.show();
		
		div.getElement('H2').set('text', title).setProperty('class', type || '');
		div.getElement('DIV').set('html', text);
		div.setProperty('class', 'confirm');
		
		div.inject($(document.body));
		overlay.inject($(document.body));
	},
	
	bookmark: function(url, description)	{
		if(Browser.is("ie"))	{
			window.external.AddFavorite(url, description);
		}
	}
	
});

/*
READS AND SAVES QUERY STRING
*/
$extend(Browser, {
	getQueryString: function(key)   {
	    return $defined(key) ?
	        this.queryString.get(key) :
	        this.queryString.toQueryString();
	},
    queryString: new Hash()
});
window.location.search.substring(1).split("&").each(function(pair)  {
    var item = pair.split("=");
    Browser.queryString.set(item[0], item[1] || '');
});

/* DEBUGGING MODE! */
if($defined(Browser.getQueryString('debug')))    {
    if($defined(Asset) && !Browser.Engine.gecko)    {
        Asset.javascript("http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js");
	}
    else if(!$defined(Asset))   {
        throw new Error("You must include mootools -more to use firebug");
    }
}

/*
Script: Popup.js
	Defines the Popup class useful for making popup windows.
License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/

Browser.Popup = new Class({
    Implements: [Options, Events],
    options: {
        width: 500,
        height: 300,
        x: 50,
        y: 50,
        toolbar: 0,
        location: 0,
        directories: 0,
        status: 0,
        scrollbars: 'auto',
        resizable: 1,
        name: 'popup'
    },
    initialize: function(url, options) {
        this.url = url || false;
        this.setOptions(options);
        if (this.url) this.openWin();
    },
    openWin: function(url) {
        url = url || this.url;
        var options = 'toolbar=' + this.options.toolbar +
			',location=' + this.options.location +
			',directories=' + this.options.directories +
			',status=' + this.options.status +
			',scrollbars=' + this.options.scrollbars +
			',resizable=' + this.options.resizable +
			',width=' + this.options.width +
			',height=' + this.options.height +
			',top=' + this.options.y +
			',left=' + this.options.x;
        this.window = window.open(url, this.options.name, options);
        if (!this.window) {
            this.window = window.open('', this.options.name, options);
            this.window.location.href = url;
        }
        var win = this.window;
        this.window.loaderFunction = function() {
            this.fireEvent('load', win);
        } .bind(this);
        this.focus.delay(100, this);
        return this;
    },
    focus: function() {
        if (this.window)
            this.window.focus();
        else if (this.focusTries++ < 10)
            this.focus.delay(100, this); //try again
        else {
            this.blocked = true;
            this.fireEvent('block');
        }
        return this;
    },
    focusTries: 0,
    blocked: null,
    close: function() {
        this.window.close();
        return this;
    }
});

$extend(Browser, {

    overlay: {
        className: 'overlay',
        instanceID: 'dispatch:ui:overlay',
        instance: null,
        onShow: function()  {
        },
        onHide: function()  {
        },
        visible: false,
        styles: {
            visible: {
                'display': '',
                'left': '',
                'position': '',
                'z-index': '499'            
            },
            hidden: {
                'display': 'none',
                'left': '-9999px',
                'position': 'absolute',
                'z-index': '0'            
            }
        }
    },
    
    createOverlay: function()   {
        var overlay = new Element('div').addClass(this.overlay.className).setStyles(this.overlay.styles.hidden).inject($(document.body));
        $(document.body).store(this.overlay.instanceID, overlay);
        this.overlay.instance = overlay;
        return overlay;
    },
    
    showOverlay: function() {
        var overlay = $defined(this.overlay.instance) ? 
            this.overlay.instance: 
            this.createOverlay();
        overlay.setStyles(this.overlay.styles.visible);
        this.overlay.onShow();
        this.overlay.visible = true;
    },
    
    hideOverlay: function() {
        var overlay = $defined(this.overlay.instance) ? 
            this.overlay.instance: 
            this.createOverlay();
        overlay.setStyles(this.overlay.styles.hidden);
        this.overlay.onHide();
        this.overlay.visible = false;
    }

});