email_regex = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i;

// navigation dropdowns (not particularly well written)
// ****************************************************
nf_timeout = 0;
na_timeout = 0;
np_timeout = 0;
ns_timeout = 0;
nav_delay = 300;

$(document).ready(function(){
	
	$('.facebook a').click(fbs_click);
	
	$('#nav-features a').hover(
		function(){
			if(nf_timeout !== 0)
				clearTimeout(nf_timeout);
				
			$('#nav ul.features').slideDown('fast');
			//$(this).addClass('hovering');
		},
		function(){
			nav_hider("nf", false);
		}
	);
	
	$('#nav-aboutthed a').hover(
		function(){
			if(na_timeout !== 0)
				clearTimeout(na_timeout);
			
			$('#nav ul.aboutthed').slideDown('fast');
			$(this).addClass('hovering');
		},
		function(){
			nav_hider("na", false);
		}
	);
	
	$('#nav-pullouts a').hover(
		function(){
			if(np_timeout !== 0)
				clearTimeout(np_timeout);
			
			$('#nav ul.pullouts').slideDown('fast');
			$(this).addClass('hovering');
		},
		function(){
			nav_hider("np", false);
		}
	);
	
	$('#nav-store a').hover(
		function(){
			if(ns_timeout !== 0)
				clearTimeout(ns_timeout);
			
			$('#nav ul.store').slideDown('fast');
			$(this).addClass('hovering');
		},
		function(){
			nav_hider("ns", false);
		}
	);
	
	$('#nav ul.features').hover(
		function(){
			clearTimeout(nf_timeout);
		},
		function(){
			nav_hider("nf", false);
		}
	);
	
	$('#nav ul.aboutthed').hover(
		function(){
			clearTimeout(na_timeout);
		},
		function(){
			nav_hider("na", false);
		}		
	);
	
	$('#nav ul.pullouts').hover(
		function(){
			clearTimeout(np_timeout);
		},
		function(){
			nav_hider("np", false);
		}		
	);
	
	$('#nav ul.store').hover(
		function(){
			clearTimeout(ns_timeout);
		},
		function(){
			nav_hider("ns", false);
		}		
	);
});

function nav_hider(obj, actuallyHide)
{
	aobj = "";
	lobj = "";
	
	if(obj == 'nf')
	{
		aobj = '#nav-features a';
		lobj = '#nav ul.features';
	}
	else if(obj == 'na')
	{
		aobj = '#nav-aboutthed a';
		lobj = '#nav ul.aboutthed';
	}
	else if(obj == 'np')
	{
		aobj = '#nav-pullouts a'
		lobj = '#nav ul.pullouts'
	}
	else if(obj == 'ns')
	{
		aobj = '#nav-store a'
		lobj = '#nav ul.store'
	}
	
	if(actuallyHide)
	{
		$(lobj).slideUp('fast');
		$(aobj).removeClass('hovering');
	}
	else
	{
		if(obj == 'nf')
			nf_timeout = setTimeout('nav_hider("'+obj+'", true)', nav_delay);
		else if(obj == 'na')
			na_timeout = setTimeout('nav_hider("'+obj+'", true)', nav_delay);
		else if(obj == 'np')
			np_timeout = setTimeout('nav_hider("'+obj+'", true)', nav_delay);
		else if(obj == 'ns')
			ns_timeout = setTimeout('nav_hider("'+obj+'", true)', nav_delay);
	}
}

