if (typeof Sui == 'undefined') {
        var Sui = {};
}

window.addEvent('domready', function() {
        Sui.initExternalLinks();
});

Sui.initExternalLinks = function() {
        $(document.body).getElements('a[rel=external]').each(function(link) {
                link.target = '_blank';
        });
}

Sui.Item = new Class({
        Implements: [Options,Events],

        elm: null,

        options: {
                label: ' '
        },

        initialize: function (options) {
                this.setOptions(options);
        },

        hide: function () {
                this.elm.setStyle('display', 'none');
                
                this.fireEvent('hide');
                
                return this;
        },

        show: function() {
                this.elm.setStyle('display', 'block');
                
                this.fireEvent('show');
                
                return this;
        },
        
        destroy: function() {
                this.elm.dispose();
                
                return this;
        }
});

Sui.Item.Text = new Class({
        Extends: Sui.Item,
        
        options: {
                text: ' '
        },

        initialize: function(options) {
                this.parent(options);
                
                this.elm = new Element('span', {
                        'class': 'suiText'
                }).appendText(this.options.text);
        }
});

Sui.Button = new Class({
        Extends: Sui.Item,
        
        options: {
                type: 'button',

                id: ''
        },

        initialize: function(options) {
                this.parent(options);

                this.elm = new Element('input', {
                        'type': this.options.type,
                        'value': this.options.label,
                        'id': this.options.id,
                        'class': 'suiButton'
                });
        }
});

Sui.Panel = new Class({
        Extends: Sui.Item,

        options: {
                items: [],

                buttons: [],

                height: 'auto',

                width: 'auto'
        },

        _getButtons: function() {
                var wrapper = new Element('div', {
                        'class': 'suiPanelButtons'
                });

                this.options.buttons.each(function(button) {
                        button.elm.inject(wrapper);
                });

                return wrapper;
        },
        
        _getItems: function() {
                var wrapper = new Element('div', {
                        'class': 'suiPanelItems'
                });

                this.options.items.each(function(item) {
                        item.elm.inject(wrapper);
                });
                
                return wrapper;
        },
        
        _getPanel: function() {
                var panel = new Element('div', {
                        'class': 'suiPanel',
                        'styles': {
                                'height': this.options.height,
                                'width': this.options.width
                        }
                });
                
                return panel;
        },

        initialize: function(options) {
                this.parent(options);

                var panel = this._getPanel();

                if (this.elm) {
                        panel.inject(this.elm);
                } else {
                        this.elm = panel;
                }

                this.elm.adopt(this._getItems());

                this.elm.adopt(this._getButtons());
        }
});

Sui.Panel.Tab = new Class({
        Extends: Sui.Panel,
        
        options: {
                activeTab: 0
        },
        
        _getItems: function() {
                var wrapper = new Element('div', {
                        'class': 'suiPanelItems'
                });

                
                var aOptions = this.options;
                var i = 0;
                this.options.items.each(function(item) {
                        var displayStyle = 'none';
                        if (i == aOptions.activeTab) {
                                displayStyle = 'block';
                        }

                        item.elm.setStyle('display', displayStyle).inject(wrapper);
                        
                        i++;
                });

                return wrapper;
        },

        _getPanel: function() {
                var panel = new Element('div', {
                        'class': 'suiPanel',
                        'styles': {
                                'height': this.options.height,
                                'width': this.options.width
                        }
                });
                
                var tabswrapper = new Element('div', {
                        'class': 'suiPanelTabs'
                }).inject(panel);

                var tabs = new Element('ul').inject(tabswrapper);
                new Element('div', {
                        'class': 'suiClear'
                }).inject(tabswrapper);

                var items = this.options.items;
                items.each(function(item) {
                        var tab = new Element('li').inject(tabs);

                        new Element('span', {
                                'styles': {
                                        'cursor': 'pointer'
                                },
                                'events': {
                                        'click': function() {
                                                items.each(function(item) {
                                                        item.elm.setStyle('display', 'none');
                                                });
                                                item.elm.setStyle('display', 'block');
                                        }
                                }
                        }).inject(tab).appendText(item.options.label);
                });

                return panel;
        },

        initialize: function(options) {
                this.parent(options);
        }
});

