$(document).ready(function() {

  var horizontal = true;
  var $panels = $('#slider .panel');
  var $container = $('#slider .scrollContainer');
  var $scroll = $('#slider .scroll').css('overflow', 'hidden');
  
  
  // float the panels left if we're going horizontal
  if (horizontal) {
    $panels.css({
      'float' : 'left',
      'position' : 'relative' // IE fix to ensure overflow is hidden
    });
    
    // calculate a new width for the container (so it holds all panels)
    $container.css('width', $panels[0].offsetWidth * $panels.length);
  }

    
  $('#slider .navigation a').click(selectNav);
  
  function selectNav() {
    $(this)
      .parents('ul:first')
        .find('a')
          .removeClass('selected')
        .end()
      .end()
      .addClass('selected');    
  }
  
  var scrollOptions = {
    target: $scroll, // the element that has the overflow
    
    // can be a selector which will be relative to the target
    items: $panels,
    
    navigation: '.navigation a',
    
    // selectors are NOT relative to document, i.e. make sure they're unique
    prev: 'img.left', 
    next: 'img.right',
    
    // allow the scroll effect to run both directions
    axis: 'xy',
    
    onAfter: trigger, // our final callback
    
    //offset: offset,
    
    // duration of the sliding effect
    duration: 800,
    
    // easing - can be used with the easing plugin: 
    // http://gsgd.co.uk/sandbox/jquery/easing/
    easing: 'swing'
  };
  
  if (window.location.hash) {
    trigger({ id : window.location.hash.substr(1) });
  } else {
    $('ul.navigation a:first').click();
  }
  
  $('#slider').serialScroll(scrollOptions);
  $.localScroll(scrollOptions);
  
  function trigger(data) {
    var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
    selectNav.call(el);
  }  
});
var Graph = function() {

/*** 
*  Function for js plot data init
*  now it is useless cuz google charts is in use
*  just for history
*
*/  

  var get_colors = function() {
    var colors = {};
    
    $('.table_container tr').each(function(i){
      colors[$(this).attr('id')] = $(this).css('background-color');
    });
    
    return colors;
  }
  
  var get_years = function() {
    var years = {};
    var i = 2002
    
    for(i; i <= 2008; i++) {
      years[i] = (new Date(i + '/01/01')).getTime();
    }
    
    return years;
  }
  
  var get_coords = function() {
    var coords_hash = {};
    years = get_years();
    
    coords_hash = {
      'coal_cell': [[years[2002], 463], [years[2003], 518], [years[2004], 664], 
        [years[2005], 952], [years[2006], 1209], [years[2007], 1794], [years[2008], 2559]],
      'chemistry_cell': [[years[2002], 454], [years[2003], 678], [years[2004], 884], 
        [years[2005], 1147], [years[2006], 1379], [years[2007], 1793], [years[2008], 2536]],
      'food_cell': [[years[2002], 337], [years[2003], 389], [years[2004], 493], 
        [years[2005], 704], [years[2006], 838], [years[2007], 979], [years[2008], 1204]],
      'machines_cell': [[years[2002], 291], [years[2003], 312], [years[2004], 410], 
        [years[2005], 436], [years[2006], 513], [years[2007], 493], [years[2008], 873]],
      'buildmater_cell': [[years[2002], 236], [years[2003], 388], [years[2004], 496], 
        [years[2005], 717], [years[2006], 860], [years[2007], 1139], [years[2008], 1454]],
      'others_cell': [[years[2002], 441], [years[2003], 511], [years[2004], 492], 
        [years[2005], 593], [years[2006], 780], [years[2007], 1013], [years[2008], 1325]]                                        
    };
    
    return coords_hash;
  }
  
  var get_industries = function() {
    var industries = [];
    
    $('.table_container tr').each(function(i){
      if($(this).attr('id') != "") {
        industries.push($(this).attr('id')); 
      }
    });
    
    return industries;
  }
  
  /*
  * public section 
  * draw method creates an object array for $.plot function 
  * and launches it
  *
  */
  this.draw = function() {
    var data_array = [];              //array of objects
    colors = get_colors();            //object 
    coords = get_coords();            //object
    industries = get_industries();    //array
    
    for(ind in industries) {
      data_array.push({
        data: coords[industries[ind]],
        color: colors[industries[ind]],
        shadow_size: 3
      });
    }
    
    $.plot($('#salary_plot'), data_array, { xaxis: { mode: "time" }});
  }
}

/*** 
*  Google Chart
*
*/  

var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
function simpleEncode(valueArray,maxValue) {

var chartData = ['s:'];
  for (var i = 0; i < valueArray.length; i++) {
    var currentValue = valueArray[i];
    if (!isNaN(currentValue) && currentValue >= 0) {
    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
    }
      else {
      chartData.push('_');
      }
  }
return chartData.join('');
}
