function update_budget_totals(){
  var stWk = sub_update('b_b') + sub_update('b_l') + sub_update('b_d') + sub_update('b_s')
	$('#b_t_week').attr('value', stWk);
	$('#b_t_month').attr('value', stWk * 4);
}

function sub_update(prefix){
	$('#'+prefix+'_week').attr('value', $('#'+prefix+'_count').attr('value') * $('#'+prefix+'_cost').attr('value'));
	$('#'+prefix+'_month').attr('value', $('#'+prefix+'_week').attr('value') * 4);
	return parseInt($('#'+prefix+'_week').attr('value'));
}

function update_value_totals(){
  var stWk = (
      ($('#v_b_count').attr('value')*1)+
      ($('#v_l_count').attr('value')*1)+
      ($('#v_d_count').attr('value')*1)
    )*($('#v_b_cost').attr('value')*1);
	$('#v_b_week').attr('value', stWk);
	$('#v_b_month').attr('value', stWk * 4);
	$('#v_t_week').attr('value',($('#b_t_week').attr('value')*1) + ($('#v_b_week').attr('value')*1));
	$('#v_t_month').attr('value',($('#b_t_month').attr('value')*1) + ($('#v_b_month').attr('value')*1));
}

function _urlparse(str){
	var arr = str.split('/');
	return arr[arr.length - 1];
}

function urlparse(str) {
	var arr = str.split('/');
	var hashing = arr[arr.length - 1].split('?');
	return hashing[0];
}

function menu_maker_sub(parent,obj){
	if(obj)	{
		$(parent).show();
		$(parent).attr('src',obj.image);
		$(parent).attr('alt',obj.name);
		$(parent).attr('descrip','<strong>' + obj.name + '</strong><br />' + obj.description);
	}else	{
		$(parent).hide();
	}
	return $(parent);
}

function menu_maker_main(src){
	$('#main_dish').attr('src',src.attr('src'));
	$('#main_descrip').html(src.attr('descrip'));
}

function menu_maker_update(e){
	menu_maker_main($(e.target));
}

function menu_maker_handler(response){
	menu_maker_sub('#breakfast',response.B).click(menu_maker_update);
	menu_maker_sub('#lunch',response.L).click(menu_maker_update);
	menu_maker_sub('#dinner',response.D).click(menu_maker_update);
	menu_maker_main($('#breakfast'));
}


function url_update(modal){
	window_fix = urlparse(window.location.hash);
	modal.w.jqmAddClose(".close_bar");
	$('#ex2').css('background-image', "url(/img/modal_back.gif)"); 
	$('#ex2').css('background-color', "#EEEEEE");
	modal.w.show();
	if(modal.t)
	{
		window.location.hash = urlparse(modal.t.href);
	}
	if(window.location.hash == "#videos")	{
		videos_init();
	}
	if(window.location.hash == "#celebrity-delivery" || window.location.hash == "#great-meals")	{
		flow_init();
	}
	if(window.location.hash == "#savings-calculator")	{
		$('#calculate_budget').click(function(e){
      			e.preventDefault();
      			update_budget_totals();
    		});
    		$('#calculate_value').click(function(e){
      			e.preventDefault();
      			update_value_totals();
    		});
	}
	if(window.location.hash == "#convinient-delivery")	{
		build_tabs("new-york","los-angeles");
	}
	if(window.location.hash == "#find-balance")	{
		build_tabs("training-tip","relaxation-tip");
	}
	if(window.location.hash == "#menu-maker")	{
		$("#household-slide").slider({
			value:2,
			min: 1,
			max: 10,
			step: 1,
			slide: function(event, ui) {
				$("#household").val(ui.value);
			}
		});
		$("#preparation-slide").slider({
			value:1,
			min: 0,
			max: 4,
			step: 1,
			slide: function(event, ui) {
				$("#preparation").val(ui.value + 'h');
			}
		});
		$("#work-slide").slider({
			value:2,
			min: 0,
			max: 10,
			step: 1,
			slide: function(event, ui) {
				$("#work").val(ui.value);
			}
		});
		$('#menu-maker').ajaxForm({
			dataType:  'json',
			success: menu_maker_handler
		});
	}
	if(window.location.hash == "#sweepstakes")	{
		/*Recaptcha.create("6Le2vwYAAAAAAPF1J6voSvKPlcHg2qnAeybc3teS",
		"captcha", {
		   theme: "red",
		   callback: Recaptcha.focus_response_field
		});*/
		sweepstakes_init();
	}
	if(window.location.hash == "#sign-up")
	{
		$('#sign-up-form').ajaxForm({
			success: sign_up_handler
		});
	}
	if(window.location.hash == "#tell-a-friend")
	{
		tell_friend_init();
	}
	if(window.location.hash == "#choices")
	{
		$('#ex2').css('background-imagurlparse(e', "none"); 
		$('#ex2').css('background-color', "white");
	}
	pageTracker._trackPageview(window.location.hash.replace("#","/"));
	//console.log(window.location.hash.replace("#","/")); 
	check_dynamic_links();
}

