// internetisbig™

// v1.0 - jason sack 5.20.09

// preload images
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
$.preloadImages("../images/input-selected.gif","http://www.internetisbig.com/images/inputsm-selected.gif");

active = 1;

$(document).ready(function() {

	// calls & actions to make it big(ger) //
	$('#makeitbigger').click(function() { makeItBigger(); });
	$('#makeitbigger').mouseover(function() { $(this).css('opacity','.6') });
	$('#makeitbigger').mouseout(function() { $(this).css('opacity','1') });

	// form functions
	$('.submit').mouseover(function () {
		$(this).css('opacity','.9');
    });
    $('.submit').mouseout(function() {
    	$(this).css('opacity','1');
    });

	// homepage
	$('.textfield input').attr('value','enter your del.icio.us username');
	
	$('.textfield input').focus(function () {
		var value = $(this).attr('value');
		if (value == 'enter your del.icio.us username') {
			$('.textfield input').attr('value','');
		};		
		$(this).css('backgroundImage','url(../images/input-selected.gif)');
		$(this).css('color','#222222');
		
	});
	$('.textfield input').blur(function () {
		var value = $(this).attr('value');
		if (value == '') {
			$('.textfield input').attr('value','enter your del.icio.us username');
			$(this).css('color','#cccccc');
		};
		$(this).css('backgroundImage','url(../images/input-waiting.gif)');
	});
	
	// my
	
	$('.form-again .textfield input').attr('value','list another del.icio.us user');
	
	$('.form-again .textfield input').focus(function () {
		var value = $(this).attr('value');
		if (value == 'list another del.icio.us user') {
			$('.textfield input').attr('value','');
		};		
		$(this).css('backgroundImage','url(http://www.internetisbig.com/images/inputsm-selected.gif)');
		$(this).css('color','#222222');
		
	});
	$('.form-again .textfield input').blur(function () {
		var value = $(this).attr('value');
		if (value == '') {
			$('.textfield input').attr('value','list another del.icio.us user');
			$(this).css('color','#cccccc');
		};
		$(this).css('backgroundImage','url(http://www.internetisbig.com/images/inputsm-waiting.gif)');
	});
	
	
	//
	
	$('.submit').click(function() {
		var user = $('.textfield input').attr('value');
		if (user != '') {
			if (user != 'enter your del.icio.us username') {
				window.location = 'http://my.internetisbig.com/?u='+user;
			}
		}
	});
	
	$(document).keydown(function(event){
		if(event.keyCode == 13){
			var user = $('.textfield input').attr('value');
			if (user != '') {
				if (user != 'enter your del.icio.us username') {
					$('.submit').css('opacity','.9');
					window.location = 'http://my.internetisbig.com/?u='+user;
				}
			}
			return false;
		}				
	});	
	
});


// Get the feed

function RSSFeed(){}

RSSFeed.Entries = function(feed_url, callback){
    requestURL = "http://pipes.yahoo.com/pipes/pipe.run?_id=6e02f3d46634fbe2924cf01905b38ef8&_render=json&_callback=?&feed=" + feed_url;
    $.getJSON(requestURL, function(json, status){
        callback(json.value.items, status);
    });
}

// INITIAL LOAD //
if ( feedUser != '') {
	RSSFeed.Entries("http://feeds.delicious.com/v2/rss/" + feedUser + "?count=100", function(json, status){
		
		jsonObj = json;
		
		feedSize = $(json).length;
		currentMax = 20;
		
		//alert(feedSize);
		
		if (feedSize < currentMax) {
			disableBig();
		}
	
		var content = "";
		$.each(json, function(i){
			//console.debug(json);
			deliciousLink = "<div class=\"site\"><a href=\"" + this['link'] + "\" target=\"_blank\"><div class=\"title\">" + this['title'] + "</div></a><div class=\"thumb\"><img src=\"http://www.artviper.net/screenshots/screener.php?url=" + this['link'] + "&w=240&h=180&sdx=1024&sdy=768&q=75&userID=e334b93abe241341\" alt=\"\" /></div><div class=\"loading\">loading website</div></div>";
			content += deliciousLink;
			if (i==19) { return false; }
		});
		$('#contents').html(content);
		$('.site a').css('opacity','0');
		$('.site a').mouseover(function(){
			$(this).css('opacity','.75');
		});
		$('.site a').mouseout(function(){
			$(this).css('opacity','0');
		});
		var countFirstLoad = $('#contents .site').size();
		if (countFirstLoad == 0) {
			$('#contents').html('<img id="nointernet" src="http://www.internetisbig.com/images/youhavenointernet.png" />');
			$('#makeitbigger').html('');
		}
		if (countFirstLoad > 0) {
			if (countFirstLoad < 20) {
				$('#makeitbigger').html('<img src="http://www.internetisbig.com/images/internetissmall.png" />');
			} else {
				$('#makeitbigger').html('<img src="http://www.internetisbig.com/images/makeitbigger.png" alt="makeitbigger" width="216" height="45" border="0" />');
			}
		}
	});
}

disableBig = function() {
	active = 0;
	$('#makeitbigger').click(function() { });
	$('#makeitbigger').css('cursor','default');	
	$('#makeitbigger').mouseover(function() { $(this).css('opacity','1'); });
	if (feedSize >= 99) {
		$('#makeitbigger').html('<img src="http://www.internetisbig.com/images/internetishuge.png" /><br /><br /><span>I\'m looking for programmers to help make it bigger &mdash; <a href="mailto:jasonsack@gmail.com">email me</a></span>');
	} else {
		$('#makeitbigger').html('<img src="http://www.internetisbig.com/images/internetissmall.png" />');
	}
}

// MAKE THAT SHIT BIG //
makeItBigger = function() {
	if (active == 1) {
		var content = "";
		$.each(jsonObj, function(i){
			if(currentMax < feedSize) {
				if (currentMax < 100) {
					if (i>(currentMax-1)) {
						deliciousLink = "<div class=\"site\"><a href=\"" + this['link'] + "\" target=\"_blank\"><div class=\"title\">" + this['title'] + "</div></a><div class=\"thumb\"><img src=\"http://www.artviper.net/screenshots/screener.php?url=" + this['link'] + "&w=240&h=180&sdx=1024&sdy=768&q=75&userID=e334b93abe241341\" alt=\"\" /></div><div class=\"loading\">loading website</div></div>";
						content += deliciousLink;
					}
					if (i==((currentMax-1)+20)) { return false; }
				} else {
					disableBig();
				}
			} else {
				disableBig();
			}
		});
		if (active == 1) {
			currentMax += 20;
			$('#contents').append(content);
			$('.site a').css("opacity","0");
			$('.site a').mouseover(function(){
				$(this).css("opacity",".75");
			});
			$('.site a').mouseout(function(){
				$(this).css("opacity","0");
			});
		}
	}
}
