/*var opaqVals = [0.8,0.3,0.5,0.15,0.8,0.8,0.4,0.7,0.6,0.8,0.7,0.9,0.8,0.7,0.6,0.5,0.4,0.4,0.7,0.4,0.4,0.6,1.0];*/
var sOpacity = 1;

var aaboCookie = "aaboSlider4";

var startSlide = readCookie(aaboCookie);
var initialized;
var continued = true;
var tcontinued = false;
var ccontinued = false;
if(!startSlide){
	startSlide = 0;
	continued = false;
}

var relI=0;


$('#slides noscript').remove();

$('#slides').cycle({ 
  fx:    'custom',
  timeout:10,
  speed: 1300,
  continuous: 1,
  cleartype: false,
  startingSlide: startSlide,
  cssBefore:{
  	opacity:0,
  	display: 'block'
  },
  before: onBefore,
  after: onAfter
});

function onBefore(curr, next, opts) {
  var index = opts.currSlide;
 	$elem = $('#'+this.id);
  if(!initialized){
  	if(index>0){
  		$elem.prev('img').css('opacity',1);
  	}
  	else{
  	}
  }
  
 	opts.animIn = {opacity: opaqVals[index]};
 	
 	if(hasClass(this,'up')){
 		
 		opts.cssBefore = {top: '-'+($elem.height()-189)+'px'};
 	}
 	if(hasClass(this,'slideText') && $.browser.msie){
 		opts.animIn = {opacity: 1};
 	}
 	
 	createCookie(aaboCookie,index,1);
 	
 	if(index==0){
		//$('.slide:not(img:last)').css('opacity',0);
		$('.slide').animate({opacity:0}, 2000);
		$('.slideText').css('opacity',0);
	}
	if(index==1){
		$('img.slide:last').css('opacity',0);
	}
	if(relI>=30)
		relI=0;
 	
}
function onAfter(curr, next, opts) {
	var index = opts.currSlide;
	var nextSlide = opts.nextSlide;
	if(!initialized){
		$('#caption #venstre2, #caption #venstre1').css({'opacity':0,'display':'none'});
		//$('#frontText #meny2,#frontText #meny1').css({'opacity':0,'display':'block'});
		$('#frontText #meny2,#frontText #meny1').css('display','none');
	}	
	if(!front && !continued){
		caption(index);
	}else{
		if(!front)
			$('#caption #venstre2,#frontText #meny12').css({'opacity':1,'display':'block'});
		//$('#caption #venstre1,#frontText #meny21').css({'opacity':1,'display':'none'});
	}
	if(front && !ccontinued){
		caption(relI);
	}
	if(front && !tcontinued){
		frontText(relI);
	}
	relI+=1;
	if(initialized && index==1){
		$('#slides').cycle('pause')
		setTimeout ( "$('#slides').cycle('resume')", 2000 );
	}
	initialized = true;
			
	if(hasClass(this,'up')){
 		$elem = $('#'+this.id);
 		$elem.css('opacity',opaqVals[index-1]);
 		$elem.animate({top:0}, 10000);
 	}
 	if(hasClass(this,'slideText') && $.browser.msie){
 		this.style.removeAttribute("filter");
 	}
}

function caption(i){
	if(i==1){
		$('#caption #venstre1').css('display','block').animate({opacity:1},1000);
	}
	if(i==12)
		$('#caption #venstre1').animate({opacity:0},1000,function(){$(this).hide();});
	if(i==13){
		//$('#frontText #meny1').css({'opacity':0,'display':'block'});
		//$('#frontText #meny1').animate({opacity:1},1000);
		
		if(front)
			$('#caption #venstre2').css('display','block');
		$('#caption #venstre2').animate({opacity:1},1000);
		continued = true;
		ccontinued = true;
	}
	/*if(i==5){
		
	}
	if(i==9){
		//$('#frontText #meny1').animate({opacity:0},1000,function(){$(this).hide();});
	}
	if(i==10){
		//$('#frontText #meny2').css({'opacity':0,'display':'block'});
		//$('#frontText #meny2').animate({opacity:1},1000);
		continued = true;
	}*/
}
function frontText(i){
	
	/*if(i==1){
		
	}
	if(i==2)
		*/
	if(i==3){
		$('#frontText #meny1').css({'opacity':0,'display':'block'});
		$('#frontText #meny1').animate({opacity:1},1000);
	}
	/*
	if(i==5){
		
	}*/
	if(i==8){
		$('#frontText #meny1').animate({opacity:0},1000,function(){$(this).remove();});
	}
	if(i==9){
		$('#frontText #meny2').css({'opacity':0,'display':'block'});
		$('#frontText #meny2').animate({opacity:1},1000);
		tcontinued = true;
	}
}

function showCaption(first){
	//$('#caption #venstre2, #caption #venstre1').css({'opacity':0,'display':'block'});
	
	if(first){
		$('#caption #venstre2').hide();
		$('#caption #venstre1').show();//.animate({opacity:1},2000);
	}else{
		$('#caption #venstre1').hide();
		$('#caption #venstre2').show();//.animate({opacity:1},2000);
	}
	if($.browser.msie){
 		document.getElementById('venstre2').style.removeAttribute("filter");
 	}
}
function showFrontText(first){
	//$('#frontText #meny2,#frontText #meny1').css({'opacity':0,'display':'block'});
	if(first){
		$('#frontText #meny2').hide();
		$('#frontText #meny1').show().animate({opacity:1},2000);
	}else{
		$('#frontText #meny1').hide();
		$('#frontText #meny2').show().animate({opacity:1},2000);
	}
	if($.browser.msie){
 		document.getElementById('venstre2').style.removeAttribute("filter");
 	}
}



/*source: http://www.quirksmode.org/js/cookies.html*/
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*source: http://www.quirksmode.org/js/cookies.html*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
