﻿var AjaxLib = {
    
    call: function(service, method, array, callback){
        $.ajax({
            type: 'POST',
            url: '/Services/' + service + '.asmx/' + method,
            data: AjaxLib.getJSON(array),
            contentType: "application/json;",
            dataType: 'json',
            success: function(data){
                callback(data.d)
            }
        });
    },
    
    getJSON: function(array){
        if (array.length == 0)
            return '{}';
    
        var json = '{';
        
        for (var i = 0; i < array.length; i = i + 2){
            json += " '" + array[i] + "' ";
            json += ": '" + array[i + 1] + "', ";
        }
        
        json = json.substr(0, json.length - 2);
        json += ' }';
        
        return json;
    }    
};

var Redirect = {

    effect: function(redirectTo) {
        $('#container').fadeOut(500, function() {
            location.href = '/' + redirectTo;
        });
    },

    to: function(redirectTo) {
        window.location.href = 'http://www.mundodohandebol.com.br/' + redirectTo;
    },

    home: function() { Redirect.to('home'); },
    allNews: function() { Redirect.to('noticias'); },
    news: function(id) { Redirect.to('noticia/' + id); },
    allMdh: function() { Redirect.to('selecao-mdh'); },
    mdh: function(id) { Redirect.to('selecao-mdh/' + id); },
    allInformatives: function() { Redirect.to('informativos'); },
    informative: function(id) { Redirect.to('informativo/' + id); },
    allColumnits: function(id) { Redirect.to('columnists.aspx?id=' + id); },
    columnits: function(id) { Redirect.to('columnists.aspx?cid=' + id); },
    allIntervieweds: function() { Redirect.to('entrevistas'); },
    intervieweds: function(id) { Redirect.to('entrevista/' + id); },
    multimedia: function() { Redirect.to('multimidia'); },
    histories: function(c, t) { Redirect.to('historico/' + c + '/' + t); },
    allproducts: function() { Redirect.to('loja-virtual'); },
    products: function (cid) { Redirect.to('loja-virtual/categoria/' + cid); },
    productsb: function (bid) { Redirect.to('loja-virtual/marca/' + bid); },
    viewproduct: function(pid) { Redirect.to('produto-detalhes/' + pid); },
    downloads: function(t) {
        if (t == 't1') Redirect.to('downloads/jogos');
        else if (t == 't2') Redirect.to('downloads/treinamento');
        else if (t == 't3') Redirect.to('downloads/iniciacao');
        else if (t == 't4') Redirect.to('downloads/saude');
        else if (t == 't5') Redirect.to('downloads/estudos-pesquisas');
        else Redirect.to('downloads');
    }
};

var Loading = {
    show: function(){
        $('div.loading').fadeIn(1);
    },
    
    hidden: function(){
        $('div.loading').fadeOut(1);
    }
};

var HistoryController = {

    generateLinks: function() {
        var histories = $('h3[history]');
        for (var i = 0; i < histories.length; i++) {
            histories[i].innerHTML = 'Ѻ histórico';
            if ($(histories[i]).attr('history') == 'informativos')
                histories[i].innerHTML += ' de ' + $(histories[i]).attr('type');
            histories[i].innerHTML += '...';
        }

        histories.click(function() {
            Redirect.histories($(this).attr('history'), $(this).attr('type'));
        });
    }
};
