/**
 * Created by JetBrains PhpStorm.
 * User: Yankee
 * Date: 01.06.11
 * Time: 22:17
 * To change this template use File | Settings | File Templates.
 */

function merge_options(obj1,obj2){
    var obj3 = {};
    for (attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (attrname in obj2) { obj3[attrname] = obj2[attrname]; }

    return obj3;
}

function radioButtonSpanClick(){
    inp = $(this).prev();
    inp.closest('div.pollblock-noa').find('input:checked').prop('checked', false).trigger('change');
    inp.prop('checked', true).trigger('change');
}
function radioButtonChange(){
    if($(this).is(':checked'))
       $(this).next().addClass('active');
    else
       $(this).next().removeClass('active');
}

function pollSubmit(){
  sub_form = $(this).closest('form');
  link = sub_form.attr('action');

  el = sub_form.find('input:checked');
  if(el.length){

      $.ajax({
          url:  link,
          type: 'POST',
          dataType: 'html',
          data: {variant: el.val()},
          success: function(data){
                        $('#poll').html(data);
                     }
      })
  }
  return false;
}

function piegraph(element){
    graph_data = eval($(element).children('.data').html());

    options    = $(element).children('.options').html();

    label_options  = {
                        show: true,
                        radius: 3/4,
                        formatter: piegraph_label,
                        background: {
                            opacity: 0.5,
                            color: '#000'
                        }
                     };
    
    if(options){
        options    = eval(options);
        label_options = merge_options(label_options, options[0]);
    }


    $.plot( $(element), graph_data, {
        series: {
                    pie: {
                        show: true,
                        radius: 1,
                        label: label_options
                    }
                },
        legend: {
            show: false
        }
    });
}

function piegraph_label(label, series){
    return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+label+'<br/>'+Math.round(series.data[0][1])+'</div>';
}


function piegraph_only_label(label, series){
    return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+label+'</div>';
}


function zeroTimeStationClick(element){
    jqElement = $(this);
    if(!jqElement.hasClass('active')){
        jqActiveStation = $('div.zerotime-list li.active');
        panActive = jqActiveStation.closest('td');
        panClicked = jqElement.closest('td');
        if(panClicked.hasClass('collapsed')){
            panActive.addClass('collapsed');
            panClicked.removeClass('collapsed');
        }
        jqActiveStation.removeClass('active');
        jqElement.closest('li').addClass('active');
    }
    return false;
}

$(function(){
  $('a#hide-overlay').click(function(){ $('div#overlay').hide(); return false;})
  $('a#show-overlay').click(function(){ $('div#overlay').show(); return false;})

  $('div.pollblock-noa input').live('change', radioButtonChange);
  $('div.pollblock-noa span').live('click',   radioButtonSpanClick);
  $('div.pollblock-noa form.ajaxed input[type="submit"]').click(pollSubmit);

  $("div.list-files h2").live("click", function(){
        $("div.list-files div.opened").removeClass("opened");
        $(this).parent().addClass("opened");
        return false;
    });

  $("div.list-files select").change(function(){
        wrap = $(this).closest("div.wrap");
        cat = wrap.children("h2").attr("id");
        cat = cat.replace('category_','');
        year = $(this).val();
        content = wrap.children("div.files-content");
        $.ajax({
            url: "/documents/list-files",
            type: "GET",
            dataType: "html",
            data: {year: year, category_id: cat},
            beforeSend: function() { content.css("opacity","0.7"); },
            complete: function() {content.css("opacity","1");},
            success: function(data) { content.children("table").replaceWith(data); }

        });

        return false;
  });

  $("div#schedule-search select[name=\"direction\"]").change(function() {
        direction = $(this).val();
        $.ajax({
           url: "/schedule/stations/"+direction,
           type: "GET",
           dataType: "html",
           success: function(data) {
               $("div#schedule-search select[name=\"from\"]").html(data);
               $("div#schedule-search select[name=\"to\"]").html(data);
           }
        });

  });
  $("div#schedule-search input[name=\"on-date\"]").datepick({ showTrigger: "#dateselect" });

  $("div.zerotime-list a.station").live('click', zeroTimeStationClick);

  $('div.pie-graph').each(function(){
    piegraph(this);
  });

    $('a.modal-select').live('click',function(){
        window.selected = null;
        link = $(this);
        $("#modal-holder").jqm({ajax: link.attr('href'),
            onLoad: function(hash){
                alt = link.attr('alt');
                if(alt)
                      alt = '<h3>'+alt+'</h3>';
                hash.w.prepend(alt);
                hash.w.append('<div style="text-align: right;"> <a href="#" class="jqmClose button">Закрыть</a></div>');
            },
            onHide: function(hash){
                modal_window_closed(link);
                hash.o.remove();
                hash.w.hide();
            }
        }).jqmShow();
        return false;


    });

});
