//function to find out which category is being moved
function getCategoryMovedId(oldArr, newArr){
    // find out which category has been moved
    var oldLen = oldArr.length;
    var newLen = newArr.length;
    var cnt  = oldLen >= newArr.length ? oldLen : newLen;
    var movedCatId;

    if(oldLen > newLen){ // one box has been moved to left
        for(i = 0; i < cnt; i++ ){
            if(oldArr[i] != newArr[i]){
                movedCatId = oldArr[i];
                break; // skip out since we've found the id
            }
        }
    }else if(newLen > oldLen){ // one box has been added
        for(i = 0; i < cnt; i++ ){
            if(newArr[i] != oldArr[i]){
                movedCatId = newsArr[i];
                break; // skip out since we've found the id

            }
        }
    }
    oldArr = new Array();
    newArr = new Array();
    return movedCatId;
}

// function to update the orientation in drag and drop
function updateOrientation(arrIdsLt, arrIdsRt, prefix, path){
    var url = path+'processPersonalize.php';
    var params = {
        left_cats : arrIdsLt,
        right_cats : arrIdsRt,
        prefix : prefix,
        personalize_mode : 'setOrientation'
    };

    // Go to Update and Save in the cookie file
    $.ajax({
        url: url,
        type: 'POST',
        data: params,
        async: false,
        dataType:'json',
        success: function(){}
    });
}

// function to set the cookie with default values if not set for the first time
function setDefaultCookie(arrLeft, arrRight, prefix, homepage, theme, path){
    var url = path+'index.php';
    var params = {
        left_cats : arrLeft,
        right_cats : arrRight,
        prefix : prefix,
        cookie_for: prefix,
        chk_def_home: homepage,
        chk_theme : theme,
        personalize_mode : 'personalize_add'
    };

    // Go to Update and Save in the cookie file
    $.ajax({
        url: url,
        type: 'POST',
        data: params,
        async: false,
        dataType:'json',
        success: function(){}
    });
}

// generalized function to sort the categories left and right
function makeCategorySortable(sortSource, sortTarget, sortHandle, sortItems, prefix, path){
    var oldArr = new Array();
    var newArr = new Array();

    $('#'+sortSource).sortable({
        connectWith: '#'+sortTarget,
        cursor: 'move',
        handle: '.'+sortHandle,
        items: '.'+sortItems,
        revert: true,
        cancel:'fixed',
        forceHelperSize: true,
        forcePlaceholderSize:true,
        start:function(event, ui) {
            $('#'+sortSource+' .'+sortItems).each(function(){
                oldArr.push($(this).attr('rel'));
            });
        },
        stop: function(event, ui) {
            var csvIdsRt = "";
            var csvIdsLt = "";

            $('#'+sortSource+' .'+sortItems).each(function(){
                newArr.push($(this).attr('rel'));
                csvIdsRt += $(this).attr("rel")+",";
            });
            var movedCatId = getCategoryMovedId(oldArr, newArr);
            // reinit the arrays
            newArr = new Array();
            oldArr = new Array();

            $('#cat'+movedCatId).disable();
            csvIdsRt = csvIdsRt.substring(0,csvIdsRt.length-1);


            $('#'+sortTarget+' .'+sortItems).each(function(){
                csvIdsLt += $(this).attr("rel")+",";
            });
            csvIdsLt = csvIdsLt.substring(0, csvIdsLt.length-1);

            var isCookieEnabledVarOld = navigator.cookieEnabled;
            if(isCookieEnabledVarOld){
                if(sortSource == 'right-col'){
                    updateOrientation(csvIdsLt, csvIdsRt, prefix, path);
                }else{
                    updateOrientation(csvIdsRt, csvIdsLt, prefix, path);
                }
            }
        }
    }).enableSelection();
}

function resetCookies(prefix, path){
    var url = path+'index.php';
    var params = {
        prefix : prefix,
        personalize_mode : 'personalize_reset'
    };

    // Go to Update and Save in the cookie file
    $.ajax({
        url: url,
        type: 'POST',
        data: params,
        async: false,
        dataType:'json',
        success: function(){ }
    });
}

