$(function(){
	clearInputs();
	initPopup();
	initPopupThermal();
	initAccordion();
	ieHover();
	initFlexibleNav();
	initGallery();
	initVideoPopup();
	initVideoPopupVid()
	initVideoPopupVid1()
	initVideoPopupVid2()
	initVideoPopupVid3()
	initVideoPopupVid4()
	initVideoPopupVid5()
	initColors();
	initVideoPlayer();
	PlayerPause();
});

function initVideoPlayer() {
	var runplayer = $("video").VideoJS()
}

function PlayerPause() {
  	var pause = $("video")[0].player.pause();
}

function initColors(){
	var holders = $('div.gallery-holder');
	var colors = $('ul.list-01 div.color');
	var colors1 = $('ul.list-01 div.color').not(':last');
	var colors2 = $('ul.list-01 div.color').filter(':last');
	holders.each(function(galleryIndex){
		var holder = $(this);
		var slides = $('li',holder);
		var slidesArray = new Array;
		for (var i = 0; i < 3; i++) {
			slidesArray.push(slides.slice(i*slides.length/3, (i*slides.length/3)+slides.length/3));
		}
		
		for (var i = 0; i < slidesArray.length; i++) {
			slidesArray[i].each(function(j){
				var slide = $(this);
				var color = $('a.img',slide);
				var slideColor = color.attr('title');
				color.removeAttr('title');
			
				color.bind('click',function(){
					slides.removeClass('active');	
					for (var k = 0; k < slidesArray.length;k++) {
						slidesArray[k].eq(j).addClass('active');
					}
					if (galleryIndex == 0) colors1.css({backgroundColor:slideColor})
					else colors2.css({backgroundColor:slideColor});
					return false;
				});
			});
		}
		slides.filter('.active').first().find('a.img').trigger('click');
	});
}

function initGallery(){
	$('div.main-gallery').slideshow();
	$('div.gallery-holder').galleryCircle();
}

