﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
//*************************************************************************************
// File     : pages_functions.js
// Requires : jquery.js (version 1.4.2+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : Jul 21, 2010
// Modified : January 20, 2011
//*************************************************************************************

var printOpen = false;
$(document).ready(function() {
    fixInfoBoxes();
    printFunctions();
    if ($('.testimonials').length > 0) {
        testimonialPagination();
    }
    if ($('.list.oils input').length > 0) {
        $('.list.oils input').attr('value', '');
    }
    trackPercent();
});


function fixInfoBoxes() {
    var title, info;
    if ($('.essentialOils').length > 0) {
        for (i = 0; i < $('.essentialOils li').length; i++) {
            title = $('.essentialOils li:eq(' + i + ') a:first-child');
            info = $('.essentialOils li:eq(' + i + ') a:last-child');
            if (title.height() > info.height()) {
                info.css('height', title.height() + 'px');
            }
        }   
    }
    if ($('.fragranceOils').length > 0) {
        for (i = 0; i < $('.fragranceOils li').length; i++) {
            title = $('.fragranceOils li:eq(' + i + ') a:first-child');
            info = $('.fragranceOils li:eq(' + i + ') a:last-child');
            if (title.height() > info.height()) {
                info.css('height', title.height() + 'px');
            }
        }
    }
}

function printFunctions() {
    if (printOpen == false) {
        if ($('.lyeCalculator label[id$="txt_notes"]').length > 0 && $('textarea[id$="txt_notes"]').length < 1) {
            if ($('body').hasClass('print')) {
            } else {
                printOpen = true;
                window.open('http://www.brambleberry.com/Pages/Print/LyeCalculatorPDF.aspx', '_blank');
            }
        }
    }

}

function testimonialPagination() {
    var l = $('.testimonials > li').length
    if (l > 0) {
        var pages = Math.ceil(l / 10);
        for (i = 0; i < l; i++) {
            if (i % 10 == 0) $('.testimonials > li:eq(' + i + ')').attr('id', 'page-head-' + ((i / 10) + 1));
            $('.testimonials > li:eq(' + i + ')').addClass('page-' + (Math.floor(i / 10) + 1));
        }
        $('.testimonials .page-1').addClass('show');
        $('.testimonials').after('<ol class="paging"></ol>');
        for (i = 0; i < pages; i++) {
            $('.paging').append('<li><a href="#page-head-' + (i + 1) + '">' + (i + 1) + '</a></li>');
        }
        $('.paging li:first-child a').addClass('current');
        $('.paging a').bind('click', function() {
            var p = $(this).attr('href').split('-')[2];
            $('.testimonials li').removeClass('show');
            $('.testimonials .page-' + p).addClass('show');
            $('.paging a').removeClass('current');
            $(this).addClass('current');
        });
        if (location.hash != "") {
            var p = location.hash.split('-')[2];
            $('.testimonials li').removeClass('show');
            $('.testimonials .page-' + p).addClass('show');
            $('.paging a').removeClass('current');
            $('.paging a:eq(' + p + ')').addClass('current');
        }
    }
}

function trackPercent() {
    if ($('.finished-weight').length > 0) {
        var t = 0;
        for (i = 0; i < $('.list.oils input[type="text"]').length; i++) {
            var e = $('.list.oils input[type="text"]').eq(i);
            if (e.attr('value') != '') {
                t = t + (e.attr('value') * 1);
            }
        }
        $('#computedPercent').html(t + '%');
        if (t < 100) {
            $('.warning-box').addClass('mistake');
            $('.warning-box .message').html("<strong>Hey! Looks like you've got to add " + (100 - t) + "% somewhere.</strong> Keep adding until you reach 100%.");
        } else if (t > 100) {
            $('.warning-box').addClass('mistake');
            $('.warning-box .message').html("<strong>Oops! You've got to remove " + (t - 100) + "% somewhere.</strong> Make sure your numbers add up to 100%.");
        } else {
            $('.warning-box').addClass('correct');
            $('.warning-box .message').html("<strong>Nice Work! You've added exactly 100%.</strong> Click CALCULATE so we can crunch these numbers for you.");
        }
    }

    $('.list.oils input[type="text"]').blur(function() {
        if ($('.finished-weight').length > 0) {
            var t = 0;
            for (i = 0; i < $('.list.oils input[type="text"]').length; i++) {
                var e = $('.list.oils input[type="text"]').eq(i);
                if (e.attr('value') != '') {
                    t = t + (e.attr('value') * 1);
                }
            }
            $('#computedPercent').html(t + '%');
            if (t < 100) {
                $('.warning-box').addClass('mistake');
                $('.warning-box').removeClass('correct');
                $('.warning-box .message').html("<strong>Hey! Looks like you've got to add " + (100 - t) + "% somewhere.</strong> Keep adding until you reach 100%.");
            } else if (t > 100) {
                $('.warning-box').addClass('mistake');
                $('.warning-box').removeClass('correct');
                $('.warning-box .message').html("<strong>Oops! You've got to remove " + (t - 100) + "% somewhere.</strong> Make sure your numbers add up to 100%.");
            } else {
                $('.warning-box').addClass('correct');
                $('.warning-box').removeClass('mistake');
                $('.warning-box .message').html("<strong>Nice Work! You've added exactly 100%.</strong> Click CALCULATE so we can crunch these numbers for you.");
            }
        }
    });
    $('.warning-box.mistake .calculate').live('click', function() {
        return false;
    });
}
