
/*--- function tabs ---*/
function initTabs() {
	$('.tabs-links').each(function() {
		var _a = 0;
		var _btn = $(this).find('a');
		_btn.each(function(_ind, _el) {
			_el.box = $('#' + _el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				if(_el.box.length){ _el.box.show(); }
				_a = _ind;
			}
			else {
				if(_el.box.length){ _el.box.hide(); }
			}
			_el.onclick = function() {
				tabChange(_ind);
				return false;
			};
		});
		function tabChange(_i) {
			if(_i != _a) {
				_btn.get(_a).box.hide();
				_btn.eq(_a).removeClass('active');
				_btn.get(_i).box.show();
				_btn.eq(_i).addClass('active');
				_a = _i;
			}
		}
	});
}
/*--- function clearInput ---*/
function clearInputs(){
	$(':text, :password').each(function(){
		this.val = this.value;
		this.onfocus = function() {
			if(this.value == this.val) { this.value = '';}
		};
		this.onblur = function() {
			if(this.value == '') { this.value = this.val;}
		};
	});
}
function cleartextarea(){
	var textareas=document.getElementsByTagName('textarea');
		for(i=0; i<textareas.length; i++)
		{
			textareas[i].onfocus=function()
			{
				if(this.value=='personal message')
				{
					this.value='';
				}
			};
			textareas[i].onblur=function()
			{
				if(this.value=='')
				{
					this.value='';
				}
			};
		}
	}
/*--- function slideGal---*/
var _num = -1;
function slideGal(){
	$('.slide-list').css('height', '230px');
	$('.slide-list').cycle({
		fx: 'scrollHorz',
		timeout: '6000',
		after: function(){
			var length = ($('.slide-list').children().length);
			_num++;
			if(_num == length)
			{
				_num = 0;
			}
			$('.num li').each(function(){
				//console.log('Current: '+$(this).attr('title'));
				if($(this).attr('title') == _num)
				{
					$(this).addClass('active');
				} else {
					$(this).removeClass('active');
				}
			});
		}
	});
}

/*--- function popup ---*/
function initPopup() {
	$('.top-nav .btn-sign-up').live("click", function(){
		displayPopup($('#sign-in'));
		return false;
	});

	$('.tell_a_friend').live('click', function(){
		displayPopup($('#shared-popup'), true);
		return false;
	});

	$('.add_review').live('click', function(){
		displayPopup($('#review-popup'), true);
		return false;
	});


	$('.description-block .btn-video').live("click", function(){
		displayPopup($('#video-popup'));
		return false;
	});
	$('.description-block').each(function(_holder){
		var f_share = true;
		var share_popup = $('.share-popup');
		$('.btn-share').click(function(){
			var more_box = $('.description-block');

			f_more = true;
			$('.btn-more').html('Read More').removeClass('less');
			//more_box.slideUp(400);
			var $x = 0;
			if($('.zebra').hasClass('no-resize'))
			{
				$x = 296;
			} else {
				$x = $('.zebra').css('height');
				$x = $x.replace('px', '');
				if($x != 'auto')
				{
					$x = $x * 1;
				}
			}
			more_box.css('height', $x);
			if(f_share){
				share_popup.hide().css('top', 0).fadeIn(300);
				f_share = false;
			} else {
				share_popup.fadeOut(300);
				f_share = true;
			}
			return false;
		});
		share_popup.find('.close').click(function(){
			share_popup.fadeOut(300);
			f_share = true;
			return false;
		});
		var f_more = true;
		//var more_box = $(this).find('.opener-box');
		var more_box = $('.description-block');
		$(this).find('.btn-more').click(function(){
			if(f_more){
				f_more = false;
				$(this).text('Read Less').addClass('less');
				more_box._auto(60);
				$('.location-list').css('margin-top', '0');
			}
			else{
				f_more = true;
				$(this).text('Read More').removeClass('less');
				//more_box.slideUp(400);
				var $x = ((($('.zebra').css('height'))));
				if($x !== 'auto')
				{
					$x = ($x.replace('px','') * 1)
				}
				more_box.css('height', $x);
				$('.location-list').css('margin-top', '-10px');
			}
			return false;
		});
		var f_reviews = true;
		$(this).find('.btn-reviews').click(function(){
			if(f_reviews){
				$('#reviews').hide().height('auto').slideDown(400);
				f_reviews = false;
				$(window).scrollTop($('#reviews').offset().top);
			}
			else{
				$('#reviews').slideUp(400);
				f_reviews = true;
			}
			return false;
		});
	});
}