function initVideoPopup(){
	var opener = $('div.btn-border a.watch')
	var popup = $('div.visual div.photo-popup');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

function initVideoPopupVid(){
	var opener = $('div.btn-border a.watch-vid')
	var popup = $('div.visual div.photo-popup');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

function initVideoPopupVid1(){
	var opener = $('div.btn-border a.watch-vid-1')
	var popup = $('div.visual div.photo-popup-1');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

function initVideoPopupVid2(){
	var opener = $('div.btn-border a.watch-vid-2')
	var popup = $('div.visual div.photo-popup-2');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

function initVideoPopupVid3(){
	var opener = $('div.btn-border a.watch-vid-3')
	var popup = $('div.visual div.photo-popup-3');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

function initVideoPopupVid4(){
	var opener = $('div.btn-border a.watch-vid-4')
	var popup = $('div.visual div.photo-popup-4');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

function initVideoPopupVid5(){
	var opener = $('div.btn-border a.watch-vid-5')
	var popup = $('div.visual div.photo-popup-5');	
	var closer = $('a.btn-close',popup);
	
	opener.click(function(){
		popup.show();
		return false;
	});
	
	closer.click(function(){
		popup.hide();
		return false;
	});
}

/*--- gallery function ---*/
jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.btn-prev',
		btNext: 'a.btn-next',
		holderList: 'div.gallery-mask',
		scrollElParent: '>ul',
		scrollEl: '>li',
		numHolder: false,
		numCreate: false,
		step: false,
		innerMargin: 0,
		curPage: false,
		onClick: null,
		easing: 'swing',
		switchTime: false,
		duration : 700
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _next = jQuery(_options.btNext, _this).length ? jQuery(_options.btNext, _this) : false;
		var _prev = jQuery(_options.btPrev, _this).length ? jQuery(_options.btPrev, _this) : false;
		var _holderList = jQuery(_options.holderList, _this);
		var _scrollElParent = jQuery(_options.scrollElParent, _holderList);
		var _scrollEl = jQuery(_options.scrollEl, _scrollElParent);
		var _numHolder = false ;
		if (_options.numHolder) _numHolder = jQuery(_options.numHolder, _this).length ? jQuery(_options.numHolder, _this) : false;
		var _step, _t = null;
		var _widthSum = 0;
		_scrollEl.each(function(){_widthSum += jQuery(this).outerWidth(true);})
		var _startPosition = _scrollEl.index(_scrollEl.filter('.active'));
		if (_startPosition==-1) _startPosition=0;
		var _easing = _options.easing;

		if (!_options.step) _step = _holderList.innerWidth();
		var _margin = _widthSum;
		_scrollElParent.append(_scrollEl.clone(true));
		_scrollElParent.prepend(_scrollEl.clone(true));

		var _offsetStartPosition =0;
		_offsetStartPosition = culcOffset(_startPosition);

		_scrollElParent.css('marginLeft', (-_margin+_options.innerMargin-_offsetStartPosition));

		//auto rotation
		if (_options.switchTime) {
			_t = setTimeout(function(){
				nextSlides();
			},_options.switchTime);
		}
		
		//button next "click"
		if (_next) {
			_next.click(function(){
				if (!_scrollElParent.is(':animated')) {
					if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
					nextSlides();
				}
				return false;
			});
		}

		//button prev "click"
		if (_prev) {
			_prev.click(function(){
				if (!_scrollElParent.is(':animated')) {
					if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
					prevSlides();
				}
				return false;
			});
		}

		//curent position
		function getCurElIndex(){
			var _curMargin = parseInt(_scrollElParent.css('marginLeft')) + _widthSum - _options.innerMargin;
			for(i=0; i < _scrollEl.length; i++){
				if (_curMargin == 0) return i;
				if (_curMargin <= _options.innerMargin) _curMargin += _scrollEl.eq(i).innerWidth();
				else _curMargin -= _scrollEl.eq(i).innerWidth();
				if (_curMargin == _options.innerMargin) return i+1;
			}
		}

		// offset of gallery if when activ element not first at start 
		function culcOffset(_ind){
			var _tmpcounter=0;
			var _pos=0;
			while (_tmpcounter < _ind){
				_pos += _scrollEl.eq(_tmpcounter).outerWidth(true);
				_tmpcounter++;
			};
			return _pos;
		}

		//go next slide
		function nextSlides(){
			if (_t) clearTimeout(_t);
			if (_options.step) {
				_curElIndex = getCurElIndex();
				_step = _scrollEl.eq(_curElIndex).innerWidth();
			};
			_margin = -parseInt(_scrollElParent.css('marginLeft'));
			_margin += _step;
			
			_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
				if (_margin >= _widthSum*2) {
					_margin = _widthSum + (_margin - _widthSum*2);
				}
				_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

				//autoslide
				if (_options.switchTime) {
					_t = setTimeout(function(){
						nextSlides();
					},_options.switchTime)
				}
			}});
		}

		//go prev slide
		function prevSlides(){
			if (_t) clearTimeout(_t);
			if (_options.step) {
				_curElIndex = getCurElIndex();
				if (_curElIndex == 0) _curElIndex= _scrollEl.length;
				_step = _scrollEl.eq(_curElIndex-1).innerWidth();
			};
			_margin = -parseInt(_scrollElParent.css('marginLeft'));
			_margin -= _step;
			_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
				if (_margin < _widthSum) {
					_margin = _widthSum*2 - (_widthSum - _margin);
				}
				_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

				//autoslide
				if (_options.switchTime) {
					_t = setTimeout(function(){
						nextSlides();
					},_options.switchTime)
				}
			}});
		}

		// Number list Create
		jQuery.fn.galleryCircle.numListCreate = function(_numHolder, _scrollEl){
			var _numListElC = '';
			for(var i=0; i<_scrollEl.length; i++){
				_numListElC += '<li><a href="">'+(i+1)+'</a></li>';
			}
			jQuery(_numHolder).html('<ul>'+_numListElC+'</ul>');
		};

		// Number list Activate
		jQuery.fn.galleryCircle.numListActive = function(_numHolder, _scrollEl){
			_curElIndex = getCurElIndex();
			if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);
			if (_numHolder) {
				jQuery('a',_numHolder).removeClass('active');
				jQuery('a',_numHolder).eq(_curElIndex).addClass('active');
			}
		};

		//click on control elemens
		function numClick() {
			jQuery(_options.numHolder, _this).find('a').click(function(){
				if (_t) clearTimeout(_t);
				var _aList = jQuery(_options.numHolder, _this).find('a');
				var _index = _aList.index(jQuery(this));
				_margin = _widthSum + _index * _scrollEl.outerWidth(true);
				_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
					if (_margin >= _widthSum*2) {
						_margin = _widthSum + (_margin - _widthSum*2);
					}
					_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
					_aList.removeClass('active').eq(_index).addClass('active');

					//autoslide
					if (_options.switchTime) {
						_t = setTimeout(function(){
							nextSlides();
						},_options.switchTime)
					}
				}});
				return false;
			});
		};

		// init creating num list
		if (_options.numCreate) jQuery.fn.galleryCircle.numListCreate(_numHolder, _scrollEl);

		// pagination first init (example Page 2/6)
		if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);

		// init activate num list item and init numClick()
		if (_options.numHolder) {
			jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);
			numClick();
		}
	});
}

