$(document).ready(function(){
	//clearFormFields({
	//	clearInputs: true,
	//	clearTextareas: true,
	//	passwordFieldText: true,
	//	addClassFocus: "focus",
	//	filterClass: "default"
	//});
	$('ul.accordion').accordion({
		active: ".selected",
		autoHeight: false,
		header: ".opener",
		collapsible: true,
		event: "click"
	});
	$('.gallery').slideShow({
		slideEl:'ul.picture-box li',
		numElement:'.items a',
		duration:500,
		autoSlideShow:false,
		event:'click'
	});
	$('div#carousel').gallery({
		duration: 400,
		listOfSlides: 'ul.carousel-list > li',
		nextBtn: 'a.next',
		prevBtn: 'a.prev',
		slideElement: 2
	});
});

// clearFormFields
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

// gallery
(function($) {
	function Gallery(el, options) {
		this._hold = $(el);
		this.initOptions(options);
		this._timer = options.autoRotation;
		this._t;
		this.initialize();
	}
	
	$.fn.gallery = function(options) {
		return new Gallery(this.get(0), options);
	};
	
	Gallery.prototype = {
		initOptions: function(_obj){
			this.options = {
				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,
				directionBox: false,
				IE: false,
				textBox:false
			};
			for(key in _obj) this.options[key] = _obj[key];
		},
		initialize: function(){
			var _this = this;
			var _hold = _this._hold;
			var _speed = _this.options.duration;
			var _timer = _this.options.autoRotation;
			var _el = _hold.find(_this.options.listOfSlides);
			if (_this.options.effect) var _list = _el;
			else var _list = _el.parent();
			var _textBox = _hold.find(_this.options.textBox);
			var _switcher = _hold.find(_this.options.switcher);
			var _next = _hold.find(_this.options.nextBtn);
			var _prev = _hold.find(_this.options.prevBtn);
			var _count = _el.index(_el.filter(':last'));
			var _w = _el.outerWidth(true);
			var _h = _el.outerHeight(true);
			if (_this.options.switcher) var _active = _switcher.index(_switcher.filter('.active:eq(0)'));
			else var _active = _el.index(_el.filter('.active:eq(0)'));
			if (_active < 0) _active = 0;
			var _last = _active;
			var flag = true;
			var der = _this.options.directionBox;
			
			// Installation directions
			if (!_this.options.direction) {
				var _wrapHolderW = Math.ceil(_list.parent().width() / _w);
				if (((_wrapHolderW - 1) * _w + _w / 2) > _list.parent().width()) _wrapHolderW--;
			}
			else{
				var _wrapHolderW = Math.ceil(_list.parent().height()/_h);
				if (((_wrapHolderW-1)*_h + _h/2) > _list.parent().height()) _wrapHolderW--;
			}
			if(_textBox){
				var typ = _textBox.outerHeight(true);
				if (!der) _textBox.css({bottom:-typ});
				else _textBox.css({top:-typ});
			}
			// Setting "fade" or "slide" effect
			if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
			else var rew = _count;
			if (!_this.options.effect) {
				if (!_this.options.direction) _list.css({marginLeft: -(_w * _active)})
				else _list.css({marginTop: -(_h * _active)})
			}
			else {
				_list.css({
					opacity: 0
				}).removeClass('active').eq(_active).addClass('active').css({
					opacity: 1
				}).css('opacity', 'auto');
				_switcher.removeClass('active').eq(_active).addClass('active');
				_list.parent().css({height: _list.eq(_active).outerHeight(true)});
				if (!der) _textBox.eq(_active).css({bottom:0});
				else _textBox.eq(_active).css({top:0});
			}
			
			// Disable or enable buttons "prev next"
			if (_this.options.disableBtn) {
				if (_count < _wrapHolderW) _next.addClass(_this.options.disableBtn);
				_prev.addClass(_this.options.disableBtn);
			}
			
			// Function to "fade"
			function fadeElement(){
				if ($.browser.msie && _this.options.IE){
					_list.eq(_last).css({opacity:0});
					_list.removeClass('active').eq(_active).addClass('active').css({opacity:'auto'});
				}
				else{
					_list.eq(_last).animate({opacity:0}, _speed);
					_list.removeClass('active').eq(_active).addClass('active').animate({
						opacity:1
					}, _speed, function(){
						if(_this.options.textBox) setTimeout(function(){
							if (!der) 
								_textBox.eq(_active).animate({bottom:0}, _speed, function(){
									_textBox.eq(_last).css({bottom:-typ});
									_last = _active;
									flag = true;
								});
							else
								_textBox.eq(_active).animate({top:0}, _speed, function(){
									_textBox.eq(_last).css({top:-typ});
									_last = _active;
									flag = true;
								});
						}, 500);
						$(this).css('opacity','auto');
					});
				}
				_list.parent().animate({height: _list.eq(_active).outerHeight(true)}, _speed);
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
				if(!_this.options.textBox) _last = _active;
			}
			
			// Function for "slide"
			function scrollEl(){
				if (!_this.options.direction) _list.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
				else _list.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
				if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
			}
			
			function toPrepare(){
				if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
				for (var i = 0; i < _this.options.slideElement; i++){
					_active++;
					if (_active > rew) {
						_active--;
						if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
					}
				};
				if (_active == rew) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
				if (!_this.options.effect) scrollEl();
				else fadeElement();
			}
			_this._hold.bind('runTimer', function(){
				
					if(_this._t) clearTimeout(_this._t);
					_this._t = setInterval(function(){
						if (flag){
							flag = false;
							if(!_this.options.effect) flag = true;
							if(!_this.options.textBox) flag = true;
							toPrepare();
						}
					}, _this._timer);
			});
			_next.click(function(){
				if(flag){
					flag = false;
					if(!_this.options.effect) flag = true;
					if(!_this.options.textBox) flag = true;
					if(_this._t) clearTimeout(_this._t);
					if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
					toPrepare();
					if (_this._timer) _this._hold.trigger('runTimer');
				}
				return false;
			});
			_prev.click(function(){
				if(flag){
					flag = false;
					if(!_this.options.effect) flag = true;
					if(!_this.options.textBox) flag = true;
					if(_this._t) clearTimeout(_this._t);
					if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_this.options.disableBtn);
					if ((_active == 0) && _this.options.circle) _active = rew + _this.options.slideElement;
					for (var i = 0; i < _this.options.slideElement; i++){
						_active--;
						if (_active < 0) {
							_active++;
							if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
						}
					};
					if (_active == 0) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
					if (!_this.options.effect) scrollEl();
					else fadeElement();
					if (_this._timer) _this._hold.trigger('runTimer');
				}
				return false;
			});
			if (_this.options.switcher) _switcher.click(function(){
				if (flag){
					flag = false;
					if(!_this.options.effect) flag = true;
					if(!_this.options.textBox) flag = true;
					_active = _switcher.index($(this));
					if(_this._t) clearTimeout(_this._t);
					if (!_this.options.effect) scrollEl();
					else fadeElement();
					if (_this._timer) _this._hold.trigger('runTimer');
				}
				return false;
			});
			if (_this._timer) _this._hold.trigger('runTimer');
		},
		stop: function(){
			var _this = this;
			if (_this._t) clearTimeout(_this._t);
		},
		play: function(){
			var _this = this;
			if (_this._t) clearTimeout(_this._t);
			if (_this._timer) _this._hold.trigger('runTimer');
		}
	}
}(jQuery));

