var linkscnt = 0;
var last = null;
var lastid = null;
var imgbackup = [];
var projectdetails = null;
var links = [];
var legacyimg = null;
var legacytitle = null;
var legacydate = null;
var legacylink = null;
var tooltipdiv = null;

$(document).ready(function(){

		if($("li.legacy").length > 0) {
			// first prepare legacy content
			// add and hide link
			// enter backup element for static/dynamic switch
			$("li.legacy").before('<li class="etr"><p><a href="'+legacylink+'#project" class="leglink"><span>'+legacytitle+'</span> '+legacydate+'</a></li>');
			
			//$("a.leglink").css({height:"1px" ,overflow:"hidden"});
			$("a.leglink").parent().parent().hide();

			$("li.legacy").find("a.close").click(function(me){
				me.preventDefault();
				$("li.legacy").slideUp('slow',function(){
					$("li.legacy").remove();
					$("a.leglink").parent().parent().slideDown('slow');
				});
				return false;
			});
		}

		$("li.etr").find("a").not(".close").not(".anchor").each(function(){
		
			if($(this).parent().parent().is(".legacy")){ 
				//has legacy set backup things
				links[linkscnt] = [legacyimg,projectdetails];
				//alert(links.toString());
				lastid = linkscnt;
				//alert(linkscnt);
			}
		
			$(this).parent().parent().attr("id","tr"+linkscnt);
			$(this).rel(linkscnt);
		//	$(this).unbind();
			$(this).click(function(me){
				me.preventDefault();
				activateClick(this);
				return false;
			});
			
			
			
			//links[linkscnt] = null;
			linkscnt++;
		});
});

function makeStatic(myid) {
	//alert(myid);
	//alert(links.toString());
	//alert(links[myid][1]);
	if (myid !== null) {
		/*$("div#flashelement"+links[myid][1]).html('<a href="#" id="l'+myid+'" rel="'+myid+'" class="imglink"><img src="'+links[myid][0]+'" width="730" height="330" /></a>');*/
		$("a#l").click(function(me){
			me.preventDefault();
			
			makeStatic(lastid);
			eval('project'+links[parseInt($(this).rel())][1]+'.write("flashelement'+links[parseInt($(this).rel())][1]+'");');
			lastid = parseInt($(this).rel());
			//alert(links[parseInt($(this).rel())][1]);
			return false;
		});
		$("a#l"+myid).hover(function(me){
			// on mouseover
			var mousePos = findPos(this);
			// if there is no div containing the tooltip
			if(!tooltipdiv)
			{
				// create a div and style it
				var div = document.createElement("div");
				tooltipdiv = div;
				$(div).css(
				{
					/*padding: "2px 2px 2px 0",
					backgroundColor: "#E0FF00",
					color: "#333",
					position: "absolute"*/
				})
				// add the title/alt attribute to it
				.html("click to scroll and zoom");
				$("body").append(div);
				//this.tooltipset = true;
			}
			$(tooltipdiv).show().css({left: mousePos[0] + "px", top: mousePos[1] + 3 + "px"});

		},function(me){
			// on mouseout
			if(tooltipdiv)
				{
					$(tooltipdiv).remove();
					tooltipdiv = null;
				}

		});
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//alert(curleft);
	//alert(curtop);
	return [curleft,curtop];
}


function restore(no) {
	$("li#p"+no).slideUp('slow',function(){
		$("li#p"+no).remove();
		$("li#tr"+no).slideDown('slow');
	});
	$("li#tr"+no).find("a").unbind();
	$("li#tr"+no).find("a").click(function(me){
				me.preventDefault();
				activateClick(this);
				return false;
	});
	if (no == lastid) {
		last = lastid = null;
	}
}

function loadDetail(target,plink,no) {
	
	$(target).after('<li class="etr" id="p'+no+'">&nbsp;</li>');
	var targetp = $("li#p"+no);
	//alert($(targetp).html());
	$(targetp).hide();
	
	
	//alert("/dynamic.php"+plink);
	$(targetp).load(("/ajax-content?"+plink),function(){
	//$(targetp).load(("dynamic.php?page_id=75"),function(){
		//alert(no);
		
		$(this).find("a.close").click(function(me){
			me.preventDefault();
			restore(no);
			return false;
		});
		$(target).slideUp('slow',function(){
		
		
		
			/// before sliding down hide image....!
			$("div#flashelement"+projectdetails).find("img").each(function(){
					$(this).hide();
					links[no] = [$(this).src(),projectdetails];
				});
		
			$(targetp).slideDown('slow',function(){
				var flashS = false;
			
				
			
				if (projectdetails != null) {
					eval('flashS = project'+projectdetails+'.write("flashelement'+projectdetails+'");');
					if(flashS) {
						projectdetails = null;
					}
				}
				//alert(flashS);
				if (!flashS) {
					//alert($("div#flashelement"+projectdetails).find("img").length);
					$("div#flashelement"+projectdetails).find("img").each(function(){
						$(this).show();
						projectdetails = null;
					});
				}
			
			});
		});
		

		
		
	});
}

function activateClick(thi) {
// this is our target row
var target = $("li#tr"+$(thi).rel())

$(thi).unbind();
var plink = $(thi).href();
var no = $(thi).rel();	//.toString();
plink = plink.substring(plink.indexOf("?"),plink.indexOf("#"));
if (lastid != null) {
	makeStatic(lastid);
	//	restore(lastid);
}
last = $(target).html();
lastid = $(thi).rel();
loadDetail(target,plink,no);

}
