var cbWindowOpen = false;
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.findCategoryObjById(o.attemptedCategoryID) ];
		o.doImages( tmp );
		jQuery.fn.colorbox.close();
		// when accepted, i have to open the category that im trying to get to...not the one that is current.
		
	},
	
	categoryClickHandler: function() { if(cbWindowOpen){ jQuery.fn.colorbox.close(); cbWindowOpen = false; }; o.setGallery(this.id); return false; },
	
	checkCatAuthorization: function(val) { var jSonRequest = new Request.JSON({ url: 'datafeed.cfm', onSuccess: o.doCatAuthorizationHandler }).get({ 'event': 'checkAuthorization', 'cat_id': o.attemptedCategoryID, 'cat_password': val }); },
	
	clearStage: function() { $('holder').getChildren().destroy(); },
	
	declineHandler: function() { jQuery.fn.colorbox.close(); },
	
	doCatAuthorizationHandler: function(d,t) {
		
		var tmp = o.categories[ o.findCategoryObjById(o.attemptedCategoryID) ];
		var response = eval(t);
		
		if(response){ if(tmp.cat_adult) { o.showAdultModal(tmp); } else { o.doImages(tmp); jQuery.fn.colorbox.close(); } } else { alert('Password is incorrect. Please try again.'); };
		
	},
	
	doCategories: function () { 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');
				var portfolioItem = new Element('a', { id: o.categories[i].cat_id, href: o.categories[i].cat_id, html: o.categories[i].cat_name }).addEvent('click', o.categoryClickHandler).injectInside(elm);	
			}
			
			new MenuMatic(); //set main menu
			o.doPages(); //set pages on menu
			
			if (swfobject.getQueryParamValue("g")) { var g = swfobject.getQueryParamValue("g"); };
			if (swfobject.getQueryParamValue("p")) { var p = swfobject.getQueryParamValue("p"); };
			if (swfobject.getQueryParamValue("l")) { var l = swfobject.getQueryParamValue("l"); };
			
			o.navigationLogic( g,p,l );
		
	},
	
	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(){ 
		
			//o.openClrBox(this.rel,this.id);
			if(this.rel != 1){ o.openClrBox(this.title,this.id) } else { window.open(this.href); }
			return false; 
			
		});
		
	},
	
	findCategoryObjById: function (cat_id) {
		
		var e = 0;
		for(var i=0;i<o.categories.length;i++){ if(o.categories[i].cat_id == cat_id){ e=i; break; }; };
		return e;
		
	},
	
	init: function () { 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++;
		
	},
	
	openClrBox: function(label,id){ if(!cbWindowOpen){ o.setPage(label, id); return false; } },
	
	setGallery: function (g) { SWFAddress.setValue('/?g='+ g + '&r=' + Math.round(Math.random()*100)); }, //SWFAddress.setValue('/?g=' + this.id + '&r=' + Math.round(Math.random()*100)); return false;
	
	setPage: function (l,p) { SWFAddress.setValue('/?l=' + l + '&p=' + p); },
	
	showAdultModal: function(data) {
		
		jQuery.fn.colorbox({ title: 'Please enter password', href: "inc/warning.html", iframe: 'true', overlayClose: false, width: 400, height: 250});
		//console.log('DISPLAY ADULT WINDOW');
		//tb_show('','inc/warning.html?height=175&width=300&modal=true');
		
	},
	
	showPasswordModal: function(data) {
		
		jQuery.fn.colorbox({ title: 'Please enter password', href: "inc/password.html", iframe: 'true', overlayClose: false, width: 400, height: 250});
		//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) };
		
	},
	
	swfAddressInitHandler: function (event) {
		//console.log("INIT");
		//console.log(event);
	},
	
	swfAddressInternalChangeHandler: function (event) {
		//console.log("INTERNAL CHANGE");
		o.navigationLogic( event.parameters.g, event.parameters.p, event.parameters.l );
	},
	
	swfAddressExternalChangeHandler: function (event) {
		//console.log("EXTERNAL CHANGE");
		o.navigationLogic( event.parameters.g, event.parameters.p, event.parameters.l );
	},
	
	swfAddressChangeHandler: function (event) {
		
			//console.log("CHANGE");
		
	},
	
	navigationLogic: function (g,p,l) {
		
		if(!g && !p){
			data = o.categories[0];
			o.attemptedCategoryID = data.cat_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); } }
		}
		
		if(g){
			data = o.categories[ o.findCategoryObjById(g) ];
			o.attemptedCategoryID = g; // 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); } }
			if(cbWindowOpen){ cbWindowOpen = false; jQuery.fn.colorbox.close(); };
		}
		
		if(p){ 
			jQuery.fn.colorbox({ title: l, href: "page.cfm?page_id=" + p, iframe: 'true', overlayClose: false, width: 850, height: 535});
			if(!cbWindowOpen){ cbWindowOpen = true; }
		}
		
		// if window is open but no p parameter exists, then close window
		if(!p){ if(cbWindowOpen){ cbWindowOpen = false; jQuery.fn.colorbox.close(); }; };
		
	}
	
}

//========================================================================================
//========================================================================================
//========================================================================================

SWFAddress.setStrict(false);
SWFAddress.addEventListener(SWFAddressEvent.INIT, o.swfAddressInitHandler);
//SWFAddress.addEventListener(SWFAddressEvent.CHANGE, o.swfAddressChangeHandler);
SWFAddress.addEventListener(SWFAddressEvent.INTERNAL_CHANGE, o.swfAddressInternalChangeHandler);
SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, o.swfAddressExternalChangeHandler);

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
		});
	
});
