// site specific javascript


if(!$.browser.safari)
{
	$(document).ready(function(){
		behavior_binder();
	});
}
else
{
	$(window).load(function(){
		behavior_binder();
	});
}


function behavior_binder(){

	$('form#search_form').submit(function(e){
		if($('input#header_search_input').val()==''){
			alert('Please enter a search term');
			e.preventDefault();
		}
	});
	$('form#newsletter_form').submit(function(e){
		if($("input:checked").length!=1){
			alert('You must confirm you are 13 years or older and have read and accept the Terms Of Use and Privacy Policy');
			e.preventDefault();
		}
	var email = $("input#form_element_1").val();
		if(isValidEmailAddress(email)) {
			
		} else {
			alert('Please fill in a valid email address.');
			e.preventDefault();
		}
	});
	$('form#contact_us_form').submit(function(e){
		if($('input#name').val()==''){
			alert('Please fill in your name.');
			e.preventDefault();
		}
	var email = $("input#email").val();
		if(isValidEmailAddress(email)) {
			
		} else {
			alert('Please fill in a valid email address.');
			e.preventDefault();
		}
	});
	$('#main_nav a').drawFont();
	$('.image_item .image_item_category').drawFont();
	$('#main_nav a').css('visibility','visible');
	$('.image_item .image_item_category').css('visibility','visible');

	// this script creates the small calendar showing the current month on page load
	$('#calendar_container').html('<div class="loading"><img src="/_images/ajax_loading.gif" width="56" height="64" alt="calendar loading..." /></div>');
	$.ajax({
		type: "POST",
		url: "/_php/calendar_builder.php",
		data: "&action=calendar",
		success: function(html){
			$('#calendar_container').html(html);
			bind_behavior();
		}
	});



	
}
var this_time;
function bind_behavior(){
	$('.prev_month a').click(function(){
		switch_calendar($(this).attr('id'));
	})
	$('.next_month a').click(function(){
		switch_calendar($(this).attr('id'));
	})
	set_tooltip();
}
function toTimestamp(year,month,day,hour,minute,second){
	var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
	return datum.getTime()/1000;
}
function switch_calendar(this_time){
	$('#calendar_container').html('<div class="loading"><img src="/_images/ajax_loading.gif" width="56" height="64" alt="calendar loading..." /></div>');
	$.ajax({
		type: "POST",
		url: "/_php/calendar_builder.php",
		data: "&action=calendar&date="+this_time,
		success: function(html){
			$('#calendar_container').html(html);
			bind_behavior();
		}
	});
	
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
	}
	
function set_tooltip(){

	$.fn.qtip.styles.mystyle = { // Last part is the name of the style
		width: 165,
		background: '#FFF',
		color: '#333',
		textAlign: 'left',
		border: {
			width: 2,
			color: '#0289b0'
		},

	   tip: 'bottomMiddle'
	}
	$('.calendar_rail td a.cal_day').qtip({
		content: { text: $(this).attr('title') },
		show: 'mouseover',
		hide: 'mouseout',
		position: {
			corner: {
				target: 'topMiddle',
				tooltip: 'bottomMiddle'
			},
			adjust: { x: 0, y: 10 }
		},
		style: 'mystyle' 
	})
}
