$(document).ready(function(){
	/*$('#stationsOverlay #stationsExit').click(function() {
		alert('hey');
		//$('*').find('.stateBox.shown').removeClass('shown').addClass('hidden');
		
	});*/

	$(function(){
		var startFrame = $('#slides .frame').length;
		$("#slides").slides({
			generatePagination: false,
			prev: 'prevArrow',
			next: 'nextArrow',
			paginationClass: 'slidePagination',
			start: startFrame
		});
	});
	
	$('#stationsPopLink').click(function() {
		$('#stationsOverlay').lightbox_me({
			centered: true
		});
		return false;
	});
	
	$('#contactUsPopLink').click(function() {
		$('#contactform .error, #contactform textarea').html('');
		$('#contactform input').val('');
		$('#contactUsOverlay').lightbox_me({
			centered: true,
			onClose: function(){
				  $(':input', '#contactform').each(function() {
				    var type = this.type;
				    var tag = this.tagName.toLowerCase(); 
				    if (type == 'text' || type == 'password' || tag == 'textarea')
				      this.value = "";
				    else if (type == 'checkbox' || type == 'radio')
				      this.checked = false;
				    else if (tag == 'select')
				      this.selectedIndex = -1;
				  });
			}
		});
		return false;
	});

	$('#signUpPopLink').click(function(){
		$('.mce_inline_error').html('');
		$('#mce-EMAIL, #mce-ZIP').val('');
		$('#success_page, #mce-success-response, #mce-error-response').hide();
		$('#sign_up').show();
		$('#signUpOverlay').lightbox_me({
			centered: true
		});
		return false;
	});
	
	/*
	$('#closeSuccessPage, #success_page .close').click(function(){
		$('#success_page').hide();
		$('#sign_up').delay(1000).fadeIn();
	});
	*/
	
	$('#bioPopLink').click(function() {
		$('#bioOverlay').lightbox_me({
			centered: true
		});
		return false;
	});
	
	$('#panelPopLink').click(function() {
		$('#panelOverlay').lightbox_me({
			centered: true
		});
		return false;
	});

	$('a#stationReturn').click(function(e) {
		$('#airtimesOverlay').lightbox_me({ 
			modalCSS:	{top: '-240px'},			
			closeSelector: '.close',
			onClose: function(){
				$('#stationsOverlay').find('.stateBox.shown').removeClass('shown').addClass('hidden');
				$('#selectRegionStation').val('0');
			}
		});
		e.preventDefault();
	});
	
	$('.airtimeBack').click(function(e) {
		
		$('#stationsOverlay').lightbox_me({
			centered: true,  
			closeSelector:  '.close',
			onClose: function(){
				$('#stationsOverlay').find('.stateBox.shown').removeClass('shown').addClass('hidden');
				$('#selectRegionStation').val('0');
			}
		}); 
	 
		return false;
	});  
	
	//stations load episode schedule		  
	$('#stationList .stations').click(function(e) {
		var stationID = $(this).attr('id').replace('station_', '');
		
		$('#airtimesOverlay_'+stationID).lightbox_me({
			centered: true,  
			closeSelector:  '.close',
			onClose: function(){
				$('#stationsOverlay').find('.stateBox.shown').removeClass('shown').addClass('hidden');
				$('#selectRegionStation').val('0');
			}
		}); 
		
		return false;
	});  
	
	// function to view episode guide visiblity .animate( properties, [ duration ], [ easing ], [ complete ] )
	$('#showepisodeguide').unbind('hover').hover(function(e){
		
		$('#episode_detail').slideDown(500);
		$('#episode_navOn').show();
		
/*
		$('#episode_detail').animate({ height:'toggle'},500);
		$('#episode_navOn').toggle();
*/
		e.preventDefault();
	});
	
	$('.episodeOverlay').mouseleave(function(){
		$('#episode_detail').slideUp(500,function() {
			$('#episode_navOn').hide();
		});
		
	});
	
	$('#confirmationExit').click(function(){
		e.preventDefault();
	});
	
	$('#showepisodeguide').blur(function(e){
		$('#episode_detail').animate({ height:'toggle'},500);
		$('#episode_navOn').toggle();
	});

	// function for clicking the zip go button
	$('#btnGo').click(function (e) {
		var input = $('#zipFind').val();

		if (!IsNumeric(input) || !isLength(input)) {
			alert('Error');
			return false;
		} else {
			//alert('Success! Submit code goes here...');
			$('#overlay').show();
			$('#popAirTimes').show();
		}
		e.preventDefault();
	});

	$('#btnSignup').click(function (e) {
		$('#overlay').show();
		$('#popSignup').show();
		e.preventDefault();
	});

	$('a.btnSubmit').click(function (e) {
		alert('validate function goes here...');
		e.preventDefault();
	});

	// function for zip field focus
	$('#zipFind').focus(function () {
		if ($(this).val() == 'Zip') {
			$(this).val('');
		}
	});

	// function for zip field blur
	$('#zipFind').blur(function () {
		if ($(this).val() == '') {
			$(this).val('Zip');
		}
	});

	/*
	//function for email field focus
	$('#emailToFriend').focus(function () {
		if ($(this).val() == 'Enter email here') {
			$(this).val('');
		}
	});

	// function for email field blur
	$('#emailToFriend').blur(function () {
		if ($(this).val() == '') {
			$(this).val('Enter email here');
		}
	});


	$('#emailToFriendSubmit').click(function (e) {
		var whEmail = $('#emailToFriend').val();
		var postUrl = $(this).attr('href');
		if (checkEmail(whEmail)) {
			var pageTitle = $('title').html();
			$.post(postUrl,{
				action: 'send_to_friend_action',
				targetEmail: whEmail,
				pageTitle: pageTitle,
				link: document.location.href
			});
			alert('Your email has been sent');
		};
		$('#emailToFriend').val('Enter email here');
		e.preventDefault();
	});
	*/
	
	$('#myForm').ajaxForm(function() { 
		alert("Thank you for your comment!"); 
	}); 

	$('#popAirTimesX, a.goBack').click(function (e) {
		var x = $(this).attr('rel');
		$('#' + x).hide();
		$('#overlay').hide();
		e.preventDefault();
	});

	$('#popSignupX').click(function (e) {
		var x = $(this).attr('rel');
		$('#' + x).hide();
		$('#overlay').hide();
		e.preventDefault();
	});
	
	// submit the search form
	$('#submitSearchForm').click(function(e) {
		$('#searchform').submit(); 
		e.preventDefault();
	});
	
	// open external links in a new window
	$('a[href^="http://"]').each(function() {
		if(this.href.indexOf(location.hostname) == -1) {
			$(this).click(function(){
				window.open(this.href);
				return false;
			});
		}
	});

	// Mini video thumb pagination 
	// NOTE: do not confuse with the Mini Thumb Container stuff below, which is for the guest and wine thumbs on the episode page - ML
	$('.miniSlider a.miniArrow.next').click(function(event){
		//alert('next');
		if ($('.miniSlideWindow .vidThumb.shown').hasClass('last')) { // if it's the last item, reveal the first one (endless loop)
			//alert('last');
			$('.miniSlideWindow').find('.shown').hide().removeClass('shown').addClass('hidden');
			$('.miniSlideWindow').find('.first').fadeIn(400).addClass('shown').removeClass('hidden');
			event.preventDefault();
		} else { // else reveal the next item
			$('.miniSlideWindow').find('.shown').hide().removeClass('shown').addClass('hidden').next().fadeIn(400).addClass('shown').removeClass('hidden');
			event.preventDefault();
		}
	});
	
	$('.miniSlider a.miniArrow.prev').click(function(event){
		//alert('previous');
		if ($('.miniSlideWindow .vidThumb.shown').hasClass('first')) { // if it's the first item, reveal the last one
			//alert('first');
			$('.miniSlideWindow').find('.shown').hide().removeClass('shown').addClass('hidden');
			$('.miniSlideWindow').find('.last').fadeIn(400).addClass('shown').removeClass('hidden');
			event.preventDefault();
		} else { // else reveal the previous item
			$('.miniSlideWindow').find('.shown').hide().removeClass('shown').addClass('hidden').prev().fadeIn(400).addClass('shown').removeClass('hidden');
			event.preventDefault();
		}
	});
});