//create jQuery plugin
$.fn.slideshow = function(options){return new slideshow(this, options);}

//constructor
function slideshow(obj, options){this.init(obj,options)}

//prototype
slideshow.prototype = {
	init:function(obj, options) {
		this.options = $.extend({
			slides:'ul.slideshow >li',
			nextBtn:'a.next',
			prevBtn:'a.prev',
			pagingHolder:'div.btn-slider div div',
			pagingTag:'li',
			createPaging:true,
			autoPlay:true,
			dynamicLoad:false,
			imgAttr:'alt',
			effect:'slideX',//fade, slideX, slideY,
			startSlide:false,
			switchTime:5000,
			animSpeed:450
		},options);
		
		this.mainHolder = $(obj);
		this.slides = $(this.options.slides,this.mainHolder);		
		this.nextBtn = $(this.options.nextBtn,this.mainHolder);
		this.prevBtn = $(this.options.prevBtn,this.mainHolder);
		this.dynamicLoad = this.options.dynamicLoad;
		this.imgAttr = this.options.imgAttr;
		this.animSpeed = this.options.animSpeed;
		this.switchTime = this.options.switchTime;
		this.effect = this.options.effect;
		this.autoPlay = this.options.autoPlay;
		this.previous = -1;
		this.loadingFrame = 1;
		this.busy = false;
		this.direction = 1;
		this.timer;
		this.pagingArray = new Array;
		this.loadArray = new Array;
		this.preloader = new Array;
		this.slidesParent = this.slides.eq(0).parent();
		this.slideW = this.slidesParent.width();
		this.slideH = this.slidesParent.height();
		
		(function(){
			if (this.options.startSlide) this.current = this.options.startSlide
			else {
				var active = -1;
				for(var i = 0; i< this.slides.length-1; i++) {
					if (this.slides.eq(i).hasClass('active')) {
						active = i;
						break;						
					}
				}
				if (active != -1) this.current = active;
				else this.current = 0;
			}
		}).apply(this);
		
		this.initPaging();
		this.setStyles();
		this.bindEvents();
		this.showSlide();
	},
	
	initPaging:function(){
		var obj = this;
		this.pagingHolder = $(this.options.pagingHolder,this.mainHolder);
		
		if (this.options.createPaging) {
			this.pagingHolder.each(function(i){
				var _this = $(this);
				_this.empty();
				var list = $('<ul>');
				for (var i = 0; i < obj.slides.length; i++) $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(list);
				_this.append(list);
			});
		}
		
		this.paging = $(this.options.pagingTag, this.pagingHolder);
		var ratio = Math.ceil(this.paging.length / this.slides.length);
		for (var i = 0; i < ratio; i++) {
			this.pagingArray.push(this.paging.slice(i*this.slides.length, (i*this.slides.length)+this.slides.length));
		}
	},
	
	setStyles:function(){
		//loader
		if (this.dynamicLoad) {
			this.loader = $('<div class="loader">');
			this.loaderDiv = $('<div>').appendTo(this.loader)
			this.loader.append(this.loaderDiv).appendTo(this.slidesParent);
		}
		
		//slides
		if (this.effect == 'fade') {
			this.slides.css({display:'none'});
			this.slides.eq(this.current).css({display:'block'});
		} else if (this.effect == 'slideX'){
			this.slides.css({display: 'none',left:-this.slideW});
			this.slides.eq(this.current).css({display:'block',left:0});
		} else if (this.effect == 'slideY'){
			this.slides.css({display:'none',top:-this.slideH});
			this.slides.eq(this.current).css({display:'block',top:0});
		}
	},
	
	bindEvents:function(){
		var obj = this;
		this.nextBtn.bind('click',function(){
			if (!obj.busy) obj.nextSlide();
			return false;
		});
		
		this.prevBtn.bind('click',function(){
			if (!obj.busy) obj.prevSlide();
			return false;
		});
		
		for (var i = 0; i < this.pagingArray.length; i++) {
			this.pagingArray[i].each(function(i){
				$(this).bind('click',function(){
					if (i != obj.current && !obj.busy) {
						obj.previous = obj.current;
						obj.current = i;
						if (obj.previous > i) obj.direction = -1
						else obj.direction = 1;
						obj.showSlide();
					}
					return false;
				});
			});
		}
		
		if (this.dynamicLoad) this.loader.bind('click',function(){
			obj.abortLoading();
		});
	},
	
	nextSlide:function(){
		this.previous = this.current;
		if (this.current < this.slides.length-1) this.current++
		else this.current = 0;
		this.direction = 1;
		this.showSlide();
	},
	
	prevSlide:function(){
		this.previous = this.current;
		if (this.current > 0) this.current--
		else this.current = this.slides.length-1;
		this.direction = -1;
		this.showSlide();
	},
	
	showSlide:function(){
		var obj = this;
		var _current = this.current;
		this.busy = true;
		clearTimeout(this.timer);
		
		if (typeof this.loadArray[_current] != 'undefined' || !this.dynamicLoad) {
			//slide already loaded
			this.switchSlide();
		
		} else {
			//slide not loaded
			this.showLoading();
			var images = $(this.dynamicLoad,this.slides.eq(this.current));
			if (images.length) {
				var counter = 0;
				images.each(function(){
					var preloader = new Image;
					obj.preloader.push(preloader);
					var img = $(this);
					preloader.onload = function(){
						counter++;
						checkImages();
					}
					preloader.onerror = function(){
						//ignore errors
						counter++;
						checkImages();
					}
					preloader.src = img.attr(obj.imgAttr);
				});
				
				function checkImages(){
					if (counter == images.length) {
						images.each(function(){
							var img = $(this);
							img.attr('src',img.attr(obj.imgAttr));
						});
						successLoad();
					}
				}
				
			} else successLoad();
		}
		
		function successLoad(){
			obj.loadArray[_current] = 1;
			obj.hideLoading();
			obj.switchSlide();
		}
	},
	
	switchSlide:function(){
		var obj = this;
		
		if (this.previous != -1) {
			var nextSlide = this.slides.eq(this.current);
			var prevSlide = this.slides.eq(this.previous);
			
			if (this.effect == 'fade') {
				nextSlide.css({display:'block',opacity:0}).animate({opacity:1},this.animSpeed,function(){
					$(this).css({opacity:'auto'});
				});
				prevSlide.animate({opacity:0},this.animSpeed,callback);
			} else if (this.effect == 'slideX'){
				nextSlide.css({display:'block',left:this.slideW*this.direction}).animate({left:0},this.animSpeed);
				prevSlide.animate({left:-this.slideW*this.direction},this.animSpeed+10,callback);
			} else if (this.effect == 'slideY'){
				nextSlide.css({display:'block',top:this.slideH*this.direction}).animate({top:0},this.animSpeed);
				prevSlide.animate({top:-this.slideH*this.direction},this.animSpeed+10,callback);
			}
		} else {
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
		
		this.refreshStatus();
		
		function callback(){
			prevSlide.css({display:'none'});
			if (obj.autoPlay) obj.startAutoPlay();
			obj.busy = false;
		}
	},
	
	refreshStatus:function(){
		for (var i = 0; i < this.pagingArray.length;i++) {
			this.pagingArray[i].eq(this.previous).removeClass('active');
			this.pagingArray[i].eq(this.current).addClass('active');
		}
		this.slides.eq(this.previous).removeClass('active');
		this.slides.eq(this.current).addClass('active');
	},
	
	showLoading:function(){
		var obj = this;
		this.loader.show();
		clearInterval(this.loadingTimer);
		obj.loadingTimer = setInterval(animateLoading, 66);
		
		function animateLoading(){
			obj.loaderDiv.css('top', obj.loadingFrame * -40);
			obj.loadingFrame = (obj.loadingFrame + 1) % 12;
		}
	},
	
	hideLoading:function(){
		this.loader.hide();
		clearInterval(this.loadingTimer);
	},
	
	abortLoading:function(){
		this.busy = false;
		this.hideLoading();
		this.current = this.previous;
		for (var i = 0; i < this.preloader.length; i++) {
			this.preloader[i].onload = null;
			this.preloader[i].onerror = null;
		}
		if (this.autoPlay) this.startAutoPlay();
	},
	
	startAutoPlay:function(){
		var obj = this;
		clearTimeout(obj.timer);
		obj.timer = setTimeout(function(){
			obj.nextSlide();
		},obj.switchTime);
	}
}

function ieHover() {
	hoverForIE6(".photo", "hover");
	hoverForIE6(".accordion .blue .item", "blue-hover");
	hoverForIE6(".accordion .yellow .item", "yellow-hover");
	hoverForIE6(".accordion .green .item", "green-hover");
	hoverForIE6(".accordion .mauve .item", "mauve-hover");
	hoverForIE6(".accordion .red .item", "red-hover");
	hoverForIE6(".accordion .blue2 .item", "blue2-hover");
	hoverForIE6(".accordion .gray .item", "gray-hover");
	hoverForIE6(".accordion .violet .item", "violet-hover");
	hoverForIE6(".accordion .main-item", "main-hover");
	hoverForIE6(".accordion span", "hover");
}

function initAccordion(){
	jQuery('ul.accordion, ul.accordion ul').acc({
		speed: 300,
		active: 'open',
		list: '.children()',
		opener: 'span',
		slide: '.slider'
	});
}

//ie6 hover
function hoverForIE6(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).mouseenter(function(){
			$(this).addClass(h_class);
		}).mouseleave(function(){
			$(this).removeClass(h_class);
		});
	}
}

