var $j = jQuery;
$j(document).ready(function() {
	
     if ($j("#carousel").length) { 
        rotatePics(1);
     }
    /*
	$j("h3.lest").click(function() {
		toggleMost($j(this),194);
	});
	$j("h3.kommentert").click(function() {
		toggleMost($j(this),108);
	});
    */
    if ($j(".aktive-piloter.small-articlelist li").length) {
		var antallLi = $j(".aktive-piloter.small-articlelist li").length;
        //alert(antallLi);
		var skiftPos = Math.ceil(antallLi/3);
        var splitPoint1 = $j(".aktive-piloter.small-articlelist li:nth-child("+skiftPos+")");
		var splitPoint2 = $j(".aktive-piloter.small-articlelist li:nth-child("+(skiftPos*2)+")");
		$j(splitPoint1).addClass('last');
		$j(splitPoint2).addClass('last');
		
		$j(splitPoint1).parent().after($j(splitPoint1).nextAll().wrapAll('<ul/>').parent());
		$j(splitPoint2).parent().after($j(splitPoint2).nextAll().wrapAll('<ul/>').parent().addClass('last'));
	}
	
});

function toggleMost(em,Height) {
	// skifter fokus vha z-index, toggle(hide/show) og .aktiv for
	// .sist-kommentert og .mest-lest
	var mother = $j(em).parent();
	$j(mother).toggleClass('aktiv');
	//$j(mother).css('z-index','0');
	$j(em).siblings('.tile-content').toggle();

	var uncle =	$j(mother).siblings('.small-articlelist');
//	$j(uncle).css('z-index','1')
	$j(uncle).children('.tile-content').toggle();
	$j(uncle).toggleClass('aktiv');
	$j(".mest").css('height',Height);
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function rotatePics(currentPhoto) {
//	var tempo = getInternetExplorerVersion() < 8 && getInternetExplorerVersion() > 0 ? 'fast' : 1000;
	var numberOfPhotos = $j('#carousel .image').length;
	currentPhoto = currentPhoto % numberOfPhotos;
		
	$j('#carousel .image').eq(currentPhoto).fadeOut(function() {
		// re-order the z-index
		$j('#carousel .image').each(function(i) {
			$j(this).css(
		        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
			);
		});
		$j(this).show();
		setTimeout(function() {rotatePics(++currentPhoto);}, 10000);
	});
}
