;(function($) {
	var busy = false;
	$.fn.portfolio = function(options) {
		var opts = $.extend({}, $.fn.portfolio.defaults, options);
		
		/*$("a.history").history(function() {
			var href = $(this).attr('href');
			var param = href.split("#");
			var args = param[1].split(",");
			new_section = args[0];
			new_id = args[1];
			//getData(new_section,new_id);
		});*/
		
		$(".history").history();
		/*$("#topnav-discipline").history();
		$("#topnav-industry").history();
		$("#topnav-region").history();*/
		
		$.ajaxHistory.initialize();
		
		//$("#topnav-"+args[0]).addClass("active");
		
		var href = location.href;
		if (href.indexOf("#")==-1) {
			$("#topnav-all").addClass("active");
			getData("all","all");
		} else {
			var param = href.split("#");
			var args = param[1].split(",");
			if (args[0]!="all") {
				$("#latestwork").hide();
			}
			$("#navigation-"+args[0]).show();
			$("#topnav-"+args[0]).addClass("active");
			$("#"+args[0]+args[1]).addClass("active");
			getData(args[0],args[1]);
		}
	
		$("a.history").click(function() {
			if (busy==true) {
				return false;
			} else {
				busy=true;
				var href = $(this).attr('href');
				var param = href.split("#");
				var args = param[1].split(",");
				var new_section = args[0];
				var new_id = args[1];
				getData(new_section,new_id);
			}
		});
		
		return this.each(function() {
			
		});
		
		
	};
	
	function getData(section,id) {
		//alert('huh');
		var breadcrumbs = "";
		$.get("ajax/ajax_get_work.php",
			{ section:section, id:id },
			function(xml) {
				var type = $('type',xml).text();
				var page = $('page',xml).text();
				var section_f = $('section',xml).text();
				var subsection = $('subsection',xml).text();
				
				if (section=="all")
					breadcrumbs = '<span class="current bg-fff">All Clients</span>';
				else
					breadcrumbs = '<span class="bg-fff"><span>'+section_f+'</span> / <span class="current">'+subsection+'</span></span>';
				
				$("#breadcrumb-text").fadeOut("normal",function() {
					$("#breadcrumb-text").empty().append(breadcrumbs).fadeIn();
				});
				
				//client list
				if (section=="all") {
					//alert('break 1');
					displayAll(xml);
				}
				else
					displayOther(xml,type,page,section);
				
			},'xml');
	}
	
	function displayAll(xml) {
		var numItems = $('workItem',xml).length;
		var cols = 3;
		var numrows = Math.ceil(numItems/cols);
		var rows = new Array();

		var rem = numItems % cols;
		var row_avg = Math.floor(numItems/cols);
		
		//calc number of items in each col
		for (var i=0;i<cols;i++) {
			rows[i]=row_avg;
			if (i<rem)
				rows[i]++;
		}
		//height of content area
		var height = 20 + numrows*30;
		
		$("#portfolio-container").fadeOut(800, function() {
			$("#portfolio-wrapper").animate({'height' : height},function(){
				$("#portfolio-container").empty();
				var count=1;
				var col=0;
				var li="";
				$("#portfolio-container").append("<ul class=\"col\"><\/ul>");
				
				$('workItem',xml).each(function(i) {
					var id = $('id',this).text();
					var name = $('name',this).text();
					var image = $('image',this).text();
					var visible = $('visible',this).text();
					
					if (li=="")
						li+='<li><ul class="row">';
					
					if (visible==1) {
						li+='<li><span class="client"><a href="client.php?id='+id+'" >'+name+'<\/a><\/span><\/li>';
					} else {
						li+='<li><span class="client">'+name+'<\/span><\/li>';
					}
					if (count==rows[col]) {
						li+='<\/ul><\/li>';
						$(".col").append(li);
						li='';
						col++;
						count=0;
					}
					count++;
				});
				$("#portfolio-container").fadeIn(800, function() { busy=false; });
			});
		});
	}
	
	function displayOther(xml,type,page,section) {
		var numrows = Math.ceil($('workItem',xml).length / 5);
		var height = 15 + numrows*170;
		//alert('break 2');
		$("#portfolio-container").fadeOut(800, function() {
			//alert('break 3');
			$("#portfolio-wrapper").animate({'height' : height},function(){
				//alert('break 4');
				$("#portfolio-container").empty();
				$('workItem',xml).each(function() {
					var id = $('id',this).text();
					var name = $('name',this).text();
					var image = $('image',this).text();
					
					var workitem = '<div class="medproject">';
					workitem += '<a href="'+page+'.php?id='+id+'"><img src="images/'+type+'/'+id+'/med-'+image+'" width="168" height="103" /></a>';
					workitem += '<div>';
					if (section=="discipline") {
						var client_id = $('client_id',this).text();
						var client_name = $('client_name',this).text();
						workitem += '<a href="'+page+'.php?id='+id+'" class="portfclient">'+client_name+'<\/a> <br /><a href="'+page+'.php?id='+id+'">'+name+'<\/a> ';
					} else {
					workitem += '<a href="'+page+'.php?id='+id+'" class="portfclient">'+name+'<\/a>';}
					workitem += '<\/div>';
					workitem += '<\/div>';
					
					$("#portfolio-container").append(workitem);

				});
				
				$(".medproject").jFade({
					trigger: "mouseover",
					property: 'background',
					start: 'f5f5f5',
					end: 'dddcdc',
					steps: 25,
					duration: 10
				}).jFade({
					trigger: "mouseout",
					property: 'background',
					start: 'dddcdc',
					end: 'f5f5f5',
					steps: 25,
					duration: 20
				});
				
				$("#portfolio-container").fadeIn(800, function() { busy=false; });
				
				$("img").lazyload({ placeholder : "images/transparent.gif", effect : "fadeIn"/*, threshold: 150 */});
			});
		});
	}
	
})(jQuery);