$(document).ready(function() {
	// home sliders
	$('#homeSlide1 .content').cycle({
		fx:			'fade',
		easing:		'easeOutQuint',
		next:		'#sliderNext',
		prev:		'#sliderPrev',
		timeout:	6000
	});
	$('#homeSlide2').cycle({
		fx:			'scrollVert',
		easing:		'easeOutQuint',
		next:		'#sliderNext',
		prev:		'#sliderPrev',
		timeout:	6000
	});
	// video slider (details page)
	$('.videoContainer').cycle({
		fx:			'scrollDown',
		next:		'#nextVideo',
		timeout:	6000
	});
	// printable popups
	$('#coupons:not(.giftCert) li a, #switcherContent li.coupons a, .printPopup, #switcherContent li.events a').popupWindow({
		width: 700,
		height: 455,
		scrollbars: 1
	});
	$('#recipes li a.print, #switcherContent li.recipes a').popupWindow({
		width: 800,
		height: 680,
		scrollbars: 1
	});
	// home video colorbox
	$('#featuredVideo .view').colorbox({inline:true,href:"#videoOfTheWeek"});
	// videos
	$('#videos li a:not(.businessName, .btn)').colorbox();
	// contact us
	$('#topbar li a#contactus').colorbox();
	// market selection
	$('a#selectMarket').colorbox();

	
	/***********************************
		Business Details
	***********************************/
	// all comments colorbox
	$('#showMoreLocations').colorbox({inline:true,href:"#additionalLocations"});
	// all comments colorbox
	$('#viewAllComments').colorbox({inline:true,href:"#allComments",width:"580px"});
	// restaurant details colorbox
	$('.restDetailsBtn').colorbox({inline:true,href:"#restDetails"});
	// business details switcher
	$('#switcherNav a').click(function(e) {
		e.preventDefault();
		var selClass = $(this).attr('class');
		if( $('#switcherContent li.'+selClass).is(':hidden') )
		{
			$('#switcherNav li.sel').removeClass('sel');
			$(this).parent().addClass('sel');
			$('#switcherContent > li:not(.fadeOut)').hide();
			$('#switcherContent li.'+selClass).fadeIn();
		}
	});
	// business details switcher ("see more")
	$('#detailsSwitcher ul#switcherContent li.fadeOut a').click(function(e) {
		e.preventDefault();
		$('#detailsSwitcher > ul#switcherContent').animate({
			height: "473px"
		}, 400, function() {
			$('#detailsSwitcher ul#switcherContent li.fadeOut').fadeOut();
		});
	});
	// large map colorbox
	$('.mapLinkOverlay a').colorbox({iframe:true, innerWidth:567, innerHeight:400});
	
	/***********************************
		Search
	***********************************/
	var search_what = $('#searchWhat').attr('alt');
	var search_where = $('#searchWhere').attr('alt');
	
	// default form values
	$('#searchWhat').defaultValue({'value':search_what});
	$('#searchWhere').defaultValue({'value':search_where});
	$('form').submit(function() {
		$('input.empty').val('');
	});
	
	/***********************************
		Registration
	***********************************/
	// datepicker for date form fields
	$("input.date").datepicker({
		dateFormat:		'yy-mm-dd',
		showAnim:		'fadeIn'
	});
	// phone number masking
	$("input.phone").mask("999-999-9999");
	// business category AJAX
	$('select#business_cats').change(function() {
		var cid = $(this).val();
		$.ajax({
			type: "POST",
			url: base_path + "businesses/get_cats_sub/",
			data: "cat_id=" + cid,
			success: function(data) {
				$('select#business_cats_sub').empty().append('<option value="">Select One</option>' + data);
			}
		});
	});
	$('select#business_cats2').change(function() {
		var cid = $(this).val();
		$.ajax({
			type: "POST",
			url: base_path + "businesses/get_cats_sub/",
			data: "cat_id=" + cid,
			success: function(data) {
				$('select#business_cats_sub2').empty().append('<option value="">Select One</option>' + data);
			}
		});
	});
	// copy billing info over to directory profile
	$('a.same').click(function(e) {
		e.preventDefault();
		$('input[name=business_name]').val($('input[name=billing_business_name]').val());
		$('input[name=address]').val($('input[name=billing_address]').val());
		$('input[name=city]').val($('input[name=billing_city]').val());
		$('select[name=st]').val($('select[name=billing_st]').val());
		$('input[name=zip]').val($('input[name=billing_zip]').val());
		$('input[name=phone]').val($('input[name=billing_contact_phone]').val());
		$('input[name=email]').val($('input[name=billing_contact_email]').val());
	});
	// auto fill login email address
	$('input[name=billing_contact_email]').change(function() {
		$('input[name=email]').val( $(this).val() );
	});
	
	/***********************************
		User Dashboard Functions
	***********************************/
	// colorboxes
	$('#editGiftCert').colorbox({inline:true,href:"#gift_cert_url",width:"420px"});
	$('#editGCC').colorbox({inline:true,href:"#gcc_account",width:"420px"});
	$('#explainDashboard').colorbox({width:"650px"});
	
	// coupons
	$('#couponType').change(function() {
		if( $(this).val() == '5' )
		{
			$('#couponAmount').replaceWith('<p id="buyGetQuantities">Buy <input type="text" name="bogo_buy" class="req" value="" /> Get <input type="text" name="bogo_get" class="req" value="" /> Free</p>');
		}
		else
		{
			if( $('#couponAmount').length == 0 )
			{
				$('#buyGetQuantities').replaceWith('<p id="couponAmount"><label>Amount/Percentage <span>*</span></label><input type="text" name="amount" class="req" value="" /></p>');
			}
		}
	});
	
	/***********************************
		Remove 'My Location'
	***********************************/
	$('#myLocation a.remove').click(function(e) {
		e.preventDefault();
		$.post(base_path + "home/remove_location/", function() {
			$('#myLocation').fadeOut();
		});
		$('#searchWhere').attr('value', '');
		$("input[name=city]").attr('value', '');
	});
	
	/***********************************
		Admin
	***********************************/
	// colorboxes
	$('#cats > li > a, #cats li ul li > a, a.newBtn').colorbox({width:"450px"});
	
	// remove empty <ul>
	$('#cats li').find('ul:not(:has(li))').remove();
	
	// striping
	$('.allUsers > li:even, #cats > li:even').addClass('alt');
	$('form#admin > ul > li:even').addClass('alt');
	
	/* selected field styling
	$('form#admin ul li input, form#admin ul li textarea, form#admin ul li select').live('focus',function() {
		$(this).parent().parent('li').addClass('sel');
	}).live('blur',function(){
		$(this).parent().parent('li').removeClass('sel');
	});*
	
	/***********************************
		Character Limits
	***********************************/
	$('textarea.max').maxLength(450);
	$('textarea#about_us').maxLength(1500);
	$('input[name=known_for]').maxLength(75);
	$('input[name=address]').maxLength(75);
	$('input[name=business_name], .chars40').maxLength(40);
	$('textarea.commentBody').maxLength(160);
	$('textarea.answerQuestion').maxLength(300);
	$('textarea.askQuestion').maxLength(100);

	/***********************************
		Confirm Actions
	***********************************/
	$('a.confirmDelete').click(function(e) {
		if(!confirm('Are you sure you wish to delete this item?'))
		{
			e.preventDefault();
		}
	});
	
	$('a.confirmPayment').click(function(e) {
		if(!confirm('This will activate the business\'s account for 12 months.  Are you sure?'))
		{
			e.preventDefault();
		}
	});
	
	/***********************************
		Form Validation
	***********************************/
	// validate form
	$('form').submit(function(e) {
		// disable submit btn (to prevent double clicking)
		//$(this).find(':submit').attr('disabled','disabled');
		// validate
		$(this).find('.req').each(function() {
			if( $(this).val() == '' )
			{
				e.preventDefault();
				$(':submit').removeAttr('disabled');
				alert('Please complete all required fields.');
				//alert($(this).attr('name')); // for debugging
				return false;
			}
		});
		// require checkbox be checked
		$(this).find('.reqCheckbox').each(function() {
			if( $(this).is(':checked') )
			{
				// all good
			}
			else
			{
				e.preventDefault();
				$(':submit').removeAttr('disabled');
				alert('You must read and agree to the Terms and Conditions in order to register.');
				return false;
			}
		});
	});
});
