var o = {

	images: new Array(),
	categories: new Array(),
	attemptedCategoryID: 0, // the id of the category that the user is trying to view. used for the adult message window and login form.
	currentIndex: -1,
	currentID: 0,
	loadIndex: 0,
	
	acceptHandler: function() {
		
		var tmp = o.categories[ o.attemptedCategoryID ];
		o.doImages( tmp );
		tb_remove();
		// when accepted, i have to open the category that im trying to get to...not the one that is current.
		
	},
	
	checkCatAuthorization: function() {
		var tmp = o.categories[ o.attemptedCategoryID ];
		var jSonRequest = new Request.JSON({ url: 'datafeed.cfm', onSuccess: o.doCatAuthorizationHandler }).get({ 'event': 'checkAuthorization', 'cat_id': tmp.cat_id, 'cat_password': $('cat_password').value });
	},
	
	categoryClickHandler: function() {
		
		var data = o.categories[this.id];
		o.attemptedCategoryID = this.id; // not the actual selected id but the id that will be used in order to load images based on acceptance.
		
		if(data.cat_private) { o.showPasswordModal(data); } else { if(data.cat_adult) { o.showAdultModal(data); } else { o.doImages(data); } }
		return false;
		
	},
	
	clearStage: function() {
		
		$('holder').getChildren().destroy();
		
	},
	
	doCatAuthorizationHandler: function(d,t) {
		
		var tmp = o.categories[ o.attemptedCategoryID ];
		var response = eval(t);
		
		if(response){ if(tmp.cat_adult) { o.showAdultModal(tmp); } else { o.doImages(tmp); tb_remove(); } } else { alert('Password is incorrect. Please try again.'); };
		
	},
	
	doCategories: function() {
		// get all categories
		var jSonRequest = new Request.JSON({ url: 'datafeed.cfm', onSuccess: o.doCategoriesSuccessHandler }).get({'event': 'getCategories'});
		
	},
	
	doCategoriesSuccessHandler: function(d,t){
		
		o.categories = eval(t);
		
		for(var i=0;i<o.categories.length;i++){
			var elm = new Element('li').injectInside('portfolio');
			
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////
			// REPLACE THIS NEXT LINE WITH A FUNCTION THAT WILL DETERMINE IF A GALLERY IS PRIVATE/ADULT/PASSWORD PROTECTED
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////
			var portfolioItem = new Element('a', { id: i, href: '#', html: o.categories[i].cat_name }).addEvent('click', o.categoryClickHandler).injectInside(elm);	
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////

		}
		
		new MenuMatic(); // render menus after menu items are injected via mootools
	
		// continue with site loading
		o.doPages();
		o.doImages();
		
	},
	
	declineHandler: function() {
		
		tb_remove();
		
	},
	
	doImages: function(data) {
		
		o.loadIndex = 0; // reset loading index
		o.images = '';
		o.currentID = data ? data.id : 0;
		
		$('catnamescroll').set( 'html', 'PORTFOLIO: <strong style="font-size:15px">' + o.categories[o.currentID].cat_name + '</strong>' );
		
		var jSonRequest2 = new Request.JSON({ url: 'datafeed.cfm', onSuccess: o.doImagesSuccessHandler }).get({'event': 'getImages', 'cat_id': o.categories[o.currentID].cat_id});
		
	},
	
	doImagesSuccessHandler: function(d,t){
		
		o.clearStage();
		o.images = eval(t);
		o.loadImage();
		
	},
	
	doPages: function() {
		
		// links to pages are already rendered on the page and we are just getting the information and reassigning the click handler
		$(document.body).getElements('a.page').addEvent('click', function(){ tb_show(this.rel, this.href + '&KeepThis=true&TB_iframe=true&height=535&width=950'); return false; });
		
	},
	
	init: function() {
		
		// do initialization code here
		o.doCategories();
		
	},
	
	isDone: function(i) {
		
		// do isDone code here
		myMooScroller.update();
		
		if(i.id != o.images.length-1) { o.loadImage(); }
		
	},
	
	loadImage: function() {

		var elm = new Element('td').injectInside('holder');
		var tmp = new Asset.image( 'images/portfolio/' + o.categories[o.currentID].cat_id + '/' + o.images[o.loadIndex].img_file_name, { id: o.loadIndex, onload: o.isDone }).setStyles({'padding-right':'2px','margin-right':'2px'}).injectInside(elm);
		o.loadIndex++;
		
	},
	
	showAdultModal: function(data) {
		
		//console.log('DISPLAY ADULT WINDOW');
		tb_show('','inc/warning.html?height=175&width=300&modal=true');
		
	},
	
	showPasswordModal: function(data) {
		
		//console.log('DISPLAY PASSWORD WINDOW');
		tb_show('','inc/password.html?height=150&width=300&modal=true');
		
		//if(data.cat_adult) { o.showAdultModal(data); } else { o.doImages(data) };
		
	}

};

window.addEvent("resize", function() { myMooScroller.update(); });
window.addEvent("domready", function() { 
	
	o.init();
	
	myMooScroller = new MooScroller('content', 'scrollKnob', {
		mode: 'horizontal', 
		wheel: true, 
		stretchMainMenu:true,
		hideWhenNoOverflow: false,
		scrollSteps: 50
	});
	
});