$.fn.cycle.defaults.timeout = 12000;
var displayProjectSamples = function(){
	var $pages = $('.scrollm ul').length;
	$('.navx, .pagecount').remove();
    if ($pages > 1) {
 		$('.scrollm').after('<div class="navx"><a href="#" class="pre" title="Previous software">Previous</a> <a href="#" class="nex" title="Next software">Next</a></div><span class="pagecount main">1/'+$pages+'</span></span>');
	}
	$('.scrollm').cycle(
	    { fx: 'scrollHorz', 
	    timeout: 0, 
	    prev: '.pre', 
	    next: '.nex', 
	    cssBefore: { opacity: 1 }, 
	    animOut: { opacity: 0 },
	    animIn: { opacity: 1 }, 
	    sync: 'true', 
	    prevNextClick: 
	        function(i, index, s){$("span.pagecount.main").html(index+1 + "/" + $pages);} 
	    });
	
    	$('.scrollm ul li .element .media .screen:not(:first-child)').remove();
	$('.scrollm ul li').hover(function(){
		$(this).addClass('gloss').fadeIn();
	}, function(){
		$(this).removeClass('gloss');
	});
};

// Load sample data, and display it. 
var projectSamples = function () {
	$('.scrollm ul li, .nav2, span.pagecount.main').remove();
	$('.scrollm').text('Filtering...').load(this.href+' ul', displayProjectSamples);
	$('.scrollv ul li a').removeClass('active');
	$(this).addClass('active');
	return false;
};

var clickTag = function() {$('.scrollv ul li a:eq(0)').click();};

var toggleTagType = function (tagtype) {
        $('.scrollv').load('/search/tags_by_type/'+tagtype, "", clickTag );
        $(".tagtypes > *").removeClass('button');
        var tagselector = "#"+tagtype;
        $(tagselector).addClass('button');
};

var toggleTheFive  = function (whichone, otherone) {
		var whichbutton = "#" + whichone + 'button';
		var otherbutton = "#" + otherone + 'button';
		$("#" + whichone + "five").removeClass('hide');
		$(whichbutton).addClass('button');
		$("#" + otherone + "five").addClass('hide');
		$(otherbutton).removeClass('button');
};

$(function() {
	$('.scrollv ul li a').live('click', projectSamples);
    clickTag();
	
	$('#purpose').live('click', function () {toggleTagType('purpose');});
	$('#alternative').live('click', function () {toggleTagType('alternative');});
	$('#latestbutton').click( function () {  toggleTheFive('latest', 'greatest'); });
	$('#greatestbutton').click( function () { toggleTheFive('greatest', 'latest'); });
});


