$(function(){
	
	function changeTitle() {
		var title = $("#title");
		var title_text = $("span.TitleArea").text();
		title.text(title_text);
	}
	
	function changeContent(content) {
		$("#contentHolder").load(content, function() {
			changeTitle();
			$("#contentHolder .workScr img").load (function() {
				var height = ($("#contentHolder .workScr").height() > $("#contentHolder .description").height()) ? $("#contentHolder .workScr").height() : $("#contentHolder .description").height();
				$("#contentHolder").height(height);
			})
			$("#title").fadeTo("slow", 1.0);
			$(this).fadeTo("slow", 1.0);
		});
	}
	
	function Inactive(arrow) {
		$(arrow).addClass("Inactive");
		$(arrow).find("a").remove();
		$(arrow).css({"opacity": "0.5"});
	}
	
	function setPrev(n){
		if (n < num_proj ){
			n++;
			if ($("#arrowLeft:empty").length > 0) {
				$("#arrowLeft:empty").append("<a href='"+category+"_proj.html?file="+file_id+"_proj"+n+"&num="+n+"'></a>");
				$("#arrowLeft").css({"opacity": "1.0"});
			} else {
				$("#arrowLeft a").attr("href",category+"_proj.html?file="+file_id+"_proj"+n+"&num="+n);
			}
		} else {
			Inactive("#arrowLeft");
		}
	}

	function setNext(n){
		if (n > 1 ) {
			n--;
			if ($("#arrowRight:empty").length>0) {
				$("#arrowRight:empty").append("<a href='"+category+"_proj.html?file="+file_id+"_proj'"+n+"&num="+n+"></a>");
				$("#arrowRight").css({"opacity": "1.0"});
			} else {
				$("#arrowRight a").attr("href",category+"_proj.html?file="+file_id+"_proj"+n+"&num="+n);
			}
		} else {
			Inactive("#arrowRight");
		}
	}
	
	function setPrevNext() {
		setPrev(cur_proj);
		setNext(cur_proj);
	}

	var file = jQuery.url.param("file")+".html";
	var cur_proj = jQuery.url.param("num");
	var num_proj = 0;
	var category = $("#categoryName").text();
	var file_id = "";
	var first = "";
	var second = "";
	
	switch(category) {
		case "web":
			file_id = "w";
			first = "works_print.html";
			second = "works_ui.html";
			num_proj = 10;
			break;
		case "print":
			file_id = "p";
			first = "works_web.html";
			second = "works_ui.html";
			num_proj = 5;
			break;
		case "ui":
			file_id = "ui";
			first = "works_web.html";
			second = "works_print.html";
			num_proj = 4;
			break;
	}
	changeContent(file);
	setPrevNext();
	
	/********** Link slider ************/
	
	function activeCurrent() {
		$("#smallMenuSlider a").eq(num_proj-cur_proj).addClass("Active");
	}

	function activateLink(link) {
		$(link+":empty").append("<a href='javascript:void(0)'></a>");
		//$(link).css({"opacity": "1.0"});
		$(link).show();
	}
	
	function inactivateLink(link) {
		$(link).find("a").remove();
		$(link).hide();
	}
	
	function setRight() {
		$("#listRight").click(function(){
			if (cur_block < num_blocks) {
				cur_block++;
				var cur_pos = parseInt($("#smallMenuSlider").css("margin-left"));
				var new_pos = cur_pos-450;
				$("#smallMenuSlider").animate({marginLeft: new_pos+"px"},700);
				if (cur_block == num_blocks) {
					//Inactive("#listRight");
					inactivateLink("#listRight");
				}
				if (cur_block > 1) {
					activateLink("#listLeft");
				}
			} else {
				//Inactive("#listRight");
				inactivateLink("#listRight");
			}
		});
	}
	function setLeft() {
		$("#listLeft").click(function(){
			if (cur_block > 1) {
				cur_block--;
				var cur_pos = parseInt($("#smallMenuSlider").css("margin-left"));
				var new_pos = cur_pos+450;
				$("#smallMenuSlider").animate({marginLeft: new_pos+"px"},700);
				if (cur_block == 1) {
					//Inactive("#listLeft");
					inactivateLink("#listLeft");
				}
				if (cur_block < num_blocks) {
					activateLink("#listRight");
				}
			} else {
				//Inactive("#listLeft");
				inactivateLink("#listLeft");
			}
		});
	}
	function setClick() {
		setRight();
		setLeft();
	}
	
	var num_cols = $("#smallMenuSlider td").length;
	var num_blocks = Math.floor(num_cols/4)+1;
	var delta = Math.floor(cur_proj/10)+1;
	var cur_block = delta;
	$("#smallMenuSlider").css("width", num_cols*150+"px");
	if (num_cols <=3) {
		activeCurrent();
		$("#smallMenuSlider").show();
	} else {
		activeCurrent();
		if (delta == 1) {
			activateLink("#listRight");
			setClick();
			$("#smallMenuSlider").show();
		} else if (delta == num_blocks) {
			activateLink("#listLeft");
			$("#smallMenuSlider").css("margin-left", -(delta-1)*450+"px");
			$("#smallMenuSlider").show();
			setClick();
		} else {
			activateLink("#listRight");
			activateLink("#listLeft");
			$("#smallMenuSlider").css("margin-left", -(delta-1)*450+"px");
			$("#smallMenuSlider").show();
			setClick();
		}
	}
})

