/* JavaScript Slider */

var STEP = 30;  /* moving step */
var WIDTH = 776;     /* slide width */



/* -----------------------------------Don't change the code below.---------------------------------------*/
var LEFT = 0;
var RIGHT = 1;
var FLAG = 0;
var handler;
var foo;


/* OOP */
//slider object constructor
function Slider()
{
	this.Iframe = document.getElementById("contentIF");	
	this.SliderDiv = this.Iframe.contentWindow.document.getElementById("sliderContentDiv");
	
	this.SlideWidth = WIDTH;
	this.ScrollStep = STEP;
	
	this.ScrollTime = Math.round(this.SlideWidth/this.ScrollStep);
	this.SliderNavigation = new Navigation();
	
	//update arrows visibility in case of arrows
	this.ReDrawArrows = function()
	{
		var next = this.GetNextSlideIndex();
		var count = this.GetSlidesCount();
		this.SliderNavigation.ReDrawArrows(next, count);		
	}
	
	//update arrows visibility in case of links
	this.ReDrawJumpArrows = function(SlideIndex)
	{	
		var count = this.GetSlidesCount();
		this.SliderNavigation.ReDrawArrows(SlideIndex, count);		
	}	
	
	//get left top corner of the NEXT slide in px
	this.GetNextSlidePosition = function(direction)
							 {								
								return (eval(this.GetNextSlideIndex(direction))*eval(WIDTH))		
							 };
							 
	//get left top corner of the GIVEN slide in px
	this.GetSlidePosition = function(SlideIndex)
							 {	
							 	var indes = -1*eval(SlideIndex);
								return (eval(indes)*eval(WIDTH));		
							 };
							 
	//get number of slides in the slider
	this.GetSlidesCount = function getSlidesNumber()
						  {							
								var contentTable, tables;
								var result = 0;
								var attr;							
									
								contentTable = this.Iframe.contentWindow.document.getElementById("content");
								tables = contentTable.getElementsByTagName("table");
								
								for(var i=0; i<tables.length; i++)
								{
									attr = tables[i].getAttribute("type");
									if(attr == "slide")
										result++;
								}							
								return result;
						 };
	
	this.SliderWidth = eval(this.GetSlidesCount())*eval(this.SlideWidth);	
	
	//get direction from current slide to the GIVEN slide (left or right)
	this.GetDirection = function(EndIndex)
						{							
							if(eval(this.GetCurrentSlideIndex()) <= eval(EndIndex))	
								return LEFT;
							else
								return RIGHT;
							
						};
						
	this.GetCurrentSlideIndex = function getCurrentSlide()
							{								
								var leftPosition;
								var result;
								
								leftPosition = this.SliderDiv.style.left.replace("px","");	
								result = eval(Math.round(leftPosition/this.SlideWidth));
								if(eval(result)<0)
									result = -1*eval(result);
								
								return result;
							};
							
	this.GetNextSlideIndex = function GetNextSlideIndex(direction)
						{							
							var slideNumber = eval(this.GetSlidesCount()) - 1;
														
							if(direction == RIGHT && eval(this.GetCurrentSlideIndex()) > 0)		
								next = eval(this.GetCurrentSlideIndex()) - 1;
							
							if(direction == LEFT && eval(this.GetCurrentSlideIndex()) < slideNumber)
								next = eval(this.GetCurrentSlideIndex()) + 1;
							
							return next;
						};
						
	//at 0 - left movement is not allowed.
	//at max slide - the right movement is not allowed.
	this.ScrollAllowed = function(direction)
						 {							 	
								if(direction == RIGHT && this.GetCurrentSlideIndex() == 0)									
									return false;
								
								if(direction == LEFT && this.GetCurrentSlideIndex() == (this.GetSlidesCount() - 1))										
									return false;	
								
								
								if(FLAG == 1)								
									return false;	
								
								
								return true;						
						 };
						 
	this.Scroll = function(time, direction)
						{												
							var CurrentPosition, NextPosition;
							
							
							//set flag
							FLAG = 1;
													
							//get position
							CurrentPosition = this.SliderDiv.style.left.replace("px","");							
							NextPosition = this.GetStepPosition(direction, CurrentPosition);								
							
							
							this.SliderDiv.style.left = NextPosition + "px";
							
							//fix chrome bug
							document.getElementById("contentIF").contentWindow.document.getElementsByTagName("div")[0].focus();
	
							
							time = time - 1;
							
							//var foo = this;
							//handler = window.setTimeout(function(){foo.Scroll(time, direction)}, 1);
							foo = this;
							handler = window.setTimeout("foo.Scroll(" + time + "," + direction + ")", 1);									
							
							if(time <= 0)
							{			
								//enableLinks();	
								this.SliderNavigation.EnableArrows();
								FLAG = 0;
								clearTimeout(handler);   		
							}								
						};	
						
						
	this.Jump = function(SlideIndex)
						{							
							this.SliderDiv.style.left = this.GetSlidePosition(SlideIndex) + "px";							
						};
						
	//get slider position after STEP
	this.GetStepPosition = function(direction, pos)
							{								
								var position;
								var nextSlidePosition = -1*eval(this.GetNextSlidePosition);
								
								//get position
								if(direction == LEFT)
								{	
									position = eval(pos) - this.ScrollStep;
									if(eval(position)<eval(nextSlidePosition))
										return nextSlidePosition;
									else
										return position;
								}
								else
								{
									position = eval(pos) + STEP;		
									if(eval(position)>eval(nextSlidePosition))
										return nextSlidePosition;
									else
										return position;		
								}	
																	
							};
							
}


