var currentColorPosition = 1;

$(document).ready(function(){
  /* Makes all "/ladda-hem"-files open in a new window (ladda hem == download) */
	$('a.historyBack').click(function(ev) {
		ev.preventDefault();
		history.go(-1);
	});

  $('a[rel="print"]').click( function() {
	 	TheNewWin = window.open($(this).attr('href'),'mypop','height=600,width=555,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no');
	  return false;
	});

  //$.scrollTo({top:'=500px', left: '+=0px'}, 500);
  //$.scrollTo({top:'=' + $("#middleBarUp").position().top + 'px', left: '+=0px'}, 500);

  if(typeof($("#toggleMenu").attr('id')) !== 'undefined'){
    $('li.mainMenuItem').hide();
  }
  $("#toggleMenu").live('click', function(ev){
    ev.preventDefault();
    $('li.mainMenuItem').toggle();
  });

  $("#toggleWorkItems").live('click', function(ev){
    ev.preventDefault();
    if($('#menuWorkItems').is(':visible')){
      $('#menuWorkItems').slideUp();
    } else {
      $('#menuWorkItems').slideDown(250);
    }
  });

  if(typeof($("#isPrint").attr('id')) !== 'undefined'){
    window.print();
  }

  tableSetup.init();
});

$(window).load(function() {
  if($('#wr_refererType').val() == 'self'){
    //Get page
    var url = document.location.href;
    var splitURL = url.split('/');
        splitURL.reverse();
    if(splitURL[0] == ''){
      var page = splitURL[1];
    } else {
      var page = splitURL[0];
    }
    if(page !== 'start' && page !== $("base").attr('href')){
      $.scrollTo($("#middleBarUp").position().top, 500);
    }
  }
  image.init();

  $('#slider').nivoSlider({
    effect:'random', //Specify sets like: 'fold,fade,sliceDown'
    slices:15,
    animSpeed:1000, //Slide transition speed
    pauseTime:4000,
    startSlide:0, //Set starting Slide (0 index)
    directionNav:true, //Next & Prev
    directionNavHide:false, //Only show on hover
    controlNav:false, //1,2,3...
    controlNavThumbs:false, //Use thumbnails for Control Nav
    controlNavThumbsFromRel:false, //Use image rel for thumbs
    keyboardNav:false, //Use left & right arrows
    pauseOnHover:true, //Stop animation while hovering
    manualAdvance:false, //Force manual transitions
    captionOpacity:0.8, //Universal caption opacity
    beforeChange: function(){
      /*$('body').animate({
          'backgroundColor' : color.getNextColor()
      }, 1500);*/
    },
    afterChange: function(){},
    slideshowEnd: function(){}, //Triggers after all slides have been shown
    lastSlide: function(){}, //Triggers when last slide is shown
    afterLoad: function(){} //Triggers when slider has loaded
  }).
    find('a.nivo-prevNav').text('').end().
    find('a.nivo-nextNav').text('');

});

var menu = {
  initVisible: function(){
    //Get page
    var url = document.location.href;
    var splitURL = url.split('/');
        splitURL.reverse();
    if(splitURL[0] == ''){
      var page = splitURL[1];
    } else {
      var page = splitURL[0];
    }

    if(typeof($('#menu a[href*="/' + page + '"]').attr('href')) !== 'undefined'){
      var $menuItem = $('#menu a[href*="/' + page + '"]');
      $(window).load(function(){
        if($menuItem.parents('ul').hasClass('submenu')){
          $menuItem.parents('#menu li').find('.submenu').show();
        } else {
          $menuItem.parents('#menu li').find('.submenu').slideDown();
        }
      });

      $menuItem.addClass('active');
      /*if($menuItem.parent('li').hasClass('submenu')){
        $menuItem.text($menuItem.text() + ' >');
      } else {
        $menuItem.text($menuItem.text() + ' >');
      }*/
    }
  }
};

var image = {
  init: function(){
    image.showImageText();
    $(window).resize(function(){
      image.showImageText();
    });
  },

  showImageText: function(){
    $('div.imageText').remove();
    $("img.imageText").each(function(){
      var thisWidth = $(this).width() - 5;
      var $wrapper = $('<div>');
          $wrapper.addClass('imageText');
      var tmpTop = $(this).position().top;
          tmpTop += $(this).height() - 25;
      var tmpLeft = $(this).position().left;
          $wrapper.css({width: thisWidth + 'px', opacity: '0.7', top: tmpTop, left: tmpLeft});
          $wrapper.text($(this).attr('title'));
      $(this).after($wrapper);
    });
  }
};

var color = {
  getNextColor: function(){
    currentColorPosition++;
    switch(currentColorPosition){
      case 1:
        return '#990202';
        break;
      case 2:
        return '#395484';
        break;
      case 3:
        return '#408439';
        break;
      case 4:
        return '#ccf249';
        break;
      default:
        currentColorPosition = 0;
        return color.getNextColor();
        break;
    }
  }
};

var tableSetup = {
  value: {

  },

  init: function(){
    this.initOnce();
  },

  initOnce: function(){
    if(this.value.initOnce == 1){
      return false;
    }

    if(typeof($('table.tablesorter')) !== 'undefined'){
      tableSetup.indexFirstColumn();
    }

    this.value.initOnce = 1;
    return true;
  },

  indexFirstColumn: function(){
    // This function is for sorting tables but leaving the first row (index-ed) as it is
    $(function() {
        // add new widget called indexFirstColumn
        $.tablesorter.addWidget({
            // give the widget a id
            id: "indexFirstColumn",
            // format is called when the on init and when a sorting has finished
            format: function(table) {
                    // loop all tr elements and set the value for the first column
                    for(var i=0; i <= table.tBodies[0].rows.length; i++) {
                            $("tbody tr:eq(" + (i - 1) + ") th:first",table).html(i);
                    }
            }
        });
    });
  }
};
