/*
 * jQuery gallerySlide v1.0.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * email: aleks_tar@ukr.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery.fn.gallerySlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li'
	},_options);

	return this.each(function(){
		var _this = $(this);
	
		var _gWidth = jQuery(_options.holderList,_this).width();
		var _liWidth = jQuery(_options.scrollEl, $(_options.holderList, _this)).innerWidth();
		var _liSum = jQuery(_options.scrollEl, $(_options.holderList, _this)).length * _liWidth;
		
		var _sec = (_liSum - _gWidth) * 18;
		
		var _maxMargin = _liSum - _gWidth + 15;
		var _posHolder = jQuery(_options.holderList,_this).offset();
		
		var _chapter = _gWidth/12;
		var _speed = 0
		var _direction = 2;
		
		jQuery(document).resize(function(){
			_posHolder = jQuery(_options.holderList,_this).offset();
		});
		
		// gallery navigation
		jQuery(_options.holderList,_this).mousemove(function(e){
			
			for (var i=0; i <= 12; i++) {
				if ((_chapter*i) > (e.pageX - _posHolder.left)) {
					switch(i){
						case 1: _speed = 5;break
						case 2:	_speed = 4;break
						case 3: _speed = 3;break
						case 4: _speed = 2;break
						case 5: _speed = 1;break
						case 12: _speed = 5;break
						case 11: _speed = 4;break
						case 10: _speed = 3;break
						case 9: _speed = 2;break
						case 8: _speed = 1;break
						default:_speed = 0;
					}					
					
					if (i < 5) _direction = 1;
					else if (i > 7) _direction = 3;
					else _direction = 2;
					break;
				}
			}
			if (_direction == 1) {
				var k = (-parseInt(jQuery(_options.scrollElParent,_this).css('marginLeft').replace('px','')))/(_liSum - _gWidth);
				jQuery(_options.scrollElParent,_this).stop();
				jQuery(_options.scrollElParent,_this)
					.animate(
						 {marginLeft:0},
						 {queue:false,easing:'linear',duration:(_sec/_speed)*k}
					);
			}
			if (_direction == 3) {
				var k = (_liSum - _gWidth + parseInt(jQuery(_options.scrollElParent,_this).css('marginLeft').replace('px','')))/(_liSum - _gWidth);
				jQuery(_options.scrollElParent,_this).stop();
				jQuery(_options.scrollElParent,_this)
					.animate(
						{marginLeft:-_maxMargin},
						{easing:'linear',queue:false,duration:(_sec/_speed)*k}
					);
			}
			if (_direction == 2) {
				jQuery(_options.scrollElParent,_this).stop();
			}
		});
		/*jQuery(_options.scrollElParent,_this)
			.animate(
				 {marginLeft:-_maxMargin},
				 {queue:false,easing:'linear',duration:_sec*2}
			);*/
	});
}
function initPage()
{
	$('div.foot-cont div.icon-bot').gallerySlide();
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);



	