/*--------------------------------------------------------------------------*/

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

/*--------------------------------------------------------------------------*/

var PopUp = function (trigger, pop, opts) {
	this.trigger = $(trigger);
	this.pop = $(pop);
		
	this.defaults = {
		centered: false,
		modal: false,
		ajax: false,
		modalOverlay: $('#overlay'),
		fadeDuration: $.browser.msie ? 0 : 200,
		openCB: function () {},
		closeCB: function () {}
	};
	this.options = $.extend({}, this.defaults, opts);
	this.setup();
};
PopUp.prototype = {
	setup: function () {
		var self = this;
		this.cache = {};
		
		this.trigger.click(function (ev) { 
			ev.preventDefault();
			self.open(); 
		});
	},
	open: function () {
		var self = this;
		
		if (this.options.centered) { 
			this.pop.center(); 
			$(window).resize(function () { self.pop.center(); });
		}
		
		if (this.options.modal) { this.initModalOverlay(); }
		
		if (this.options.ajax) {
			var error = $('<div />').text('An Error Has Occurred.  Please Try Again.').addClass('error');
			var loader = $('<div />').addClass('loader').append($('<div />').text('Loading...'));
			this.pop.find('.inner:first').append(loader);
			var url = this.trigger.attr('href');
			
			
			if (!self.cache[url]) {
				$(self.pop).find('.content:first').html('');
				self.pop.find('.loader').show();
				$.ajax(
					{ 
						url: url,
						dataType: 'html',
						context: this,
						success: function (r) {	
							
							$(self.pop).find('.content:first').html(r);
							self.options.openCB();
							
							if (self.options.centered) { self.pop.center(); }
							
							self.pop.find('.loader').hide();
							self.pop.fadeIn(self.options.fadeDuration);
							
							self.cache[url] = r;
						},
						error: function () {
							self.pop.find('.loader').hide();
							$(self.pop).find('.content:first').html(error);
							self.pop.fadeIn(self.options.fadeDuration);
						}
					}
				);
			}
			else {
				$(this.pop).find('.content:first').html(self.cache[url]);
				this.options.openCB();
				this.pop.fadeIn(this.options.fadeDuration);
			}
		}
		else { this.pop.fadeIn(this.options.fadeDuration); }
		
		if (!this.hasOpened) {
			var close = $('<div />').text('Close').addClass('close');
			this.pop.append(close);
			this.pop.find('.close').click(function () { self.close(); });
			this.hasOpened = true;
		}		
		this.options.openCB();
		$(document).unbind('click');

	},
	close: function () {
		this.options.closeCB();
		this.pop.fadeOut(this.options.fadeDuration);
		
		if ($.browser.msie) { this.modalOverlay.hide(); }
		else { this.modalOverlay.fadeOut(this.options.fadeDuration); }
	},
	initModalOverlay: function () {
		if (!this.hasOpened) {
			var body = $('body:first');
			body.append($('<div />').attr('id', 'overlay').hide());
			this.modalOverlay = $('#overlay');
			this.modalOverlay.css("height", body.height());
		}
		
		if ($.browser.msie) { this.modalOverlay.show(); }
		else { this.modalOverlay.fadeIn(this.options.fadeDuration); }
	}
};

/*--------------------------------------------------------------------------*/

var Mega = function (trigger, menu) {
	this.trigger = $(trigger);
	this.menu = $(menu);
	this.setup();
};
Mega.prototype = {
	setup: function () {
		var self = this;
		this.trigger.hoverIntent({
			sensitivity: 1,
			interval: 50,
			over: function () { self.menu.show(); },
			out: function () { return; }
		});
		this.menu.mouseleave(function () { $(this).hide(); });
	}
};

/*--------------------------------------------------------------------------*/

var SlideShow = function (container, slides, delay, initial) {
    this.container = $(container);
    this.slides = $(slides);
    this.delay = delay;
    this.current = initial;
	this.togglersContainer = this.container.find('.togglers ul');
    this.setup();
};
SlideShow.prototype = {
    setup: function () {
    	   	
        var self = this;
        this.showOnly($(this.slides[this.current]));
		
		this.slides.each(function (idx) {
			//var li = $('<li>Slide ' + idx + '</li>');
			//self.container.find('.togglers ul').append(li);
		});
		
		this.togglers = this.togglersContainer.find('li');
		$(this.togglers[0]).addClass('first');
		$(this.togglers.last()).addClass('last');
        $(this.togglers[this.current]).addClass('active');
        
        this.togglers.bind('click', function () {
            self.pause();
            self.goTo($(this).index());
        });
		
		this.start();
    },
    showOnly: function (el) {
        this.slides.hide();
        el.show();
    },
    goTo: function (idx) {
        var self = this;
		
        if (idx === this.current) {	return; }
		
        $(this.togglers[this.current]).removeClass('active');
		
        this.fade = $(this.slides[this.current]).stop(true, true).hide();
        this.current = idx;
		
        $(this.togglers[this.current]).addClass('active');
        	
		this.appear = $(this.slides[this.current]).stop(true, true).show();
		
        this.start();
    },
    start: function () {
        if (this.timer) { return; }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1500);
    },
    pause: function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        else { return; }
    },
    next: function() {
        this.timer = false;
        if (this.current === this.slides.length - 1) { this.goTo(0); }
        else { this.goTo(this.current + 1); }
        this.start();
    }
};

/*--------------------------------------------------------------------------*/

