jQuery.fn.vvGalleryScroll = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		btnPrev: 'a.link-prev',
		btnNext: 'a.link-next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		thumbs: false,
		thumbsCreate: false,
		duration : 400,
		step: false,
		pause: false,
		event: 'click',
		circleSlide: true,
		disableClass: 'disable',
		autoSlide:false,
		onBeforeChange: null,
		onChange: null
	},_options);

	return this.each(function(){
		var _this = this;

		var _holderBlock = jQuery(_options.holderList,_this);
		var _moover = jQuery(_options.scrollElParent,_holderBlock).css('position','relative');
		var _liWidth = jQuery(_options.scrollEl,_moover).outerWidth(true);
		var _gWidth = _holderBlock.width();
		var _liSum = jQuery(_options.scrollEl,_moover).length * _liWidth;
		var _thumbs = _options.thumbs ? (jQuery(_options.thumbs,_this).length ? jQuery(_options.thumbs,_this): false) : false;
		var _step = 0, _t = null, _mM=0;
		var _pause =  _options.pause ? jQuery(_options.pause,_this) : false;
		var _play = _options.play ? jQuery(_options.play,_this) : false;
		var _a = 0;
		if(_options.thumbsCreate){
			var _html = '<ul>';
			for(var i=0; i<jQuery(_options.scrollEl,_moover).length; i++) {
				_html+='<li><a href="#">'+(i+1)+'</a></li>';
			};
			_html += '</ul>';
			_thumbs.html(_html);
			_thumbs = _thumbs.find('a');
		}
		if(_thumbs) _thumbs.removeClass('active').eq(_a).addClass('active');
//step
		if(typeof(_options.step)=='number') _step = _options.step;
		else if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;
		if(!_options.circleSlide){
			if(_mM == 0) jQuery(_options.btnPrev,_this).addClass(_options.disableClass);
			else if(_mM ==_liSum-_gWidth) jQuery(_options.btnNext,_this).addClass(_options.disableClass);
		};
//prev btn
		if(jQuery(_options.btnPrev, _this)){
			jQuery(_options.btnPrev, _this).bind(_options.event, function(){
				if(!jQuery(this).hasClass(_options.disableClass)){
					jQuery(_options.btnNext,_this).removeClass(_options.disableClass);
					if(_mM - _step<0) (_mM!=0) ? _mM=0 : (_options.circleSlide ? _mM = _liSum - _gWidth : jQuery(_options.btnPrev,_this).addClass(_options.disableClass));
					else _mM -= _step;
					if(_a!= 0) _a--;
					else _a =jQuery(_options.scrollEl,_moover).length-1;
					_this.rotate(_mM);
				}
				return false;
			});
		};
//next btn
		if(jQuery(_options.btnNext, _this)){
			jQuery(_options.btnNext, _this).bind(_options.event, function(){
				if(!jQuery(this).hasClass(_options.disableClass)){
					jQuery(_options.btnPrev,_this).removeClass(_options.disableClass);
					if(_mM + _step >_liSum - _gWidth) (_mM!= _liSum - _gWidth) ? (_mM = _liSum - _gWidth) : (_options.circleSlide ? _mM = 0 : jQuery(_options.btnNext,_this).addClass(_options.disableClass));
					else _mM += _step;
					if(_a!= jQuery(_options.scrollEl,_moover).length-1) _a++;
					else _a =0;
					_this.rotate(_mM);
				}
				return false;
			});
		};
//animate
		_this.rotate = function(_mM){
			if(_t) clearTimeout(_t);
			if (jQuery.isFunction(_options.onBeforeChange)) {
				_options.onBeforeChange.apply(_this);
			}
			if(!_options.circleSlide){
				if(_mM == 0) jQuery(_options.btnPrev,_this).addClass(_options.disableClass);
				else if(_mM ==_liSum-_gWidth) jQuery(_options.btnNext,_this).addClass(_options.disableClass);
			};
			if(_thumbs) _thumbs.removeClass('active').eq(_a).addClass('active');
			_moover.animate({left: -_mM},{duration: _options.duration, queue: false, complete: function(){
				if (jQuery.isFunction(_options.onChange)) {
					_options.onChange.apply(_this);
				}
				_this.autoslide();
			}});
		};
//autoslide
		_this.autoslide = function(){
			if(_options.autoSlide && _liSum >= _gWidth){
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					jQuery(_options.btnPrev,_this).removeClass(_options.disableClass);
					if(_mM + _step >_liSum - _gWidth) (_mM!= _liSum - _gWidth) ? (_mM = _liSum - _gWidth) : (_options.circleSlide ? _mM = 0 : jQuery(_options.btnNext,_this).addClass(_options.disableClass));
					else _mM += _step;
					if(_a!= jQuery(_options.scrollEl,_moover).length-1) _a++;
					else _a =0;
					_this.rotate(_mM);
				}, _options.autoSlide);
			};
		};
		_this.autoslide();
