var $j = jQuery.noConflict();
var timeout = null;

$j(document).ready(function() {
        
     if ($j(".carousel").length) { 
        rotatePics(1);
     }
        
    // gå til det banner # som er klikket på
    $j('.carousel p span').each(function() {
        var antallSpan = $j('.carousel p span').length;
        var index = $j(this).index();
        $j(this).click(function() {
            if (typeof timeout == "number") clearTimeout(timeout);
            rotatePics(index-1);
        });        
    });
    
    // del en ul liste i to lister
	if ($j("#placeholder-bottom .medlemsverktoy li").length) {
		var antallLi = $j("#placeholder-bottom .medlemsverktoy li").length;
        //alert(antallLi);
		var skiftPos = Math.ceil(antallLi/3);
        var splitPoint1 = $j("#placeholder-bottom .medlemsverktoy li:nth-child("+skiftPos+")");
		var splitPoint2 = $j("#placeholder-bottom .medlemsverktoy 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'));
	}
	
	$j("#notloggedin a").click(function() {
		var iemode = IEMode();
		if (iemode && iemode == 7) {
			$j("#placeholder-content-no-right").css("position","static");
		}
		$j("#mask").css("display","block");
		$j("#loginform").css("display","block");
	});
	
	$j("#lukk").click(function() {
		$j("#mask").css("display","none");
		$j("#loginform").css("display","none");
	});

	$j("#newsletter-form-register input.textinput").focus(function(){
		if ($j(this).val() == "Skriv inn din e-post") {
			$j(this).val('');
		}
	}).blur(function(){
		if ($j(this).val() == "") {
			$j(this).val('Skriv inn din e-post');
		}
	});
    
	
});

function rotatePics(currentPhoto) {
    
  //  if (typeof timeout == "number") clearTimeout(timeout);
    var numberOfPhotos = $j('.carousel div').length;
	currentPhoto = currentPhoto % numberOfPhotos;
		
	$j('.carousel div').eq(currentPhoto).fadeOut(function() {
		// re-order the z-index
		$j('.carousel div').each(function(i) {
			$j(this).css(
		        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
			);
		});
		$j(this).show();       
		timeout = setTimeout(function() {rotatePics(++currentPhoto,true);}, 4000);             
	});
}

function validate(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
	  //$j("[name=tl_epost]").val('Ugyldig E-post!').css({'background-color':'red','color':'white'});
	  return false;
   }
   return true;
}

function IEMode() {
	engine = null;
	if (window.navigator.appName == "Microsoft Internet Explorer")
	{
	   // This is an IE browser. What mode is the engine in?
	   if (document.documentMode) // IE8 or later
		  engine = document.documentMode;
	   else // IE 5-7
	   {
		  engine = 5; // Assume quirks mode unless proven otherwise
		  if (document.compatMode)
		  {
			 if (document.compatMode == "CSS1Compat")
				engine = 7; // standards mode
		  }
		  // There is no test for IE6 standards mode because that mode  
		  // was replaced by IE7 standards mode; there is no emulation.
	   }
	   // the engine variable now contains the document compatibility mode.
	}
	return engine;
}



// Change effect on the fly
//galleries[0].settings.effect = 'fade';

