var StolabConfig = {
  'Produktsida': {                                              // id p? body-taggen f?r inst?llningar per sida (<body id="product-detail"> )
    speed: 1200, 
    fields: {}
  },
  'Produktlista': {                                              // id p? body-taggen f?r inst?llningar per sida (<body id="product-detail"> )
    speed: 1200, 
    fields: {}                                            // justera tiden f?r bl?ddrandet (default: 1200 ms)
  },
  'Serier': {                                              // id p? body-taggen f?r inst?llningar per sida (<body id="product-detail"> )
    speed: 1200, 
    fields: {}                                            // justera tiden f?r bl?ddrandet (default: 1200 ms)
  },
  'moebler': {
    speed: 1200,
    fields: {
      ".images_column": "mobler/mobler-bild.aspx",
      ".info_column": "mobler/mobler-relaterade.aspx"
    }
  },
  'Textsida': {
    speed: 1200, 
    fields: {
      ".images_column": "mobler/mobler-bild.aspx",
      ".info_column": "mobler/mobler-relaterade.aspx"
    }
  },
  'HTML': {
    speed: 1200, 
    fields: {
      ".images_column": "mobler/mobler-bild.aspx",
      ".info_column": "mobler/mobler-relaterade.aspx"
    }
  },
  'Formgivare': {
    speed: 1200, 
    fields: {
      "#formgivare_detail": "/formgivare/form-text.aspx", 
      "#category_wrapper": "/formgivare/form-produkter.aspx"
    }
  },
  'Nyhetsarkiv': {
    speed: 1200, 
    fields: {}
  },
  'Nyhet': {
    speed: 1200, 
    fields: {}
  },
  fadeTo: 0.4,                                                 // inst?llningar f?r loading (visas om det g?r on?digt l?ngsamt att ladda bilderna, s? anv?ndaren f?r direkt feedback)
  loadingImgSrc: 'images/loading.gif'
}

/*
 * stolab.se
 * 
 * Author:
 *   fkw.se
 * 
 * Requires:
 *   - jQuery
 *   - jQuery Scrollable
 *
 */

var Stolab = {
  type: '',
  load: false,
  api: null,
  areaLoadCount: 0,
  init: function () {
    var speed = 1200;
    var type = $('body').attr('id');
    if (type != 'Hem'){
      if(StolabConfig[type] && StolabConfig[type].speed){
        speed = StolabConfig[type].speed;
      }
      var s = $("div.scrollable").scrollable({
        speed: speed,
        clickable: true, 
        size: Stolab.sliderSize(), 
        api: true, 
        circular: true
      });
      Stolab.addObservers();
      Stolab.type = type;
      Stolab.api = s;
    }
    $('.scrollnav .back').hide();
  },
  nextObserver: function(e){
    e.preventDefault();
    if(!Stolab.load){
      var current = Stolab.api.getClickIndex(), next = current+1;
      if(!Stolab.last()){
        $('.items img:eq('+ next +')').click();
      }
    }
    Stolab.unbindNextBack();
    return false;
  },
  last: function(){
    return Stolab.api.getSize() == Stolab.api.getClickIndex()+1;
  },
  first: function(){
    return Stolab.api.getClickIndex() == 0;
  },
  backObserver: function(e){
    e.preventDefault();
    var c = Stolab.api.getClickIndex(), previous = c-1;
    if(!Stolab.load){
      if(!Stolab.first()){
        $('.items img:eq('+ previous +')').click();
      }
    }
    Stolab.unbindNextBack();
    return false;
  },
  bindNextBack: function(){
    $('.scrollnav .next').bind('click', Stolab.nextObserver);
    $('.scrollnav .back').bind('click', Stolab.backObserver);
  },
  unbindNextBack: function(){
    $('.scrollnav .next').unbind('click');
    $('.scrollnav .back').unbind('click');
  },
  addObservers: function () {
    $('.items img.slide').live('click', function(e){
      e.preventDefault();
      var l = $(this), count = 0;
      $('.loading').remove();
      for (var i in StolabConfig[Stolab.type]['fields']) count++;
      Stolab.areaLoadCount = count;
      $.each(StolabConfig[Stolab.type]['fields'], function(f, p){
        $(f).css('display', 'block');
        var i = l.attr('id').replace('p', '');
        Stolab.rep(p, f, {id: i});
      });
    });
    Stolab.bindNextBack();
    $('#slide img').hover(function() {
      $(this).stop().fadeTo("fast", 0.6);
    }, function() {
      $(this).stop().fadeTo("fast", 1);
    });
  }, 
  loading: function (e) {
    if(Stolab.load){
      $(e).stop().fadeTo("fast", StolabConfig.fadeTo);

      var loader = $('<div id="' + e.replace(/[#.]/, '') + '-loading" class="loading"><img src="'+StolabConfig.loadingImgSrc+'" /><span>Laddar</span></div>').css(Stolab.coordinates(e));
      $('body').append(loader);
    }
  },
  sliderSize: function () {
    var m = $('#slide').width() % $('#slide img:last').width(), s = $('#slide').width() / $('#slide img:last').width();
    return Math.round(s);
  },
  coordinates: function(el){
    // get the center point of the container!
    var l = $('.loading'), el = $(el);
    return {
      position: 'absolute', 
      left: el.offset().left + el.width()/2 - l.width()/2, 
      top: el.offset().top + el.height()/2 - l.height()/2
    };
  },
  rep: function(u, e, d){
    Stolab.load = true;
    window.setTimeout("Stolab.loading('"+e+"')", 1000); 
    $.ajax({ 
      url: u, 
      data: d,
      success: function(data){
        Stolab.apply(data, e);
        Stolab.load = false;
      }
    });
  }, 
  apply: function(d, e){
    $(e).fadeOut(400, function(){
      $('#' + e.replace(/[#.]/, '') + "-loading").remove();
      $(this).empty().append(d);
      if(Stolab.last()){
        $('.scrollnav .next').hide();
      }else if(Stolab.first()){
        $('.scrollnav .back').hide();
      }      
      $(this).fadeIn(400, function(){
        $(e).fadeTo("fast", 1);
        if(Stolab.areaLoadCount==1) {
          Stolab.bindNextBack();
        }else{
          Stolab.areaLoadCount = Stolab.areaLoadCount-1;
        }
      });
    })
  }
};


$(document).ready(function() {
  Stolab.init();
});