function initFlexibleNav()
{
	initNav({
		menuId: "nav",
		hoverClass: "hover",
		sideClasses: true,
		flexibility: true,
		menuPaddings: 0,
		minWidth: "li"
	});
	initNav({
		menuId: "menu1",
		cleverMode: true,
		hoverClass: "hover",
		flexibility: true,
		sideClasses: true,
		menuPaddings: 20,
		minWidth: 20
	});
}
function initNav(o)
{
	if (!o.menuId) o.menuId = "nav";
	if (!o.cleverMode) o.cleverMode = true;
	if (!o.flexibility) o.flexibility = true;
	if (!o.dropExistenceClass) o.dropExistenceClass = true;
	if (!o.hoverClass) o.hoverClass = "hover";
	if (!o.menuHardCodeClass) o.menuHardCodeClass = "menu-hard-code";
	if (!o.sideClasses) o.sideClasses = true;
	if (!o.center) o.center = false;
	if (!o.menuPaddings) o.menuPaddings = 0;
	if (!o.minWidth) o.minWidth = 0;
	if (!o.coeff) o.coeff = 1.7;
	var n = document.getElementById(o.menuId);
	if(n)
	{
		n.className = n.className.replace(o.menuHardCodeClass, "");
		var lfl = [];
		var li = n.getElementsByTagName("li");
		for (var i=0; i<li.length; i++)
		{
			li[i].className += (" " + o.hoverClass);
			var d = li[i].getElementsByTagName("div").item(0);
			if(d)
			{
				if(o.flexibility)
				{
					var a = d.getElementsByTagName("a");
					for (var j=0; j<a.length; j++)
					{
						var w = a[j].parentNode.parentNode.offsetWidth;
						if(w > 0)
						{
							if(typeof(o.minWidth) == "number" && w < o.minWidth)
								w = o.minWidth;
							else if(typeof(o.minWidth) == "string" && li[i].parentNode == n && w < li[i].offsetWidth)
								w = li[i].offsetWidth - 5;
							a[j].style.width = w - o.menuPaddings + "px";
						}
					}
					d.style.width = li[i].getElementsByTagName("div").item(1).clientWidth + "px";
				}
				var t = document.documentElement.clientWidth/o.coeff;
				if(li[i].parentNode != n && (!o.cleverMode || fPX(li[i]) < t))
				{
					d.style.right = "auto";
					d.style.left = li[i].parentNode.offsetWidth + "px";
					d.parentNode.className += " left-side";
				}	
				else if(li[i].parentNode != n && (o.cleverMode || fPX(li[i]) >= t))
				{
					d.style.left = "auto";
					d.style.right = li[i].parentNode.offsetWidth + "px";
					d.parentNode.className += " right-side";
				}
				else if(li[i].parentNode == n && o.cleverMode && fPX(li[i]) >= t)
				{
					li[i].className += " right-side";
				}
				if(li[i].parentNode == n && o.center)
					d.style.left = -li[i].getElementsByTagName("div").item(1).clientWidth/2 + li[i].clientWidth/2 + "px";
			}
			if(o.dropExistenceClass && li[i].getElementsByTagName("ul").length > 0)
			{
				li[i].className += (" " + o.dropExistenceClass);
				li[i].getElementsByTagName("a").item(0).className += (" " + o.dropExistenceClass + "-link");
				li[i].innerHTML += "<em class='pointer'></em>";
			}
			if(li[i].parentNode == n) lfl.push(li[i]);
		}
		if(o.sideClasses)
		{
			lfl[0].className += " first-child";
			lfl[0].getElementsByTagName("a").item(0).className += " first-child-link";
			lfl[lfl.length-1].className += " last-child";
			lfl[lfl.length-1].getElementsByTagName("a").item(0).className += " last-child-link";
		}
		for (var i=0; i<li.length; i++)
		{
			li[i].className = li[i].className.replace(o.hoverClass, "");
			li[i].onmouseover = function()
			{
				this.className += (" " + o.hoverClass);
			}
			li[i].onmouseout = function()
			{
				this.className = this.className.replace(o.hoverClass, "");
			}
		}
	}
	function fPX(a)
	{
		var b = 0;
		while (a.offsetParent) {b += a.offsetLeft; a = a.offsetParent;}
		return b;
	}
}

