$(document).ready(function(){
  menu.initVisible();

  /* 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;
	});
});

$(window).load(function() {
  image.init();

  $('#slider').nivoSlider({
    effect:'random', //Specify sets like: 'fold,fade,sliceDown'
    slices:15,
    animSpeed:1000, //Slide transition speed
    pauseTime:5000,
    startSlide:0, //Set starting Slide (0 index)
    directionNav:false, //Next & Prev
    directionNavHide:true, //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(){},
    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
  });

  tableSetup.init();

  if(typeof($("#isPrint").attr('id')) !== 'undefined'){
    window.print();
  }
});

var menu = {
  initVisible: function(){
    //Get page
    var url = document.location.href;
        url = url.replace('http://', '');
    var splitURL = url.split('/');
        //splitURL.reverse();
    var i;
    var pageConcated = '';
    try {
      for(i in splitURL){
        if(splitURL[i].indexOf('.') > 0){
          // Do nothing for this, current item is the domain
        } else {
          pageConcated = pageConcated + '/' + splitURL[i];
        }
      }
    } catch(errno){
      // IE8-fix, no need to do anything
    }

    if(typeof($("#toggleMenuWrapper").attr('id')) !== 'undefined'){
      $(".toggleItem").hide();
    }

    $("#toggleMenu").live('click', function(ev){
      ev.preventDefault();
      $("li.toggleItem").toggle();
    });

    if(pageConcated !== '' && pageConcated !== '/'){
      if(typeof($('#menu a[href$="' + pageConcated + '"]').attr('href')) !== 'undefined'){
        var $menuItem = $('#menu a[href$="' + pageConcated + '"]');
        $menuItem.parents('li').find('.submenu').show();
        $menuItem.addClass('active');
      }
    } else {
      if(typeof($('#menu a[href="/start/"]').attr('href')) !== 'undefined'){
        var $menuItem = $('#menu a[href="/start/"]');
        $menuItem.parents('li').find('.submenu').show();
        $menuItem.addClass('active');
      }
    }
  }
}

var image_v1 = {
  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 image = {
  init: function(){
    image.showImageText();
  },

  showImageText: function(){
    //$('div.imageText').remove();
    $("img.imageText").each(function(){
      var thisWidth = $(this).width();
      var $wrapper = $('<div>');
          $wrapper.addClass('caption');
          $wrapper.css({width: thisWidth + 'px'});
      var thisMarginLeft = $(this).css('marginLeft');
          $wrapper.css({
            marginTop: $(this).css('marginTop'),
            marginRight: $(this).css('marginRight'),
            marginBottom: $(this).css('marginBottom'), 
            marginLeft: $(this).css('marginLeft')
          });
      $(this).wrap($wrapper);
      //var tmpTop = $(this).position().top;
      //    tmpTop += $(this).height() - 25;
      //var tmpLeft = $(this).position().left;
          $(this).after('<br />' + $(this).attr('title'));
      //$(this).after($wrapper);
    });
  }
}

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);
                    }
            }
        });
    });
  }
};
