jQuery(function($){ 	
	var view = $.cookie('view');
	function setView() {
	if (view === 'list') { 
		listView();
	} else if (view === 'thumb') {
		thumbView();
	} else {
		scrollView();
	}
	}	
	setView();
	hideShowLabel($("#searchinput"), $(".searchlabel"));		
	
	$("#m-projects").click(function() { 
		thumbView();
	});

	$("#m-blog").click(function() { 
		scrollView();
	});
	
	var hauContent = $("#content");
	//Bind Functions
	$("#list").bind('click', listView);
	$("#scroll").bind('click', scrollView);
	$("#thumb").bind('click', thumbView);
	
	//Thumb	
	if(view === "thumb") {
		var	thumb = $(".thumb");
		var filterClick = $("#filterThumb li a");
		$(thumb).live('mouseover', thumbHoverIn);
		$(thumb).live('mouseout', thumbHoverOut);
		$(filterClick).bind("click", thumbFilter);
	}

	//List	
	if(view === "list") { 
		var worksList = $(".group ul li");
		$(worksList).bind('mouseover', listHoverIn);
		$(worksList).bind('mouseout', listHoverOut);
	
		//Numbers in List	
		$(".group").each(function() { 
			var thisGroup = $(this);
			var thisGroupType = $(thisGroup).find(".type");
			var typeGroupSize = $(thisGroup).find("a").size();
			var typeTextSize = $(thisGroupType).text() + "(" + typeGroupSize + ")";   
			$(thisGroupType).text(typeTextSize);
		});
	}
	
	//Front
	if($(hauContent).hasClass("front")) { 
		var fthumb = $(".fthumb");
		$(fthumb).bind('mouseover', thumbHoverIn);
		$(fthumb).bind('mouseout', thumbHoverOut);
	}

	//Scroll
	if(view === "scroll" || $(hauContent).hasClass("single")) { 
		var posts = $(".post-content");
		var hauMore = $(".more");
		slideShow(posts);
		$(hauMore).live('click', moreToggle);
		$(hauMore).live('mouseover', moreHoverIn);
		$(hauMore).live('mouseout', moreHoverOut);
	}
	
	//Projects Single
	if($(hauContent).hasClass("single") && $(hauContent).hasClass("projects")) {
		$(".more").trigger("click");
	}
	
	//Infinte Scroll
	var nav = $(".pagenav");
	var navnext = $(nav).children(".next");

	$('#content').infinitescroll({
		navSelector  : nav,            
		nextSelector : navnext,    
		itemSelector : "#content div.post",
		loadingImg   : "http://hauben.nl/site/img/loader.gif",          
		loadingText  : "",      
		animate      : true,
		donetext 	 : "No more Results",                        
		bufferPx     : 40
		},function() { 
			setView();
			slideShow($(".post-content"));
			load = "<div id=\"load\"><a id=\"loadmore\" href=\"\">More</a></div>";
			$(load).appendTo("#content");
	});
	
	//Get Load More Button 
	$(window).unbind('.infscr');
	$("#load").live('click', function() { 
		$(document).trigger('retrieve.infscr');
		$(this).remove();
//		$(this).appendTo("#content").show();
		return false;
	});
	
	
	/* Tags Function */
	if(view !== "thumb" || $(hauContent).hasClass("single")) {
		$(window).scroll(function(){ 
			scrolled = $(window).scrollTop() + 150; 
					
			// Loop through all posts and gets their offset from the top and their height
			$(".post").each(function(i, elem) {  
				posttop = $(elem).offset().top; 
				postbottom = posttop + $(elem).height(); 
				
				//checks if the scrolled amount is bigger 
				//than the posttop but smaller than the bottom of that post
				if (scrolled > posttop && scrolled < postbottom ) {  
					
					$(".post").not(elem).removeClass("active");
					$("#sidebar li").removeClass("active");
					$(elem).addClass("active");
					activeElem = $("#content .active");				
					$("#tags li a").not(activeElem).removeClass("active");				
					
					mySomething = $(activeElem).attr("class").split(" "); 
					mySomething.splice(0,1);
	
					for(i=0; i<mySomething.length; i++) {
						activetags =  "#tags li ." + mySomething[i];
						if(activetags != "#tags li .") {
							$(activetags).addClass("active");
						}	
					}				
					
					if($(activeElem).attr("id")!="") { 
						relSidebar = $(activeElem).attr("id");				 
					} else {
						relSidebar = null;
					}
					if($(relSidebar)!=null) {
						relSidebarClass = "#rel ." + relSidebar;
						$(relSidebarClass).addClass("active").prependTo("#sidebar #rel");
					}				
				} 
			});
		});
	}	

});


