var newsExpand     = false;
var imageExpand    = false;
var memberExpand   = false;
var creativeExpand = false;

// Verfeinerungsoptionen einschränken
var newsRefineCategories;
var newsRefineLetters;
var imageRefineCategories;
var imageRefinePeriods;
var memberRefineCountries;
var memberRefineCategories;

$(document).ready(function(){
    $(".toggle-list").css('display', 'none'); 
    
    if ($("#type").attr("value") == 'gs_type_news') {
        toggleElement('#news-results');
    }
    
    if ($("#type").attr("value") == 'gs_type_image') {
        toggleElement('#image-results');
    }
    
    if ($("#type").attr("value") == 'gs_type_member') {
        toggleElement('#members-results');
    }
    
    if ($("#type").attr("value") == 'gs_type_creative') {
        toggleElement('#creative-results');
    }
    
    var searchField = $("#fulltext_keyword");
    var searchFieldDefault = "Suche nach News, Images und Members";
    
    searchField.focus(function() {
        if ($(this).attr("value") == searchFieldDefault) {
            $(this).attr("value", "");
            $(this).addClass("active");
        }
    });
    
    searchField.blur(function() {
       
        if (!$(this).attr("value")) {
            $(this).attr("value", searchFieldDefault);
            $(this).removeClass("active");
        }   
    });
    
    newsRefineCategories   = $("li.news-refine-category");
    newsRefineLetters      = $("li.news-refine-letters");
    //imageRefineCategories  = $("li.image-refine-category");
    imageRefinePeriods     = $("li.image-refine-period");
    memberRefineCountries  = $("li.member-refine-country");
    memberRefineCategories = $("li.member-refine-category");
    
    newsRefineCategories.each(function(i) {
        if (i > 2) { this.style.display = "none"; }
    });
    
    newsRefineLetters.each(function(i) {
        if (i > 2) { this.style.display = "none"; }
    });

    /*imageRefineCategories.each(function(i) {
        if (i > 2) { this.style.display = "none"; }
    });*/
    $(".expand-image-item").each(function(i) {
        this.style.display = "none";     
    });
    
    imageRefinePeriods.each(function(i) {
        if (i > 2) { this.style.display = "none"; }
    });
    
    memberRefineCountries.each(function(i) {
        if (i > 2) { this.style.display = "none"; }
    });
    
    memberRefineCategories.each(function(i) {
        if (i > 2) { this.style.display = "none"; }
    }); 
});

function parentSelect(el, childClass)
{
    if($(el).attr("selected")) {
        $(childClass).attr('selected', 'selected');
    } else {
        $(childClass).removeAttr('selected'); 
    }
}

function radioCheckbox(activeBox)
{
    if ($(activeBox).attr('checked')) { 
        $('input[name=gs_image_period]').attr('checked', false);
        $(activeBox).attr('checked', true);
    } else {
        $(activeBox).attr('checked', false);
    }

}

// Verfeinerungsoptionen erweitern
function expandRefine(refineGroup, el)
{
    var expItems = $(refineGroup); 
    
    expItems.each(function(i) {
        if (this.style.display == "none") {
            this.style.display = "block";
            $(el).html('CLOSE OPTIONS');
        } else {
            this.style.display = "none";
            $(el).html('MORE OPTIONS');
        }
    });
}

function ajaxRequest(type, phrase, category, key, page, limit) 
{
    $.ajax({
        type: "POST",
        url:  "/search/sectionsearch",
        data: "type=" + type + "&phrase=" + phrase + "&category=" + category + "&key=" + key + "&page=" + page + "&limit=" + limit,
        success: function(msg) {
            $("#content").html(msg);
            //scrollTo(0,0);
        }
    });
}

// Akkordeon-Toggle für Verfeinerungsoptionen
function toggleElement(id) 
{
    
    //$(".toggle-list").css('display', 'none');
    
    var el = $(id);
    if (el.css('display') == 'none')
        el.css('display', 'block');
    else
        el.css('display', 'none');
    
}


function checkCharacterCount()
{
    var searchString = $("#fulltext_keyword").attr("value"); 
    
    if (searchString.length > 1) {
        return true;
    } else {
        $("#fulltext_keyword").attr("value", "Suche nach News, Images und Members");
        return false;
    }
    
    
    
}