var root = "http://www.wearedesignstudio.com/";

function show_client() {
		var selected = "client";
		var new_selected = "";
		var category = "";
		
		$("#menu li a").removeClass("selected");
		$("#menu li .client").addClass("selected");
		$("#posts").empty();
		$("#client_login").show();
}

function lightup() {
	$('.lightbox').lightBox({
		imageLoading: root + 'images/lightbox/lightbox-loader.gif',
		imageBtnClose: root + 'images/lightbox/lightbox-btn-close.gif',
		fixedNavigation: true
	});
}

function showmore() {
	$(".show_more").livequery('click', function(e) { 
		e.preventDefault();
		$(this).hide();
		$(this).next().show();
		$.scrollTo($(this).parent().parent().parent(), 400, {offset: -50}); 
		$(this).parent().parent().next().find(".more").slideDown(500);
	});
}

function close() {
	$(".close").livequery('click', function(e) { 
		e.preventDefault();
		$(this).hide();
		$(this).prev().show();
		where = $(this).parent().parent();
		$(this).parent().parent().next().find(".more").slideUp("slow");
	});
}

$(document).ready(function() {

//Animated Scroll

	showmore();
	close();
	
	$(".image_expand").livequery('click', function(e) {
		e.preventDefault();
		
		var display = $(this).parent().parent().find(".more").css("display");
		var more = $(this).parent().parent().prev().find(".show_more");
		
		if(display==="none") {
			more.hide();
			more.next().show();
			$.scrollTo($(this).parent().parent().parent(), 400, {offset: -50}); 			
			$(this).parent().parent().find(".more").slideDown(500);
		} else {
			more.show();
			more.next().hide();
			$(this).parent().parent().find(".more").slideUp(500);
		}
	});
	
//Menu

	var selected = "all";
	var new_selected = "";
	var category = "";
	var enable = 1;
		
	$("#menu li a").click(function(e) {
		e.preventDefault();
		if(enable === 1) {
			enable = 0;
			$("#menu li a").removeClass("selected");
			$(this).addClass("selected");
			new_selected = $(this).attr("rel");
			
			if(new_selected !== selected) {
				selected = new_selected;		
				$("#stage").fadeOut("medium", function() {
					$("#client_login").hide();
					$("#posts").empty();
					if(selected === "client") {
						$("#client_login").show();
						$("#stage").fadeIn("medium", function() {
							enable = 1;
						});
					} else {
						$("#loading").fadeIn("fast", function() {
							ajaxRequest = $.ajax({
								type: "POST",
								url: root + "index.php/ajax_load/posts/category/"+selected,
								data: "",
								success: function(html){
									$("#loading").fadeOut("fast", function() {
										$("#posts").append(html).show();
										$("#stage").fadeIn("medium");
										category = "category/" + selected + "/";
										load = true;
										scrolled = 1;
										next_page = 5;
										enable = 1;
										lightup();
										showmore();
										close();
									});
								}
							});
						});
					}
				});
			}
		}
	});
	
	
	
//scroll
	
	var next_page = 5;
	var load = true;
	var noScroll = 0;
	
	$(window).scroll(function(){
		if(next_page/5 <= pages-1) {
			if($(window).scrollTop() == $(document).height() - $(window).height()) {
				if(noScroll != 1) {				
					if(load === true) {
						load = false;
						//alert(next_page/10 + " -- " + pages);
						$("#loading").fadeIn("medium");
						var timeOut = setTimeout(function() {
							$("#loading").fadeOut("fast", function() { $('#takeforever').fadeIn() });
						}, 10000);
						nexturl = root + "index.php/ajax_load/posts/" + category + "P" + next_page;					
						$.ajax({
							url: nexturl,
							cache: false,
							success: function(html){
								$(".post:last").after(html);
								next_page = next_page + 5;
								$("#loading").fadeOut("fast");
								$('#takeforever').hide();
								clearTimeout(timeOut)
								load = true;
								lightup();
								showmore();
								close();

							}
						});
					}
				}
			}
		} else {
			$("#theend").fadeIn("fast");
		}
	});

//lightbox
	lightup();

});