function clearInputs()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
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";
	}
}

/*--- accordion ---*/
jQuery.fn.acc = function(_options){
	var _options = jQuery.extend({
		speed: 400,
		active: 'active',
		list: '.children()',
		opener: 'a.opener',
		slide: 'div.slide'
	}, _options);
	
	return this.each(function(){
		var _list = eval('jQuery(this)' + _options.list);
		_list.each(function(){
			if($(this).hasClass('open')){
				$(this).children().children().children('span').addClass('active-opener');
			}
		});
		var _active = _options.active;
		var _speed = _options.speed;
		var _a = _list.index(_list.filter('.' + _active + ':eq(0)'));
		if(_a != -1) _list.removeClass(_active).eq(_a).addClass(_active);
		for(var i = 0; i < _list.length; i++){
			_list.eq(i).data('btn', _list.eq(i).children().children(_options.opener).eq(0));
			_list.eq(i).data('box', _list.eq(i).children(_options.slide).eq(0));
			if(i == _a) _list.eq(i).data('box').css('display', 'block');
			else _list.eq(i).data('box').css('display', 'none');
			_list.eq(i).data('btn').data('ind', i);
			_list.eq(i).data('btn').click(function(){
			    if (_list.eq(jQuery(this).data('ind')).data('box').length != 0) {
					changeEl(jQuery(this).data('ind'));
					return false;
				}
			});
		}
		var anim_f = true;
		var a_h, ind_h, _k;
		function changeEl(_ind){
		    if(anim_f){
		        anim_f = false;
		        if(_a == _ind){
					_list.eq(_a).find('span').removeClass('active-opener');
		            _list.eq(_a).removeClass(_active).data('box').animate({height: 0}, {
		                duration: _speed,
		                complete: function(){
		                    jQuery(this).css({display:'none', height:'auto'});
		                    _a = -1;
		                    anim_f = true;
							removeLevels();
		                }
		            });
					
		        }
		        else{
		            _list.eq(_ind).data('box').css('display', 'block');
		            ind_h = _list.eq(_ind).data('box').outerHeight();
		            _list.eq(_ind).data('box').height(0);
		            if(_a != -1){
						_list.eq(_a).find('span').removeClass('active-opener');
		                a_h = _list.eq(_a).removeClass(_active).data('box').outerHeight();
		                _k = a_h/ind_h;
		            }
					_list.eq(_ind).children().children('span').addClass('active-opener');
		            _list.eq(_ind).addClass(_active).data('box').animate({height: ind_h}, {
		                duration: _speed,
		                step: function(t_h){
		                    if(_a != -1) _list.eq(_a).data('box').height(a_h - t_h*_k);
		                },
		                complete: function(){
		                    _list.eq(_ind).data('box').height('auto');
		                    if(_a != -1) _list.eq(_a).data('box').css({display:'none', height: 'auto'});
		                    _a = _ind;
		                    anim_f = true;
							removeLevels();
		                }
		            });
		        }
		    }
		}
	});
}