Sui.Window = new Class({
        Extends: Sui.Panel,

        options: {
        },

        initialize: function (options) {
                this.parent(options);
                
                var panel = this.elm;
                panel.setStyle('width', 'auto');

                this.elm = new Element('div', {
                        'class': 'suiWindow',
                        'styles': {
                                'width': this.options.width,
                                'position': 'absolute'
                        }
                });
                
                var header = new Element('div', {
                        'class': 'suiWindowHeader'
                }).inject(this.elm);
                new Element('span').inject(header).appendText(this.options.label);

                var content = new Element('div', {
                        'class': 'suiWindowContent'
                }).inject(this.elm);

                panel.inject(content);

                var footer = new Element('div', {
                        'class': 'suiWindowFooter'
                }).inject(this.elm);
                new Element('span').inject(footer).appendText(' ');

                this.elm.inject($(document.body)).makeDraggable({
                        handle: header
                });

                this.elm.setStyle('top', ($(document.body).getCoordinates().height - this.elm.getCoordinates().height) / 2);
                this.elm.setStyles({
                        'width': this.elm.getCoordinates().width,
                        'margin-left': (($(document.body).getCoordinates().width - this.elm.getCoordinates().width) / 2),
                        'display': 'none'
                });
        }
});

Sui.Form = {};

Sui.Form.Panel = new Class({
        Extends: Sui.Panel,

        options: {
                method: 'post',

                action: '',
                
                legend: true
        },

        _getPanel: function() {
                var panel = new Element('fieldset', {
                        'class': 'suiFormPanel'
                });
                
                if (this.options.legend) {
                        new Element('legend').inject(panel).appendText(this.options.label);
                }

                return panel;
        },

        initialize: function (options) {
                this.parent(options);
                
                var panel = this.elm;
                
                this.elm = new Element('form', {
                        'method': this.options.method,
                        'action': this.options.action,
                        'class': 'suiForm',
                        'id': this.options.id
                }).adopt(panel);
        }
});

Sui.Form.Input = new Class({
        Extends: Sui.Item,
        
        input: null,

        options: {
                value: '',

                type: 'text',
                
                id: '',
                
                name: ''
        },

        _getElm: function() {
                var input = new Element('input', {
                        'type': this.options.type,
                        'value': this.options.value
                });
                
                if (this.options.type == 'radio' || this.options.type == 'checkbox') {
                        input.addClass('suiInputCheck');
                } else {
                        input.addClass('suiInput');
                }

                if (this.options.id.length > 0) {
                        input.id = this.options.id;
                }
                if (this.options.name.length > 0) {
                        input.name = this.options.name;
                }
                
                input.addEvent('click', function() {
                        $(document.body).getElements('input').each(function(i) {
                                if (i.name == input.name) {
                                        i.checked = false;
                                }
                        });

                        this.checked = true;
                });

                return input;
        },

        _getLabel: function() {
                var label = new Element('label', {
                        'for': this.options.id,
                        'id': this.options.id + 'Label',
                        'class': 'suiFieldLabel'
                });
                
                var labelText = this.options.label;
                if (this.options.type != 'radio' && this.options.type != 'checkbox') {
                        labelText = labelText + ':';
                }
                label.appendText(labelText);

                return label;
        },
        
        _setWrapper: function() {
                this.elm = new Element('div', {
                        'class': 'suiField'
                });
        },

        initialize: function(options) {
                this.parent(options);

                this._setWrapper();
                
                if (this.options.type == 'radio' || this.options.type == 'checkbox') {
                        this.input = this._getElm().inject(this.elm);
                        this.elm.adopt(this._getLabel());
                } else {
                        this.elm.adopt(this._getLabel());
                        this.input = this._getElm().inject(this.elm);
                }

                new Element('div', {
                        'class': 'suiClear'
                }).inject(this.elm);
        },
        
        getValue: function() {
                return this.input.value;
        }
});

Sui.Form.Textarea = new Class({
        Extends: Sui.Form.Input,

        options: {
        },

        _getElm: function() {
                var txta = new Element('textarea', {
                        'id': this.options.id,
                        'class': 'suiTextarea'
                });
                txta.appendText(this.options.value);

                return txta;
        },

        initialize: function(options) {
                this.parent(options);
        }
});

