function StatusUpdater(s){
	var that = this;
	var service = s;
	
	this.startUpdater = function(){
		$j("#" + service).everyTime(60000, function(i){
			that.updateStatus();
		});
	}
	
	this.updateStatus = function(){
		
		$j.ajax({
			url: "/updates/" + service,
			cache: false,
				success: function(html){
					html = html.replace(/<!--[a-zA-Z0-9\s\.]+-->/g, "");
				
					current = jQuery.trim($j("#" + service).html()).replace(/<!--[a-zA-Z0-9\s\.]+-->/g, "");
				
					// strip html encoded special characters
					current = current.replace(/&[a-zA-z]+;/g, "");
				
					// strip spaces & special chars from html & current
					// if they don't match, update the content
					if(html.replace(/\W+/g, "") != current.replace(/\W+/g, "")){
						$j("#" + service).fadeOut("fast", function(){$j("#" + service).html(html);$j("#" + service).fadeIn("slow");});
					}
					
				}
			})
	}
}

function FlickrUpdater(container){
	var i = 1;
	var pages = 0;
	var set = null;
	var updater = this;
	
	this.getSet = function(){
		return set;
	}
	this.setSet = function(s){
		set = s;
		$j("#" + container + " .prev").fadeOut("fast");
	}
	
	this.getPages = function(){
		return set;
	}
	this.setPages = function(p){
		pages = p;
	}
	
	this.updateFlickrPhoto = function(set){
			if(i >= pages - 2){
				$j("#" + container + " .next").fadeOut("fast");
			//	i = pages - 2;
			}
			if(i >= 2){
				$j("#" + container + " .prev").fadeIn("fast");
			}
			
			if(i <= 1){
				$j("#" + container + " .prev").fadeOut("fast");
			//	i = 1;
			}
			if(i <= pages - 2){
				$j("#" + container + " .next").fadeIn("fast");
			}
			
		$j("#" + container + "container .flickr_img").fadeTo("fast", 0);

			$j.ajax({
			url: "/flickrGallery/gallery/" + i + '/' + set,
			cache: false,
				success: function(html){
					html = html.replace(/<!--[a-zA-Z0-9\s\.]+-->/g, "");
					$j("#" + container + "container .flickr_img").css("background-image", "url(" + html + ")").fadeTo("fast", 1);
				}
		});
	}
	
	$j("#" + container + " .next").click(function(){
			i ++;
			updater.updateFlickrPhoto(set);
			return false;
		});
	
	$j("#" + container + " .prev").click(function(){
			i --;
			updater.updateFlickrPhoto(set);
			return false;
		});
}

function FeedLoader(){
	var xhr;
	
	this.loadFriendFeed = function(href,divName,name){
		if(xhr){
			xhr.abort();
		}
		$j(divName).fadeTo(250, 0);
		$j('#feed .loader').fadeIn();
		if(!name){
			$j('#who_close_btn').fadeTo(250, 0);
		}
		$j('#who').fadeTo(250, 0, function(){
			if(name){
				$j(this).text('loading ' + name + "'s feed.").fadeTo(250, .25);
			} else {
				$j(this).text('is loading.').fadeTo(250, .25);
			}
		});
	//	console.log('href = ' + href + ' ' + divName + ' ' + name);
		xhr = $j.ajax({
			url: href,
			success: function(html){
				$j('#feed .loader').fadeOut();
				
				var element = $j(divName);

				element.html(html);
				element.fadeTo(250, 1);

				element.find("#masonry").vgrid({
						easeing: "none",
						time: 0,
						delay: 0
					});

				$j('#who').fadeTo(250, 0, function(){
					if(name){
						$j('#who_close_btn').fadeTo(250, 1);
						$j(this).text(name).fadeTo(250, 1);
					} else {
						$j(this).text('').fadeTo(250, 1);
					}
				});
				
				var divPaginationLinks = divName+" .pagination a"; 
				$j(divPaginationLinks).click(function() {
					var thisHref = $j(this).attr("href");
					loadFriendFeed(thisHref,divName); 
					return false; 
				}); 
			}
		});
	}
}

function QuestionLoader(){
	var xhr;
	var divPaginationLinks;
	var qloader = this;
	
	this.loadQuestions = function(href,divName) {
		if(xhr){
			xhr.abort();
		}
		$j(divName).fadeTo(250, 0);
	//	console.log(href);
		xhr = $j.ajax({
			url: href,
			success: function(html, status, obj){
				
			//	console.log("status " + status + " obj " + obj);
			//	console.log("SUCCESS " + html);
				
				$j(divPaginationLinks).unbind();
				
				var element = $j(divName);

				element.html(html);
				element.fadeTo(250, 1);

				divPaginationLinks = divName + " .pagination a"; 
				$j(divPaginationLinks).click(function() {
					var thisHref = $j(this).attr("href");
					qloader.loadQuestions(thisHref,divName); 
					return false; 
				}); 
			}
		});
	}
}

$j = jQuery.noConflict();

$j(function(){
	
	// add click event to questions pagination
	var questionLoader = new QuestionLoader();
	$j('#video .pagination a').click(function(){
		questionLoader.loadQuestions($j(this).attr('href'), '#video');
		return false;
	});
	
	$j('#questions li a').click(function(){
		questionLoader.loadQuestions($j(this).attr('href'), '#video');
		return false;
	});
	
	// HOME PAGE::
	// add hash tag to work links for users that have js available
	$j('a.readmore_btn[href*=/work/]').each(function(){
			$j(this).attr('href', $j(this).attr('href').replace('/work/', '/work/#/'));
		});
	
	// if #lastfm exists, start updater
	if($j("#lastfm").attr("id")){
		var twitterUpdater = new StatusUpdater('twitter');
		twitterUpdater.startUpdater();
	
		var lastfmUpdater = new StatusUpdater('lastfm');
		lastfmUpdater.startUpdater();
	}
	
});
