(function($)
{	
	// event manager
	$.extend({
		mouseLocation:{
			// variables
			top:0,
			left:0,
			
			// methods		
			get:function(){return {top:this.top, left:this.left}}
		},
		galleryPagination:{
			// variables
			li:"#GalleryListing .pagination li",
			links:"#GalleryListing .pagination a",			
			slider:".slider",

			// methods
			init:function(){
				$(this.links).live('click', function(){$.galleryPagination.page(this);return false;});
				$(this.links).first().trigger('click')
			},
			page:function(obj)
			{
				var o	= typeof(obj) == "string" ? eval(obj) : $(obj);
				var delta =	parseInt(o.html()) - 1;
				$(this.links).removeClass("on");
				o.addClass("on");
				$(this.slider).animate({left:-(delta * 140)}, 'fast');
				return false;
			},
			filter:function(count)
			{
				var li = $(this.li).get(count - 1);			
				$(li).nextAll().fadeOut('fast');
				
				var li = $(this.li).get(count);			
				$(li).prevAll().fadeIn('fast');
			}
		},
		tagsListing:{
			// variables
			tags:"#Tags .tags div",
			links:"#Tags .tags div a",
			
			// methods
			resize:function()
			{
				$(this.tags).each(
					function()
					{
						var fontsize = parseInt($(this).css("font-size")) + parseInt($(this).attr("rel") * 4);
						$(this).css({"font-size":fontsize});
					}
				);
				this.bind();
			},
			bind:function()
			{
				$(this.links).bind('click', function(){$.galleryListing.filter($(this).attr("rel"));$.google.log('32teeth','Tag',$(this).attr("rel"));return false;})
			}
		},
		galleryListing:{
			// variables
			GalleryListing:"#GalleryListing",
			SelectedGallery:"#SelectedGallery",
			slider:"#GalleryListing .slider",			
			ul:"#GalleryListing .slider ul",			
			li:"#GalleryListing .slider li",
			links:"#GalleryListing .slider a",
			GalleryPreview:"#GalleryPreview",
			data:{},
			timeout:{},
						
			// methods
			init:function(){
				$(this.links).live('click', function(){$.galleryListing.get(this);return false;});
				$(this.links).live('mouseover', function(){$.galleryListing.preview(this, true);});
				$(this.links).live('mouseout', function(){$.galleryListing.preview(this, false);});
				$(this.GalleryListing).bind('mouseout', function(){$.galleryListing.preview(this, false);});
				$(this.SelectedGallery).bind('mouseover', function(){$.galleryListing.preview(this, false);});
			},
			get:function(obj)
			{
				var o	= typeof(obj) == "string" ? eval(obj) : $(obj);
				var set	= o.attr("rel");
				$(this.links).removeClass("on");
				o.addClass("on");
				$.selectedGallery.get(set, true);
				$.linked.set("set", set);
				$.google.log('32teeth','Gallery',set);
			},
			preview:function(obj, follow)
			{			
				var o		= typeof(obj) == "string" ? eval(obj) : $(obj);				
				var preview	= $(this.GalleryPreview);
				var rel		= o.attr("rel");
				var d		= $.mouseLocation.get();
				var offset	= $(this.GalleryListing).offset();
				preview.hide();
				if(follow)
				{
					preview.html("<img src=\"http://32teeth.org/images/uploads/" + $.galleryListing.data[rel].preview + "\"/>")
					preview.css({top:d.top - 50, left:d.left- offset.left + 50});
					preview.fadeIn();
				}
			},
			filter:function(tag)
			{
				$(this.slider).fadeOut(
					'fast', 
					function()
					{
						$(this).html("");
						
						var column 	= 0;
						var html	= "";
						var data	= {}
						for(prop in $.galleryListing.data)
						{
							if($.galleryListing.data[prop].tags.indexOf(tag) != -1)
							{
								data[prop] = $.galleryListing.data[prop];
							}
						}
						for(prop in data)
						{
							if(column == 0){html += "<ul>";}						
							html += "<li rel=\"" + data[prop].tags + "\"><a href=\"#\" rel=\"" + prop + "\">" + data[prop].title + "</a></li>";
							column++;
							if(column == 9){column = 0;html += "</ul>";}				
						}
						if(column < 9){html += "</ul>";}
						$($.galleryListing.slider).append(html).fadeIn('fast');						
						$.gallerySlider();
						$.galleryPagination.filter($($.galleryListing.ul).length);
						$.linked.set("tag", tag);
					}					
				);
			}
		},
		selectedGallery:{
			// variables
			GalleryPreview:"#GalleryPreview",
			SelectedGallery:"#SelectedGallery",
			title:"#SelectedGallery h3",
			photo:"#SelectedGallery .photo",
			description:"#SelectedGallery .description",
			list:"#SelectedGallery .pagination",
			links:"#SelectedGallery .pagination li a",			
			photos:[],
			
			// methods
			get:function(id)
			{
				$.galleryListing.timeout = setTimeout(function(){$($.galleryListing.GalleryPreview).fadeOut('fast');}, 100);
				$.ajax(
					{
						url:"services/photo.service.php",
						cache:false,
						data:{action:"get",id:id},
						dataType:"json",
						type:"POST",
						success:function(response)
						{
							$.selectedGallery.set(response);
						},
						error:function()
						{
							var message = "failed: " + service;
							!window.console ? alert(message) : console.log(message) ;
						}
					}
				);
				$.gallerySlider(true);
			},
			
			set:function(data)
			{
				this.photos = data.photos;
				$(this.title).html(data.title).hide().fadeIn();
				$(this.description).html(data.description).hide().fadeIn();
				this.pagination(data.photos.length);
				!$.linked.hash.photo ? this.view($(this.links).first()) : $(this.links + ":eq(" + ($.linked.hash.photo - 1) + ")").trigger('click');
			},
			
			pagination:function(max)
			{
				$(this.list).empty()
				for(var n = 0; n < max; n++)
				{
						$(this.list).append("<li><a href=\"#\">" + (n+1) + "</a></li>");
				}
				$(this.list).children().children("a").bind('click', function(){$.selectedGallery.view(this);return false;})
			},
			
			view:function(obj)
			{
				var o		= typeof(obj) == "string" ? eval(obj) : $(obj);	
				var i		= parseInt(o.html()) - 1;
				$(this.links).removeClass("on");
				o.addClass("on");	
				$.linked.set("photo", i);
				$(this.photo).html("<img src=\"http://32teeth.org/images/uploads/" + this.photos[i] + "\" />")
			}
		},
		gallerySlider:function(open)
		{
			var slider = $("#GallerySlider");
			var left	= !open ? 0 : -710; 
			slider.animate({left:left},500, function(){$.galleryListing.timeout = setTimeout(function(){$($.galleryListing.GalleryPreview).fadeOut('fast');}, 100);})

			var holder = $("#GalleryHolder");			
			var height	= !open ? 220 : 405; 			
			holder.animate({height:height},500, function(){$.galleryListing.timeout = setTimeout(function(){$($.galleryListing.GalleryPreview).fadeOut('fast');}, 100);})			
			
			if(!open){$.linked.set("set", false);$.linked.set("photo", false);}
		},
		
		/*
			feature
		*/
		featureProjects:{
			// variables
			feature:"#Feature",
			image:"#Feature img.project",
			title:"#Feature .title",
			description:"#Feature .description",
			seed:0,
			features:[],
			
			// methods
			get:function()
			{
				$.ajax(
					{
						url:"services/featured.service.php",
						cache:false,
						dataType:"json",
						type:"GET",
						success:function(response)
						{
							$.featureProjects.features = response;
							$.featureProjects.seed		= Math.floor(Math.random()*($.featureProjects.features.length));
							$.featureProjects.set();
							
						},
						error:function()
						{
							var message = "failed: " + service;
							!window.console ? alert(message) : console.log(message) ;
						}
					}
				);
				
				$(".next").bind('click', function(){$.featureProjects.move(-1);});
				$(".back").bind('click', function(){$.featureProjects.move(1);});
			},
			
			set:function()
			{
				var feature = this.features[this.seed];
				$(this.title).fadeOut()
				$(this.description).fadeOut()
				$(this.image).hide().attr({src:feature.image}).load(
					function()
					{
						$(this).fadeIn();
						$($.featureProjects.title).hide().html(feature.title).fadeIn();
						$($.featureProjects.description).hide().html(feature.description).fadeIn();
						$.google.log('32teeth','Project',feature.title);
						$($.featureProjects.description + " a").bind('click', function(){$.google.log('32teeth','ProjectClickOut',feature.title);})
					}
				)
			},
			
			move:function(dir)
			{
				var next = this.seed + dir;
				this.seed = next > this.features.length - 1 ? 0 : next < 0 ? this.features.length - 1 : next;
				this.set();
			}
		},
		
		linked:{
			// variabls
			hash:{},
			
			// methods
			check:function(param, value){
				var hash = this.get();
				hash		= hash.slice(1).split("&");
				for(var n = 0; n < hash.length; n++)
				{
					var prop	= hash[n].split("=");
					this.hash[prop[0]] = prop[1]
				}

				// set selected
				if(this.hash.set){$($.galleryListing.links + "[rel='" + this.hash.set + "']").trigger('click');}
				if(this.hash.tag){$($.tagsListing.links + "[rel='" + this.hash.tag + "']").trigger('click');}				
			},
			
			set:function(param, value)
			{
				this.hash[param] = value;
				var hash = "";
				for(prop in this.hash){hash += prop + "=" + this.hash[prop] + "&";}
				hash = hash.slice(0, -1);
				window.location.hash = hash
			},
			
			get:function(){
				return window.location.hash;
			}
			
		},
		
		/*
			google code
		*/
		google:{
			// var
			tracker:false,

			// page load
			page:function()
			{
				this.tracker._trackPageview();
			},

			log:function(category, action, label)
			{
				if(this.tracker)
				{
					this.tracker._trackEvent(category, action, label)
				}
			}			
		}
	})
})(jQuery)

// run it and pass it in!
$(document).ready(
	function()
	{
		setTimeout(function(){
			try
			{
				$.google.tracker = _gat._getTracker("UA-1812163-26");
				$.google.page();
			}
			catch(err){}			
		}, 1000);
		if($($.galleryPagination.PhotoListing))
		{
			$(document).bind('mousemove',function(e){$.mouseLocation.top = e.pageY;$.mouseLocation.left = e.pageX;}); 
			$("a:contains('back to sets')").bind('click', function(){$.gallerySlider();$("#GalleryPreview").hide()})
			$.galleryPagination.init();
			$.galleryListing.init();
			$.tagsListing.resize();
		}
		
		if(window.location.hash){$.linked.check();}
		if($($.featureProjects.Feature)){$.featureProjects.get();}
		$("a[id!='']").bind('click',function(){$.google.log('32teeth', 'LinkOut', $(this).attr('id'));});
	}
)