/**
	@class
	@description funded ideas
*/
REFRESH_APP.fundedideas  = {
	init: function() {
		this.initMosaic();
		this.initFiltering();	
		
		var filters = ["categories-filter", "amounts-filter", "month-filter"];
		for(var i=0, len=filters.length; i<len; i++) {
			var qsVal = getParameterByName(filters[i]);
			
			if(qsVal !== "") {
				$("#" + filters[i]).val(qsVal)
			}
		}
        this.filterAttr();
		this.getResults();
	},
	/**
	 * Mosaic Behavior
	 */
	initMosaic: function() {
		$(".mosaic > div").hover(
			function(e) {
				$(this).children(".learn-more").show();
			},
			function(e) {
				$(this).children(".learn-more").hide();
			}
		);
	},
	/**
		Filtering 
	*/
	initFiltering: function() {
		$('#month-filter').change(function(){
		    var month = $('#month-filter').children("option:selected").attr('class');
		    var amount = $('#amounts-filter').children("option:selected").attr('class');
		    var category = $('#categories-filter').children("option:selected").attr('class');
		    
		    if(month != amount && month != "all"){
		        $('#amounts-filter').val('0');
		    }
		    if(month != category && month != "all"){
		        $('#categories-filter').val('0');
		    }            
		    
		    REFRESH_APP.fundedideas.filterAttr();
		});
		
		$('.ideas-filter select').change(REFRESH_APP.fundedideas.getResults);
		$('.ideas-filter input[type=checkbox]').click(REFRESH_APP.fundedideas.getResults);
	},
	filterAttr: function(){
	    var month = $('#month-filter').children("option:selected").attr('class');
	    
	    var filter;
	    if (month == "2011"){
	        filter = "2011"
	    } else if(month == "2010") {
	        filter = "2010"
	    } else {
	        filter = "all"
	    }
	    REFRESH_APP.fundedideas.filterDropdown(filter);
	},
	filterDropdown: function(filter){
        if(filter == "all"){
            $('.ideas-filter select').children().show();
        } else {
            $('#amounts-filter option').not('.all').hide();
            $('#categories-filter option').not('.all').hide();
            
            $('#amounts-filter').children("." + filter).show();
            $('#categories-filter').children("." + filter).show();
        }
	},
	showCheckbox: function() {
		if($("#facebook-logout").length != 0 || $("#profile-pcna").length != 0) {
    		$("#ideasihelped-checkbox").show();
        	$("#lbl-ideasihelped-checkbox").show();
    	}
	},
	/**
		Get Results based on filters 
	*/
	getResults: function() {
		var ic = $('#categories-filter').val();
		var im = $('#amounts-filter').val();
		var month = $('#month-filter').val();
		var helped = $('#ideasihelped-checkbox').is(':checked');
		
		if(helped) { helped = 1; } else { helped = 0; }
		
		var wrapper = $('#fundedideas-content');
		wrapper.html('').addClass('loading');
		
		var contentWrapper = $('#fundedideas-results');
		contentWrapper.addClass('loading');
		
		var url = '/grant-recipients/getideas'+'?ic='+ic+'&im='+im+'&month='+month + '&helped=' + helped + "&ajaxCalled=1";
		
		if( $('#recent-updates').length > 0 ) url = '/grant-recipients/getupdates'+'?category=' + ic + '&amount=' + im + '&month=' + month + '&helped=' + helped + "&ajaxCalled=1";
		if( $('#success-stories').length > 0 ) url = '/grant-recipients/getstories'+'?month='+month + '&helped=' + helped + "&ajaxCalled=1";
		
		$.ajax({
			url: url,
			type: "GET",
			dataType: "html",
			success: function(html){
				if(html != "") {
					contentWrapper.removeClass('loading');
					wrapper.removeClass('loading').html(html);
				} else {
					wrapper.removeClass('loading').html('<p class="load-no-result">No matching ideas.</p>');
				}
			},
			error: function() {
				wrapper.removeClass('loading').html('<p class="load-no-result">An error has occurred, please, try again.</p>');
			}
		});
	}
}