function initH() {
	var _h = $('.container').outerHeight() + $('.modules-holder').outerHeight() + $('.footer-holder').outerHeight();
	if(_h < $(window).height())
	{
		_h = $(window).height();
	}
	return _h;
}
function displayPopup(_popup, _alt) {
	var popup_h;
	var _duration = 300;
	if($('#popup_fader').length === 0){ 
		$(document.body).append('<div id="popup_fader"></div>');
	}
	popup_h = $('#popup_fader');
	_popup.css({display: 'none', 'top': $(window).scrollTop()});
	if(!_alt)
	{
		popup_h.css({
			position:'absolute',
			top: 0,
			left: 0,
			zIndex: 10000,
			background: '#000',
			opacity: 0,
			height: initH(),
			width: '100%',
			display: 'none'
		});
	} else {
		popup_h.css({
			position:'absolute',
			top: 0,
			left: 0,
			zIndex: 10000,
			background: '#ccc',
			opacity: 0,
			height: initH(),
			width: '100%',
			display: 'none'
		});
	}
	popup_h.show();
	hideSelectBoxes(popup_h);
	hideAds();
	popup_h.fadeTo(_duration, 0.64, function() {
		_popup.fadeIn(_duration/2);
	});
	_popup.find('.close').one('click', function() {
		_popup.fadeOut(_duration/2, function() {
			popup_h.fadeOut(_duration, function() {
				showSelectBoxes(popup_h);
				showAds();
			});
		});
		return false;
	});
	$(window).unbind('resize').resize(function() {
		if(popup_h.is(':visible')){
			popup_h.height(initH());
		}
	});
}
/*--- ie hover ---*/
function ieHover(_list) {
	if ($.browser.msie && $.browser.version < 7) {
		$(_list).hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}
}
/*--- custom selects ---*/
jQuery.fn.customSelect = function(_options) {
_options = jQuery.extend({
	selectStructure: '<div class="selectArea"><div class="left"></div><div class="disabled"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a></div>',
	selectText: '.center',
	selectBtn: '.selectButton',
	selectDisabled: '.disabled',
	optStructure: '<div class="selectOptions min"><ul></ul></div>',
	optList: 'ul'
}, _options);
return this.each(function() {
	var select = jQuery(this);
	if(!select.hasClass('outtaHere')) {
		if(select.is(':visible')) {
			var replaced = jQuery(_options.selectStructure);
			var selectText = replaced.find(_options.selectText);
			var selectBtn = replaced.find(_options.selectBtn);
			var selectDisabled = replaced.find(_options.selectDisabled).hide();
			var optHolder = jQuery(_options.optStructure);
			var optList = optHolder.find(_options.optList);
			if(select.attr('disabled')){
				selectDisabled.show();
			}
			select.find('option').each(function() {
				var selOpt = $(this);
				var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
				if(selOpt.attr('selected')) {
					selectText.html(selOpt.html());
					_opt.addClass('selected');
				}
				_opt.children('a').click(function() {
					optList.find('li').removeClass('selected');
					select.find('option').removeAttr('selected');
					$(this).parent().addClass('selected');
					selOpt.attr('selected', 'selected');
					selectText.html(selOpt.html());
					select.change();
					optHolder.hide();
					return false;
				});
				optList.append(_opt);
			});
			replaced.width(select.outerWidth());
			replaced.insertBefore(select);
			optHolder.css({
				width: select.outerWidth(),
				display: 'none',
				position: 'absolute'
			});
			jQuery(document.body).append(optHolder);
			
			var optTimer;
			replaced.hover(function() {
				if(optTimer)
				{
					clearTimeout(optTimer);
				}
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			optHolder.hover(function() {
				if(optTimer)
				{
					clearTimeout(optTimer);
				}
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			selectBtn.click(function() {
				if(optHolder.is(':visible')) {
					optHolder.hide();
				}
				else {
					optHolder.css({
						top: replaced.offset().top + replaced.outerHeight(),
						left: replaced.offset().left,
						display: 'block'
					});
				}
				return false;
			});
			select.addClass('outtaHere');
		}
	}
});
};
/*--- hide/show selects ---*/
function hideSelectBoxes(object) {
	if ($.browser.msie && $.browser.version < 7) {
		var selects_list = $('select');
		object.each(function() {
			var _el = $(this);
			var t = _el.offset().top;
			var l = _el.offset().left;
			var w = _el.outerWidth();
			var h = _el.outerHeight();
			var el_selects = [];
			selects_list.filter(':visible').not(_el.find('select')).each(function(){
				var _select = $(this);
				var s_t = _select.offset().top;
				var s_l = _select.offset().left;
				var s_w = _select.outerWidth();
				var s_h = _select.outerHeight();
				var _ver = false, _hor = false;
				if((t - s_t > 0) ? (t - s_t < s_h) : (t - s_t + h > 0)){ _ver = true; }
				if((l - s_l > 0) ? (l - s_l < s_w) : (l - s_l + w > 0)){ _hor = true; }
				if(_ver && _hor) {
					_select.css('visibility', 'hidden');
					el_selects.push(this);
				}
			});
			this.sboxes = el_selects;
		});
	}
}
function showSelectBoxes(object) {
	if ($.browser.msie && $.browser.version < 7) {
		object.each(function() {
			if(this.sboxes.length > 0) {
				$(this.sboxes).css('visibility','visible');
			}
		});
	}	
}
function hideAds()
{
	$('.banner').css('visibility', 'hidden');
}
function showAds()
{
	$('.banner').css('visibility', 'visible');
}

/**
 * Sets the calling object's height to the computed height of
 * it's children plus any height specified as a parameter.
 *
 * Uses a match() call on each childs class to prevent larger
 * hidden elements from counting towards the total (can be
 * removed if you don't need it, I did).
 *
 * Author: Joshua Kendall
 */
$.fn._auto = function(height)
{
	var self = $(this);
	self.children().each(function(){
		var child = $(this);
		var _class = child.attr('class');
		if(_class.match('popup') === null)
		{
			height += child.height();
		}
	});
	self.css('height', height);
	return false;
};

function initPage(){
	initTabs();
	initPopup();
	ieHover('input, li, div');
	clearInputs();
	cleartextarea();
	$('.tickets-block select, .refine select').customSelect();
	$('div.loader-overlay').fadeOut('slow');
	slideGal();
	if($('div').hasClass('.nav-block'))
	{
		var more_box = $('.description-block');
		f_more = true;	
		$('.btn-more').html('Read More').removeClass('less');
		//more_box.slideUp(400);
		var $h = $('.zebra').height();
		$('.zebra').css('top', 0);
		more_box.css('height', $h);
		$('.location-list').css('margin-top', '-10px');
	}

}
if (window.addEventListener)
{
	window.addEventListener("load", initPage, false);
} else if (window.attachEvent){
	window.attachEvent("onload", initPage);
}
