function activateLink(linkNum)
{
	$('span[id^="link"]').removeClass('link_button_up').addClass('link_button');
	$('#link' + linkNum).addClass('link_button_up');
}

function showSlide(num)
{
	if((num > 0) && (num <= totalItems))
	{
		if(($.browser.webkit) || ($.browser.msie && ($.browser.version == 7)))
		{
			if($.browser.msie && ($.browser.version == 7))
			{
				slideCount = num - currSlide;
				toMoveBy = slideCount * slideWidth;
			}
			else
			{
				toMoveBy = parseInt($('.' + class_name + ':eq(' + (num - 1) + ')').position().left) + parseInt($('#slide').position().left);
			}
			
			topSlider = new slidable($('#slide'), toMoveBy, 'horizontal', 300, 'topSlider');
		}
		else
		{
			slideCount = num - currSlide;
			toMoveBy = slideCount * slideWidth;
			
			$('#slide').animate({"left": '-=' + toMoveBy}, 300);
		}
		
		currSlide = num;
		activateLink(currSlide);
	}
}

function moveForward()
{
	slideToShow = currSlide + 1;
	showSlide(slideToShow);
	
	if((slideToShow > 1) && (slideToShow <= totalItems) && ((slideToShow % 9) == 1))
	{
		moveBottomSlider(true); //true is for forward motion
	}
}

function moveBackward()
{
	slideToShow = currSlide - 1;
	showSlide(slideToShow);
	
	if((slideToShow > 1) && ((slideToShow % 9) == 0))
	{
		moveBottomSlider(false); //false is for backward motion
	}
}

function loadNext()
{
	slideToShow = (Math.ceil(currSlide / 9) * 9) + 1;
	
	if(slideToShow <= totalItems)
	{
		showSlide(slideToShow);
		moveBottomSlider(true); //true is for forward motion
	}
}

function loadPrevious()
{
	slideToShow = ((Math.ceil(currSlide / 9) - 2) * 9) + 1;
	
	if(slideToShow > 0)
	{
		showSlide(slideToShow);
		moveBottomSlider(false); //false is for backward motion
	}
}

//if true is passed to this function, the bottom slider moves in the forward direction
//if false is passed to this function, the bottom slider moves in the backward direction
function moveBottomSlider(showNext)
{
	if(($.browser.webkit) || ($.browser.msie && ($.browser.version == 7)))
	{
		if(showNext)
			currSet++;
		else
			currSet--;
		
		if($.browser.msie && ($.browser.version == 7))
		{
			if(showNext)
				toMoveBy = 950;
			else
				toMoveBy = -950;
		}
		else
		{
			toMoveBy = parseInt($('.bot_panel_port:eq(' + (currSet - 1) + ')').position().left) + parseInt($('#links_slide').position().left);
		}
		
		bottomSlider = new slidable($('#links_slide'), toMoveBy, 'horizontal', 300, 'bottomSlider');
	}
	else
	{
		if(showNext)
			$('#links_slide').animate({"left": '-=950'}, 300);
		else
			$('#links_slide').animate({"left": '+=950'}, 300);
	}
}

//this function is the replacement for jQuery's 'animate' function for
//Safari, Chrome and IE 7.
function slidable(slider, slideAmount, direction, duration, objName)
{
	this.slider = slider;
	this.slideAmount = slideAmount;
	this.prop = (direction == 'vertical') ? 'top' : 'left';
	this.duration = duration;
	this.objName = objName;
	this.timeout = 50;
	this.frameCount, this.dVal;
	
	if(this.duration <= this.timeout)
	{
		this.timeout = this.duration;
		this.frameCount = 1;
		this.dVal = this.slideAmount;
	}
	else
	{
		this.frameCount = Math.ceil(this.duration / this.timeout);
		this.dVal = Math.floor((this.slideAmount * this.timeout) / this.duration);
	}
	
	this.animate = function()
	{
		currVal = getCSSVal(this.slider, this.prop);
		
		if(this.slideAmount != 0)
		{
			if(Math.abs(this.slideAmount) < Math.abs(this.dVal))
				this.dVal = this.slideAmount;
			
			this.slideAmount -= this.dVal;
			currVal -= this.dVal;
			
			this.slider.css(this.prop, currVal);
			
			setTimeout(this.objName + '.animate();', this.timeout);
		}
		else
		{
			//After zooming in/out n IE 7, in the horizontal sliders everyting slides
			//but text. The following script fixes it.
			if($.browser.msie && ($.browser.version == 7))
			{
				$('body').hide().show();
			}
		}
	}
	
	this.animate();
}

//this function can be used to get width, height, left, top etc. as integer values
function getCSSVal(elem, prop)
{
	val = elem.css(prop);
	val = parseInt(val.replace('px', ''));
	
	if(isNaN(val))
		val = 0;
	
	return val;
}

var currSlide = 1;
var currSet = 1;

$(document).ready(function() {
	if(($.browser.msie) && ($.browser.version == 7))
	{
		$('body').delay(50).hide().show();
	}
	
	if($.browser.webkit)
	{
		$(window).resize(function() {
			showSlide(currSlide);
		});
	}
});
