/**
 * array with ids of selected tags
 */
 var selectedTags = new Array();
 var removedTags  = new Array();
/**
 * Type value for tags grid filtering
 */
var filterTagsTypeValue = '';
/**
 * Tag name value for tags grid filtering
 */
var filterTagsNameValue = '';

/**
* Filters the tags grid using the type id
*/
function filterTagsGridByType(typeId){

    //resets the filtering
    if(typeId == '0'){
        filterTagsTypeValue = '';
    //filters
    }else{
        filterTagsTypeValue = typeId;
    }

    //applies filtering to the grid
    filterTagsHandler();
    
    //reloads the grid
    dhtmlxGridBuildAndLoadQuery(mygrid);
}

/**
* Filters the tags grid using the name of the tag
*/
function filterTagsGridByName(){
    var nameMask = $("#tagsFilterName").val();
    
    //resets the filtering
    if(nameMask == ''){
        filterTagsNameValue = '';
    //filters
    }else{
        filterTagsNameValue = nameMask;
    }

    //applies filtering to the grid
    filterTagsHandler();
    
    //reloads the grid
    dhtmlxGridBuildAndLoadQuery(mygrid);
}



/**
 * Users Tags filters (type and name) and generates the 
 * general filtering string to be used by the grid for
 * loading.
 */
function filterTagsHandler(){
    gridFilterString = '';
    
    if(filterTagsTypeValue != ''){
        gridFilterString = 'typeId='+filterTagsTypeValue;
    }
    if(filterTagsNameValue != ''){
        if(filterTagsTypeValue != ''){
           gridFilterString = gridFilterString + '&';
        }
        gridFilterString = gridFilterString+'name='+filterTagsNameValue;
        
    }
    
    
    
    if(selectedTags.length != 0) {
        $('#selected-values').val(selectedTags);
        if (gridFilterString != '')
            gridFilterString = gridFilterString + '&';
        gridFilterString = gridFilterString+$('#selected-values').serialize();
        if (removedTags.length>0) {
            gridFilterString= gridFilterString+'&removed-values='+removedTags.join(",");
        }
    } else {
        $('#selected-values').val("");
    }
}

function selectTagsWithChildren(tagId,tagValue)
{
    addToSelectedValues(tagId,tagValue)
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url:  baseUrl+'get-children-tags',
        data:"id="+tagId,
        error: function(response) {
            alert("failure");
        },
        success: function(response) {
            $(response).each(function(key,tag){
                addToSelectedValues(tag.id,tag.name)
            })
            //reloads the grid
            filterTagsHandler();
            dhtmlxGridBuildAndLoadQuery(mygrid);            
        }
    });
    
}

/**
 * adds the selected tag to list of selected values to perform the search against
 * and reload the grid for tags 
 * 
 */
 function addToSelectedValues(tagId,tagValue) {
    //adds tagId to array of selected values
    if($.inArray(tagId,selectedTags) == -1) {
        selectedTags.push(tagId);
    
        //adds element to box of selected values
        //url decode and replace + by spaces
        var liHtml = '<li id="'+tagId+'"><span>'+tagValue.replace(/\+/g, ' ')
                     +' <a href="javascript:removeFromSelectedValues('+tagId+')">'
                     +'<img src="'+publicUrl+'/img/icons/remove.png" alt="remove"></a></span></li>';
        $('#selected').append(liHtml);
    }
    
    if($.inArray(tagId, removedTags) != -1) {
        var indexOfTag = $.inArray(tagId,removedTags);
        removedTags.splice(indexOfTag ,1);        
    }
 }
 /**
  * removes the tag from selected values 
  */
 function removeFromSelectedValues(tagId) {
    //removes tagId from array of selected values
    if($.inArray(tagId, selectedTags) != -1) {
        var indexOfTag = $.inArray(tagId,selectedTags);
        selectedTags.splice(indexOfTag ,1);
    
        //removes element from the list of selected values
        $('#selected #'+tagId).remove();
        
        if($.inArray(tagId,removedTags) == -1) {
            removedTags.push(tagId);
        }
    }
    //reloads the grid
    filterTagsHandler();
    dhtmlxGridBuildAndLoadQuery(mygrid);
 }
 
 /**
  * checks all checkboxes matched in selector
  */
 function checkAll(selector) {
    $(selector+" :checkbox").attr('checked',true);
 }
 /**
  * unchecked all checkboxes matched in selector
  */
 function uncheckAll(selector) {
    $(selector+" :checkbox").attr('checked', false);
 }
 
 /**
 * check/uncheck all sub items of a node when it is check/uncheck
 */
function onNodeCheck(nodeId,state,tree) {
    tree.setSubChecked(nodeId,state);
}

/**
 * set the value of hidden input with list of checked subitems of tree
 */
function fillHiddenInput(treeObject, hiddenInpuId) {
    var checkedValues = treeObject.getAllChecked().split(",");
    var parentNodes   = treeObject.getAllItemsWithKids().split(",");
    var checkedKids = $.grep(checkedValues, function (a) { 
                                    if ($.inArray(a, parentNodes) == -1) 
                                        return a; });
    $("#"+hiddenInpuId).val(checkedKids.join(","));                                    
}