/* FUNCTIONS */
	//View Functions
	function thumbView() {
		$("#view li a").removeClass("sel");
		$("#thumb").addClass("sel");
		$(".thumb").show();
		$.cookie('view', 'thumb', { path: "/"});
		var view = 'thumb';
	}
	function scrollView() {
		$("#view li a").removeClass("sel");
		$("#scroll").addClass("sel");
		$.cookie('view', 'scroll', { path: "/"});
		var view = 'scroll';
	}
	function listView() { 
		$("#view li a").removeClass("sel");
		$("#list").addClass("sel");
		$.cookie('view', 'list', { path: "/"});
		var view = 'list';
	}

	//SlideShow Function 
	function slideShow(elem) { 
		$(elem).each(function() { 
			//Set Variables
			var	post = $(this);	
			var slide = $(post).children(".slide");
			var slidenav = $(post).children(".slidenav");
			var nextSlide = $(post).find(".next");
			var prevSlide = $(slidenav).children(".prev");
			var countSlide = $(slidenav).children(".count");	

				//Initiate Cycle Plugin		
				$(slide).cycle({ 
					speed: '400',
					timeout: 0,
					prev: prevSlide,
					next: nextSlide,
					after: counter
				});			
				function counter(curr,next,opts) { 
					var slides = '' + (opts.currSlide +1) + '/' + opts.slideCount;
					$(countSlide).html(slides);	
				}				
		});
	};

	//More Info For Projects 	
	function moreToggle() {
		button = $(this);
		more = $(button).siblings(".morecontent");		
		if($(button).hasClass("open")) {
			$(more).hide();
			$(button).removeClass("open");
		} else {
			$(button).addClass("open");
			$(more).show();
		}
		return false;
 	};
	function moreHoverIn() {
		button = $(this);
		more = $(button).siblings(".morecontent");
		$(more).show();
	}
	function moreHoverOut() {
		button = $(this);
		more = $(button).siblings(".morecontent");
		if(!($(button).hasClass("open"))) { 
			$(more).hide(); 
		}
	}
	
	//Thumbs Overlay	
	function thumbHoverIn() {
		$(this).find(".thumbtitle, .thumbhover").show();

		if($(this).hasClass("thumb")) { 
			var activeThumb = $(this).parent(".post");			
			var relTags = $(activeThumb).attr("class").split(" "); 
			relTags.splice(0,1);
	
			for(i=0; i<relTags.length; i++) {
				var activeThumbTags =  "#tags li ." + relTags[i];
				if(activeThumbTags != "#tags li .") {
					$(activeThumbTags).addClass("active");
				}	
			}				
	
			var relSide = $(activeThumb).attr("id");
			if(relSide != "") { 
				relSidebar = relSide;				 
			} else {
				relSidebar = null;
			}
			if(relSidebar!=null) {
				relSidebarClass = "#rel ." + relSidebar;
				$(relSidebarClass).addClass("active").prependTo("#sidebar #rel");
			}
		}
	}

	function thumbHoverOut() {
		$(this).find(".thumbtitle, .thumbhover").hide();
		$("#rel li").removeClass("active");
		$("#tags li a").removeClass("active");
	}	

	function listHoverIn() {
		var	activeList = $(this);
		var relTags = $(activeList).attr("class").split(" "); 
		relTags.splice(0,1);
		
		for(i=0; i<relTags.length; i++) {
			var activeListTags =  "#tags li ." + relTags[i];
			if(activeListTags != "#tags li .") {
				$(activeListTags).addClass("active");
			}	
		}				

		var relSide = $(activeList).attr("id");
		if(relSide != "") { 
			relSidebar = relSide;				 
		} else {
			relSidebar = null;
		}
		if(relSidebar!=null) {
			relSidebarClass = "#rel ." + relSidebar;
			$(relSidebarClass).addClass("active").prependTo("#sidebar #rel");
		}		
	}
	
	function listHoverOut() {
		$("#rel li").removeClass("active");
		$("#tags li a").removeClass("active");
	}
	
	function thumbFilter() {
		var tfThis = $(this);
		var allTf = $("#filterThumb li a");
		var hauThumbs = $(".post");
		var tuFilter = "." + $(tfThis).attr("id");

		$(allTf).removeClass("sel");
		$(tfThis).addClass("sel");		
			if(tuFilter == ".all") {
				$(hauThumbs).show();
			} else {
				$(hauThumbs).show();
				$(hauThumbs).not(tuFilter).hide();			
			}
		return false;
	}

	function hideShowLabel(input, label) { 
		$(input).data("default", label);						
		$(input).bind("blur", function() { 
			value = $(this).val();			
			if(!value) { 
				$(label).show();
			} else {
				$(label).hide(200);
			}		
			$(label).removeClass("infocus");
		});
		$(input).bind("focus", function() { 
			$(label).hide();
		});				
		$(input).trigger("focus").trigger("blur");
	}