// jQuery slideShow v1.2.0
jQuery.fn.slideShow = function(_options){    
	// defaults options	    
	var _options = jQuery.extend({		
		slideEl:'div.slide',		
		linkNext:'a.next',		
		linkPrev:'a.prev',		
		linkPause:'a.pause',		
		numElement:'div.slideNav li a',		
		duration:500,		
		autoSlideShow:false,		
		switchTime:3000,
		noCicle:false,
		disableClass:'no-active',
		event:'click',		
		currentEl:'span.cur',		
		allEl:'span.all'    
	},_options);
    return this.each(function(){
	    var _THIS = jQuery(this);
		var _slideEl = $(_options.slideEl, _THIS);
		
		var _linkNext = $(_options.linkNext, _THIS);
		var _linkPrev = $(_options.linkPrev, _THIS);
		var _linkPause = $(_options.linkPause, _THIS);
		var _numElement = $(_options.numElement, _THIS);
		var _currentEl = $(_options.currentEl, _THIS);
		var _allEl = $(_options.allEl, _THIS)

		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _numElActive, _timer = false, _hover = false, _current = 0, _next = 0, _pause = true;
		
		if (!_slideEl.filter('.active').length) {
			_slideEl.eq(0).addClass('active');
			_current = 0;
		} else {
			_current = _slideEl.index(_slideEl.filter('.active'));
		}
		_slideEl.not(".active").hide();
		
		if ($(_numElement).length && _options.numElement) activeNumEl();
		if (_options.autoSlideShow) {
			_pause = false;
			_timer = setTimeout(function(){nextEl()},_switchTime);
			if (_linkPause.length && _options.linkPause) _linkPause.addClass('play')	
			contentHover();
		}
		if (_options.currentEl && _currentEl.length) {
			_allEl.html(_slideEl.length);
			currentNum();
		}
		if (_linkNext.length && _options.linkNext) {
			_linkNext.click(function(){
				nextEl();
				return false;
			});
		}
		if (_options.noCicle) {
			_linkPrev.addClass('prev-'+_options.disableClass);
		}
		if (_linkPrev.length && _options.linkPrev) {	
			_linkPrev.click(function(){
				_linkNext.removeClass('next-'+_options.disableClass);
				if (!_slideEl.is(':animated') && !$(this).hasClass('prev-'+_options.disableClass)) {
					if (_timer) clearTimeout(_timer);
					_next = _current-1;
					if (_next < 0) _next = _slideEl.length-1;
					if (_options.noCicle && _next-1 < 0) {
						$(this).addClass('prev-'+_options.disableClass);
					}
					fadeElement();
					activeNumEl();
					pauseCode();
				}
				return false;
			});
		}
		if (_numElement.length && _options.numElement) {
			_numElement.bind(_options.event, function(){
				if (!_slideEl.is(':animated')) {
					_next = _numElement.index($(this));
					_linkNext.removeClass('next-'+_options.disableClass);
					_linkPrev.removeClass('prev-'+_options.disableClass);
					if (_options.noCicle && _next+1 >= _slideEl.length) _linkNext.addClass('next-'+_options.disableClass);
					if (_options.noCicle && _next-1 < 0) _linkPrev.addClass('prev-'+_options.disableClass);
					if (_timer) clearTimeout(_timer);
					if (!_slideEl.eq(_next).hasClass("active")){
						fadeElement();
						activeNumEl();
						pauseCode();
					}
				}
				return false;
			});
		}
		if (_linkPause.length && _options.linkPause) {
			_linkPause.click(function(){
				if (!_pause) {
					$(this).removeClass('play');
					clearTimeout(_timer);
					_pause = true;
				} else {
					$(this).addClass('play');
					_timer = setTimeout(function(){nextEl()},_switchTime);
					_pause = false;
				}
				return false;
			});
		}
		function fadeElement(){
			if (_current != _next) {
				_slideEl.removeClass('active');
				_slideEl.eq(_current).addClass('hide');
				_slideEl.eq(_next).fadeIn(_duration, function(){
					_slideEl.filter('.hide').hide().removeClass('hide');
				}).addClass('active');
				_current = _next;
				if (_options.currentEl && _currentEl.length) currentNum();
			}
		};
		function currentNum() {
			_currentEl.html(_current+1);
		}
		function activeNumEl() {
			_numElement.parent().removeClass("active");
			_numElement.eq(_current).parent().addClass("active");
		};
		function nextEl(){
			_linkPrev.removeClass('prev-'+_options.disableClass);
			if (!_slideEl.is(':animated') && !_linkNext.hasClass('next-'+_options.disableClass)) {
				if (_timer) clearTimeout(_timer);
				_next = _current+1;
				if (_next == _slideEl.length) _next = 0;
				if (_options.noCicle && _next+1 >= _slideEl.length) {
					_linkNext.addClass('next-'+_options.disableClass);
				}
				fadeElement();
				activeNumEl();
				pauseCode();
			}
		};
		function pauseCode(){
			if (!_pause) {
				if (_linkPause.length && _options.linkPause) {
					if (_linkPause.hasClass('play')) {
						_timer = setTimeout(function(){nextEl()},_switchTime);
					}					
				} else {
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
			}
		}
		function contentHover() {
			_hover = true;
			_slideEl.mouseenter(function() {
				if (_timer) clearTimeout(_timer);
			}).mouseleave(function(){
				pauseCode();
			});			
		}
    });
}
