// JavaScript Document

toursHandler=function(containerElement,ulElement, status){
	this.container=$(containerElement);
	this.ul=$(ulElement);
	this.stat=$(status)
	this.num=this.ul.children()[0].childNodes.length;
	//alert(this.ul.children()[0].childNodes)
	
	this.singleWidth=115;
	this.position=0;
	this.showing=Math.round(this.ul.width()/this.singleWidth);
	
	this.maxPosition = this.num-this.showing;
	
	this.left=$(document.createElement('a')).attr('href','#').appendTo(this.container).click($.proxy(this.goLeft,this)).css(toursHandler.css.left).hide();
	this.left_img=$(document.createElement('img')).attr('src','images/prev_botton.png').appendTo(this.left).css(toursHandler.css.left_img);
	this.right=$(document.createElement('a')).attr('href','#').appendTo(this.container).click($.proxy(this.goRight,this)).css(toursHandler.css.right).hide();
	this.right_img=$(document.createElement('img')).attr('src','images/next_botton.png').appendTo(this.right).css(toursHandler.css.right_img);
	this.container.hover($.proxy(this.containerHover,this),$.proxy(this.containerUnhover,this));
	this.positionButtons()	
	$(window).resize($.proxy(this.positionButtons,this));
	
	this.stat_elements=new Array();
	for(var i=0;i<=this.maxPosition;i++){
		var st=$(document.createElement('a')).attr('href','#').appendTo(this.stat).css(toursHandler.css.statButton);
		$(document.createElement('img')).attr('src','images/wrap_19x19.png').appendTo(st);
		st.data('t',this);
		st.data('index',i);
		st.hover(this.statButtonHover,this.statButtonUnhover)
		st.click(this.goTo);
		this.stat_elements.push(st);
	}
	this.scrollToPosition(0);
	this.left_vis=0;
	this.right_vis=0;
}
toursHandler.css=new Object();
toursHandler.css.left		={position:'absolute'};
toursHandler.css.left_img	={};
toursHandler.css.right		={position:'absolute', textAlign:'right'};
toursHandler.css.right_img	={float:'right'};
toursHandler.css.statButton		={width:'19px', height:'19px', margin:'0px 10px 0px 0px', padding:'0px 0px 0px 0px', background:"url('images/statButton.png') no-repeat", float:'left'}
toursHandler.css.statButton_h	={background:"url('images/statButton_h.png') no-repeat"}

toursHandler.prototype.positionButtons=function(){
	this.dim=new Object();
	this.dim.w=this.container.width();
	this.dim.h=this.container.height();
	this.dim.x=this.container.offset().left;
	this.dim.y=this.container.offset().top;
	this.dim.rightLeft=this.dim.x+this.dim.w-66
	
	this.zone=new Array();
	this.zone[0]=new Object();
	this.zone[0].top=this.dim.y;
	this.zone[0].right=this.dim.x+200;
	this.zone[0].botom=this.dim.y+this.dim.h;
	this.zone[0].left=this.dim.x;
	
	this.zone[1]=new Object();
	this.zone[1].top=this.dim.y;
	this.zone[1].right=this.dim.rightLeft;
	this.zone[1].botom=this.dim.y+this.dim.h;
	this.zone[1].left=this.dim.rightLeft-200;
	
	var top=(this.dim.y+95);
	var left=this.dim.x;
	var right=this.dim.rightLeft;
	this.left.css({top:top+'px', left:left+'px'});
	this.right.css({top:top+'px', left:right+'px'});
}
toursHandler.prototype.showLeft=function(){
	if(this.left_vis==0){
		this.left.width(0);
		this.left.animate({width:'66px'},200,$.proxy(function(){ this.left_vis=1;},this));
	}
}
toursHandler.prototype.hideLeft=function(){
	if(this.left_vis==1){
		this.left.animate({width:'1px'},200,$.proxy(function(){this.left.hide(); this.left_vis=0;},this));
		
	}
}

toursHandler.prototype.showRight=function(){
	if(this.right_vis==0){
		this.right.width(0);
		this.right.css('left',(this.dim.rightLeft+66)+'px');
		this.right.animate({width:'66px',left:this.dim.rightLeft+'px'},200,$.proxy(function(){ this.right_vis=1;},this));
	}
}
toursHandler.prototype.hideRight=function(){
	if(this.right_vis==1){
		this.right.animate({width:'1px',left:(this.dim.rightLeft+66)+'px'},200,$.proxy(function(){this.right.hide(); this.right_vis=0;},this));
		
	}
}


toursHandler.prototype.containerHover=function(){
	if(	this.position>0					){ this.showLeft	(); }
	if(	this.position<this.maxPosition	){ this.showRight	();	}
	//this.container.mousemove($.proxy(this.mouseMove,this));
}
toursHandler.prototype.containerUnhover=function(){
	this.hideLeft();
	this.hideRight();
	//this.container.unbind( 'mousemove' );
}
toursHandler.prototype.mouseMove=function(e){
	if(e.pageX<this.zone[0].right && e.pageX>this.zone[0].left && e.pageY>this.zone[0].top && e.pageY<this.zone[0].botom){
		this.showLeft();
	}else{
		this.hideLeft();
	}
	if(e.pageX<this.zone[1].right && e.pageX>this.zone[1].left && e.pageY>this.zone[1].top && e.pageY<this.zone[1].botom){
		this.showRight();
	}else{
		this.hideRight();
	}
}
toursHandler.prototype.statButtonClear=function(){
	this.stat_elements[this.position];
}
toursHandler.prototype.statButtonHover=function(){
	$(this).css('backgroundPosition','bottom');
}
toursHandler.prototype.statButtonUnhover=function(){
	if($(this).data('selected')=='1'){return}
	$(this).css('backgroundPosition','top');
}

toursHandler.prototype.goTo=function(){
	var a=$(this);
	var t=a.data('t');
	var index=a.data('index');
	a.blur();
	if(index==0){t.hideLeft();
	}else{t.showLeft	();}
	if(index==t.maxPosition){t.hideRight();
	}else{t.showRight	();}
	
	//t.position=index;
	t.scrollToPosition(index);
	return false;
}
toursHandler.prototype.goLeft=function(){
	this.left.blur();
	if(this.position==0){ return false;}
	if(this.position<=0){this.position=0;}
	else{this.scrollToPosition(this.position-1); this.showRight	();}
	if(this.position==0){this.hideLeft();}
	//this.scrollToPosition();
	return false;
}

toursHandler.prototype.goRight=function(){
	this.right.blur();
	if(this.position==this.maxPosition){return false;}
	if(this.position>this.maxPosition){this.scrollToPosition(this.maxPosition); }
	else{this.scrollToPosition(this.position+1); this.showLeft	();}
	if(this.position==this.maxPosition){this.hideRight();}
	
	return false;
}
toursHandler.prototype.scrollToPosition=function(pos){
	this.stat_elements[this.position].css('backgroundPosition','top');
	this.stat_elements[this.position].data('selected','')
	this.position=pos;
	this.ul.animate({scrollLeft:this.singleWidth*this.position+'px'},{duration: 500,easing:'swing'});
	this.stat_elements[this.position].css('backgroundPosition','bottom');
	this.stat_elements[this.position].data('selected','1')
	//this.ul.scrollLeft(this.singleWidth*this.position);
}

