/*
 * Copyright (c) 2010. Benhauer.pl
 */

function updateApiUnitsLeft() {
    $('#apiUnitsLeft').load(contextPath + '/data/apiUnitsLeft.htm');
}

function updateOptimizedKeywordsLeft() {
    $('#optimizedKeywords').load(contextPath + '/data/optimizedKeywordsUnitsLeft.htm');
}

function hideHelp(closeIco, helpId) {
    $.ajax({
        type: "GET",
        url: contextPath + "/data/hideHelp.htm?helpId=" + helpId,
        success: function() {
            $(closeIco).parent().animate({ opacity: 'hide' }, "slow", function() {
                $(closeIco).parent().remove();
            });
        },
        error: function() {
            $(closeIco).parent().animate({ opacity: 'hide' }, "slow", function() {
                $(closeIco).parent().remove();
            });
        }
    });
}

function popItUp(url) {
    var newwindow = window.open(url, 'adwmini', 'height=640,width=800,toolbar=no,scrollbars=yes,resizable=yes');
    if (window.focus)
        newwindow.focus();

    return false;
}

function markAll(value, selector, suffix) {
    if (value)
        $("" + selector + " :checkbox[id^=" + suffix + "]").attr('checked', 'checked');
    else
        $("" + selector + " :checkbox[id^=" + suffix + "]").removeAttr('checked');

    return false;
}

function copyDown(id, option) {
    var dataCellValue = $(":text[name=" + option + id + "]").val();
    $(":checkbox[id^=selected][checked=true]").each(function(index, e) {
        var elementId = e.id.split('_')[1];
        $(":text[name=" + option + elementId + "]").val(dataCellValue);
    });
    return false;
}

function showButton(id, option) {
    $(".copyDownButton").hide();
    $(".copyDownButton[id=" + option + "Button_" + id + "]").show();
    return false;
}

function zebraRows(selector, className) {
    $(selector).removeClass(className).addClass(className);
}

function makeSortable(tableSelector) {
//grab all header rows
    $(tableSelector + ' thead th').each(function(column) {
        if ($(this).is('.sortable')) {
            $(this).click(function() {
                var findSortKey = function($cell) {
                    return ($cell.find('.sort-key').length > 0 ? $cell.find('.sort-key').val().toUpperCase() : '') + ' ' + $cell.text().toUpperCase();
                };
                var sortDirection = $(this).is('.sorted-asc') ? -1 : 1;

                //step back up the tree and get the rows with data
                //for sorting
                var $rows = $(this).parent().parent().parent().find('tbody tr').get();

                //loop through all the rows and find
                $.each($rows, function(index, row) {
                    row.sortKey = findSortKey($(row).children('td').eq(column));
                });

                //compare and sort the rows alphabetically
                $rows.sort(function(a, b) {
                    if (a.sortKey < b.sortKey) return -sortDirection;
                    if (a.sortKey > b.sortKey) return sortDirection;
                    return 0;
                });

                //add the rows in the correct order to the bottom of the table
                $.each($rows, function(index, row) {
                    $(tableSelector + ' tbody').append(row);
                    row.sortKey = null;
                });

                //identify the column sort order
                $(tableSelector + ' th').removeClass('sorted-asc sorted-desc');
                var $sortHead = $(tableSelector + ' th').filter(':nth-child(' + (column + 1) + ')');
                sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');

                //identify the column to be sorted by
                $('td').removeClass('sorted')
                        .filter(':nth-child(' + (column + 1) + ')')
                        .addClass('sorted');

                $(tableSelector + ' tbody tr td').removeClass('evenRow');
                zebraRows(tableSelector + ' tbody tr:odd td', 'evenRow');
            });
        }
    });
}

function truncate(str, limit) {
    var i;

    var bits = str.split('');
    if (bits.length > limit) {
        for (i = bits.length - 1; i > -1; --i) {
            if (i > limit) {
                bits.length = i;
            }
            else if (' ' === bits[i]) {
                bits.length = i;
                break;
            }
        }
//        bits.push('...');
    }
    return bits.join('');
}
