function updateExcludedWords()
{
    var excludedWordsId = '#excluded_keywords';

    if ($.trim($('#exact_phrase').val()).length > 0 || $.trim($('#included_keywords').val()).length > 0) {
        $(excludedWordsId).attr("disabled", false);
    }
    else
    {
        $(excludedWordsId).attr("disabled", true);
        $(excludedWordsId).val('');
    }
}

function refreshWithParameter(name, value) {
    var reg = new RegExp(name + "=([^\\&]+)", "g");
    var currentLocation = window.location.href;
    var newLocation;
    var param = name + "=" + value;

    if (currentLocation.match(reg)) {
        newLocation = currentLocation.replace(reg, (param));
    }
    else {
        if (currentLocation.indexOf('?') == -1) {
            newLocation = currentLocation + '?' + param;
        } else {
            newLocation = currentLocation + '&' + param;
        }
    }

    window.location.href = newLocation;
}

function getNextImageUrl(image_id){
    var image = getImageId(image_id, 1)
    verifyPagingButtonsVisibility(image)
    return createImageUrl(image);
}

function getPrevImageUrl(image_id){
    var image = getImageId(image_id, -1)
    verifyPagingButtonsVisibility(image)
    return createImageUrl(image);
}

function createImageUrl(image){
    if(image){
        return '/images/' + image + '?group=' + getUrlParameter('group')
    }else{
        return window.location.href
    }
}

function getImageId(image_id, offset){
    var images = getImageIds();
    if(!images) return null;

    var img_index = findImageArrayIndex(images, image_id) + offset;

    return images[img_index];
}

function getImageIds(){
    var search_id = getUrlParameter('group');
    if(search_id == '') return null;

    if(!getCookie(search_id)) return null;
    var images = getCookie(search_id).split('+');
    return images;
}


function verifyPagingButtonsVisibility(image_id){
    var images = getImageIds();
    if(!images){
        displayNextButton(false);
        displayPrevButton(false)
    }

    if(image_id == images[0]) displayPrevButton(false);
    if(image_id == images[images.length-1]) displayNextButton(false);
}

function findImageArrayIndex(images, element){
    for(var i=0; i < images.length; i++ ){
        if(images[i] == element){
            return verifyIndex(i, images.length - 1);
        }
    }
    return null;
}

function verifyIndex(index, max_size){
    if(index < 0 ) index = 0;
    if(index > max_size) index = max_size;

    return index;
}

function displayNextButton(display){
    var show = '';
    if (!display) show = 'none';
    document.getElementById('btnNextSearchResult').style.display = show;
}

function displayPrevButton(display){
    var show = '';
    if (display==false) show = 'none';
    document.getElementById('btnPrevSearchResult').style.display = show;
}