function removeLevels(){
	$('ul.accordion').eq(0).children('li').each(function(){
		if(!$(this).hasClass('open')){
			var _cont = $(this).find('div.slide').find('li.open');
			$(this).find('span').removeClass('active-opener');
			_cont.each(function(){
				if($(this).hasClass('open')){
					$(this).find('span').click();
				}
			});
		}
	});
}

//Popup Lightbox
function initPopup(){
	var _btn = $('div.section').find('a.photo');
	var change_speed = 1000; 
	var _fader = $('<div id="jquery-overlay"></div>');
	var _height = 0;
	var _width = 0;
	var _page = $('body > div:eq(0)');
	$('body').append(_fader);
	var _minWidth = _page.outerWidth();
		if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
		if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
	_fader.css({
		'position':'absolute',
		'top':0,
		'left':0,
		'backgroundColor':'#000',
		'zIndex':'101',
		'opacity':'0'
	});
	_fader.hide();
	var _foolFlag = false;
	_btn.each(function(_num){
		var _href = $(this).attr('href');
		var _title = $(this).find('img').attr('alt');
		var _box = $('<div class="popup-holder" id="lightbox'+(_num+1)+'"><div class="image-holder"><img src="'+_href+'" alt="image" /></div><div class="text-holder"><p>'+_title+'</p><a class="closer" href="#">Close</a></div></div>');
		$('body').append(_box);
		_box.css('opacity','0');
		_box.css('top', -9999);
		$(this).click(function(){
			if(_foolFlag == false){
				_foolFlag = true;
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				}
				else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
				if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
				var _overFlag = false;
				var _top = $(window).scrollTop() + 100;
				_box.css('top', _top);
				_fader.show();
				var _Opopup = $('#lightbox'+(_num+1)+'');
				_fader.animate({opacity: 0.7}, {queue:false, duration:change_speed});
				_box.animate({opacity: 1}, {queue:false, duration:change_speed});
				var _closer = $('#lightbox'+(_num+1)+'').find('a.closer');
				_closer.click(function(){
					_overFlag = true;
					_fader.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_fader.hide();
						_foolFlag = false;
					}});
					_box.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_box.css('top', -9999);
						_overFlag = true;
					}});
					return false;
				});
				$('div.popup-holder').mouseover(function(){
					_overFlag = true;
				}).mouseout(function(){
					_overFlag = false;
				});
				var _body = $('body');
				_body.click(function(){
					if (_overFlag == false) {
					_overFlag = true;
					_fader.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_fader.hide();
						_foolFlag = false;
					}});
					_box.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_box.css('top', -9999);
					}});
					}
				});
			}
			return false;
		});
	});
		$(window).resize(function(){
			if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
		if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
		});
}

