$(document).ready(function(){

	if ($(window).height()>($("#content").height())) {
		$("#bottomBar").css("margin-top", 35);
		$("#bottomBar").css("margin-top", ($(window).height()-$("#content").height()+35));
	}
	
	$(window).bind('resize', function() {
		
		$("#bottomBar").css("margin-top", 35);

		if ($(window).height()-$("#content").height()+35>0) {
			$("#bottomBar").css("margin-top", ($(window).height()-$("#content").height()+35));
		}
		
	});	
	
	$("ul.box li, ul.submenu li").bind("click.resize", function () {	
		
		if ($(this).children("a").attr("target")!="_blank" && $(this).children("a").attr("href")!="#")
			window.location=$(this).children("a").attr("href");
	});
	
	$("a").bind("click.hash", function (hashClick) {
		if ($(this).attr("href")=="#")
			hashClick.preventDefault();
	});
	
	
	$(".detail").hide();
		
	$("ul.submenu > li").each (function () {
		imageMargin($(this));
	})
	
	$("ul.box > li").each (function () {

		resizeTitle ($(this));
		
		if ($(this).find("span").is(".detail")) {
			$(this).prepend("<a href=\"#\" class=\"button\">detail</a>");
			resizeTitle ($(this));
		}
		
		imageMargin($(this));
	});

	$("a.button").bind ("click.button", function(evt) {
	
    	evt.preventDefault();
    	
    	var $thisLi = $(this).parent();
    	
    	if ($thisLi.find("span.detail").first().css('display') == "none") {
    		$thisLi.find("span.detail").show();
    		$thisLi.find("span.detail").prev().hide();
    	} else {
    		$thisLi.find("span.detail").hide();
    		$thisLi.find("span.detail").prev().show();
    	}

		imageMargin ($thisLi);
    	
    	$thisLi.find("a.title").css("background-position", "0 "+ ($thisLi.find("a.title").height()+1) +"px");
	});
});

function imageMargin (liElem) {
    var $newMargin = 4, n=0;
    liElem.find("img").css("margin-bottom",$newMargin+"px");
    	
    while ((liElem.find("img").height()+$newMargin+18)<liElem.height() && n<20) {
    	$newMargin = $newMargin+15;
    	liElem.find("img").css("margin-bottom",$newMargin+"px");
    	n++;
}	}

function resizeTitle (liElem) {

	var  n=0, tag1, tag2, aTitle = liElem.find("a.title");
	if 	(aTitle.height()>20) {
		
		if (aTitle.html()==aTitle.text()) {
			aTitle.wrapInner("<span></span>");
			aTitle.append("<span>...</span><span class=\"detail\"></span>");
			$(".detail").hide();
		}
		
		while (aTitle.height()>20 && n<100) {

			if (aTitle.children("span:first-child").text().length>70)
				aTitle.children("span:first-child").text().match(/^(.{70})(.+)$/);
			
			else
				aTitle.children("span:first-child").text().match(/^(.+)(.{1})$/);

			tag1 = RegExp['$1'];
			tag2 = RegExp['$2'];
				
			aTitle.children("span:first-child").text(tag1);
			aTitle.children("span:last-child").text(tag2+aTitle.children("span:last-child").text());
			n++;
}	}	}

