

var imgGallery = {
	
	path : "http://www.americantinceilings.com/",
	thumb : "images/db/thumbs/",
	server : "/gallery/request.php",
	
	init : function(){
		
		Cookie.set('guid', (Cookie.get('guid') || this._genGUID() ), { path : '/' });
		
		
	//	this.phrase = $('phrase');
	//	$('search').addEvent('click', this.keywordSearch.bind(this));
		
		this.categories = $('categories').addEvent('change', this.changeCategory.bind(this));
		this.tags = $('tags').addEvent('change', this.tagSearch.bind(this));
		
		this.header = $('imgHeader');
		
		this.thumbs = $('thumbs');
		
		this.makeRequest({ method : 'getPopulatedCategories', params : {}});
		
		Lightbox.init({showControls: true});
		
	},
	
	keywordSearch : function(){
		this.makeRequest({ method : 'tagSearch', params : {'phrase' : this.phrase.value }})
	},
	
	tagSearch : function(){
		if(this.tags.getValue() == "false" ) return;
		this.makeRequest({ method : 'getTagImages', params : {'tagid' : this.tags.getValue()} });
	},
	
	makeRequest : function(request){
		request.params.guid = Cookie.get('guid');
		new Json.Remote(this.server, { onComplete : this.processRequest.bind(this)}).send(request);
	},
	
	processRequest : function(result){
		switch(result.method){
			case 'getPopulatedCategories' :
				this.updateCategories(result.data);
				break;
			case 'getPopulatedTags' :
				this.updateTags(result.data);
				break;
			case 'tagSearch' :
				this.header.innerHTML = "Search results for keyword: "+this.phrase.value;
				this.phrase.value = '';
				this.loadImages(result.data);
				break;
			case 'getTagImages' :
				this.header.innerHTML = "Search results for "+
					this.tags.options[this.tags.selectedIndex].text+
					" in category "+this.categories.options[this.categories.selectedIndex].text;
				this.loadImages(result.data);
				break;
		}
	},
	
	updateCategories : function(categories){
			this.categories.getChildren().each(function(child){ child.remove(); });
				new Element('option').setProperty('value','false')
					.appendText('-- Select a Category --').injectInside(this.categories);
			categories.each(function(cat){
				new Element('option').setProperty('value',cat.id).appendText(cat.name).injectInside(this.categories);
			},this);
		
	},
	
	changeCategory : function(){
		if( this.categories.value == 'false') return;
		this.makeRequest({ method : 'getPopulatedTags', params : {'catid' : this.categories.value}});
	},
	
	updateTags : function(tags){
		this.tags.setStyle('display', 'inline');
		this.tags.getChildren().each(function(child){ child.remove(); });
		new Element('option').setProperty('value', 'false')
			.appendText('-- Select an Option --').injectInside(this.tags);
			tags.each(function(tag){
				new Element('option').setProperty('value',tag.id).appendText(tag.tag).injectInside(this.tags);
			},this);		
	},
	
	loadImages : function(images){
		this.thumbs.getChildren().each(function(child){ child.remove(); })
		Lightbox.anchors = [];
		images.each(function(img,i){
			var a =  new Element('a').setProperties({
				'href' : this.path+img.path,
				'rel'  : 'lightbox[search]',
				'title' : 'search results'})
				.adopt( 
					new Element('img').setProperties({'src' : this.path+this.thumb+img.thumb,'border' : 0}))
				.injectInside(this.thumbs);
			a.onclick = Lightbox.click.pass(a, Lightbox);
			Lightbox.anchors.push(a);
		},this);
		this.header.setStyle("display","block");
		this.thumbs.setStyle("display","block");

	},
	
	_genGUID : function(){
		var guid = "";
		for (var i = 1; i <= 32; i++)
 		{
 			var n = Math.floor(Math.random() * 16.0).toString(16);
 			guid += n;
 			if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) guid += "-";
 		}
		return guid;
	}
	
}

Window.onDomReady(imgGallery.init.bind(imgGallery));
