$(function() {
	Projects.init();
	Dialog.init();
	Categories.init();
	NavAdditional.init();
});

var Projects = {	
	Triggers: null,
	init: function() {
		var cc = this;
		cc.Triggers = $('#List a');
		cc.events();
		cc.render();
	},
	events: function() {
		var cc = this;
		cc.Triggers.bind('click', function() {
			var url = $(this).attr('href');
			Dialog.load(url);
			return false;
		});	
	},
	render: function() {
		var cc = this;
	}
};

var NavAdditional = {
	Triggers: null,
	init: function() {
		var cc = this;
		cc.render();
	},
	render: function() {
		var cc = this;
		Cufon.replace('#NavAdditional h2, #NavAdditional a');
	}
}

var Categories = {
	Triggers: null,
	init: function() {
		var cc = this;
		cc.Triggers = $('#NavSecondary a');
		cc.Projects = $('#List li');
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Triggers.bind('click', function() {
			var category = $(this).attr('rel');
			if(category=='all') {
				$('#List li').show();	
			}
			else {
				$('#List li:not(.'+category+')').hide();
				$('#List li.'+category).show();	
			}
			
			cc.Triggers.removeClass('selected');
			$(this).addClass('selected');
			Cufon.replace('#NavSecondary a');
			return false;
		});
	}
}

var Dialog = { 
	Window: null,
	Overlay: null,
	Wrapper: null,
	Details: null,
	Image: null,
	init: function() {
		var cc = this;
		cc.Overlay = $('#Overlay');
		cc.Window = $('#Dialog');
		cc.render();
	},
	load: function(url) {
		var cc = this;
		Loading.show();
		cc.Window.html('');
		cc.Overlay.show();
		$.ajax({
		  url: url+'/'+hiRes,
		  cache: true,
		  success: function(html){
		    cc.Window.html(html);
			cc.Wrapper = $('#Dialog div.wpr');
			cc.Details = $('#Details');
			
			
			
			$("div#Tabs ul").tabs("div#Panes > div", {
				current: 'selected',
				onClick: function(event, tabIndex) { 
			        if(tabIndex==1) {
						cc.Details.hide();
					}
					else {
						cc.Details.show();
					}
			    }
			});
			cc.prevnext();
			cc.Window.hide();
			
			if($('#Video').length>0) {
				cc.show(711,400);
			}
		  }
		});
	},
	prevnext: function() {
		var cc = this;
		var links = $('#PrevNext a:not("#Close")');
		links.each(function() {
			var link = $(this);
			if(link.hasClass('disabled')) {
				link.css('opacity', .3);
				link.click(function() {
					return false;
				});
			}
			else {
				link.click(function(){
					var url = $(this).attr('href');
					cc.load(url);
					return false;	
				});
			}
		});	
	},
	show: function(w, h) {
		var cc = this;
		Loading.hide();
		Cufon.replace('#Tabs a, #Details p, #Profile p');
		
		var width = w+54;
		if(width<500) width = 500;
		var height = h+104;
		cc.Window.css({
			'margin-left': -width/2+'px',
			'margin-top': -height/2+'px'
		});
		cc.Wrapper.css({
			'width': width,
			'height': height
		});
	
		cc.Window.fadeIn('fast');
		$('#Close').click(function() {
			cc.hide();
			return false;
		});
		cc.Overlay.click(function() {
			cc.hide();
		});
	},
	hide: function() {
		var cc = this;
		cc.Window.fadeOut('fast', function(){
			cc.Overlay.hide();
		});
	},
	render: function() {
		var cc = this;
		cc.Window.hide();
	}
};