// facebook sharing function
function fbs_click(){
	u = location.href;
	t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

// calendar hooks
// ****************************************************
$(document).ready(function(){
	if($('.calendar').length > 0)
	{
	  admin = false;
	  calendar_url = '/calendar';
	  
	  if($('.calendar.admin').length > 0) {
	    admin = true;
	    calendar_url += '/admin';
    }
    
		$('.calendar .nav .previous a').click(function(){
			var month = calendar_get_month();
			var year = calendar_get_year();
		
			$.get(site_path_prefix + (month == 1 ? year-1 : year) + '/' + (month == 1 ? 12 : month - 1) + calendar_url, {}, function(data){
				$('.calendar h4').remove();
				$('.calendar .cal-header').remove();
				$('.calendar .week').remove();
				$('.calendar').append(data);
				
				if (!admin)
  				calendar_update_nav();
			});
			
			$(this).blur();
			return false;
		});
		$('.calendar .nav .next a').click(function(){
			var month = calendar_get_month();
			var year = calendar_get_year();
			
			$.get(site_path_prefix + (month == 12 ? year+1 : year) + '/' + (month == 12 ? 1 : month + 1) + calendar_url, {}, function(data){
				$('.calendar h4').remove();
				$('.calendar .cal-header').remove();
				$('.calendar .week').remove();
				$('.calendar').append(data);
				
				if (!admin)
  				calendar_update_nav();
			});
			
			$(this).blur();
			return false;
		});
	}
});

function calendar_get_month()
{
	var h4id = $('.calendar h4').attr('id');
	return (1 * h4id.substring(h4id.indexOf('m')+1, h4id.indexOf('-')));
}

function calendar_get_year()
{
	var h4id = $('.calendar h4').attr('id');
	return (1 * h4id.substring(h4id.indexOf('y')+1));
}

function calendar_update_nav()
{
	var curr_month = calendar_get_month();
	var curr_year = calendar_get_year();
	
	var prev_month = curr_month-1;
	var prev_year = curr_year;
	var next_month = curr_month+1;
	var next_year = curr_year;
	
	if(prev_month <= 0)
	{
		prev_month = 12 - prev_month;
		prev_year--;
	}
	if(next_month >= 13)
	{
		next_month = 0 + (next_month-12);
		next_year++;
	}
	
	$('.calendar .nav .previous a').attr('href', '?month='+prev_month+'&year='+prev_year);
	$('.calendar .nav .next a').attr('href', '?month='+next_month+'&year='+next_year);
}

// search box
// ****************************************************
$(document).ready(function(){
	var defaultValue = "Looking for something?";
	
	$('#query').focus(function(){
		if($(this).attr('value') == defaultValue)
			$(this).attr('value', '');
	});
	
	$('#query').blur(function(){
		if(typeof($(this).attr('value')) == 'undefined' || $.trim($(this).attr('value')) == '')
			$(this).attr('value', defaultValue);
	});
	
	$('#search_form').bind('submit', function(){
	  if($('#query').attr('value') == defaultValue || $.trim($('#query').attr('value')) == ''){
	    $('#query').attr('value', $.trim($(this).attr('value')));
	    $('#query').focus();
	    return false;
	  }
	});
});

// subscribe form shipping/billing address checkbox
// ****************************************************
$(document).ready(function(){
  var body = $('body');
  if(body.hasClass('about') && body.hasClass('subscribe'))
  {
    var checkbox = $('#subscription_same_address');
    var saved = {};
    
    var save_data = function(prefix)
    {
      return {
        'address' : $('#'+prefix+'_address' ).attr('value'),
        'address2': $('#'+prefix+'_address2').attr('value'),
        'city'    : $('#'+prefix+'_city'    ).attr('value'),
        'state'   : $('#'+prefix+'_state'   ).attr('value'),
        'zipcode' : $('#'+prefix+'_zipcode' ).attr('value'),
        'country' : $('#'+prefix+'_country' ).attr('value')
      };
    }
    
    var load_data = function(prefix, data)
    {
      $('#'+prefix+'_address' ).attr('value', data['address' ]);
      $('#'+prefix+'_address2').attr('value', data['address2']);
      $('#'+prefix+'_city'    ).attr('value', data['city'    ]);
      $('#'+prefix+'_state'   ).attr('value', data['state'   ]);
      $('#'+prefix+'_zipcode' ).attr('value', data['zipcode' ]);
      $('#'+prefix+'_country' ).attr('value', data['country' ]);
    }
    
    checkbox.change(function() {
      // box just got checked
      if(checkbox.attr('checked'))
      {
        // save old data
        saved = save_data('subscription_credit_card_payment_attributes');
        
        // replace data
        load_data('subscription_credit_card_payment_attributes', save_data('subscription'));
      }
      else
      {
        load_data('subscription_credit_card_payment_attributes', saved);
      }
    });
    
    var inputchange = function() {
      if(checkbox.attr('checked'))
      {
        saved = null;
        checkbox.attr('checked', false);
      }
    };

    $('#subscription_credit_card_payment_attributes_address' ).keydown(inputchange).change(inputchange);
    $('#subscription_credit_card_payment_attributes_address2').keydown(inputchange).change(inputchange);
    $('#subscription_credit_card_payment_attributes_city'    ).keydown(inputchange).change(inputchange);
    $('#subscription_credit_card_payment_attributes_state'   ).keydown(inputchange).change(inputchange);
    $('#subscription_credit_card_payment_attributes_zipcode' ).keydown(inputchange).change(inputchange);
    $('#subscription_credit_card_payment_attributes_country' ).keydown(inputchange).change(inputchange);
    
    var linkinputs = function(input_from, input_to) {
      var func = function() {
        if(checkbox.attr('checked'))
          input_to.attr('value', input_from.attr('value'));
      };
      
      input_from.keydown(func).change(func);
    };
    
    linkinputs($('#subscription_address' ),$('#subscription_credit_card_payment_attributes_address' ));
    linkinputs($('#subscription_address2'),$('#subscription_credit_card_payment_attributes_address2'));
    linkinputs($('#subscription_city'    ),$('#subscription_credit_card_payment_attributes_city'    ));
    linkinputs($('#subscription_state'   ),$('#subscription_credit_card_payment_attributes_state'   ));
    linkinputs($('#subscription_zipcode' ),$('#subscription_credit_card_payment_attributes_zipcode' ));
    linkinputs($('#subscription_country' ),$('#subscription_credit_card_payment_attributes_country' ));
  }
});
