/*--- IE6 hover function ---*/
function ieHover(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).live('mouseover', function(){
			$(this).addClass(h_class);
		}).live('mouseout', function(){
			$(this).removeClass(h_class);
		});
	}
}

/*---- clear inputs function ---*/
function clearInputs(h_hold){
	if(!h_hold) var h_hold = $('body');
	h_hold.find('input:text, input:password, textarea').not($('input.input-file').siblings('input')).each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
/*--- input type file function ---*/
function inputFile(h_hold){
	if(!h_hold) var h_hold = $('body');
	h_hold.find('input.input-file').each(function(){
		var _el = $(this);
		var _field = _el.siblings('input:text');
		var _parent = _el.parent();
		_field.attr('readOnly', true);
		_el.change(function(){
			_field.val(this.value);
		}).hover(function(){
			_parent.addClass('file-hover');
		}, function(){
			_parent.removeClass('file-hover');
		}).css('opacity', 0);
	});
}
/*--- accordion function ---*/
function initAccordion(h_hold){
	if(!h_hold) var h_hold = $('body');
	h_hold.find('div.archive > ul.video-list').each(function(){
		var _list = $(this).children();
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a != -1){
			_list.removeClass('active').eq(_a).addClass('active');
		}
		_list.each(function(_i){
			var _el = $(this);
			_el.removeClass('active');
			this._btn = _el.children('div.heading');
			this._box = _el.children('div.datalist');
			this._h = this._box.height();
			if(_i == _a) this._box.show();
			else this._box.hide();
			this._btn.click(function(){
				if(_a != -1){
					_list.eq(_a).removeClass('active');
					_list.get(_a)._box.stop().animate({height: 0}, 400, function(){
						$(this).css({display: 'none', height: 'auto'});
					});
				}
				if(_a != _i){
					_list.eq(_i).addClass('active');
					if(_list.get(_i)._box.is(':hidden')){
						_list.get(_i)._box.show();
						_list.get(_i)._h = _list.get(_i)._box.height();
						_list.get(_i)._box.height(0);
					}
					_list.get(_i)._box.stop().animate({ height: _list.get(_i)._h}, 400, function(){
						$(this).height('auto');
					});
					_a = _i;
				}
				else{
					_a = -1;
				}
				return false;
			});
		});
	});
}
/*--- vertical slide function ---*/
function verticalSlide(h_hold){
	if(!h_hold) var h_hold = $('body');
	h_hold.find('div.v-gallery-1, div.v-gallery-2').each(function(){
		var _hold = $(this);
		var btn_top = _hold.find('a.btn-up');
		var btn_bottom = _hold.find('a.btn-down');
		var list_hold = _hold.find('div.frame > ul');
		var _list = list_hold.children();

		var hold_h = list_hold.parent().height();
		var list_h = 0;
		var _a = 0;
		var _t = 0;

		for(var i = 0; i < _list.length; i++){
			list_h += _list.eq(i).outerHeight(true);

			// TROS_MOD
			if(_list.eq(i).hasClass('active')) {
			    if(i > 0) {
			     _a = i;
			     _t = _list.eq(i).outerHeight() * i;
			     moveListTo(_t);
			    }
			}
		}

		if(list_h > hold_h){
			btn_top.addClass('not-available');
			btn_bottom.removeClass('not-available');
		}
		else{
			btn_top.addClass('not-available');
			btn_bottom.removeClass('not-available');
		}
		if(_t > 0 && btn_top.hasClass('not-available')) {
		    btn_top.removeClass('not-available')
		}
		btn_top.click(function(){
			if(!btn_top.hasClass('not-available')) moveList(false);
			return false;
		});
		btn_bottom.click(function(){
			if(!btn_bottom.hasClass('not-available')) moveList(true);
			return false;
		});
		function moveList(_f){
			if(_f) _t += _list.eq(++_a).outerHeight();
			else _t -= _list.eq(--_a).outerHeight();

			// set button available/not-available
			if(_t == 0) btn_top.addClass('not-available');
			else btn_top.removeClass('not-available');
			if(list_h - _t <= hold_h) btn_bottom.addClass('not-available');
			else btn_bottom.removeClass('not-available');

			list_hold.animate({top:-_t},{ queue:false, duration: 500});
		}

		/**
		  * TROS_MOD: Move scroll list to position X
		  *
		  *   _x = position to scroll to
		  *
		  */
        function moveListTo(_x){
            if(_x > 0) {
		      list_hold.animate({top:-_x},{ queue:false, duration: 500});
            }
		}
	});
}
/*---- horizontal gallery ---*/
function horizontalSlide(h_hold){
	if(!h_hold) var h_hold = $('body');
	h_hold.find('div.h-gallery-1').each(function(){
		var _hold = $(this);
		var info_hold = _hold.siblings('div.album-gall-hold');
		var btn_prev = _hold.find('a.btn-prev');
		var btn_next = _hold.find('a.btn-next');
		var list_hold = _hold.find('div.holder > ul');
		var _btn = list_hold.children();
		var _a = _btn.index(_btn.filter('.active')); // TROS_MOD: changed active into .active to make it work
		var hold_w = list_hold.parent().width();
		var list_w = list_hold.children().length * list_hold.children().outerWidth();
		var _step = hold_w;
		var _m = 0;
		if(list_w > hold_w){
			btn_prev.addClass('not-available');
			btn_next.removeClass('not-available');
		}
		else{
			btn_prev.addClass('not-available');
			btn_next.addClass('not-available');
		}
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			var _el = this;
			_el._f = false;
			_el._box = $(_el).find('div.album-gall-el');
			if(_i == _a){
				info_hold.append(_el._box);
				_el._f = true;
				_el._box.addClass('album-gall-el-active');
			}
			/*
			_el.onclick = function(){
				changeEl(_i);
				return false;
			}
			*/
		});
		// TROS_MOD
		if(_a > 0) {
		    var _p = parseInt( ((_a)/4) );
		    _m = _p * _step;
		    moveListTo( _p, _step);
		}

		btn_prev.click(function(){
			if(!btn_prev.hasClass('not-available')) moveList(false);
			return false;
		});
		btn_next.click(function(){
			if(!btn_next.hasClass('not-available')) moveList(true);
			return false;
		});
		function changeEl(_ind){
			if(_ind != _a){
				if(!_btn.get(_ind)._f){
					if($.browser.msie) _btn.get(_ind)._box.hide();
					else _btn.get(_ind)._box.css('opacity', 0);
					info_hold.append(_btn.get(_ind)._box);
					_btn.get(_ind)._f = true;
				}
				if($.browser.msie){
					_btn.get(_a)._box.removeClass('album-gall-el-active').hide();
					_btn.get(_ind)._box.addClass('album-gall-el-active').show();
				}
				else{
					_btn.get(_a)._box.stop().removeClass('album-gall-el-active').animate({opacity: 0}, 700, function(){ $(this).hide();});
					_btn.get(_ind)._box.stop().show().addClass('album-gall-el-active').animate({opacity: 1}, 700);
				}
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_a = _ind;
			}
		}
		function moveList(_f){
			if(_f) _m += _step;
			else _m -= _step;
			if(list_w - _m <= hold_w) btn_next.addClass('not-available');
			else btn_next.removeClass('not-available');
			if(_m == 0) btn_prev.addClass('not-available');
			else btn_prev.removeClass('not-available');
			list_hold.animate({left: -_m}, {queue: false, duration: 800});
		}


		/**
		  * TROS_MOD: Move scroll list to position X
		  *
		  *   _x = position to scroll to
		  *
		  */
        function moveListTo(_n, _s){
            var _x = _n * _s;
            if(_x > 0) {
		      list_hold.animate({left:-_x},{ queue:false, duration: 800});
            }
            if(_n > 0) {
                btn_prev.removeClass('not-available');
            }
		}
	});
}
/*--- ajax integration ---*/
function ajaxInt(){
	$('div.with-ajax').each(function(){
		var _cont = $(this);
		var _hold = _cont.find('div.ajax-hold');
		var _list = _cont.find('ul.ajax-nav > li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').eq(_a).addClass('active');
		var _f = false;
		_list.each(function(_i){
			var _el = this;
			_el._box = false;
			_el._url = $(_el).find('a').attr('href');
			if(_i == _a){
				$.ajax({
					url: _el._url,
					dataType: 'html',
					success: function(_html){
						_el._box = $(_html);
						_hold.append(_el._box);
						_el._box.addClass('ajax-el-active');
						afterAjax(_el._box);
						_f = true;
					}
				});
			}
			_el.onclick = function(){
				changeEl(_i);
				return false;
			}
		});
		function changeEl(_ind){
			if(_ind != _a && _f){
				_f = false;
				if(_list.get(_ind)._box){
					if($.browser.msie){
						_list.get(_a)._box.removeClass('ajax-el-active').hide();
						_list.get(_ind)._box.addClass('ajax-el-active').show();
					}
					else{
						_list.get(_a)._box.stop().removeClass('ajax-el-active').animate({opacity: 0}, 800, function(){ $(this).hide();});
						_list.get(_ind)._box.stop().show().addClass('ajax-el-active').animate({opacity: 1}, 800);
					}
					_list.eq(_a).removeClass('active');
					_list.eq(_ind).addClass('active');
					_a = _ind;
					_f = true;
				}
				else{
					_hold.height(_hold.height());
					if($.browser.msie){
						$.ajax({
							url: _list.get(_ind)._url,
							dataType: 'html',
							success: function(_html){
								_list.get(_ind)._box = $(_html);
								_list.get(_ind)._box.hide();
								_hold.append(_list.get(_ind)._box);
								_list.get(_a)._box.removeClass('ajax-el-active').hide();
								_list.get(_ind)._box.addClass('ajax-el-active').show();
								afterAjax(_list.get(_ind)._box);
								_hold.height('auto');
								_list.eq(_a).removeClass('active');
								_list.eq(_ind).addClass('active');
								_a = _ind;
								_f = true;
							}
						});
					}
					else{
						$.ajax({
							url: _list.get(_ind)._url,
							dataType: 'html',
							success: function(_html){
								_list.get(_ind)._box = $(_html);
								_list.get(_ind)._box.css('opacity', 0);
								_hold.append(_list.get(_ind)._box);
								_list.get(_a)._box.stop().removeClass('ajax-el-active').animate({opacity: 0}, 800, function(){ $(this).hide();});
								_list.get(_ind)._box.addClass('ajax-el-active').animate({opacity: 1}, 800);
								afterAjax(_list.get(_ind)._box);
								_hold.height('auto');
								_list.eq(_a).removeClass('active');
								_list.eq(_ind).addClass('active');
								_a = _ind;
								_f = true;
							}
						});
					}
				}

			}
		}
	});
}
/*---lightbox function ---*/
function initLightbox(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _fader = $('#fader');
	var _popup = -1;
	_fader.css({
		display: 'none',
		opacity: 0,
		height: initH(),
		width: '100%'
	});
	$('a.with-popup').live('click', function(){
		displayPopup(this);
		return false;
	});
	function displayPopup(_btn){
		_fader.css({opacity: 0, display:'block', height: initH()});
		if(_btn._popup){
			_fader.fadeTo(300, 0.5, function(){
				_popup = _btn._popup;
				if($.browser.msie) _popup.show();
				else _popup.fadeIn(400);
//				_popup.css('top', ($(window).scrollTop() + $(window).height()/2 - _popup.outerHeight()/2));
				_popup.css('top', ($(window).scrollTop() + 120));
				_fader.height(initH());
			});
		}
		else{
			_fader.fadeTo(300, 0.5, function(){
				$.ajax({
					url: _btn.href,
					dataType: 'html',
					success: function(_html){
						_btn._popup = $(_html);
						_btn._popup.css('display','none');
						afterAjax(_btn._popup);
						_btn._popup.appendTo('body').find('a.close-btn').click(function(){
							if($.browser.msie){
								_popup.hide();
								_popup = -1;
								_fader.fadeOut(400);
							}
							else{
								_popup.fadeOut(300, function(){
									_popup = -1;
									_fader.fadeOut(400);
								});
							}
							return false;
						});
						setTimeout(function(){
							_popup = _btn._popup;
							if($.browser.msie) _popup.show();
							else _popup.fadeIn(400);
//							_popup.css('top', ($(window).scrollTop() + $(window).height()/2 - _popup.outerHeight()/2));
							_popup.css('top', ($(window).scrollTop() + 120));
							_fader.height(initH());
						}, 100);
					}
				});
			});
		}
	}
	$(document).keydown(function(e){
		if(!e) evt = window.event;
		if (e.keyCode == 27 && _popup){
			if($.browser.msie){
				_popup.hide();
				_popup = -1;
				_fader.fadeOut(400);
			}
			else{
				_popup.fadeOut(300, function(){
					_popup = -1;
					_fader.fadeOut(400);
				});
			}
		}
	});
	_fader.click(function(){
		if(_popup != -1){
			if($.browser.msie){
				_popup.hide();
				_popup = -1;
				_fader.fadeOut(400);
			}
			else{
				_popup.fadeOut(300, function(){
					_popup = -1;
					_fader.fadeOut(400);
				});
			}
		}
		return false;
	});
}
/*--- page height detect function ---*/
function initH(){
	var _h = $('#page').outerHeight();
	if(_h < $(window).height()) _h = $(window).height();
	if(_h < $('body').height()) _h = $('body').height();
	return _h;
}
/*--- after ajax function ---*/
function afterAjax(_hold){
	horizontalSlide(_hold);
//	clearInputs(_hold);
	inputFile(_hold);
}

$(document).ready(function(){
//	clearInputs();
	inputFile();
	ieHover('.video-list li, .purple-scheme .video-list li, .photo-gallery li .frame, .purple-scheme .photo-gallery li .frame, .gallery-list li .frame, .purple-scheme .gallery-list li .frame, .video-gallery li .frame, .purple-scheme .video-gallery li .frame', 'hover');
	initAccordion();
	verticalSlide();
	horizontalSlide();
	ajaxInt();
	initLightbox();
});