
$(window).load(
    function() {
	set_client_tabs();
});

$(function(){

	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	if ($('#birthday').length) {
			$( "#birthday" ).datepicker();
	}
	
	// Set active class for tabbing navs
	if ($('.tabbed-nav').length) {
	
		var diff = newheight = left_bottom = right_bottom = newheight = 0;
		
		$('.tabbed-nav a').click(function() {
			$('.tabbed-nav a').removeClass('current');
			$(this).addClass('current');
			var $tab = $(this).attr('href') + '-tab';
			var $cur_client = null;

			if ($('.tab-content').length) {
				$('.tab-content').each(function() {
					var $testimonial = this.id.replace('-tab','-testimonial');
					if ($tab == this.id) { 
						$cur_client = $testimonial;
						$(this).css('display','block'); 
						if ($('#'+$testimonial).length) {
							$('#'+$testimonial).css('display','block'); 
						}
					}
					else { 
						$(this).css('display','none'); 
						if ($('#'+$testimonial).length) {
							$('#'+$testimonial).css('display','none'); 
						}
					}
				});
			}
			// If the content is short, the client testimonials column won't fill out. This
			// slightly complex calc fixes it.
			left_bottom = parseInt($('#level-content').css('height'));
			right_bottom = parseInt($('#video-spot').css('height'))+parseInt($('#video-spot').css('margin-bottom'))+parseInt($('#'+$cur_client).css('height'));
			diff = left_bottom-right_bottom;
			if (diff > 0) { 
				newheight = parseInt($('#'+$cur_client+' div').css('height'))+diff+1;
				$('#'+$cur_client+' div').css('height',newheight+'px'); 
			}
			return false;
		});
		
	}

	// Assign thumbs to large image objects by the thumb url
	if ($('.tab-content').length) {
		$('.tab-content .featured-thumbs a').click(function() {
			var this_href=$(this).attr('href');
			$(this).parents('div').children('div.featured-image').contents('img').attr('src',this_href);
			return false;
		});
	}
	
	// Every page
	$('#find-salon-form').submit(function() { return checkZip(this); });
	$('#bronze-buzz-form').submit(function() { return checkBBemail(this); });
	$('#zip-search').focus(function() { toggleVal(this,1,'your zip code'); });
	$('#zip-search').blur(function() { toggleVal(this,0,'your zip code'); });
	$('#bb-email').focus(function() { toggleVal(this,1,'your email address'); });
	$('#bb-email').blur(function() { toggleVal(this,0,'your email address'); });
	
	// 12 days page only, in december.
	if ($('.twelve-row a').length) {
		$('.pp12').click(function(event){
			var url = this.href;
			return newWin(url,640,500);
			event.preventDefault();
		});
	}
});

// Due to the layout of level bottom designs, some testimonial divs will run short of the copy, leaving a white gap.
// Like the onclick above, these adjust the first-loading tab and non-tab client boxes.

function set_client_tabs () {

	var diff=left_bottom=right_bottom=newheight=0;
	
	if ($('.tab-content').length) {
		left_bottom = parseInt($('#level-content').css('height'));
		$('.tab-content').each(function() {
			$thistest =  this.id.replace('-tab','-testimonial');
			if (! $('#'+$thistest).hasClass('hidden-div')) {
				right_bottom = parseInt($('#video-spot').css('height'))+parseInt($('#video-spot').css('margin-bottom'))+parseInt($('#'+$thistest).css('height'));
				diff = left_bottom-right_bottom;
				if (diff > 0) { 
					newheight = parseInt($('#'+$thistest+' div').css('height'))+diff+1;
					$('#'+$thistest+' div').css('height',newheight+'px'); 
				}
			}
		});
	}
	
	// As above, but on load of the page for no tabs.
	if ($('#level-content').length && ! $('.tab-content').length) {
		var left_bottom = parseInt($('#level-content').css('height'));
		var right_bottom = parseInt($('#video-spot').css('height'))+parseInt($('#video-spot').css('margin-bottom'))+parseInt($('.client-box').css('height'));
		var diff = left_bottom-right_bottom;
		if (diff > 0) { 
			var newheight = parseInt($('.client-box').css('height'))+diff+1;
			$('.client-box').css('height',newheight+'px'); 
		}
	}
}

// Top zip code form

function checkZip(form) {
	var regex = /^\d{5}$|^\d{5}-\d{4}$/;
	var msg='';
	if ((document.getElementById('zip-search')) && (! regex.test(document.getElementById('zip-search').value)==true)) {
		msg += 'Please enter a five digit zip code, numbers only, no spaces.\n';
	}
	if (msg !='') { alert(msg); }
	else { form.submit(); }
	return false;
}

// Bottom email opt in

function checkBBemail(form) {
	var regex = /.*\@.*[.].*/;
	var msg='';
	if (document.getElementById('bb-email') && ! (regex.test(document.getElementById('bb-email').value)==true)) {
		msg += 'Please enter a valid email address.';
	}
	if (msg !='') { alert(msg); }
	else { form.submit(); }
	return false;
}

// both of above 

function toggleVal(obj,state,def) {
	if ((state==1) && (obj.value==def)) { obj.value=''; }
	else if (obj.value=='') { obj.value=def; }
}

// For new windows requiring a close button, not actually in the URL
function buttonWindow(img,ttl,w,h) {

	if ((! img) || (! ttl)) { return; }
	var day= new Date();
	var id = day.getTime();
	var ww = w+75;
	var wh = h+125;
	if ((screen.height) && (wh > screen.height-100)) { wh = screen.height-100; }
	var params = 'width='+ww+',height='+wh+',scrollbars,resizable';
	var msg='<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org/TR\/html4\/loose.dtd">\n'+
	'<meta http-equiv="Content-Type" content="text\/html; charset=iso-8859-1">\n'+
	'<html><head><title>'+ttl+'<\/title><\/head>'+
	'<style type="text\/css">\n'+
	'html,body { font-family: Arial,Helvetica,Sans-Serif; }\n'+
	'h3,p,form { text-align: center; }\n'+
	'<\/style><body>\n'+
	'<h3>'+ttl+'<\/h3>\n <p><img src="'+img+'"  style="border: 1px solid #000;" alt="'+ttl+'"></p>\n'+
	'<form><input type="button" onClick="window.print();" value="Print Coupon">\n'+
	'<input type="button" onClick="javascript:window.close();" value="Close Window">\n'+
	'<hr width="100%" size="1"><\/form><\/body><\/html>\n';
	var win = open('',id,params);
	win.document.write(msg);
	win.document.close();
	return false;
}

// For a new window URL, no image . . . 
function newWin(url,w,h) {
	if (! url) { return; }
	var day= new Date();
	var id = day.getTime();
	var ww = w+75;
	var wh = h+125;
	if ((screen.height) && (wh > screen.height-100)) { wh = screen.height-100; }
	var params = 'width='+ww+',height='+wh+',scrollbars,resizable';
	var win = open(url,id,params);
	return false;
}
