

$(document).ready(function() {

	// Automatically submit when sorting lists
	$('.sortmenu select').change(function() {
		$(this).closest("form").submit();
	});


	// Hide fallback stuff
	$('.javascript_off').hide();

	// Show javascript stuff
	$('.javascript_on').show();


	

  	// Slide for the front page

		$('.template_front .slideshow-holder').after('<div id="nav" class="nav">').cycle({
			fx:                'scrollLeft', // name of transition effect (or comma separated names, ex: fade,scrollUp,shuffle)
			speed:             'fast',       // speed of the transition (any valid fx speed value)
			timeout:           8000, // milliseconds between slide transitions (0 to disable auto advance)
			pager:             '#nav',       // selector for element to use as pager container
			before:            function() { if (window.console) console.log(this.src); }, // transition callback (scope set to element to be shown):     function(currSlideElement, nextSlideElement, options, forwardFlag)
			after:             function() { $('p.description').html($(this).find("img").attr("rel")); }, // transition callback (scope set to element that was shown):  function(currSlideElement, nextSlideElement, options, forwardFlag)
      pause:             true,         // true to enable "pause on hover"
      pauseOnPagerHover: true          // true to pause when hovering over pager link


			// fx:             'fade', // name of transition effect (or comma separated names, ex: fade,scrollUp,shuffle)
      // timeout:         4000,  // milliseconds between slide transitions (0 to disable auto advance)
      // timeoutFn:       null,  // callback for determining per-slide timeout value:  function(currSlideElement, nextSlideElement, options, forwardFlag)
      // continuous:      0,     // true to start next transition immediately after current one completes
      // speed:           1000,  // speed of the transition (any valid fx speed value)
      // speedIn:         null,  // speed of the 'in' transition
      // speedOut:        null,  // speed of the 'out' transition
      // next:            null,  // selector for element to use as click trigger for next slide
      // prev:            null,  // selector for element to use as click trigger for previous slide
      // prevNextClick:   null,  // callback fn for prev/next clicks:    function(isNext, zeroBasedSlideIndex, slideElement)
      // prevNextEvent:  'click.cycle',// event which drives the manual transition to the previous or next slide
      // pager:           null,  // selector for element to use as pager container
      // pagerClick:      null,  // callback fn for pager clicks:    function(zeroBasedSlideIndex, slideElement)
      // pagerEvent:     'click.cycle', // name of event which drives the pager navigation
      // allowPagerClickBubble: false,  // allows or prevents click event on pager anchors from bubbling
      // pagerAnchorBuilder: null, // callback fn for building anchor links:  function(index, DOMelement)
      // before:          null,  // transition callback (scope set to element to be shown):     function(currSlideElement, nextSlideElement, options, forwardFlag)
      // after:           null,  // transition callback (scope set to element that was shown):  function(currSlideElement, nextSlideElement, options, forwardFlag)
      // end:             null,  // callback invoked when the slideshow terminates (use with autostop or nowrap options): function(options)
      // easing:          null,  // easing method for both in and out transitions
      // easeIn:          null,  // easing for "in" transition
      // easeOut:         null,  // easing for "out" transition
      // shuffle:         null,  // coords for shuffle animation, ex: { top:15, left: 200 }
      // animIn:          null,  // properties that define how the slide animates in
      // animOut:         null,  // properties that define how the slide animates out
      // cssBefore:       null,  // properties that define the initial state of the slide before transitioning in
      // cssAfter:        null,  // properties that defined the state of the slide after transitioning out
      // fxFn:            null,  // function used to control the transition: function(currSlideElement, nextSlideElement, options, afterCalback, forwardFlag)
      // height:         'auto', // container height
      // startingSlide:   0,     // zero-based index of the first slide to be displayed
      // sync:            1,     // true if in/out transitions should occur simultaneously
      // random:          0,     // true for random, false for sequence (not applicable to shuffle fx)
      // fit:             0,     // force slides to fit container
      // containerResize: 1,     // resize container to fit largest slide
      // pause:           0,     // true to enable "pause on hover"
      // pauseOnPagerHover: 0,   // true to pause when hovering over pager link
      // autostop:        0,     // true to end slideshow after X transitions (where X == slide count)
      // autostopCount:   0,     // number of transitions (optionally used with autostop to define X)
      // delay:           0,     // additional delay (in ms) for first transition (hint: can be negative)
      // slideExpr:       null,  // expression for selecting slides (if something other than all children is required)
      // cleartype:       !$.support.opacity,  // true if clearType corrections should be applied (for IE)
      // cleartypeNoBg:   false, // set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
      // nowrap:          0,     // true to prevent slideshow from wrapping
      // fastOnEvent:     0,     // force fast transitions when triggered manually (via pager or prev/next); value == time in ms
      // randomizeEffects:1,     // valid when multiple effects are used; true to make the effect sequence random
      // rev:             0,     // causes animations to transition in reverse
      // manualTrump:     true,  // causes manual transition to stop an active transition instead of being ignored
      // requeueOnImageNotLoaded: true, // requeue the slideshow if any image slides are not yet loaded
      // requeueTimeout:  250,   // ms delay for requeue
      // activePagerClass: 'activeSlide', // class name used for the active pager link
      // updateActivePagerLink: null // callback fn invoked to update the active pager link (adds/removes activePagerClass style)


		});

		


	// Slide for projects and articles

	$('.template_object .slideshow-holder').after('<div id="nav" class="nav">').cycle({
		fx:                'none', // name of transition effect (or comma separated names, ex: fade,scrollUp,shuffle)
		speed:             'fast',       // speed of the transition (any valid fx speed value)
		timeout:           5000, // milliseconds between slide transitions (0 to disable auto advance)
		pager:             '#nav',       // selector for element to use as pager container
		before:            function() { if (window.console) console.log(this.src); }, // transition callback (scope set to element to be shown):     function(currSlideElement, nextSlideElement, options, forwardFlag)
		after:             function() { $('p.description').html($(this).find("img").attr("rel")); }, // transition callback (scope set to element that was shown):  function(currSlideElement, nextSlideElement, options, forwardFlag)
    pause:             false,        // true to enable "pause on hover"
    pauseOnPagerHover: true         // true to pause when hovering over pager link
    // next:              '.nextslide', // selector for element to use as click trigger for next slide
	});


	// Advance to next slide on click)
	$('.template_object .slideshow .slide-element a').click(function(ev) {
	  ev.preventDefault();
		// alert('Neste Ja');
	  $('.template_object .slideshow-holder').cycle('next');
  });

	//ALTERNATIVE // Advance to next slide on click)
	//ALTERNATIVE $('.xtemplate_object .slideshow .slide-element a').bind('click', function(ev) {
	//ALTERNATIVE   ev.preventDefault();
	//ALTERNATIVE 	alert('Neste');
	//ALTERNATIVE   $('.template_object .slideshow').cycle('next');
  //ALTERNATIVE });




	// Add play/pause button

	$('#nav').after('<div id="playpause"><a class="playing" title="Pause" href="#"></a></div>');


	// Handle clicking on play/pause

	$('#playpause a').bind('click', function(ev) {
		var wasPlaying = $(this).hasClass('playing');

		$('.slideshow-holder').cycle(wasPlaying ? 'pause'            :'resume'            );
		$(this).attr('title',           wasPlaying ? 'Play':'Pause');

		$(this).toggleClass('playing');

		return false;
  });


	// Drop play/pause button when there's only one image

	if ($('#nav a').length == 0) {
	  $('#playpause').hide();
	};


  /* Making life harder for email bots */
  var links = document.getElementsByTagName("a");
  var herf;
  for (i=0; i<links.length; i++) {
    href = links[i].href;
    if (href.indexOf("mailtox:") >= 0) {
      var address = href.split(":");
      if (address[1].indexOf("-") == 0) {
        address = address[1].split("-");
        links[i].href = "mailto:"+address[1]+"@laboremus.no";
	links[i].innerHTML = address[1]+"@laboremus.no";
      } else {
        links[i].href = "mailto:"+address[1]+"@laboremus.no";
	links[i].innerHTML = address[1]+"@laboremus.no";
      }

    }
  }



});

