// default

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

//
function checkRegexp(obj,regexp) {
	if (!(regexp.test(obj.val()))){
		return false;
	} else {
		return true;
	}
}

//
function checkEmail(obj){
	return checkRegexp(obj, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
}

$(function(){
	
	//
	$.ajaxSetup({ 
			scriptCharset: "utf-8"
	});
	
	// navigation
	$('#navigation>ul>li').mouseover(function(){ $(this).children('ul').css({display:'block'})}).mouseout(function(){ $(this).children('ul').css({display:'none'})});
	
	// homepage photo
	$('.photo').cycle({ 
		fx:				'fade', 
		speed:			1000,
		timeout:		4000,
		pause:			1,
		cleartype:		1,
		cleartypeNoBg:	1
	 });
	
	try{
		// image
		$('a.image').fancybox();
		// video
		$('a.video').fancybox({ 'frameWidth': 640, 'frameHeight': 356});
		// alert
		$('a.alert').fancybox({ 'frameWidth': 640, 'frameHeight': 200, 'hideOnContentClick': false});
		$('a.alert:first').trigger('click');
	} catch (err){
		
	}

	
	// more
	$('.moreBtn').show().click(function(){ if($(this).find('a').attr('href') == '#'){ var tempID = $(this).attr('id'); $(this).hide('slow'); $('#txt_' + tempID.substring(4)).show('slow', function(){ $(this).css({'display':'inline'}); }); return false; }else{ return true;} });
	$('.moreTxt').hide();
	
	// flag
	$('a.flag').click(function (){
		var tempID = $(this).attr('id');
		var showCommentID = tempID.substring(5);
		
		$.getJSON('/ajax.php', {'action':'flag', 'object':'comment', 'id':showCommentID}, function(data){
			if(data.success == true){
				$('#comment_' + showCommentID).find('.flag').fadeOut('slow');
			} else {
				
			}
		});		
	});

	//
	$.fn.qtip.styles.cal = { 
	   width: 140,
	   textAlign: 'center',
	   border: {
		  width: 2,
		  radius: 5,
		  color: '#a9b721'
	   },
	   tip: 'bottomLeft',
	   name: 'dark'
	}
	
	$('td>.available').each(function(){
		$(this).qtip({
		  content: 'available by phone<br />' + $(this).attr('title'),
		  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
		  style: 'cal'
		});
	});
	$('td>.door').each(function(){
		$(this).qtip({
		  content: 'available at the door<br />' + $(this).attr('title'),
		  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
		  style: 'cal'
		});
	});
	$('td>.unavailable').qtip({
      content: 'sold out',
	  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
	  style: 'cal'
	});
	
	$('td>.online').each(function(){
		$(this).qtip({
		  content: 'available online<br />' + $(this).attr('title') + '<br />click to order',
		  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
		  style: 'cal'
		});
	});
	$('.delete').qtip({
	  content: 'delete item',
	  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
	  style: 'cal'
	});

	
	// email list form
	$('#email_list').ajaxForm({
		dataType		:	'json',
		beforeSubmit	:	function(){
								$('#join_email').css({background:'#ffffff'});
								if(checkEmail($('#join_email'))){
									return true;
								} else {
									$('#join_email').css({background:'#ffcccc'});
									return false;
								}
							},  
        success			:	function(response, status){
								if(response.success == true){
									$('#email_list').fadeOut();
								} else {
									$('#join_email').css({background:'#ffcccc'});
								}
							}
	});
	
	// comment form
	$('#comment_form').ajaxForm({
		dataType		:	'json',
		beforeSubmit	:	function(){
								$('#email').css({background:'#ffffff'});
								if(checkEmail($('#email'))){
									return true;
								} else {
									$('#email').css({background:'#ffcccc'});
									return false;
								}
							},  
        success			:	function(response, status){
								if(response.success == true){
									$('#comment_form').fadeOut();
								} else {
									$('#email').css({background:'#ffcccc'});
								}
							}
	});
	
});

function useBilling(){

	$('#s_firstName').val($('#firstName').val());
	$('#s_lastName').val($('#lastName').val());
	$('#s_address1').val($('#address1').val());
	$('#s_address2').val($('#address2').val());
	$('#s_city').val($('#city').val());
	$('#s_state').val($('#state').val());
	$('#s_zipCode').val($('#zipCode').val());
	
	return false;

}