function videos_init()
{
	$('#tell_friend_video').ajaxForm({
	success: form_response_load_page
	});
}

function form_response_load_page(response)
{
	$('#ex2').html(response);
	$('#ex2').jqmAddClose("#tell_friend_close");
	$('#ex2').jqmAddClose(".close_bar");
	videos_init();
}


function send_to_friend_handler(response)
{
	$('#ex2').html(response);
	$('#ex2').jqmAddClose("#tell_friend_close");
	$('#ex2').jqmAddClose(".close_bar");
	tell_friend_init();
}

function tell_friend_init()
{
	$('#tell_friend').ajaxForm({
		success: send_to_friend_handler
	});
	$('#learn_more').click(learn_more_handler);	
}

function sweepstakes_init()
{
	$('#sweepstakes_form').ajaxForm({
	success: sweepstakes_handler
	});
	$('#ex2').jqmAddClose(".close_bar");
}

function learn_more_handler(event)
{
	$('#ex2').jqmHide();
	sweepstakes_init();
}

function sign_up_handler(response)
{
	$('#ex2').jqmHide();
	$('#ex1').jqm({ajax:'signup-submit',onLoad:function(){$('#ex1').jqmAddClose(".close_bar");}});
	$('#ex1').jqmShow();
}

function sweepstakes_handler(response)
{
	$('#ex2').html(response);
	sweepstakes_init();
}

function build_tabs(id1,id2){
  $("#" + id1).css('display', 'block');

  $('.tab_link').click(function(e){
    e.preventDefault();

    if("#"+id1 == urlparse(e.target.href)){
      active   = id1;
      inactive = id2;
    }else{
      active   = id2;
      inactive = id1;
    }
    $("#" + active + "-link").addClass("selected");
    $("#" + inactive + "-link").removeClass("selected");
    $("#" + active).css('display', 'block');
    $("#" + inactive).css('display', 'none');
  });
}

function url_reset(modal){
	modal.w.hide();
	modal.o.hide();
	window.location.hash = "index";
}

function check_dynamic_links()
{
	$('#ex2').jqmAddTrigger('.al');
}

function do_nothing(){}

function flow_init(){
	celebflow.init({
    ImageFlowID : 'celebrity_images',
    reflections : false,
    reflectionP : 0.0,
    onClick     : do_nothing
  });
}

var celebflow = new ImageFlow();
var modal_box;
$(function(){
	if(urlparse(window.location.hash) != "#index" && window.location.hash != "")
	{
		$('#ex1').jqm({ajax: urlparse(window.location.hash).replace("#",""), onLoad: url_update, onHide: url_reset, overlay:1, ajaxText: 'Loading..'});
		$('#ex1').jqmShow();
	}
	modal_box = $('#ex2').jqm({ajax: '@href', trigger: 'a.profile_item', onLoad: url_update, onHide: url_reset, overlay:1, ajaxText: 'Loading..'});
	$('#ex2').jqmAddTrigger('#rtnav ul li a');
	$('#ex2').jqmAddTrigger('#footer-call a');
	check_dynamic_links();
	flow_init();
});
