function flexigridDelete(com, grid) {
    if (confirm('Delete ' + $('.trSelected',grid).length + ' items?')) {
        var ids = [];
        var i = 0;
        $('.trSelected', grid).each(function () {
            ids[i++] = this.id.replace("row", ""); //<?php //Remove the "row" thing and only leave the numeric value...?>
        });
        $.post("/admin/manage/delete", {
           model: currentFlexiGridModelName,
           "ids[]": ids
        }, function (data) {
            //FIXME: Lingual improvments...
            if (data == 1) {
                $("#" + currentFlexiGridModelName + "_grid").flexOptions({newp: 1}).flexReload();
            } else {
                alert("no");
            }
        });
    }
}

function flexigridAdd(com, grid) {
    //Redirect to add url...
    document.location = "/admin/manage/add/?model=" + currentFlexiGridModelName;
}

function flexigridEdit(com, grid) {
    document.location = "/admin/manage/edit/?model=" + currentFlexiGridModelName + "&id=" + $('.trSelected', grid)[0].id.replace("row", "");
}

function blogApprove(com, grid) {
    _doApproveOrReject(grid, 1);
}

function blogReject(com, grid) {
    _doApproveOrReject(grid, 0);
}

function _doApproveOrReject(grid, isApprove) {
    var ids = [];
    var i = 0;
    $('.trSelected', grid).each(function () {
        ids[i++] = this.id.replace("row", ""); //<?php //Remove the "row" thing and only leave the numeric value...?>
    });
    $.post("/admin/manage/blogapprove", {
       "ids[]": ids,
       "isApprove": isApprove
    }, function (data) {
        $("#" + currentFlexiGridModelName + "_grid").flexOptions({newp: 1}).flexReload();
        if (data == 1) {
            //Refresh the list...
        } else {
            alert("no");
        }
    });
}

/**
 * Used in search bar text field in header file
 *
 */
function emptyOnClick(message) {
    if ($("#search_bar_text").val() == '') {
		$("#search_bar_text").val(message);
	}
}

function slideHiddenStats() {
    $('#hidden_statbar').toggle('slow', function(){
    	$('#statbar_mid').toggle('slow', function (){ 
    		resizeLeftRight();
    	});
    });
    biggerStatsOpen = !biggerStatsOpen;
    
}

function nextStat() {
    if (currentStat + 1 > maxCatStat) {
        shiftStat(1);
    } else {
        shiftStat(currentStat+1);
    }
}

function previousStat() {
    if (currentStat - 1 < 1) {
        shiftStat(maxCatStat);
    } else {
        shiftStat(currentStat - 1);
    }
}

function shiftStat(newStat) {
    if (biggerStatsOpen) {
        slideHiddenStats();
    }
    $('#statbar_cat_' + newStat).fadeIn(1000);
    $('#statbar_cat_' + currentStat).fadeOut(1000);
    $('#statbar_cat_' + newStat).show();
    $('#statbar_cat_' + currentStat).hide();
    currentStat = newStat;
}

function nextNewsPage() {
	if (currentNewsPage == maxNewsPageCount) {
		shiftNewsPage(1);
	} else {
        shiftNewsPage(currentNewsPage + 1);
    }
}

function previousNewsPage() {
	if (currentNewsPage == 1) {
		shiftNewsPage(maxNewsPageCount);
	} else {
        shiftNewsPage(currentNewsPage - 1);
    }
}

function shiftNewsPage(displayedNewsPage) {
	$('#favbar_mid_' + displayedNewsPage).fadeIn(1000);
	$('#favbar_mid_' + currentNewsPage).fadeOut(1000);
	$('#favbar_mid_' + displayedNewsPage).show();
	$('#favbar_mid_' + currentNewsPage).hide();
	currentNewsPage = displayedNewsPage;
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function resizeLeftRight() {
	var h = getDocHeight();
    $('#left_content').height(h+15);
    $('#left_content_border').height(h);
    $('#right_content').height(h+15);
    $('#right_content_border').height(h);
}
	
$(document).ready(function() {
	resizeLeftRight();
});

//if browser width changed my drag-pull recalculate the height
$(window).resize(function() {
	resizeLeftRight();
});
