$(document).ready(function() {
	jQuery.easing.def = "easeInOutQuart";
	$("#projHome h3:last").fadeTo(0, 0.0);

	get_rss_feed();

	function get_rss_feed() {
		var url = "http://opentransmission.com/feed/";
		$.get('proxy.php?url='+ url, function(d) {
			$("#feedContent").empty();
			$(d).find('item').each(function() {
				var $item = $(this);

				var title = $item.find('title').text();
				var link = $item.find('link').text();
				var pubDate = $item.find('pubDate').text();

				var html = "<span class=\"date\">" + pubDate + "</span>";
				html += "<a href=\"" + link + "\" target=\"_blank\" class=\"link\">" + title + "</a>";

				$('#feedContent').append($(html));  
			});
		});
	
	}; 

});

$("#projectGrid div").click(function(){
	var ajax_load = "loading..."; 
	var project= $(this).attr("id"); 
	$("#detail").html(ajax_load);
	$("html, body").animate({scrollTop:0, easing: "swing"}, 800);
	$("#projectSlide").animate({ left: -992, easing: "swing" }, 800, function(){
	$("#projects").animate({ height: 540, easing: "swing" }, 800);
	});
	   
	$("#projHome").animate({ left: -148, easing: "swing" }, 800);
	$("#projHome h3:last").fadeTo(800, 1.0, loadAjax);
	   
	   
	function loadAjax(evt){
		$("#media").html("<img src='./shared/images/ajax-loader.gif' class='loader' />");
		$("#detail").html("loading...");
		
		
		$.ajaxSetup ({
		cache: true
		});

		var loadUrl = "./shared/includes/detail.php #" + project  + "Media"; 
		var loadUrls = "./shared/includes/detail.php #" + project  + "Detail"; 
		
		$("#media").load(loadUrl, [project], function(response, status, xhr) {
		  if (status == "error") {
		    var msg = "Sorry but there was an error: ";
		    $("#media").html(msg + xhr.status + " " + xhr.statusText);
		  }
		  
		  slideShow();
		});
		
		$("#detail").load(loadUrls, [project], function(response, status, xhr) {
		  if (status == "error") {
		    var msg = "Sorry but there was an error: ";
		    $("#detail").html(msg + xhr.status + " " + xhr.statusText);
		  }
		});
		
		
		function slideShow(evt){
		    var currentPosition = 0;
			var slideWidth = 636;
			var slides = $('#media .slider div');
			var numberOfSlides = slides.length;
			
			//alert(currentPosition);
			$('.slider').css('width', slideWidth * numberOfSlides + 10 * numberOfSlides);
			manageControls(currentPosition);
			
			
			$('.control').bind('click', function(evt){
				  if($(this).attr('id')=='leftControl'){
				  	if(currentPosition>0){
				  		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
						$('.slider').animate({"margin-left": (slideWidth+10)*(-currentPosition), easing: "swing" }, 800, function(){
							manageControls(currentPosition);
						});
				  	}
				  }
				  
				  if($(this).attr('id')=='rightControl'){
				  	if(currentPosition<numberOfSlides-1){
				  		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
						$('.slider').animate({"margin-left": (slideWidth+10)*(-currentPosition), easing: "swing" }, 800, function(){
							manageControls(currentPosition);
						});
				  	}
				  }
				
			});
			
			function manageControls(position){
				if(position==0){ $('#leftControl').animate({ opacity: .4 }, 400); } else{ $('#leftControl').animate({ opacity: 1 }, 400) }
				if(position==numberOfSlides-1){ $('#rightControl').animate({ opacity: .4 }, 400) } else{ $('#rightControl').animate({ opacity: 1 }, 400) }
				$('#score').html((currentPosition + 1) + " / " + numberOfSlides);
			}

		};
	
	};

   
});
	
$("#projHome h3:last").click(function(){
	$('.control').unbind('click');
   $("#projects").animate({ height: 800, easing: "swing" }, 800, function(){
   		$("html, body").animate({scrollTop:0, easing: "swing"}, 800);
       $("#projectSlide").animate({ left: 0, easing: "swing" }, 800);
       $("#projHome").animate({ left: 0, easing: "swing" }, 800);
	   $("#projHome h3:first").fadeTo(800, 1.0);
	   $("#projHome h3:last").fadeTo(800, 0.0, function(){
	   		$("#media").html("");
	   		$("#detail").html("Loading...");
	   		$('#score').html("");
	   	});
	   	
   });
});




