$(document).ready(function(){  
	//startload(1);

	var nomove = false;
	var moving = false;
	var xmlloading = false;
	
	$("#latest_projects").find("li").find("a").css("opacity", 0.85);
	$("div#project_box").find("div#center_project_box").find("a").css("opacity", 0.85);
	$("ul#top_menu").find("li").find("a").addClass("nobg");	

	
	$("#latest_projects").find("li").find("a").hover(
		function() {
			$(this).stop().animate(
				{ opacity: 1 },
				300
			)
		},
		function() {
			$(this).stop().animate(
				{ opacity: 0.85 },
				300
			)
		}                           
	);
	
	$("div#project_box").find("div#center_project_box").find("a").hover(
		function() {
			$(this).stop().animate(
				{ opacity: 1 },
				300
			)
		},
		function() {
			$(this).stop().animate(
				{ opacity: 0.85 },
				300
			)
		}                           
	);
	
	$("ul#top_menu").find("li").find("a").click(function(e) {
		e.preventDefault;
		setTimeout("document.location = '" + $(this).attr("href") + "';",350);
		$("ul#top_menu").find("li").find("span").animate(
				{ backgroundPosition: '(0px -108px)', color: '#2b3447' },
				300,
				"linear",
				function() { $(this).css('font-weight','500') }
		);
		nomove = true;
		$(this).stop().animate(
				{ backgroundPosition: '(0px 0px)', color: '#ffffff' },
				300,
				"linear",
				function() { $(this).css('font-weight','900'); }
			);
		return false;
	});
	
	$("div#project_box").find("div#left_project_box").find("a").click(function(e) {
		e.preventDefault;
		if(!moving && (parseInt($("ul#projects_list").css('left')) < 0)) {
			moving = true;
			$("ul#projects_list").stop().animate(
					{ left: '+=137px' },
					300,
					"linear",
					function() { moving=false; }
			);
		}
		return false;
	});

	$("div#project_box").find("div#center_project_box").find("a").click(function(e) {
		e.preventDefault;
		if(!xmlloading) {
			xmlloading = true;
			$("img#project").hide();
			$('#special_offer').find(".inner").addClass('loading');
			$("#project_specs").find("ul").each(function() {$(this).remove();});
			index = parseInt($(this).attr("rel"));
			
			$.post("/projectsserver/", { project_id: index },
				function(data){
					var imagepath = new String($("image",data).text());
					var link = null;
					if($("link",data).length > 0) {
						link = new String($("link",data).text());
					} 
					
					// $("img#project").attr("src",imagepath);
					$("img#project").load(function () {							
						$(this).hide();
						$('#special_offer').find(".inner").html("");
						$('#special_offer').find(".inner").removeClass('loading').append(this);
						$(this).fadeIn();
					}).error(function () {
		   
					}).attr('src', imagepath);
		
		
					if(link != null) {
						var image = $("img#project").parent().html();
						$("img#project").parent().html("<a href='" + link + "'>" + image + "</a>");
						$("img#project").show();
						$("img#project").css("opacity",1);						
					} else {
						
					}
					
					
					
					$("block",data).each(function(i) {
						var title = ($(this).find("title").text());
						var list;
						if(i%2 == 0) {
							list  =  '<ul class="project_specs project_specs_new">';
						} else {
							list  =  '<ul class="project_specs">';
						}
							
							
						list = list + '<li class="legend">' + title + '</li>';
						
						$(this).find("element").each(function(j) {
							list = list + "<li>" + $(this).text() + "</li>";
						});
						
						list = list + '</ul>';
						$("#project_specs").append(list);
					});
					xmlloading = false;
				}, "xml");
		}
		return false;
	});
	
	$("div#project_box").find("div#right_project_box").find("a").click(function(e) {
		e.preventDefault;
		
		if(!moving && (parseInt($("ul#projects_list").css('left')) - 274 > (-parseInt($("ul#projects_list").css('width'))))) {
			moving = true;
			$("ul#projects_list").stop().animate(
					{ left: '-=137px' },
					300,
					"linear",
					function() { moving=false; }
			);
		}
		return false;
	});	
	
	$("ul#top_menu").find("li").find("a").hover(
		function() {
			if(!nomove) {
				$(this).stop().animate(
					{ backgroundPosition: '(0px 0px)', color: '#ffffff' },
					300,
					"linear",
					function() { $(this).css('font-weight','900'); }
				)
			}
		},
		function() {
			if(!nomove) {
				$(this).stop().css('font-weight','500').animate(
					{ backgroundPosition: '(0px -108px)', color: '#2b3447' },
					300
				)
			}
		}                           
	);
});

function startload(index) {
	xmlloading = true;
	
	$("img#project").hide();
	$('#special_offer').find(".inner").addClass('loading');
	$("#project_specs").find("ul").each(function() {$(this).remove();});
	
	$.post("/projectsserver/", { project_id: index },
		function(data){
			var imagepath = new String($("image",data).text());

			
			// $("img#project").attr("src",imagepath);
			$("img#project").load(function () {
				$(this).hide();
				$('#special_offer').find(".inner").removeClass('loading').append(this);
				$(this).fadeIn();
			}).error(function () {
   
			}).attr('src', imagepath);


			
			
			$("block",data).each(function(i) {
				var title = ($(this).find("title").text());
				var list;
				if(i%2 == 0) {
					list  =  '<ul class="project_specs project_specs_new">';
				} else {
					list  =  '<ul class="project_specs">';
				}
					
					
				list = list + '<li class="legend">' + title + '</li>';
				
				$(this).find("element").each(function(j) {
					list = list + "<li>" + $(this).text() + "</li>";
				});
				
				list = list + '</ul>';
				$("#project_specs").append(list);
			});
			xmlloading = false;
		}, "xml");
}