//disable btns next/prev if need
		if (_liSum <= _gWidth) {
			jQuery(_options.btnPrev,_this).addClass(_options.disableClass).unbind(_options.event).click(function(){return false;});
			jQuery(_options.btnNext,_this).addClass(_options.disableClass).unbind(_options.event).click(function(){return false;});
		};
//thumbs
		_thumbs.bind(_options.event, function(){
			var _ind = _thumbs.index(this);
			_a = _ind;
			_thumbs.removeClass('active').eq(_a).addClass('active');
			_this.rotate(_step*_ind);
			return false;
		});
	//play|pause
		if(_pause && _pause.length){
			_pause.click(function(){
				_play.removeClass('active');
				jQuery(this).addClass('active');
				if(_t) clearTimeout(_t);
				return false;
			});
		};
		if(_play && _play.length){
			_play.click(function(){
				_pause.removeClass('active');
				jQuery(this).addClass('active');
				_this.autoslide();
				return false;
			});
		};
	});
};

jQuery.fn.dynamicBlocks = function(_opt){
	// defaults options
	var _opt = jQuery.extend({
		opener:'a',
		changeStyle:'left',
		setParam:'0',
		event:'click',
		duration:500
	},_opt);
	
	return this.each(function(){
	var _obj = jQuery(this);
		var _defParam = parseInt(_obj.css(_opt.changeStyle));
		var _startParam = parseInt(_obj.css(_opt.changeStyle));
		var _setParam = parseInt(_opt.setParam);
		var _opener = jQuery(_opt.opener, _obj);
		var _duration = _opt.duration;
		var _style = _opt.changeStyle.toString();
		
		_obj.addClass("blockDefault")
		// clic event
		if (_opt.opener && _opt.event != 'move') {
			_opener.bind(_opt.event, function(){
				if (!_obj.hasClass('blockChange')){
					_obj.addClass('blockChange');
					_obj.removeClass("blockDefault");
					eval('_obj.animate({'+_style+' : '+_setParam+'}, '+_duration+', false)');
				} else {
					eval('_obj.animate({'+_style+' : '+_defParam+'}, '+_duration+', false, function(){_obj.removeClass("blockChange"), _obj.addClass("blockDefault")})');
				}
				return false; 
			});
		// move event
		} else if (_opt.event == 'move') {
			var _changeX = false, _revers = 1;
			// set change parametr
			if (_style == 'left' || _style == 'right' || _style == 'marginLeft' || _style == 'marginRight') _changeX = true;
			if (_style == 'bottom' || _style == 'right' || _style == 'marginRight' || _style == 'marginBottom') _revers = -1;
			
			_opener.click(function(){return false});
			var _firstDown = true, _xDef, _yDef;
			_opener.bind('mousedown', function(e){
				if (!e) e = window.event;
				_xDef = e.pageX;
				_defParam = parseInt(_obj.css(_opt.changeStyle));
				$(document).bind('mousemove', function(ev){
					if (!ev) ev = window.event;
					var _x = ev.pageX;
					var _def, _change, _dif;
					_obj.removeClass('blockDefault').addClass('blockChange');
					_dif = _defParam + (_x-_xDef)*_revers;
					if(_dif>_setParam){_dif = _setParam;}
					if(_dif< _startParam){_dif = _startParam;_obj.removeClass('blockChange').addClass('blockDefault');}
					_obj.css(_style, _dif);

					return false;
				});
				$(document).bind('mouseup', function(e){
					$(document).unbind('mousemove');
					$(document).unbind('mouseup');
					return false;
				});
				return false;
			});
		}
	});
}

