if (!document.trace) {
	this.trace = function(output) {
		//if (console.log && output) console.log(output);
	}
}


window.addEvent('domready', function() {
	var b = new GameBrowser();
});


/**
 * Game Browser View Helper
 * @uses mootools-release-1.11.js
 */
var GameBrowser = new Class({
	
	
	initialize : function() {
		trace('GameBrowser.initialize()');
		
		this.scrollPane = new ScrollPane();
		this.scrollBar = new ScrollBar();
		
		this.registerView($('browser_widget'));
	},
	
	
	registerView : function(mainDocElement) {
		trace('GameBrowser.registerView()');
		
		if (!mainDocElement) return;
		
		mainDocElement.addEvent('mousedown', this.onMouseDownInside.bindWithEvent(this));
		mainDocElement.addEvent('mouseenter', this.onMouseEnter.bindWithEvent(this));
		mainDocElement.addEvent('mouseleave', this.onMouseLeave.bindWithEvent(this));
		
		var scrollPaneElement = this.getChildrenByClass(mainDocElement, 'scrollpane')[0];
		this.scrollPane.registerView(scrollPaneElement);
		
		var scope = this;
		scrollPaneElement.addEvent('onScrollX', function() { scope.onScroll(); });
		scrollPaneElement.addEvent('onScrollResize', function() { scope.onScrollResize(); });
		
		this.scrollBar.attachTo(this.scrollPane);
		
		// create the page l / r buttons...
		
		this.pageLeft = new ScrollPageButton(mainDocElement, {x: 8, y: 90, width: 19, height: 19, background: sn_img_base_url + '/browser_button_page_left_bg.png'});
		this.pageLeft.view.addEvent('onScrollPage', function() { scope.onPageLeft(); });
		
		this.pageRight = new ScrollPageButton(mainDocElement, {x: 922, y: 90, width: 19, height: 19, background: sn_img_base_url + '/browser_button_page_right_bg.png'});
		this.pageRight.view.addEvent('onScrollPage', function() { scope.onPageRight(); });
		
		// create the drop down selector...
		
		var dropDownContentElement = $('browser_nav_filter');
		this.dropDown = new DropDownSelector(dropDownContentElement, {x: 530, y: 5, width: 210});
		this.dropDown.view.addEvent('onSetSelection', function(selectedClass) { scope.onFilterByClass(selectedClass); });
	},
	
	
	onScroll : function() {
		this.scrollBar.updatePosition();
	},
	
	
	onScrollResize : function() {
		this.scrollBar.updateSize();
		
		if (this.scrollPane.contentWidth <= this.scrollPane.parentWidth) {
			if (this.pageLeft.enabled) this.pageLeft.disable();
			if (this.pageRight.enabled) this.pageRight.disable();
		} else {
			if (!this.pageLeft.enabled) this.pageLeft.enable();
			if (!this.pageRight.enabled) this.pageRight.enable();
		}
	},
	
	
	onPageLeft : function() {
		this.scrollPane.scrollByPage(-1);
	},
	
	
	onPageRight : function() {
		this.scrollPane.scrollByPage(1);
	},
	
	
	onFilterByClass : function(selectedClass) {
		this.scrollPane.filterChildren(selectedClass);
		this.scrollPane.setScrollAmountX(0);
	},
	
	
	onMouseDownInside : function(e) {
		e.preventDefault();
	},
	
	
	onMouseEnter : function(e) {
		//trace('GameBrowser.onFocus()');
	},
	
	
	onMouseLeave : function(e) {
		//trace('GameBrowser.onBlur()');
	},
	
	
	getChildrenByClass : function (docElement, className) {
		
		if (!docElement || !className) return;
		
		var result = new Array();
		var children = docElement.getChildren();
		
		for (var i = 0; i < children.length; ++i) {
			if (children[i].hasClass(className)) result.push(children[i]);
		}
		
		return result;
	},
	
	toString : function() {
		return "[GameBrowser]";
	}
		
});
