/* 16.05.2011 */
jQuery(document).ready(function() {
	InitGallery();
	InitCarousel();
	if(jQuery('ul.accordion').length){
		jQuery('ul.accordion').accordion({
			active: ".selected",
			autoHeight: false,
			header: ".opener",
			collapsible: true,
			event: "click"
		});
	}
	/* if(jQuery('ul.cms-nav-sidebar').length){
		jQuery('ul.cms-nav-sidebar').accordion({
			active: ".selected",
			autoHeight: false,
			header: ".icl-level-1 a",
			collapsible: true,
			event: "click"
		});
	} */
});
function InitGallery(){
	jQuery('.hold-gallery').Gallery({
		speed: 800,
		duration: 8200,
		list: 'ul.gallery>li',
		pager: '.switcher ul'
	});
};
jQuery.fn.Gallery = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1500,
		duration: 6000,
		list: 'ul.fade>li',
		prev: '.prev',
		next: '.next',
		pager: 'ul.switcher',
		pause: ''
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _duration = _options.duration;
		var _list = _hold.find(_options.list);
		var _h = _hold.height();
		var _w = _hold.width();
		_list.each(function(){
			var _img = jQuery(this).find('img');
			_img.css({
				marginLeft: (_w -_img.width())/2,
				marginTop: _h -_img.height()
			})
		})
		var _prev = _hold.find(_options.prev).hide();
		var _next = _hold.find(_options.next).hide();
		var _pause = _hold.find(_options.paused);
		var _f = true;
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) {_a = 0;_list.eq(_a).addClass('active')}
		var _i, _old = _a, _t;
	/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager).empty();
		_list.each(function(i){
			jQuery('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
		});
		var _thumb = _num.find('li');
		_thumb.eq(_a).addClass('active');
	/*-------------------------------------------------*/
		if (jQuery.browser.msie && jQuery.browser.version < 7){		_list.hide().eq(_a).show();
		}else{		_list.show().css({opacity:0}).eq(_a).css({opacity:1});	}
		Run(_a);
		
		function Run(_a){
			_t = setTimeout(function(){
				_a++; if (_a >= _list.length){_a=0}
				ChangeFade(_a);
			}, _duration);
		};
		function ChangeFade(_new){
			if(_new != _old){
				if(jQuery.browser.msie && jQuery.browser.version < 7){
					_list.eq(_old).removeClass('active').hide();
					_list.eq(_new).addClass('active').show();
				}else{
					_list.eq(_old).removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
					_list.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
				}
				_thumb.eq(_old).removeClass('active');
				_thumb.eq(_new).addClass('active');
				_old=_new;_a=_new;
				if(_t) clearTimeout(_t);
				if (_f){Run(_new);}
			};
		};
		_pause.click(function(){
			_f = false;
			clearTimeout(_t);
			return false;
		});
		_thumb.click(function(){
			_i = _thumb.index(jQuery(this));
			ChangeFade(_i);
			_a = _i;
			return false;
		});
		_next.click(function(){
			_a++; if (_a == _list.length){_a=0}
			ChangeFade(_a);
			return false;
		});
		_prev.click(function(){
			_a--; if (_a == -1){_a = _list.length-1}
			ChangeFade(_a);
			return false;
		});
		_hold.mouseenter(function(){
			_prev.show();
			_next.show();
		}).mouseleave(function(){
			_prev.hide();
			_next.hide();
		})
	});
};
function InitCarousel(){
	var _speed = 1200,
		_duration = 8000;
	var _hold = jQuery('div.carusel');
	var _holder = _hold.find('div.carousel-frame');
	var _slide = _holder.find('ul.sliders');
	var _list = _slide.find('>li');
	var _prev = _hold.find('.prev');
	var _next = _hold.find('.next');
	
	var _d = _list.eq(0).outerWidth(true);
	var _x=0, _t;
	var _wh = _holder.width();
	var _ws = _list.length*_d;
	var _margin = parseInt(_list.eq(0).css('marginRight'));
	
	Run(_x);
	function Run(_x){	
		_t = setTimeout(function(){
			_x+=_d; if (_x >= _ws- _wh){_x=0}
			Slide(_x);
		}, _duration);
	};
	function Slide(_x){
		_slide.animate({marginLeft: -_x}, {queue:false, duration:_speed});
		if(_t) clearTimeout(_t);
		Run(_x);
	};
	
	_next.click(function(){
		if (_x < _ws- _wh -_margin){_x = _x + _d;}
		else{_x = 0;}
		Slide(_x);
		return false;
	});
	
	_prev.click(function(){
		if (_x > 0){_x = _x - _d;}
		else{_x = _ws -_wh -_margin;}
		Slide(_x);
		return false;
	});
};