var Carousel = function (container, increment, delay, slideSel, opts) {
	this.container = $(container);	
	this.increment = increment;
	this.contents = this.container.find('.slides:first');
	if (this.container.find('.slide').length){ this.slides = this.container.find('.slide'); }
	else {this.slides = slideSel;}
	
	this.prevBtn = this.container.find('.prev:first');
	this.nextBtn = this.container.find('.next:first');
	this.slideCount = this.slides.length / this.increment;
	if (this.contents.position()!= null){
		this.offset = this.contents.position()['left'];
	}
	this.width = $(this.slides[0]).outerWidth(true);
	this.current = 0;
	this.delay = delay | 5;
	
	this.defaults = {
		togglers: this.container.find('.togglers li')
	};
	this.options = $.extend({}, this.defaults, opts);	

	this.setup();
};
Carousel.prototype = {
	setup: function () {
		var self = this;
		
		this.prevBtn.click(function (ev) {
			ev.preventDefault();
			$(this).parent('.slider').children('.active').removeClass('active')
			$(this).addClass('active');
			self.back();
		});
		
		this.nextBtn.click(function (ev) {
			ev.preventDefault();
			$(this).parent('.slider').nextAll('.active').removeClass('active')
			$(this).addClass('active');
			
			self.forward();
		});
		
		this.options.togglers.click(function (ev) {
			ev.preventDefault();
			self.goTo($(this).index()); 
		});
		
		if (this.slideCount == 0) {
			this.prevBtn.addClass('disabled');
			this.nextBtn.addClass('disabled');
		}	
		
		this.goTo(0);
		
		/*this.start();*/
	},
	goTo: function (idx) {		
		var self = this;
	    if (this.current === idx) { return; }
		if (idx < 0 || idx >= this.slideCount) { return; }
		this.current = idx;	
		
		if (this.current == this.slides.length-1) {
			/*console.log('last');*/
		}
	
		$(this.contents).animate(
			{ left: (-(idx * self.width) + self.offset) },
			{ duration: 300, easing: 'easeOutQuad' }
		);

		if (this.options.togglers.length) {
			this.options.togglers.removeClass('active');
			$(this.options.togglers[idx]).addClass('active');		
		}		
		/*return this.current;*/
	},
	start: function () {
        if (this.timer) { return; }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1000);
	},
	pause: function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        else { return; }
	},
	back: function () {
		this.pause();
        if (this.current === 0) {
		
        	this.goTo(this.slides.length - 1);	
        }
        else {
            this.goTo(this.current - 1);	
        }
        /*this.start();	*/
	},
	forward: function () {
        this.pause();
        this.next();
	},
	next: function () {
        if (this.current+1 === this.slides.length) {
			/*this.timer = false;*/
			
			this.goTo(0);
			
		}
        else {
            this.goTo(this.current + 1);
        }
        /*this.start();*/
	}
}

/*--------------------------------------------------------------------------*/

function externalLinks() {
 	if (!document.getElementsByTagName) return;
 	var externals = $("a[rel='external']");
	
	externals.each(function () {
		if ($(this).attr('href')) {
			$(this).bind('click', function (ev) {
				ev.preventDefault();
				window.open($(this).attr('href'));
			});
		}
	});
}

/*--------------------------------------------------------------------------*/
$(document).ready(function() {

	/* background stretcher */
	$.backstretch("static/img/bg.jpg");

	/* slideshows */
	var barSlideshow = $('#content.bar .slider');
	new SlideShow(barSlideshow, barSlideshow.find('.slide'), 3, 0);

	var loungeSlideshow = $('#content.lounge .slider');
	new SlideShow(loungeSlideshow, loungeSlideshow.find('.slide'), 3, 0);

	var eventsSlideshow = $('#content.events .slider');
	new SlideShow(eventsSlideshow, eventsSlideshow.find('.slide'), 3, 0);

	var photosSlideshow = $('#content.photos .slider');
	new SlideShow(photosSlideshow, photosSlideshow.find('.slide'), 5, 0);

	/* nav dropdowns */
	$('#nav li').each(function () { new Mega($(this), $(this).find('.dropdown')); });

	/* contact form */
	var contactForm = $('#contact_form');
	if (contactForm.length) {
		contactForm.validate({
			rules: {
				Cname: "required",
				Cphone: "required",
				Cdate: "required",
				Cemail: {
						required: true,
						email: true
				}
			},
			messages: {
				Cname: {
					required: "Please enter a name."
				},
				Cphone: {
					required: "Please enter a phone #."
				},
				Cdate: {
					required: "Please enter a date."
				},
				Cemail: {
					required: "Please enter a valid email address."
				}
			}
		});
	}

	var map = $('#map');
	new PopUp($('#footer .map'), map, { 
		modal: true, 
		centered: true,
		openCB: function () { }
	});

	var specials = $('#specials');
	new PopUp($('.specials'), specials, { 
		modal: true, 
		centered: true,
		openCB: function () { }
	});

	var event1 = $('#event1');
	new PopUp($('.event1'), event1, { 
		modal: true, 
		centered: true,
		openCB: function () { }
	});

	var event2 = $('#event2');
	new PopUp($('.event2'), event2, { 
		modal: true, 
		centered: true,
		openCB: function () { }
	});

	var event3 = $('#event3');
	new PopUp($('.event3'), event3, { 
		modal: true, 
		centered: true,
		openCB: function () { }
	});

	var event4 = $('#event4');
	new PopUp($('.event4'), event4, { 
		modal: true, 
		centered: true,
		openCB: function () { }
	});

});
