function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return false;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}



$(function(){
	//make a globally accessible vaiable that holds the base URL for the site - with a trailing slash!!!
	$.thisURL = $('link[rel=index]').attr('href');

	$('div.slideshow').fadeGallery({
		prev : 'div.btns-holder a.prev',
		next : 'div.btns-holder a.next, a.link-next',
		gallery : 'ul.slide-holder',
		thumbnails : 'ul.slide-vertical > li',
		autoplay : 8000,
		duration : 700
	});
	initslider();
	$('div.text-slider').gallery({
		listOfSlides: 'ul.text-scroll > li',
		nextBtn: 'div.btns-area a.next',
		prevBtn: 'div.btns-area a.prev'
	})
	initTabs();
	$('div.slide-panel').gallery({
		listOfSlides: 'ul.slide-vertical > li',
		nextBtn: 'div.control a.go-top',
		prevBtn: 'div.control a.go-down',
		direction: 'vertical'
		
	})
	
});

function initTabs() {
	$('ul.tabset').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');

		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);

			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').attr('href')).hide();
				});
				_link.addClass('active');
				_tab.show();
				return false;
			});
		});
	});
}

function initslider(){

	var _duration = 700;
	$('div.slide-panel').each(function(){
		var _slide = $(this);
		var _slideH = _slide.outerHeight();
		var _opener = $('span.tab-close a',_slide);
		var animation = false;
		_opener.click(function(e){
			if (!animation) {
				animation = true;
				if (!_slide.hasClass('closed')) {
					_slide.addClass('closed').animate({marginTop:-_slideH}, _duration, function(){
						_opener.html('open');
						_opener.parent().addClass('tab-open').animate({bottom:-43}, 200, function(){animation=false})
					});
				} else {
					_slide.removeClass('closed');
					_opener.html('close');
					_opener.parent().animate({bottom:-22}, 200, function(){
						_slide.animate({marginTop:0}, _duration,function(){animation=false})
					}).removeClass('tab-open');
				}
			}
			e.preventDefault();
		});
	})
	
}

jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		gallery : '.gallery',
		prev : 'a.prev, a.btn-prev, a.link-prev',
		next : 'a.next, a.btn-next, a.link-next',
		play : 'a.play',
		pause : 'a.pause, a.stop',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 4000,
		duration : 500
	}, options);
	
	return this.each(function(){
		var holder = $(this),
			gallery = $(options.gallery, holder),
			prev = $(options.prev, holder),
			next = $(options.next, holder),
			play = $(options.play, holder),
			pause = $(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = $(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('a'),
			autoheight = options.autoheight,
			IE = options.IE,
			timer,
			current;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (autoheight) gallery.css({height: slides.eq(current).height()});
		slides.css({
			opacity:0,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		slides.eq(current).css({
			opacity:1,
			display:'block',
			position:'relative'
		});
		if (IE && $.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			if (IE && $.browser.msie) {
				slides.eq(current).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
			} else {
				slides.eq(current).stop().removeClass('active').animate({
					opacity:0
				},options.duration, function(){
					$(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.duration).css({
					position:'relative',
					zIndex:1
				});
			}
			if (autoheight) {
				if (gallery.css('height') == 'auto') {
					gallery.stop().css({
						height: slides.eq(current).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				} else {
					gallery.stop().animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(current).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			
			if (timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			} 
			return false;
		});
		pause.click(function(){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
			return false;
		});
		// thumbnails
		thumbs.each(function(i){
			/*$(this).mouseenter(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, autoplay);
					}
				}
				return false;
			})*/
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},autoplay);
		}
	});
}

$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };

function Gallery(context, options) { this.init(context, options); };

Gallery.prototype = {
	options:{},
	init: function (context, options){
		this.options = $.extend({
			duration: 700,
			slideElement: 1,
			autoRotation: false,
			effect: false,
			listOfSlides: 'ul > li',
			switcher: false,
			disableBtn: false,
			nextBtn: 'a.link-next, a.btn-next, a.next',
			prevBtn: 'a.link-prev, a.btn-prev, a.prev',
			circle: true,
			direction: false,
			event: 'click',
			IE: false
		}, options || {});
		var _el = $(context).find(this.options.listOfSlides);
		if (this.options.effect) this.list = _el;
		else this.list = _el.parent();
		this.switcher = $(context).find(this.options.switcher);
		this.nextBtn = $(context).find(this.options.nextBtn);
		this.prevBtn = $(context).find(this.options.prevBtn);
		this.count = _el.index(_el.filter(':last'));
		
		if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
		else this.active = _el.index(_el.filter('.active:eq(0)'));
		if (this.active < 0) this.active = 0;
		this.last = this.active;
		
		this.woh = _el.outerWidth(true);
		if (!this.options.direction) this.installDirections(this.list.parent().width());
		else {
			this.woh = _el.outerHeight(true);
			this.installDirections(this.list.parent().height());
		}
		
		if (!this.options.effect) {
			this.rew = this.count - this.wrapHolderW + 1;
			if (!this.options.direction) this.list.css({marginLeft: -(this.woh * this.active)});
			else this.list.css({marginTop: -(this.woh * this.active)});
		}
		else {
			this.rew = this.count;
			this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
			this.switcher.removeClass('active').eq(this.active).addClass('active');
		}
		
		if (this.options.disableBtn) {
			if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
			if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
		}
		
		this.initEvent(this, this.nextBtn, this.prevBtn, true);
		this.initEvent(this, this.prevBtn, this.nextBtn, false);
		
		if (this.options.autoRotation) this.runTimer(this);
		
		if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
	},
	installDirections: function(temp){
		this.wrapHolderW = Math.ceil(temp / this.woh);
		if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
	},
	fadeElement: function(){
		if ($.browser.msie && this.options.IE){
			this.list.eq(this.last).css({opacity:0});
			this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
		}
		else{
			this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
			this.list.removeClass('active').eq(this.active).addClass('active').animate({
				opacity:1
			}, {queue:false, duration: this.options.duration, complete: function(){
				$(this).css('opacity','auto');
			}});
		}
		if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		this.last = this.active;
	},
	scrollElement: function(){
		if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
		else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
		if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
	},
	runTimer: function($this){
		if($this._t) clearTimeout($this._t);
		$this._t = setInterval(function(){
			$this.toPrepare($this, true);
		}, this.options.autoRotation);
	},
	initEventSwitcher: function($this, el){
		el.bind('mouseenter', function(){
			$this.active = $this.switcher.index($(this));
			if($this._t) clearTimeout($this._t);
			if (!$this.options.effect) $this.scrollElement();
			else $this.fadeElement();
			if ($this.options.autoRotation) $this.runTimer($this);
			return false;
		});
	},
	initEvent: function($this, addEventEl, addDisClass, dir){
		addEventEl.bind($this.options.event, function(){
			if($this._t) clearTimeout($this._t);
			if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
			$this.toPrepare($this, dir);
			if ($this.options.autoRotation) $this.runTimer($this);
			return false;
		});
	},
	toPrepare: function($this, side){
		if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
		if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
		for (var i = 0; i < $this.options.slideElement; i++){
			if (side) {
				if ($this.active + 1 > $this.rew) {
					if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
				}
				else $this.active++;
			}
			else{
				if ($this.active - 1 < 0) {
					if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
				}
				else $this.active--;
			}
		};
		if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
		if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
		if (!$this.options.effect) $this.scrollElement();
		else $this.fadeElement();
	},
	stop: function(){
		if (this._t) clearTimeout(this._t);
	},
	play: function(){
		if (this._t) clearTimeout(this._t);
		if (this.options.autoRotation) this.runTimer(this);
	}
}