function IsNumeric(input) {
	return (input - 0) == input && input.length > 0;
}

function isLength(input) {
	return input.length == 5;
}

function checkEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(email) == false) {
		//alert('Please enter a valid email address');
		return false;
	} else {
		//alert('Success! Submit code goes here...');
		return true;
	}
}

isie = (navigator.userAgent.indexOf('MSIE') > 0);

function hideAllByClass(className){
   i = 0;
   a = document.getElementsByTagName("div");
   while (element = a[i++]) {
      if (element.className == className) {
         element.style.display = 'none';
      }
   }
} //hideAllByClass

// Episode page scripts
$(document).ready(function() {
	
	var IE7 = (navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true;
	$('.miniThumbContainer .box').click(function() {
		
		$('.miniThumbContainer .box.curPop').removeClass('curPop');
		var newParentFeature = $(this).parents('.featureContainer');
		var str_to_replace = (newParentFeature.attr('id') == 'meetTheDrinkers') ? 'drinker_' : 'wine_';
		var parent_id = $(this).attr('id').replace(str_to_replace, '');
		var newPop = newParentFeature.find('.drinkerPop.pop_'+parent_id);
		var curPop = newParentFeature.parent().find('.drinkerPop.visible');
		var curParentFeature = curPop.parent();
		
		curPop.removeClass('visible');
		curPop.css('visibility', 0);
		var curHeight_current = curPop.height();
		//var curHeight_new =(curHeight_current - (curPop.css('padding')*2)) - curParentFeature.innerHeight();
		var curHeight_new =curHeight_current  - curParentFeature.innerHeight();
		curPop.css('height',curHeight_new);
		curPop.hide();
		
		newPop.show();
		newPop.css('top', -newPop.innerHeight());
		var newHeight_current = newPop.height();
		//var newHeight_new = ((newHeight_current - (newPop.css('padding')*2)) + newParentFeature.innerHeight());
		var newHeight_new = newHeight_current  + newParentFeature.innerHeight();
		newPop.css('height', newHeight_new);
		
		newPop.css('visibility', 1);
		newPop.addClass('visible');

			
		$('.featureContainer').removeClass('active');
		
		newParentFeature.addClass('active');

		$(this).addClass('curPop');

	});

	$('.closeX').click(function() {
		var curPop = $(this).parent();
		curPop
			.css('visibility', 0)
			//.height((curPop.height() - (curPop.css('padding')*2)) - curPop.parent().innerHeight())
			.height((curPop.height() - curPop.parent().innerHeight()))
			.hide()
			.removeClass('visible');
		curPop.parent().removeClass('active');
		$('.miniThumbContainer .box.curPop').removeClass('curPop');
	});
	
});


function scriptInit() {
if (!document.getElementById) {
	return;
	}
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
	elm.addEventListener(evType, fn, useCapture);
	return true;
	} else if (elm.attachEvent) {
	var r = elm.attachEvent('on' + evType, fn);
	return r;
	} else {
	elm['on' + evType] = fn;
	}
}

function checkActive() {
	var a = document.getElementsByTagName("#topNav a");
	if (window.location.href.substr(location.href.length - 1, 1) == '/') {
		var loc = window.location.href + 'index.html'; 
	}
	else {
		var loc = window.location.href;
	}
	
	for(var i=0; i < a.length; i++) {
		if (a[i].href == loc) {
		 
			a[i].setAttribute("class", "active");
			a[i].setAttribute("className", "active");
		}
	}
}


addEvent(window, 'load', checkActive);


function changeStation(){
	selectLength = document.airTimesStationsForm.selectRegionStation.length-1;
	selectValue = ($('#selectRegionStation').val()).replace(/\s/g,'');
	//alert(selectValue);
	if (selectValue != 0) {
		$('#stationsOverlay').find('.stateBox.shown').removeClass('shown').addClass('hidden');
		$('#stationsOverlay').find('.' + selectValue).removeClass('hidden').addClass('shown');
		$('#stationsOverlay').lightbox_me({
			centered: true,
			onClose: 
			function(){
				$('#stationsOverlay').find('.stateBox.shown').removeClass('shown').addClass('hidden');
				$('#selectRegionStation').val('0');
			}
		});
		return false;
	}
}
