var SpeedMedia = {
	init : function() {
		this.searchForm = $('#search-form');
		$('#search-icon').click(this._openSearchBar.bind(this));
		this._setupWidgets();
	},
	_openSearchBar : function() {
		var t = this;
		if(this.searchForm.hasClass('open')) {
			$('#search-form input[type="submit"]').fadeOut(500,
				function(){
					t.searchForm.animate({'width':'0'}, 800 ,
						function(){
							t.searchForm.hide();
					});	
			}) 
		}else {
			t.searchForm.show();
			this.searchForm.animate({'width':'200px'}, 800 ,
				function(){
					$('#search-form input[type="submit"]').fadeIn(500); 	
				});
		}
		this.searchForm.toggleClass('open');
	},
	_setupWidgets : function() {
		$('.widget').each(function() {
			var data = $.parseJSON($(this).find(SpeedMedia.BaseWidget.WIDGET_DATA_SEL).html())
			if (data && data.type)
			{
				try {
					switch(data.type)
					{
						case 'gallery':
							new SpeedMedia.GalleryWidget(this);
							break;
						case 'featured-gallery':
							new SpeedMedia.GalleryWidget(this);
							break;
						default:
							new SpeedMedia.BaseWidgets(this);
							break;
					}

				} catch(e) {
					$log("You have a problem on this widget");
				}
			} else new SpeedMedia.BaseWidget(this); // useless widget
		});	
	},
};

SpeedMedia.Event = Class.extend({
	_construct : function() {
		this.__event = {};
	},
	addEventHandler : function(event, func) {
		if (!this.__event[event]) this.__event[event] = new Array();
		if ($.inArray(func, this.__event[event]) < 0) this.__event[event].push(func);
		return this;
	},
	removeEventHandler : function(event, func) {
		var i = $.inArray(func, this.__event[event]);
		if (i >= 0) this.__event[event].splice(i, 1);
		return this;
	},
	removeEventHandlers : function(event) {
		 this.__event[event].length = 0;
		 return this;
	},
	fireEvent : function(event) {
		if (this.__event[event]) {
			if (1 < arguments.length) {
				var $arguments = Array.prototype.slice.call(arguments, 1);
				$arguments.unshift(event);
				for(i=0;i<this.__event[event].length;i++) {
					this.__event[event][i].apply(this, $arguments);
				}
			} else {
				for(i=0;i<this.__event[event].length;i++) {
					this.__event[event][i](event);
				}
			}
		}
	}
});






