﻿/* Jquery Functions / widgets / behaviours / etc */



function rerender(target, source) {
    //alert(source);
    $.ajax({
        url: source,
        cache: true,
        beforeSend: function () {
            showLoadingFor(target);
        },
        success: function (html) {
            $(target).html(html);            
            hideLoadingFor(target);
        }
    });        
	
}

function showLoadingFor(target) {
	$(target).addClass('showLoadingOverlay');
	$(target).append('<div class="overlay"><div class="loadingMessage"><img src="/Content/images/spin.gif"/><span>Loading...</span></div></div>');
}
function hideLoadingFor(target){
	$(target).removeClass('showLoadingOverlay');
	$(target).find('.overlay').remove();
}

/* Class Activity behaviours */

function showMoreComments(controller,target,id,lcid) {
	showLoadingFor(target);
	disableShowMore();
	$.get("/"+controller+"LatestComments?id=" + id + "&lcid=" + lcid,
		function (data) {
			hideLoadingFor(target);
			$(target).append(data);
			enableShowMore();
		}
	);
}

function getLastCommentId(){
	return $("ul.lastComments").children().last().attr("id");
}

function disableShowMore(){
	$("#showMoreCommentsAction").hide();
}

function enableShowMore(){
	if($("ul.lastComments").children().last().hasClass("hasMore")){
		$("#showMoreCommentsAction").show();
	}
}



/* Recent activities*/

function showMoreActivities(controller,target,userId, lastActivityId) {
	showLoadingFor(target);
	disableShowMoreActivities();
	$.get("/"+controller+"RecentActivities?id=" + userId + "&lastActivityId=" + lastActivityId,
		function (data) {
			hideLoadingFor(target);
			$(target).append(data);
			enableShowMoreActivities();
		}
	);
}

function getLastActivityId(){
	return $("ul.lastActivities").children().last().attr("id");
}

function disableShowMoreActivities(){
	$("#showMoreActivitiesAction").hide();
}

function enableShowMoreActivities() {
    if ($("ul.lastActivities").children().last().hasClass("hasMore")) {
        $("#showMoreActivitiesAction").show();
    }
}

function downloadManager(Id, filename) {

    // Inicio el download
    window.location.href = filename;

    // Sumo el contador
    var url = "/Challenge/DownloadCounter";
    $.ajax({
        url: url,
        cache: false,
        dataType: "script",
        type: "GET",
        data: "id=" + Id,
        success: function (data) {
        }
    });
}