REFRESH_APP.leaderboard  = {
	init: function() {
		this.getResults();
	},

	getResults: function() {
		
		var wrapper = $('#leaderboard-content');
		wrapper.html('').addClass('loading');
		
		var url = '/leaderboard/loadmore?pos=0&size=10&c=' + Math.random();

		$.ajax({
			url: url,
			type: "GET",
			dataType: "html",
			success: function(html){
				wrapper.removeClass('loading').html(html);
			},
			error: function() {
				wrapper.removeClass('loading').html('<p class="load-no-result">An error has occurred, please, try again.</p>');
			}
		});
	}
}

REFRESH_APP.finalists  = {
	init: function() {
		this.getResults();
	},

	getResults: function() {
		
		var wrapper = $('#finalist-content');
		wrapper.html('').addClass('loading');
		
		var url = '/finalists/loadmore?pos=0&size=10';

		$.ajax({
			url: url,
			type: "GET",
			dataType: "html",
			success: function(html){
				wrapper.removeClass('loading').html(html);
			},
			error: function() {
				wrapper.removeClass('loading').html('<p class="load-no-result">An error has occurred, please, try again.</p>');
			}
		});
	}
}

REFRESH_APP.loadIdeas = {
		
	/**
		init load more
	*/
	init: function() {
		$('#load-more').live('click',REFRESH_APP.loadIdeas.loadMore);
		REFRESH_APP.loadIdeas.bgFollower();
	},
	
	/**
		Load More button event
	*/	
	loadMore: function(e) {
		e.preventDefault();
		var href = this.href;
		$(this).addClass('loading');
		$.post(href+'&ajaxCalled=1', REFRESH_APP.loadIdeas.loadMoreOnLoad);
	},
	
	/**
		Load More button onLoad event
	*/	
	loadMoreOnLoad: function(data) {
		if(data) {
			var obj = $('#load-more');
			obj.before(data).remove();
		}
	},
	
	/**
		BG FOLLOWER
	*/
	bgFollower: function(){
	
		var obj = $('.ideas-row');
		if(obj.length < 1) return;
	
		obj.live('mouseover', function(){
			var finalPos = $(this).position().top;
			rowBg.animate({
	          top: finalPos
	    	}, 70);
		});
		
		var rowBg = $('#rowHighlight');
		var defPos = $('.ideas-row:first').position().top;
		
		var defHeight = $('.ideas-row:first').height();

		// SET STYLES
		rowBg.css({
			"height": defHeight,
			"width": "100%",
			"position": "absolute",
			"top": defPos,
			"left":"0px",
			"background-color": "#DFFDFF",
			"zIndex": "1"
		});

	}

};

/**
	@class
	@description Special Case for additional winner
*/
REFRESH_APP.additionalWinner = {
	init: function() {
		var container = $("div.ideas-row").eq(2);
		container = $(container).find("div.idea-small").eq(0);
		link = $(container).find("a").attr("href");
		$(container).find("a").removeClass("idea-link");
		$(container).find("a").addClass("idea-link-special");
		var span = $(container).find("span").eq(0).html($(container).find("span").eq(0).text() + " <a href='#special' class='arrow'>*</a> ");
		$("span.arrow").css({"font-size":"18px","display":"inline","padding-top":"5px"});
		
		$(container).find("a").click(function(e) {
			e.preventDefault();
		});
		
		$(container).find("span.idea-content").hover(function() {
			$(this).css("text-decoration","underline");
			$(this).css("color","#fff");
		}, function() {
				$(this).css("text-decoration","none");
			
		});
		
		$(container).find("span.idea-content").click(function() {
			location.href = link;
		});
		
		$(container).find("span.idea-metadata a").click(function() {
			location.href = "#special";
		
		});
	}
}

jQuery(function($) {

	REFRESH_APP.loadIdeas.init();
	
	new PEP.Tooltip();

	if($("body.fundedideas").length != 0) {
		REFRESH_APP.fundedideas.init();
	}
	
	if($("body#leaderboard").length != 0) {
		REFRESH_APP.leaderboard.init();
	}
	
	if($("body#finalists").length != 0) {
		REFRESH_APP.finalists.init();
	}
	
});
