window.addEvent('load', attachSourceCodeToUrl);

function attachSourceCodeToUrl()
{		
	elementCount = document.getElementsByName('lplink').length;
		//alert(elementCount);
	for (i=0; i<elementCount; i++) {
    		document.getElementsByName('lplink')[i].href = "javascript:loadNewReg();"	
  	}
}

function getBaseURL()
{
	var baseURL = document.location.href;
	baseURL = baseURL.substring(0,baseURL.indexOf("/",baseURL.indexOf("//")+2));
	return baseURL;
}
	
function loadNewReg()
{
	var sURL = getBaseURL()+scriptNameString+"/site/homepage.jsp?"+linkString;		
	 	
	//unescape(window.location);
	
	var indexOfHash = sURL.indexOf('#');
	if(indexOfHash >= 0)
	{
		sURL = sURL.substring(0,indexOfHash);
	}
	if(window.location.protocol == 'http:')
		sURL = sURL.replace(/http/, "https");
	
	if(sURL.indexOf("load=offerDetails") == -1){
		if(sURL.indexOf('?')==-1)
				{
					sURL = sURL + "?load=offerDetails";
				}else
				{
					sURL = sURL + "&load=offerDetails";
				}
	 }
	 if(debug=="on")
	 {
	 	sURL = sURL + "&debug=on";
	 }
	
  //alert(sURL);
  window.location.replace( sURL );	
}

function initTeaser(teaserName, offsetDelay) {
  var dynamicTeaser = {
	offsetXY: [-1000, -1000],
	
	init: function(container, offsetDelay) {
	  if (!container && !$(container)) {
		return;
	  }
	  this.container = $(container);
	  this.activeXY = $(container).getPosition();
	  this.teaser = $ES('.teaser',this.container);
	  this.activeTeaser = this.teaser[0];
	  this.activeTeaserNum = 1;
	  this.numberTeaser = this.teaser.length;
	  this.pagination = $ES('.paging',this.container)[0];

	  var teaserLink = $E('.teaserlink'+this.activeTeaserNum,this.container);
	  if(teaserLink)
		  teaserLink.addClass('selected');
	  this.animateTeaser = true;
	  this.animateDuration = 10000;
	  
	  if(this.pagination)
		  this.pagination.addEvent("click", this.toggleTopTeaser.bindWithEvent(dynamicTeaser));
	  
	  // set visibility of all teaser to hidden except the first teaser
	  var length = this.numberTeaser;
	  for (var i = 1; i < length; i++) {
		$(this.teaser[i]).setStyle('display', 'none');
		$(this.teaser[i]).setStyle('opacity', 0);		
	  }
	  if(this.animateTeaser)
	  {
		this.animate.delay(this.animateDuration + (offsetDelay+1)*3000, this); //Offset delay is used so that not all teasers change at the same time
	  }
	},
	toggleTopTeaser: function(e) {	  
	  e.stop();
	  var t = e.target;
	  var href = t.href;
	  
	  if(href!=null && href!='')
	  {
		 var page = href.substring( href.indexOf('#teaser')+8 );
		 if (parseInt(page) != this.activeTeaserNum ) {
			this.animateTeaser = false;
		 }
		 this.changeTeaser(page);
	  }
	},
	animate: function ()
	{
		if(this.animateTeaser && this.teaser.length > 1)
		{			
			var changeToTeaser = this.activeTeaserNum+1;
			if(changeToTeaser > this.teaser.length)
			{
				changeToTeaser = 1;
			}
			this.changeTeaser(changeToTeaser);
			this.animate.delay(this.animateDuration, this);
		}
	},
	changeTeaser: function (page)
	{		 
	  if ( page == this.activeTeaserNum ) {
		return;
	  }
	  $E('.teaserlink'+this.activeTeaserNum,this.container).removeClass('selected');
	  this.activeTeaserNum = page;
	  $E('.teaserlink'+this.activeTeaserNum,this.container).addClass('selected');
	  
	  if ( page != NaN ) {
		var nextTeaser = function() {
					  var that = dynamicTeaser;
					  // position offset
					  that.activeTeaser.setStyle('display', 'none');
					  // position the new selected teaser
					  that.teaser[page-1].setStyle('display', 'block');
					  // set active teaser
					  that.activeTeaser = that.teaser[page-1];
					  // display the new teaser
					  var myAnim2 = new Fx.Style(this.activeTeaser, 'opacity',
						  {
							duration: 500, 
							transition: Fx.Transitions.linear
						  });					  
					  myAnim2.start(0,1);
					};
		var myAnim1 = new Fx.Style(this.activeTeaser, 'opacity', 
			{
				duration: 500, 
				transition: Fx.Transitions.linear,
				onComplete: nextTeaser.create({bind: this})
			});
		myAnim1.start(1,0);
	  }
	}
  }
  dynamicTeaser.init(teaserName, offsetDelay);
}

function initTeaserElements()
{
	var teaserArray = $ES('.teaserElements', this.container);
	if(teaserArray!=null && teaserArray.length > 0)
	{
		for(var i=0;i<teaserArray.length;i++)
		{
			var teaserName = teaserArray[i].id;
			initTeaser(teaserName, i);
		}
	}
}
window.addEvent('load', initTeaserElements);