var cookie = new Hash.Cookie('quitCalculator');

function validate() {
    var input;
    var valid = true;

    $$(['#q_one', '#q_two', '#q_three']).each(function(e) {
       if(e.getStyle('opacity').toInt() == 1) {
           if((input = e.getElement('input'))) {
               e.removeClass('invalid');
               
               if(!input.value || input.value.toInt() == 0) {
                   e.addClass('invalid');
                   valid = false;
               }
           }
       }
    });
    
    return valid;
}

function calculate() {
    if(!validate()) return false;
    
    $('calc').setStyles({
        display:'none'
    });
    $('results').setStyles({
        display:''
    });
	$('how').setStyles({
		display:''
	});
	
    var a = $('q_one').getElement('input').value.toInt();
	if(period == 'week') a = a / 7;
    var b = $('q_two').getStyle('opacity').toInt() == 0 ? 1 : $('q_two').getElement('input').value.toInt();
    var d = $('q_three').getElement('input').value.toInt();
        
    $('per_year').innerHTML = '$' + Math.round((a/b) * 365 * d);
    $('per_month').innerHTML = '$' + Math.round((a/b) * 30 * d);
    $('per_week').innerHTML = '$' + Math.round((a/b) * 7 * d);

	if($$('.input_email')[0].value.length > 0) {
		$('calc_form').set('send', {url: 'quitCalculator.php'});
		$('calc_form').send();
	}
	
	cookie.set('q_one', $('q_one').getElement('input').value);
	cookie.set('q_two', $('q_two').getElement('input').value);
	cookie.set('q_three', $('q_three').getElement('input').value);
	cookie.set('email', $('email').getElement('input').value);
	cookie.save();
    
    return false;
}

function configure(per_period, per_packet, cost, p) {
	period = p;
	
    $('q_one').fade(per_period ? 'show' : 'hide');
    $('q_two').fade(per_packet ? 'show' : 'hide');
    $('q_three').fade(cost ? 'show' : 'hide');
    
    $$('#q_one label')[0].innerHTML = per_period + ' per ' + period + ':';
    $$('#q_two label')[0].innerHTML = per_packet + ' per packet:';
    $$('#q_three label')[0].innerHTML = 'Cost per ' + cost + ' ($):';
}

function cigarettes_by_packet() {
    configure('Cigarettes', 'Cigarettes', 'packet', 'day');
}

function cigarettes_by_grams() {
    configure('Grams', 'Grams', 'packet', 'week');
}

function pipes_by_grams() {
    configure('Grams', 'Grams', 'packet', 'week');
}

function cigars() {
    configure('Cigars', false, 'cigar', 'week');
}

var menu;
var period = 'day';

function hideMenu() {
    menu.fade('out');
    document.removeEvent('mouseup', hideMenu);
    return false;
}

function clickMenu(e) {
    var text = e.target.innerHTML.toLowerCase().replace(/ /g, '_');
    eval(text + '();');

	$('mr_bungle').className = text;
	cookie.set('configuration', text);
	cookie.save();
	
    return false;
}

window.addEvent('domready', function() {
    menu = $('calculator_dropdown');
    menu.set('tween', {
        duration: 250
    });
    menu.setStyles({
        opacity: 0,
        display: ''
    });
    
    $('change').addEvent('click', function() {
        var position = $('change').getCoordinates();
        menu.setStyles({
            left: position['left']-229 + 'px',
            top: position['bottom']-164 + 'px',
            position: 'absolute'
        });
        menu.fade('toggle');
        document.addEvent('mouseup', hideMenu);
        return false;
    });
    
    menu.getElements('li a').each(function(e) {
        e.addEvent('click', clickMenu); 
    });

    var new_save_link = new Element('a', {href: '#'});
	new_save_link.replaces($('save_button')).addEvent('click', calculate);

	var howBody = $('how').getElement('.body');
	howBody.setStyles({display:''});
	var howSlideFx = new Fx.Slide(howBody);
	howSlideFx.hide();

	$('how').getElement('.title a').addEvent('click', function() {
		howSlideFx.toggle();
	});

	// var fields = "q_one,q_two,q_three,email".split(",");
	// 
	// fields.each(function(field) {
	// 	if(cookie.has(field))
	// 		$(field).getElement('input').value = cookie.get(field);
	// });
	// 
	// if(cookie.has('configuration')) {
	// 	var configuration = cookie.get('configuration');
	// 	eval(configuration + '()');
	// 	$('mr_bungle').className = configuration;
	// }
});