//navigation object constructor
function Navigation()
{
	this.LeftArrow = document.getElementById("leftArrow");	
	this.RightArrow = document.getElementById("rightArrow");
	this.ReDrawArrows = function(NextSlideIndex, SlidesCount)
						{							
							if(eval(NextSlideIndex) == 0)
							{
								setAttrForElement("class", "hideArrow", this.LeftArrow);
								setAttrForElement("class", "showRightArrow", this.RightArrow);	
							}							
							else
							{
								if(eval(NextSlideIndex) == (eval(SlidesCount)-1))
								{
									setAttrForElement("class", "hideArrow", this.RightArrow);
									setAttrForElement("class", "showLeftArrow", this.LeftArrow);	
								}
								else
								{
										setAttrForElement("class", "showLeftArrow", this.LeftArrow);	
										setAttrForElement("class", "showRightArrow", this.RightArrow);			
								}
							}							
						};
	this.DisableArrows = function()
						{							
							this.LeftArrow.onclick = function () { };
							this.RightArrow.onclick = function () { };								
						};
	this.EnableArrows = function()
						{	
							this.LeftArrow.onclick = function () { scrollInit(RIGHT);}
							this.RightArrow.onclick = function () { scrollInit(LEFT);}							
						};

}
/* end of OOP */


/* scrolling initialization */
/* direction parameter: 0 (left) or 1  (right) */
function scrollInit(direction)
{	    
	var nextSlide, nextSlidePosition;	
	var slider = new Slider();
	
	nextSlide = slider.GetNextSlideIndex(direction);	
	nextSlidePosition = slider.GetNextSlidePosition(direction);
	
	//scroll it
	if(slider.ScrollAllowed(direction))
	{	
		//set arrows visibility	and lock them
		slider.ReDrawArrows();
		slider.SliderNavigation.DisableArrows();
		slider.Scroll(slider.ScrollTime, direction);
	}
}

//jump from link
function scrollLink(id)
{	
	var slider = new Slider();	
    slider.ReDrawJumpArrows(id);	 
    slider.Jump(id);
}


//set attribute to the element
function setAttrForElement(attr_name, attr_value, element)
{
	var attr;
	
	attr = document.createAttribute(attr_name);
	attr.value = attr_value;
	element.setAttributeNode(attr);	
	
	//element.attributes[attr_name].value = attr_value;
}


Function.prototype.bind=function(object)
{
  var method=this;
  return function()
  {
    return method.apply(object,arguments);
  }
}