/*fadeGallery*/
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		listSelector: '> li',
		navHolder:		false,
		navCreate:		false,
		thumbsSelector: 'li',
		prev:			'a.prev',
		next:			'a.next',
		swichTime:		false,
		event:			'click',
		delay:			900,
		fadeIEfix:		false,
		onChange:		null
	},_options);
	return this.each(function(){
		var _swichTime = _options.swichTime;
		var _d = (_options.fadeIEfix) ? ($.browser.msie ? 0 : _options.delay) : (_options.delay);
		var _this = $(this);
		var _list = $(_options.listSelector, _this);
		var _linksHold = $(_options.navHolder, _this);

		if(_options.navCreate){
			var _htmlNav ='<ul>';
			for(var i=0; i<_list.length; i++) {
				_htmlNav += '<li><a href="#">'+(i+1)+'</a></li>';
			}
			_htmlNav +='</ul>';
			_linksHold.html(_htmlNav);
		}
		var _links = $(_options.thumbsSelector, _linksHold);
		var _btnPrev = $(_options.prev , _this);
		var _btnNext = $(_options.next , _this);
		var _a = _links.index(_links.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var _t;
		_list.removeClass('active').css({display: 'none', opacity: 0}).eq(_a).addClass('active').css({display: 'block', opacity: 1}).css('opacity', 'auto');
		_links.eq(_a).addClass('active');

		autoSwitch();
		function autoSwitch(){
			if (_swichTime){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, _swichTime);
			}
		}

		if (_btnPrev){
			_btnPrev.click(function(){
				var _prevItem = 0;
				if (_a > 0) _prevItem = _a-1;
				else _prevItem = _list.length-1;
				changeEl(_prevItem);
				return false;
			})
		}
		if (_btnNext){
			_btnNext.click(function(){
				var _nextItem = 0;
				if (_a < _list.length - 1) _nextItem = _a+1;
				else _nextItem = 0;
				changeEl(_nextItem);
				return false;
			})
		}
		if(_links){
			_links.bind(_options.event,function(){
				var _ind = _links.removeClass('active').index($(this).addClass('active'));
				changeEl(_ind);
				return false;
			});
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_list.is(':animated')) _list.stop(true, true);
			if(_ind != _a){
				_links.removeClass('active').eq(_ind).addClass('active');
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
				_list.eq(_ind).addClass('active').css({opacity: 0, display:'block'}).animate({opacity: 1}, {queue:false, duration:_d,complete:function(){
					$(this).css('opacity', 'auto');
					_a = _ind;
					autoSwitch();
					if (jQuery.isFunction(_options.onChange)) {
						_options.onChange.apply(_this);
					};
				}});
			}
		}
	});
};

