//jQuery.noConflict();
//jQuery(document).ready(function(){
								  
$(function($){
    var items = {
        'bike':             { 'cost': '20',  'name': 'Bike'},
        'book':             { 'cost': '1',   'name': 'Book'},
        'chair':            { 'cost': '10',  'name': 'Chair'},
        'coat-jacket':      { 'cost': '9',   'name': 'Coat/Jacket'},
        'dress':            { 'cost': '7',   'name': 'Dress'},
        'dvd-cd':           { 'cost': '3',   'name': 'DVD/CD'},
        'jeans':            { 'cost': '6',   'name': 'Jeans'},
        'lamp':             { 'cost': '8',   'name': 'Lamp'},
        'plate-mug-glass':  { 'cost': '1',   'name': 'Plate/Mug/Glass'},
        'pot-pan':          { 'cost': '2',   'name': 'Pot/Pan'},
        'purse':            { 'cost': '5',   'name': 'Purse'},
        'shirt-blouse':     { 'cost': '4',   'name': 'Shirt/Blouse'},
        'shoes':            { 'cost': '4',   'name': 'Shoes'},
        'suit':             { 'cost': '10',  'name': 'Suit'},
        'sweater':          { 'cost': '5',   'name': 'Sweater'},
        'tv':               { 'cost': '25',  'name': 'TV'},
        'video-game':       { 'cost': '3',   'name': 'Video Game'},
        'working-computer': { 'cost': '250', 'name': 'Working computer'}
    };
var impact_type = [
        {
            'name': "job search support",
            'rate': 36,
            'person': 'Doyle McGee',
            'text': "Your donations gave me a new beginning.",
            'image': "doyle-mcgee.jpg",
            'link': 'http://donate.goodwill.org/your-impact/doyle-mcgee/'
        },
        {
             'name': "Financial planning class",
            'rate': 38,
            'person': 'Rudy Batilla',
            'text': "Your donations allowed me to achieve my dreams.",
            'image': "rudy-batilla.jpg",
            'link': 'http://donate.goodwill.org/your-impact/rudy-batilla/'
        },
        {
            'name': "on-the-job training",
            'rate': 31,
            'person': 'Mel Yawn',
            'text': "Your donations prepared me for a rewarding new career.",
            'image': "mel-yawn.jpg",
            'link': 'http://donate.goodwill.org/your-impact/mel-yawn/'
        },
        {
            'name': "career counseling",
            'rate': 47,
            'person': 'Christie Jarrell',
            'text': "Your donations supported my transition to full-time work.",
            'image': "christie-jarrell.jpg",
            'link': 'http://donate.goodwill.org/your-impact/christie-jarrell/'
        },
        {
            'name': "resume preparation",
            'rate': 35,
            'person': 'Patrice Baugh',
            'text': "Your donations gave me the confidence to succeed.",
            'image': "patrice-baugh.jpg",
            'link': 'http://donate.goodwill.org/your-impact/patrice-baugh/'
        }
    ];


    // only pick an hour type once per page load.
    var hour_type = Math.floor(Math.random() * impact_type.length);
    var conversion_name = impact_type[hour_type]['name'];
    var conversion_rate = impact_type[hour_type]['rate'];
    jQuery(ids['impact-line2']).html(conversion_name + " for someone like " + impact_type[hour_type]['person']);
    jQuery(ids['impact-story']).html("See how " + conversion_name + " helped");
    jQuery(ids['impact-name']).html('My Story: ' + impact_type[hour_type]['person']);
    jQuery(ids['impact-story-text']).html(impact_type[hour_type]['text']);
    jQuery(ids['impact-story-image']).attr('src', weblocation + impact_type[hour_type]['image']);
    jQuery(ids['impact-story-link']).attr('href', impact_type[hour_type]['link']);
	jQuery(ids['impact-story-img-link']).attr('href', impact_type[hour_type]['link']);
	
    var donations = [];
    var per_page = 5;

    function get_float(elem) {
        var value = elem.html();
        value = $.trim(value);
        value = parseFloat(value);
        return value;
    }

    function display_donations(page) {
        var donation_list = jQuery(ids['your-items']).children("dl");
        donation_list.empty();
        var offset = donations.length - (page * per_page);
        var last = Math.max(offset - per_page, 0);
        var line = 1;
        for (var d = offset-1; d >= last; d--) {
            if ((line % 2) == 0) {
                donation_list.append("<dt class=\"alt\">" + donations[d].name + "</dt><dd class=\"alt\">" + donations[d].num + "<a href=\"#\" title=\"Remove this item\" class=\"remove\" id=\"donation-" + d + "\">remove</a></dd>");
            }
            else {
                donation_list.append("<dt>" + donations[d].name + "</dt><dd>" + donations[d].num + "<a href=\"#\" title=\"Remove this item\" class=\"remove\" id=\"donation-" + d + "\">remove</a></dd>");
            }
            line++;
        }
        jQuery(".remove").click(remove_donation);
        /*var pages = Math.ceil(donations.length / 5.0);
        pages = Math.max(1, pages);
        var pager = jQuery(ids['items-paging']);
        pager.empty();
        if (pages > 1) {  // don't paginate one page
            for (var p = 0; p < pages; p++) {
                pager.append("<li><a href=\"#\" class=\"page-selector\">" + (p+1) + "</a></li>");
            }
        }
        jQuery(".page-selector").click(display_donations_page);*/
		
    }

    function display_donations_page(event) {
        var page = jQuery(event.target).html();
        page = $.trim(page);
        page = parseInt(page);
        display_donations(page - 1);
    }

    function display_hours() {
        var total = 0.0;
        for (var i = 0; i < donations.length; i++) {
            total += donations[i].cost;
        }
        var hours = total / conversion_rate;
        if (hours < 1.0) {
            jQuery(ids['impact-line1']).html("minutes of");
            var minutes = hours * 60;
            jQuery(ids['impact-total']).html("" + Math.floor(minutes));
        }
        else {
            jQuery(ids['impact-line1']).html("hours of");
            jQuery(ids['impact-total']).html("" + hours.toFixed(1));
        }
    }

    function add_donation_handler(event) {
        var sections = ["clothing", "household", "electronics"];
        for (var i = 0; i < sections.length; i++) {
            if (jQuery(ids['ci'] + sections[i]).hasClass("current")) {
                var item = jQuery(ids['dc'] + sections[i] + "-select").val();
                var num = jQuery(ids['dc'] + sections[i] + "-num").val();
                if (item && ! isNaN(num)) {
                    num = Math.floor(num);
                    if (num > 0) {
                        var cost = items[item]['cost'] * num;
                        donations.push({"name": items[item]['name'], "cost": cost, "num": num});
                        display_donations(0);
                        display_hours();
                    }
                }
                break;
            }
        }
        // add_donation(donation);
        return false;
    }

    function remove_donation(event) {
        var id = event.target.id.split('-')[1];
        var deleted = donations.splice(id, 1);
        display_donations(0);
        display_hours();
        return false;
    }

    function clear_donations(event) {
        donations = [];
        display_donations(0);
        display_hours();
    }

    jQuery(ids['submit']).click(add_donation_handler);
    jQuery(ids['clear']).click(clear_donations);

	var containers = jQuery(ids['form'] + ' .container');
	jQuery(ids['type-buttons'] + ' a').click(function () {
		containers.hide().filter(this.hash).show();
		
		jQuery(ids['type-buttons'] + ' li').removeClass('current');
		jQuery(this).parent().addClass('current');
		
		return false;
	}).filter(':first').click();

});


