var GalleryScroller = new Class({
	initialize: function(category, theme) {
		this.category = category;
		this.theme = theme;

		// gallery values
		
		if (this.category == 'NBA' || this.category == 'NHL' || this.category == 'Collegiate' || this.category == 'Holiday') {
			this.perpage = 9;
			this.unitwidth = 90;
			this.pagesize = 810;
			$('iconContainer').setStyle('width', 810);
		} else {
			this.pagesize = 828;
			this.perpage = 6;
			this.unitwidth = 138;
		}
		$('prevArrowLink').addEvent('click', this.ScrollPage.bind(this, -1));
		$('nextArrowLink').addEvent('click', this.ScrollPage.bind(this, 1));

		this.LoadGalleryItems();
	},
	LoadGalleryItems: function() {
		var filepath = 'include/json/' + this.category.toLowerCase() + '_inuse.json';
		var myRequest = new Request({
			url: filepath,
			method: 'get',
			onSuccess: this.DisplayGalleryItems.bind(this),
			onFailure: function(xhr) {

			},
			onException: function(headerName, value) {

			}
		}).send();
	},
	DisplayGalleryItems: function(responseText, responseXML) {
		if (this.gallerydata = JSON.decode(responseText)) {
			this.pages = Math.ceil(this.gallerydata.length / this.perpage);
			var icon = '';
			var divClass = '';
	
			$('iconContainer').adopt(
				new Element('div', { 'id': 'flowContainer' }).setStyles({ 'width': this.pages * this.pagesize, 'height': 106 }).adopt(
					//new Element('span', { 'id': 'iconsList' })
				)
			);
	
			this.myScroller = new Fx.Scroll('iconContainer');
			
	
			var SelectedId = 0;
			var caption = '';
			for (var x = 0; x < this.gallerydata.length; x++) {
				// Free theme exclusions
				if (Referer == 'mycolorsdell' && this.gallerydata[x].link == 'amdgame') { continue; }
				else if (Referer != 'mycolorshp' && this.gallerydata[x].link == 'nba-hp') { continue; }
				else if (window.location.pathname.indexOf('/allstar09/') != -1 && (this.gallerydata[x].link == 'amdgame' || this.gallerydata[x].link == 'diamond' || this.gallerydata[x].link == 'quest')) { continue; }
				else if (window.location.pathname.indexOf('/hp/') != -1 && (this.gallerydata[x].link == 'amdgame' || this.gallerydata[x].link == 'quest')) { continue; }

				icon = 'icons/' + this.category.toLowerCase() + '/' + this.gallerydata[x].link + '.png';
				divId = (this.gallerydata[x].link == this.theme) ? 'SelectedIcon' : 'DeselectedIcon';
				divClass = (this.gallerydata[x].link == this.theme) ? 'sel_' + (this.unitwidth-10) : '';
				if (this.gallerydata[x].link == this.theme) { SelectedId = x+1; }
	
				$('flowContainer').adopt(
					new Element('a', { 'href': '?p=' + this.gallerydata[x].link + '&c=' + this.category.toLowerCase(), 'class': 'img', 'id': 'ImgIcon' + x }).setStyles({ 'top': 0, 'padding-top': 5, 'padding-left': 0, 'padding-right': 0, 'visibility': 'visible', 'width': this.unitwidth, 'height': 106, 'left': 0 }).adopt(
						new Element('div', { 'id': divId, 'class': divClass }).setStyles({ 'top': 0, 'left': 0 }).adopt(
							new Element('div', { 'class': 'imgObject', 'id': 'Img' + x }).setStyles({ 'left': 5, 'top': 5, 'width': (this.unitwidth-10), 'height': 96, 'visibility': 'visible', 'background-image': 'url(' + icon + ')' })
						)
					)
				);
				
				if (this.category != 'Rides') {
					$('Img' + x).adopt(
						new Element('span', { 'id': 'Caption'+x, 'class': 'TxtDesc', 'html': this.gallerydata[x].name }).setStyles({ 'position': 'absolute', 'bottom': 0, 'left': 0, 'width': (this.unitwidth-10), 'text-align': 'center', 'display': 'block' })
					);
				}
			}
	
			this.currentPage = Math.ceil(SelectedId / this.perpage)-1;
			this.GoToPage(this.currentPage, true);
		} else { alert('error'); }
	},
	GoToPage: function(pageNum, rush) {
		var pos = pageNum * this.pagesize;
		if (rush)
			this.myScroller.set(pos, 0);
		else
			this.myScroller.start(pos, 0);
		if (pageNum == 0) { $('prevArrow').getElement('a').set('id', 'prevArrowDIS'); }
		else { $('prevArrow').getElement('a').set('id', 'prevArrowLink'); }
		if (pageNum == this.pages-1) { $('nextArrow').getElement('a').set('id', 'nextArrowDIS'); }
		else { $('nextArrow').getElement('a').set('id', 'nextArrowLink'); }
	},
	ScrollPage: function(dir) {
		this.currentPage = (this.currentPage + dir).limit(0, this.pages-1)
		this.GoToPage(this.currentPage, false);
	}
});