$(document).ready(function(){

	if($('#facebook-feed').length) {
		Feed.init();
	}

	padFooter();
	
	$(window).bind('resize',function() {
		padFooter();
	});
	
	
	
   //ADAM - NEXT 16 LINES OF CODE - SET UP LOGO AS HOME BUTTON
   $.logoStartPos_HOME = 0;

	//apply a listener to the logo box
   $('#header .logo a').bind('mousedown',function() {
     $.logoStartPos_HOME = $(this).parent().parent().position().left;
   });
   
     //apply a mouseup event listener here
     $('#header .logo a').bind('mouseup',function() {
		if($.logoStartPos_HOME == $(this).parent().parent().position().left){
		//load the homepage via AJAX - if we're not already on it
			var thisLocation = location.href.replace(/#/,'');
			var homeLocation = 	$('link[rel=index]').attr('href').replace(/\?$/,''); //replace the trailing slash, if it exists
			if(thisLocation != homeLocation) {
				loadContent(homeLocation);	
			}
			
			collapseReserve();					
			
		}
     });
     

	$('.slide-hold').dynamicBlocks({
		opener:'.logo',
		changeStyle:'left',
		setParam:'0',
		event:'move'
	});
	
	$('.top-form-tabs').each(function(){
		/*var _links = $('a.tab', this);
		var _tabs = $('.slide-box div.tab', this);
		var _holder = $('.slide-hold', this);*/
		var _links = $('a.tab');
		var _tabs = $('.slide-box div.tab');
		var _holder = $('.slide-hold');
		_links.click(function(e){
			e.preventDefault();
			var _tab = $($(this).attr('href'));
			if(parseInt(_holder.css('left')) < 0){
				_holder.css('z-index','20');
				_holder.removeClass('blockDefault').addClass('blockChange').animate({left: 0},400);
			};
			if(_tab.is(':visible')) return false;
			_tabs.filter(':visible').fadeOut(300,function(){
				_tab.fadeIn(300);
			});
			return false;
		});
	});
	
	var _holder = $('.top-form-tabs .slide-hold');
	var _tabs = $('.top-form-tabs div.tab');
	
	$('#bottom-tabs a.tab').each(function() {
		$(this).bind('click',function(e) {
			e.preventDefault();
			var _tab = $($(this).attr('href'));
			var destination = $('#header').offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
			if(parseInt(_holder.css('left')) < 0){
				_holder.css('z-index','-1');
				_holder.removeClass('blockDefault').addClass('blockChange').animate({left: 0},400);
			};
			if(_tab.is(':visible')) return false;
			_tabs.filter(':visible').fadeOut(300,function(){
				_tab.fadeIn(300);
			});
		});
	});
	
	
	//put a listener on every clickable element to pull the reserving box backin
	$('a').bind('click',function() { collapseReserve($(this)); });
	
	
	$('#header').fadeGallery({
		listSelector: '.fade-gal img',
		navHolder:		'div.gallery-nav',
		navCreate:		true,
		thumbsSelector: 'li',
		swichTime:		5000,
		delay:			900
	});
	jQuery('div.drop .block').vvGalleryScroll({
		holderList: 'div.gal-hold',
		scrollElParent: '> ul',
		scrollEl: '> li',
		event: 'mouseenter',
		thumbs: 'ul.gal-nav a'
	});
	
});

//handler to collapse the reservation element
//	elem - jQuery object
function collapseReserve(elem) {
	if($('#date_picker').length != 0) { removeCal(); }
	if(elem && elem.parents('div.top-form-tabs').length != 0) {
		return;
	}	
	if(elem && elem.hasClass('tab')){
		return;
	}
	if(parseInt($('.top-form-tabs .slide-hold').css('left')) > -367){
		$('.top-form-tabs .slide-hold').removeClass('blockDefault').addClass('blockChange').animate({left: -367},400);
	}
}

var Feed = {
	_holder : false,
	_allItems : false,
	_currentItem : 0,
	_paused : false,
	_interval : false,

	init : function() {
		Feed._holder = $('#facebook-feed');
		Feed._allItems = Feed._holder.find('p');
		Feed._allItems.eq(Feed._currentItem).animate({opacity:1},250);
		
		Feed.addListeners();
		
		Feed._interval = setTimeout("Feed.goTo("+(Feed._currentItem+1)+")",5000);
	},
	
	addListeners : function() {
		Feed._holder.bind('mouseenter',function() { Feed.pause(); });
		Feed._holder.bind('mouseleave',function() { Feed.start(); });
		Feed._holder.find('li.u a').bind('click',function(e) {
			e.preventDefault();
			Feed.pause();
			Feed.goTo((Feed._currentItem-1));
		});
		
		Feed._holder.find('li.d a').bind('click',function(e) {
			e.preventDefault();
			Feed.pause();
			Feed.goTo((Feed._currentItem+1));
		});
	},
	
	goTo : function(i) {
		Feed._allItems.eq(Feed._currentItem).animate({opacity:0},250);	
		Feed._currentItem = (i > Feed._allItems.length-1) ? 0 : ((i<0) ? Feed._allItems.length-1 : i);
		Feed._allItems.eq(Feed._currentItem).animate({opacity:1},250,function() {
			if(!Feed._paused) { Feed._interval = setTimeout("Feed.goTo("+(Feed._currentItem+1)+")",5000); }
		});
	},
	
	pause : function() {
		Feed._paused = true;
		clearInterval(Feed._interval);
	},
	
	start : function() {
		Feed._paused = false;
		Feed._interval = setTimeout("Feed.goTo("+(Feed._currentItem+1)+")",5000);	
	}
};

function padFooter() {
	//span.network - width/2+560-100
	//ul.socials - width/2+560-35
	$('#social_bar ul.socials').css('left',$(window).width()/2+560-130+'px').css('width','95px');
	$('#social_bar span.network').css('left',$(window).width()/2+560-180+'px')
	//#preview - width/2-560
	$('embed.music').css('left',$(window).width()/2-605-10+'px');
	$('#address_phone').css('left',parseInt($('embed.music').css('left'))+90+'px').css('width','450px');
}
