jQuery(function(){
	clearInputs();
	jQuery('div.navigation, #prev-next').each(function(){
		if(jQuery(this).find('a').length == 0) jQuery(this).remove();
	});
	//showsGallery();
	innerGallery();
	jQuery('div.widget_gallery ul.gall-list').each(function(){
		jQuery(this).find('a').fancybox({
			'overlayShow'	: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
	});
	initPopup();
	jQuery('iframe[src*=youtube]').each(function(){
		if(this.src.indexOf('wmode') == -1) this.src = this.src + '?&wmode=transparent';
	});
	jQuery('.booking-date input').datepicker();
	initTestimonials();
});
/*--- testimonials ---*/
function initTestimonials(){
	/*--- simple fade ---*/
	var stay_time = 5000; //in ms
	var change_speed = 500; //in ms
	jQuery('#testimonials-list blockquote').each(function(){
		var _hold = jQuery(this);
		var _list = _hold.children('div');
		var _a = _list.index(_list.filter('.active:eq(0)')) != -1 ? _list.index(_list.filter('.active:eq(0)')) : 0;
		var _t, _f = true;
		_list.removeClass('active').css({display: 'block', opacity: 0}).eq(_a).addClass('active').css('opacity', 1);
		
		if(_f && stay_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_hold.stop().height(_hold.height()).animate({height: _list.eq(_ind).outerHeight()}, change_speed/2, function(){ jQuery(this).css('height', 'auto');});
				_list.eq(_a).removeClass('active').animate({opacity: 0},{queue:false, duration:change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1},{queue:false, duration:change_speed});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- popup ---*/
function initPopup(){
	if(jQuery('#fader').length == 0) jQuery('body').append('<div id="fader"></div>');
	var _fader = jQuery('#fader');
	_fader.hide();
	var _popup = -1;
	
	jQuery('a.request-callback').each(function(){
		var _el = this;
		if(_el.hash && _el.hash.length > 1){
			_el._popup = jQuery(_el.hash);
			if(_el._popup.length){
				_el.onclick = function(){
					if(_el._popup){
						_popup = _el._popup;
						showPopup();
					}
					return false;
				}
				_el._popup.find('a.btn-close').click(function(){
					hidePopup();
					return false;
				});
			}
		}
	});
	jQuery('div.widget_video a').click(function(){
		jQuery('#video-popup iframe').attr('src', this.href);
		_popup = jQuery('#video-popup');
		showPopup();
		return false;
	});
	jQuery('#video-popup a.btn-close').click(function(){
		jQuery('#video-popup iframe').attr('src', '');
		hidePopup();
		return false;
	});
	_fader.click(function(){
		hidePopup();
		return false;
	});
	
	jQuery(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27) hidePopup();
	});
	function showPopup(){
		_fader.css({
			opacity: 0,
			height: initH(),
			display:'block'
		});
		_popup.css({top: jQuery(window).scrollTop()});
		if(jQuery.browser.msie && jQuery.browser.version < 7) jQuery('div.wrapper:eq(0) select').css('visibility', 'hidden');
		_fader.fadeTo(400, 0.5, function(){
			if(_popup != -1){
				if(jQuery.browser.msie) _popup.show();
				else _popup.fadeIn(200);
				_popup.css({top: jQuery(window).scrollTop()+ (jQuery(window).height() - _popup.outerHeight())/2});
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
			}
		});
	}
	function hidePopup(){
		if(_popup != -1){
			if(jQuery.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
				if(jQuery.browser.version < 7) jQuery('div.wrapper:eq(0) select').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
	}
	function initH(){
		var _h = jQuery('div.wrapper:eq(0)').outerHeight();
		if(_h < jQuery(window).height()) _h = jQuery(window).height();
		if(_h < jQuery('body').height()) _h = jQuery('body').height();
		return _h;
	}
}
/*--- inner gallery ---*/
function innerGallery(){
	var change_speed = 500; //in ms
	jQuery('div.gallery-hold').each(function(){
		var _hold = jQuery(this);
		var img_hold = _hold.find('div.img');
		var btn_prev = _hold.find('a.btn-prev');
		var btn_next = _hold.find('a.btn-next');
		var list_hold = _hold.find('ul.nav');
		var _list = list_hold.children();
		var _btn = list_hold.find('a');
		var _a = _list.index(_list.filter('.active:eq(0)')) != -1 ? _list.index(_list.filter('.active:eq(0)')) : 0;
		
		_btn.eq(_a).data('img', jQuery('<img src="'+_btn.eq(_a).attr('href')+'" />'));
		_btn.eq(_a).data('img').appendTo(img_hold).addClass('active');
		
		_list.removeClass('active').eq(_a).addClass('active');
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		var _f = true;
		btn_prev.click(function(){
			if(_a >  0) changeEl(_a - 1);
			else changeEl(_list.length - 1)
			return false;
		});
		btn_next.click(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		function changeEl(_ind){
			if(_ind != _a && _f){
				_f = false;
				_list.eq(_a).removeClass('active');
				_list.eq(_ind).addClass('active');
				_btn.eq(_a).data('img').removeClass('active').stop().animate({opacity:0}, change_speed);
				if(_btn.eq(_ind).data('img')){
					_btn.eq(_ind).data('img').addClass('active').stop().animate({opacity:1}, change_speed);
					_a = _ind;
					_f = true;
				}
				else{
					_btn.eq(_ind).data('img', jQuery('<img />'));
					_btn.eq(_ind).data('img').css('opacity', 0).appendTo(img_hold).load(function(){
						jQuery(this).addClass('active').animate({opacity: 1}, change_speed);
						_a = _ind;
						_f = true;
					});
					_btn.eq(_ind).data('img').attr('src', _btn.eq(_ind).attr('href'));
				}
			}
		}
	})
}
/*--- shows gallery ---*/
function showsGallery(){
	var move_speed = 300; //in ms
	jQuery('#shows-gallery').each(function(){
		var _hold = jQuery(this);
		var btn_prev = _hold.find('a.btn-prev');
		var btn_next = _hold.find('a.btn-next');
		var list_hold = _hold.find('div.hold > ul');
		var _step = list_hold.children().outerWidth();
		var hold_w = list_hold.parent().width();
		var list_w = list_hold.children().outerWidth() * list_hold.children().length;
		var _m = 0;
		jQuery(window).resize(function(){
			hold_w = list_hold.parent().width();
			if(_m > 0) btn_prev.show();
			else btn_prev.hide();
			if(list_w - _m > hold_w) btn_next.show();
			else btn_next.hide();
		});
		if(list_w - _m <= hold_w){
			btn_prev.hide();
			btn_next.hide();
		}
		else{
			btn_prev.hide();
			btn_next.show();
		}
		
		btn_prev.click(function(){
			moveList(false);
			return false;
		});
		btn_next.click(function(){
			moveList(true);
			return false;
		});
		function moveList(_f){
			hold_w = list_hold.parent().width();
			if(_f){
				if(list_w -_m >= hold_w) _m += _step;
				else _m = 0;
			}
			else{
				if(_m > 0) _m -= _step;
				else _m = Math.ceil((list_w - hold_w)/_step)*_step;
			}
			if(_m > 0) btn_prev.show();
			else btn_prev.hide();
			if(_m < list_w - hold_w) btn_next.show();
			else btn_next.hide();
			list_hold.animate({left:-_m},{queue:false, duration: move_speed});
		}
	});
}
/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
