var defaultMovePoint=function(){}
var movePoint=defaultMovePoint;
TImageChanger=function(container, img,href){
	this.img=new Array();
	this.href=new Array();
	this.container=null;
}
TImageChanger.prototype.init=function(container, img,href){
	//alert(img);
	this.img=img;
	this.href=href;
	this.self=this;
	this.container=container;
	this.change=this.changeDefault;
	this.aftershow=this.aftershowDefault;
	this.CurrentPromo=1;
	this.onImageLoad=this.onImageLoadDefault;
}
TImageChanger.prototype.onImageLoadDefault=function(position){
	movePoint(position);
}
TImageChanger.prototype.Prev=function(){	scroller.onmouseup=function(event){
		this.onmouseup=function(event){
			scrollerJump(event);
		}
		return false;
	}
	this.CurrentPromo-=2;
	this.showPicture();
	this.CurrentPromo++;
	return void(0);
}
TImageChanger.prototype.Next=function(){	scroller.onmouseup=function(event){		this.onmouseup=function(event){			scrollerJump(event);
		}
		return false;
	}
	this.showPicture();
	this.CurrentPromo++;
	return void(0);
}
TImageChanger.prototype.aftershowDefault=function(){
	$(this.container).find('img:first').remove();
}
TImageChanger.prototype.showPicture=function(){
	var i = new Image();
	i.controller=this;
	i.activeLink=this.href[this.CurrentPromo];
	i.currentPromo=this.CurrentPromo;
	i.onclick=function(){
		document.location.href=this.activeLink;
	}
	if(typeof(this.img[this.CurrentPromo])!='undefined')
	$(i).load(function(){
		$(this.controller.container).append(this);
		$(this.controller.container).find('img:first').css({'z-index': 1});
		this.controller.onImageLoad(i.currentPromo);
		$(this).css({opacity: 0.0, 'z-index': 2}).animate({opacity: 1.0}, 1000, function() {
				this.controller.aftershow();
			})
	}).attr('src', this.img[this.CurrentPromo]+'?rnd='+Math.random(300000)).css({position:'absolute',top:0,left:0,'z-index':8});
}
TImageChanger.prototype.changeDefault=function(){
	if (this.CurrentPromo == this.img.length) {
		this.CurrentPromo = 0;
	}
	this.showPicture();
	this.CurrentPromo++;
}
var promoImgch = new TImageChanger();
jQuery.fn.dynamicSlideshow = function(attr) {
	attr = attr || {};
	attr.duration = attr.duration || 5000;
	function initSlider(container,img,href) {
		promoImgch.init(container, img,href);
		setInterval( 'promoImgch.change()', attr.duration );
	};

	$(this).each(function(){
		var img = [];
		var href = [];
		$(this).find("a").each(function(){
			img.push($(this).attr("title"));
			href.push($(this).attr("href"));
		});
		var j = new Image();
		var container = this;
		$(this).empty();
		j.activeLink=href[0];
		j.container=this;
		j.onclick=function(){
			document.location.href=this.activeLink;
		}
		j.src=img[0]+'?rnd='+Math.random(300000);
		$(j).css({position:'absolute',top:0,left:0,'z-index':0}).load(function(){
			$(this.container).append(this);
			initSlider(container, img,href);
		});
	});
}


var scroller=document.getElementById('scroller');
var scrollerLength=scroller.offsetWidth-10;
var startPos=scroller.offsetLeft;
var elemsCnt=document.getElementById('slideshow').getElementsByTagName('a').length;
var segment=Math.floor(scrollerLength/elemsCnt);
var pointer=document.getElementById('pointer');
var dragTimer=0;
var dragMode='false';
dragPointer=function(event){
	if(dragMode=='true'){
		if(typeof(event)=='undefined'){
			event=window.event;
		}
		if(startPos==false){
			startPos=event.clientX-pointer.offsetLeft;
		}
		if(((newpos=(event.clientX-startPos))<10)||(newpos>(scrollerLength-pointer.offsetWidth))){
			return false;
		}
		newOffset=event.clientX-startPos;
  		pointer.style.left=newOffset+'px';
        promoImgch.CurrentPromo=Math.ceil(newOffset/scrollerLength*elemsCnt)-1;
		pointer.innerHTML=promoImgch.CurrentPromo+1;
	}
}
var startDragPointer=function(){
	document.body.onmousemove=dragPointer;
	document.body.onmouseup=stopDrag;
	startPos=false;
	scroller.onmouseup=function(){};
	dragMode='true';
}
stopDrag=function(){
	promoImgch.onImageLoad=function(){
		this.onImageLoad=this.onImageLoadDefault;
	}
 	promoImgch.showPicture();
	scroller.onmouseup=function(event){
		scrollerJump(event);
	}
	dragMode='false';
}

pointer.onmousedown=startDragPointer;
var setPointerToPosition=function(position){
	offsetSegment=(10+(position*segment))+'px';
	currentOffset=pointer.offsetLeft+'px';
	$(pointer).css({'left':currentOffset}).animate({'left':offsetSegment},1000);
	pointer.innerHTML=(position+1);
}
movePoint=function(position){	document.getElementById('promoNext').href=promoImgch.href[position];
	setPointerToPosition(position);
}
setPointerToPosition(0);
scrollerJump=function(event){		if(typeof(event)=='undefined'){
			event=window.event;
		}
		if(startPos==false){
			startPos=event.clientX-pointer.offsetLeft;
		}
		newpos=event.clientX-startPos-scrollerLength+30;
		if(newpos<10) newpos=10;
		if(newpos>(scrollerLength-pointer.offsetWidth)){newpos=(scrollerLength-pointer.offsetWidth);}
		promoImgch.CurrentPromo=Math.ceil(newpos/scrollerLength*elemsCnt);
		pointer.innerHTML=promoImgch.CurrentPromo;
		promoImgch.CurrentPromo-=1;
		promoImgch.showPicture();
		dragMode='false';
}
scroller.onmouseup=function(event){	scrollerJump(event);
}
startPromoScrolling=function(){
	$(function(){
		$("#slideshow").dynamicSlideshow();
	});
}

setTimeout('startPromoScrolling()',500);
