
/* - search_production.js - */
function getKeyword(){
    var results = jQuery('#field_search_prod').attr('value');
    if (results == 'mots clés' || results == 'stichwort'){
        results = '';
    }
	return results;
}

function getDateStatus(){
	return jQuery('#choix_date').attr("class");
}

function getType(){
	return jQuery('#choix_content').attr('rel');
}

function getAxe(){
    var result = 'undefined';
    jQuery('#menu_axes a').each(function(index) {
        if( jQuery(this).hasClass('selected') == true){
            result = this;
        }
    });
    return result;
}

function getThematique(){
    var result = 'undefined';
    jQuery('#thematiques_list a').each(function(index) {
        if(jQuery(this).hasClass('selected') == true){
            result = this;
        }
    })
    return result;
}

function initParams(){
	params = {};
	params['from'] = 'p';
	return params;
}

function do_selection(type, value){
    if(type != 'sort_order' && type != 'ty'){
        reinit_selection()
    }
    jQuery.data(document.body, "do_selection_value", value);
    
    if(type == 'a'){
        jQuery.data(document.body, "search_axe", search_axe);
        jQuery('#menu_axes a').each(function(index) {
            var value = jQuery.data(document.body, "do_selection_value");
            if(this.rel == value){
                jQuery(this).attr('class', 'selected');
            }
        });
    }
    
    if(type == 't'){
        jQuery('#thematiques_list a').each(function(index) {
            var value = jQuery.data(document.body, "do_selection_value");
            if(this.rel == value){
                jQuery(this).attr('class', 'selected');
            }
        });
    }
    
    if(type == 'ty'){
        var span = '<span id="desc_type">Type</span><span id="choix_content" rel="'+value[0].rel+'">'+value.children('span').html()+'</span>'
        jQuery("#choix_type").html(span);
        jQuery("#choix_type").attr('rel', value[0].rel);
    }
    
    if(type == 'sort_order'){
        jQuery('#choix_date').attr('class', value);
    }
}

function reinit_selection(){
    jQuery('#menu_axes a').attr('class', '');
    jQuery('#thematiques_list a').attr('class', '');
    jQuery('#field_search_prod').attr('value', '');
    jQuery("#choix_type").html('<span id="desc_type">Type</span><span id="choix_content" rel="tous">'+jQuery("#type_list ul li a span.tous").text()+'</span>');
    jQuery("#choix_type").attr('rel', 'tous');
    jQuery('#choix_date').attr('class', 'descending');
}

function search_axe(axe){
    do_selection('a', axe)
	params = initParams();
	params['a'] = axe;
	search(params, 'replace')
}

function search_thematique(thematique){
	params = initParams();
	params['t'] = thematique;
	search(params, 'replace')
}

function search_type(type){
    params = initParams();
    params['s'] = getKeyword();
    params['sort_order'] = getDateStatus();
    
    var axe = getAxe();
    if(axe != 'undefined'){
        params['a'] = axe.rel;
    }
    
    var thematique = getThematique();
    if(thematique != 'undefined'){
        params['t'] = thematique.rel;
    }
    
    if(type != 'tous'){
        params['ty'] = type;
    }
    search(params, 'replace')
}

function search_complete(){
    params = initParams();
    params['s'] = getKeyword();
    params['sort_order'] = getDateStatus();
    
    var axe = getAxe();
    if(axe != 'undefined'){
        params['a'] = axe.rel;
    }
    
    var thematique = getThematique();
    if(thematique != 'undefined'){
        params['t'] = thematique.rel;
    }
    
    var type = getType();
    if(type != 'tous'){
        params['ty'] = type;
    }
    
    search(params, 'replace')
}





function search_more(){
	params = initParams();
	params['pagination'] = true;
	params['s'] = getKeyword();
    params['sort_order'] = getDateStatus();
    
    var axe = getAxe();
    if(axe != 'undefined'){
        params['a'] = axe.rel;
    }
    
    var thematique = getThematique();
    if(thematique != 'undefined'){
        params['t'] = thematique.rel;
    }
    
    var type = getType();
    if(type != 'tous'){
        params['ty'] = type;
    }
    
	search(params)
}




function search_pagination_callback(data){
    jQuery('#pagination_link_productions').remove();
	jQuery('#listing').append(data);
	init_fav_click_production()
	init_pagination_click();
}

function search_replace_callback(data){
    jQuery('#listing').html(data);
    init_fav_click_production()
    init_pagination_click()
}


function beforesendcallback(){
    var template = "<img src='loading_transparent.gif'/></div>"
    var lolilo = jQuery('#pagination_link_productions')
    jQuery('#pagination_link_productions').html(template);
}

function errorcallback(){
    var template = "<img src='fleche.png' alt='Voir toutes les rencontres de l\'ADEUS' /> <a> Page suivante</a><div class='clear'></div>"
    jQuery('#pagination_link_productions').html(template);
}



function search(params, contain){
    callback = search_pagination_callback
    if(contain == 'replace'){
        callback = search_replace_callback
    }
    var params_formatted = jQuery.param(params);
	jQuery.ajax({
   			url: "adeusrecherche",
   			async: true,
   			data: params_formatted,
   			success: callback,
   			beforeSend : beforesendcallback,
   			error: errorcallback
 		});
	
}

function extractUrlParams() {
    var t = location.search.substring(1).split('&');
    var f = [];
    for (var i = 0; i < t.length; i++) {
        var x = t[i].split('=');
        f[x[0]] = x[1];
    }
    return f;
}
function init_fav_click_production(){
    jQuery('a.fav_star').click(function()
    {
        del = false;
        fl = '';
        id = jQuery(this).attr('title');
        params = id
        if(jQuery(this).hasClass('fav_registred'))
        {
            del = true;
            params += '&del=1';
        }
        if(jQuery(this).hasClass('fiche_liaison'))
        {
            fl = ' fiche_liaison';
            params += '&fl=1';
        }
        jQuery.ajax({
            url: "fav_mgmt",
            processData: false,
            async: true,
            data: 'f='+params,
            success: function(data)
            {
                if(del)
                    jQuery('a.fav_star[title='+id+']').attr('class', 'fav_star' + fl);
                else
                    jQuery('a.fav_star[title='+id+']').attr('class', 'fav_star fav_registred' + fl);
            }
        });
        
        return false;
    });
}
function init_pagination_click(){
    jQuery('#pagination_link_productions').click(function(){
       search_more();
    });
}


function init_production_listing(){
    var request_params = initParams();
    var params = extractUrlParams();
    for(var param in params){
        if (param == 'a'){
            request_params['a'] = params[param];
        }
        if (param == 't'){
            request_params['t'] = params[param];
        }
        if (param == 'ty'){
            request_params['ty'] = params[param];
        }
        
    }
    search(request_params, 'replace');
    
    
}









