// !Document Ready
jQuery(document).ready(function(){
	  
	  var m = document.location.href.match(/\/#([a-z0-9]+)/i);
	  	if (m != null) {var id = m && m[1];} else {var id = "";}
	   
  	if (id != "") {
  	// var id contains our URL ID. Now load it into #col3.
		var form_data = {
			id: id,
			ajaxmode: "1"
		};
		
		$('#col3').fadeOut('fast', function() {
			// Add the Ajax loader
			$('#col3').html('<div class="ajaxloader"></div>');
		});
		$('#col3').fadeIn('fast');
		$('.resultselected').removeClass('resultselected');
		$(this).parent().parent().addClass('resultselected');
		$.ajax({
			url: base_url + 'code/view',
			type: 'POST',
			data: form_data,
			success: function(msg) {
				$('#col3').fadeOut('fast', function() {
					$('#col3').html(msg);
					$('#col3').fadeIn('fast');
				});
			}
		});
	};		

	/* Fix to make #col2 take up 100% of REMAINING vertical height */
	$("#col3").height($(window).height()-$('#col1').height()-20);
	$("#col2").height($(window).height()-$("#tabscontainer").height()-0);
	$(".nextcontent").height($('#col2').height()-$('#tabscontainer').height()-$("#hcadspace").height());	
	/* Now make that refresh when the window resizes */
	$(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {
		$("#col2").height($(window).height()-$("#tabscontainer").height()-20);
		// Do something similar to .nextcontent
		$(".nextcontent").height($('#col2').height()-$('#tabscontainer').height()-$("#hcadspace").height());	
	}	
	
	// Initialise the jQuery TimeAgo plugin
	$("abbr.timeago").timeago();
	
	// Check for new awards on page load
	checkawards();
	
	// Format the Code Switcher - live, so it works on Ajax loads
	$('.nowcodedark').live('click',function() {
		$('code').removeClass('codedark').addClass('codelight');
		$('.codeswitcher').removeClass('nowcodedark').addClass('nowcodelight');
	});
	$('.nowcodelight').live('click',function() {
		$('code').removeClass('codelight').addClass('codedark');
		$('.codeswitcher').removeClass('nowcodelight').addClass('nowcodedark');
	});
	
	// Search bar default text
	$('input#searchinput').val('Search...').css("color", "#999999").focus(function() {
		$(this).css('color', "#222");
		if ($(this).val() == "Search...") {
			$(this).val('');
		}
	});
		
	// !Homepage View scripts
	
	$('#newesttab').addClass('selectedtab'); 
	$('.nextcontent').addClass('newestcol');
	
	$('#newesttab').click(function() {
	
		$('li').removeClass('selectedtab');
		$('#newesttab').addClass('selectedtab');
		$('.nextcontent').removeClass('featuredcol topratedcol').addClass('newestcol');
			
		$(".nextcontent").fadeOut("fast", function() {
			$.ajax({
				url: base_url + "code/newest",
				success: function(msg) {
					$('.nextcontent').html(msg);
					$(".nextcontent").fadeIn("fast");
					_gaq.push(['_trackPageview', '/code/newest/ajax']);
				}
			})
		});
	});
	
	$('#featuredtab').click(function() {
		$('li').removeClass('selectedtab');
		$('#featuredtab').addClass('selectedtab');
		$('.nextcontent').removeClass('newestcol topratedcol').addClass('featuredcol');
					
		$(".nextcontent").fadeOut("fast", function() {
			$.ajax({
				url: base_url + "code/featured",
				success: function(msg) {
					$('.nextcontent').html(msg);
					$(".nextcontent").fadeIn("fast");
					_gaq.push(['_trackPageview', '/code/featured/ajax']);
				}
			})
		});
	});

	$('#topratedtab').click(function() {
		$('li').removeClass('selectedtab');
		$('#topratedtab').addClass('selectedtab');
		$('.nextcontent').removeClass('featuredcol newestcol').addClass('topratedcol');
	
		$(".nextcontent").fadeOut("fast", function() {
			$.ajax({
				url: base_url + "code/toprated",
				success: function(msg) {
					$('.nextcontent').html(msg);
					$(".nextcontent").fadeIn("fast");
					_gaq.push(['_trackPageview', '/code/toprated/ajax']);
				}
			})
		});
	});

     
});

// !checkawards plugin 
// Set the interval to run (5 seconds)
setInterval(checkawards, 5000);
	
function checkawards() {
	/*$.ajax({
		url: base_url + "auth/checkawards",
		success: function(msg) {
			if (msg != "") {
				$('#awards').html(msg).animate({right: '-=226'}, 500).delay(8000).animate({right: '+=226'}, 100);
			}
		}
	}) */
};

// !loadMore loads more snippets when scrolled to the bottom
function loadMore(fetchkind) {	
	
	var form_data = {
		currentpage: newpage,
		kind: fetchkind
	};
	
	//$('.nextcontent').append('<div class="ajaxloader" id="code-row-load"></div>');
	$('.morebutton').html('<div class="ajaxloader"></div>');

	$.ajax({
		url: base_url + "code/loadmore",
		type: 'POST',
		data: form_data,
		success: function(msg) {
			$('.nextcontent').append(msg); 	
			$('.ajaxloader').remove();
		}
	});

};