Sui.LightBox = new Class({
        Implements: [Options,Events],
        
        elm: null,
        
        options: {
                title: '',

                src: ''
        },

        initialize: function(options) {
                this.setOptions(options);

                this.elm = new Element('div', {
                        'styles': {
                                'position': 'fixed'
                        }
                });
                this.elm.setStyles({
                        'left': ($(document.body).getCoordinates().width - this.elm.getCoordinates().width) / 2,
                        'top': ($(document.body).getCoordinates().height - this.elm.getCoordinates().height) / 2
                });
                
                var img = new Element('img', {
                        'src': this.options.src
                }).inject(this.elm);

                if (this.options.title.length > 0) {
                        var title = new Element('div', {
                                'class': 'suiLightBoxBottom',
                                'styles': {
                                        'width': img.getCoordinates().width
                                }
                        }).inject(this.elm).appendText(this.options.title);
                }
        },
        
        getValue: function() {
                return this.input.innerHTML;
        }
});

var ContextMenu = new Class({

	//implements
	Implements: [Options,Events],

	//options
	options: {
		actions: {},
		menu: 'contextmenu',
		stopEvent: true,
		targets: 'body',
		trigger: 'contextmenu',
		offsets: { x:0, y:0 },
		onShow: $empty,
		onHide: $empty,
		onClick: $empty,
		fadeSpeed: 200
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options)
		
		//option diffs menu
		this.menu = $(this.options.menu);
		this.targets = $$(this.options.targets);
		
		//fx
		this.fx = new Fx.Tween(this.menu, { property: 'opacity', duration:this.options.fadeSpeed });
		
		//hide and begin the listener
		this.hide().startListener();
		
		//hide the menu
		this.menu.setStyles({ 'position':'absolute','top':'-900000px', 'display':'block' });
	},
	
	//get things started
	startListener: function() {
		/* all elements */
		this.targets.each(function(el) {
			/* show the menu */
			el.addEvent(this.options.trigger,function(e) {
				//enabled?
				if(!this.options.disabled) {
					//prevent default, if told to
					if(this.options.stopEvent) { e.stop(); }
					//record this as the trigger
					this.options.element = $(el);
					//position the menu
					this.menu.setStyles({
						top: (e.page.y + this.options.offsets.y),
						left: (e.page.x + this.options.offsets.x),
						position: 'absolute',
						'z-index': '2000'
					});
					//show the menu
					this.show();
				}
			}.bind(this));
		},this);
		
		/* menu items */
		this.menu.getElements('a').each(function(item) {
			item.addEvent('click',function(e) {
				if(!item.hasClass('disabled')) {
					this.execute(item.get('href').split('#')[1],$(this.options.element));
					this.fireEvent('click',[item,e]);
				}
			}.bind(this));
		},this);
		
		//hide on body click
		$(document.body).addEvent('click', function() {
			this.hide();
		}.bind(this));
	},
	
	//show menu
	show: function(trigger) {
		//this.menu.fade('in');
		this.fx.start(1);
		this.fireEvent('show');
		this.shown = true;
		return this;
	},
	
	//hide the menu
	hide: function(trigger) {
		if(this.shown)
		{
			this.fx.start(0);
			//this.menu.fade('out');
			this.fireEvent('hide');
			this.shown = false;
		}
		return this;
	},
	
	//disable an item
	disableItem: function(item) {
		this.menu.getElements('a[href$=' + item + ']').addClass('disabled');
		return this;
	},
	
	//enable an item
	enableItem: function(item) {
		this.menu.getElements('a[href$=' + item + ']').removeClass('disabled');
		return this;
	},
	
	//diable the entire menu
	disable: function() {
		this.options.disabled = true;
		return this;
	},
	
	//enable the entire menu
	enable: function() {
		this.options.disabled = false;
		return this;
	},
	
	//execute an action
	execute: function(action,element) {
		if(this.options.actions[action]) {
			this.options.actions[action](element,this);
		}
		return this;
	}
	
});