//Popup Lightbox Thermal - 766px
function initPopupThermal(){
	var _btn = $('div.columns').find('a.photo');
	var change_speed = 1000; 
	var _fader = $('<div id="jquery-overlay"></div>');
	var _height = 0;
	var _width = 0;
	var _page = $('body > div:eq(0)');
	$('body').append(_fader);
	var _minWidth = _page.outerWidth();
		if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
		if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
	_fader.css({
		'position':'absolute',
		'top':0,
		'left':0,
		'backgroundColor':'#000',
		'zIndex':'101',
		'opacity':'0'
	});
	_fader.hide();
	var _foolFlag = false;
	_btn.each(function(_num){
		var _href = $(this).attr('href');
		var _title = $(this).find('img').attr('alt');
		var _box = $('<div class="popup-holder2" id="lightbox'+(_num+1)+'"><div class="image-holder"><img src="'+_href+'" alt="image" /></div><div class="text-holder"><p>'+_title+'</p><a class="closer" href="#">Close</a></div></div>');
		$('body').append(_box);
		_box.css('opacity','0');
		_box.css('top', -9999);
		$(this).click(function(){
			if(_foolFlag == false){
				_foolFlag = true;
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				}
				else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
				if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
				var _overFlag = false;
				var _top = $(window).scrollTop() + 100;
				_box.css('top', _top);
				_fader.show();
				var _Opopup = $('#lightbox'+(_num+1)+'');
				_fader.animate({opacity: 0.7}, {queue:false, duration:change_speed});
				_box.animate({opacity: 1}, {queue:false, duration:change_speed});
				var _closer = $('#lightbox'+(_num+1)+'').find('a.closer');
				_closer.click(function(){
					_overFlag = true;
					_fader.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_fader.hide();
						_foolFlag = false;
					}});
					_box.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_box.css('top', -9999);
						_overFlag = true;
					}});
					return false;
				});
				$('div.popup-holder2').mouseover(function(){
					_overFlag = true;
				}).mouseout(function(){
					_overFlag = false;
				});
				var _body = $('body');
				_body.click(function(){
					if (_overFlag == false) {
					_overFlag = true;
					_fader.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_fader.hide();
						_foolFlag = false;
					}});
					_box.animate({opacity: 0}, {queue:false, duration:change_speed, complete:function(){
						_box.css('top', -9999);
					}});
					}
				});
			}
			return false;
		});
	});
		$(window).resize(function(){
			if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
		if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
		});
}

$(document).ready(function(){
	initPopup();
	initPopupThermal();
	jQuery('ul.accordion, ul.accordion ul').acc({
		speed: 300,
		active: 'open',
		list: '.children()',
		opener: 'span',
		slide: '.slider'
	});
});
