﻿//Page Load
var strAjaxHandler = "/ajax/default.aspx";

$(document).ready(function() {

    if ($("a[rel=photos]").length > 0) {
        $("a[rel=photos]").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'titlePosition': 'over',
            'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
                return title;
            }
        });
    }

    //Set Default Values
    $('input.default').each(function() {
        var default_val = $(this).attr('default');
        if ($(this).val() == '') {
            $(this).val(default_val);
        }
        else if ($(this).val() != $(this).attr('default')) {
            $(this).addClass('focus');
        }

        $(this).focus(
            function() {
                if (default_val == $(this).val()) {
                    $(this).val('');
                }
                $(this).addClass('focus');
            }
        ).blur(
            function() {
                if ('' == $(this).val()) {
                    $(this).val(default_val);
                    $(this).removeClass('focus');
                }
            }
        );
    });


    //Clear Default Values
    $('form').submit(function() {
        $(this).find('input.default').each(function(index, value) {
            if ($(value).val() == $(value).attr('default')) {
                $(value).val('');
            }
        });
    });

    if ($('#slideshow').length > 0) {
        $('#slideshow').cycle({
            fx: 'fade',
            pager: '.slide_list',
            // callback fn that creates a thumbnail to use as pager anchor
            pagerAnchorBuilder: function(idx, slide) {
                return '<li><a class="dot"></a></li>';
            }
        });

        $('#slideshow').hover(function() {
            $('#slideshow').cycle('pause');
        }, function() {
            $('#slideshow').cycle('resume');
        });
    }

    //Ken, I think these two line only need to execute
    //if ($('.conference-schedule').length > 0) {
    
    // setup variables for session hours
    $('.chkSession').change(getSessionHours);

    $('').keyup(getSessionHours);
    //}
    
    getGlobalNavigations(); //this line executes every page load

    if ($('#herobox').length > 0)
        getHomepageHero(1);

    if ($('#home_bottom').length > 0)
        $('#home_threeboxes').css('height', $('#home_bottom').css('height'));
});

function getGlobalNavigations() {
    var data = {
        'action': 'get_global_navigation'
    };
    jQuery.ajax({
        cache: false,
        type: "GET",
        url: this.strAjaxHandler,
        dataType: "json",
        data: data,
        success: function(response) {
            if (response) {
                if (response.status == "success") {
                    $("#global").html(response.html);
                }
            }
        }
    ,
        error: function(error) { }
    });

    return false; //prevent propagation
}

function getHomepageHero(site_id) {
    if ($("#herobox").length > 0) {
        var data = {
            'action': 'get_homepage_hero',
            'site_id': site_id
        };
        jQuery.ajax({
            cache: false,
            type: "GET",
            url: this.strAjaxHandler,
            dataType: "json",
            data: data,
            success: function(response) {
                if (response) {
                    if (response.status == "success") {
                        $("#hero").html(response.html);
                    }
                }
            }
    ,
            error: function(error) { }
        });
    }
    return false; //prevent propagation
}

function toggleDivBlock(divElementID) {
    if (divElementID == "")
        divElementID = 'header_signin_form';
    if ($('#' + divElementID).css('display') == "block")
        $('#' + divElementID).slideUp();
    else
        $('#'+divElementID).slideDown();
    
    return false;
}

function getSessionHours() {
    var dblGrandTotal = 0;
    var txtHours = null;
    
    // loop through each check box, if checked add minutes to grandtotal
    $('.chkSession').each(function() {

        if (this.checked) {
            txtHours = document.getElementById('idTxtHours_' + this.value);
            dblGrandTotal += parseInt(txtHours.value);
        }   //end if

    }   //end function

    );   //end loop

    if (document.getElementById('idTxtTotalMinutes')) 
        document.getElementById('idTxtTotalMinutes').value = dblGrandTotal;

}   //end function

/* SPECIAL CODE FOR NATURAL STANDARDS DATABASE */
// Set to your organization name
var groupName = "Institute for Functional Medicine";

function rc4Encrypt(key, pt) {
    s = new Array();
    for (var i = 0; i < 256; i++) {
        s[i] = i;
    }
    var j = 0;
    var x;
    for (i = 0; i < 256; i++) {
        j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
        x = s[i];
        s[i] = s[j];
        s[j] = x;
    }
    i = 0;
    j = 0;
    var ct = '';
    for (var y = 0; y < pt.length; y++) {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        x = s[i];
        s[i] = s[j];
        s[j] = x;
        //ct += String.fromCharCode(pt.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
        ct += hexVal(pt.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
    }
    return ct;
}

function hexVal(n) {
    var hexDigits = "0123456789abcdef";
    return hexDigits.charAt(Math.floor(n / 16)) + "" + hexDigits.charAt(n % 16);
}

function addLeadingZero(n) {
    return (n < 10 ? "0" + n : n);
}

function getDateStr(aDate) {
    var y = aDate.getUTCFullYear();
    var m = aDate.getUTCMonth() + 1;
    var d = aDate.getUTCDate();
    return y + "-" + addLeadingZero(m) + "-" + addLeadingZero(d);
}

function getNsPassword(groupName, key) {
    var today = new Date();
    var dateStr = getDateStr(today);
    var str = rc4Encrypt(key, dateStr);
    return rc4Encrypt(str, groupName);
}

/**
 * Return the a tag html for linking to the
 * natural standards database
 */
function createNsLink(key, link_title) {
    var nsPw = getNsPassword(groupName, key);
    if (link_title == "") link_title = "Natural Standards Database";
    var strLink = "<a target=\"_blank\" href=\"http://naturalstandard.com/NSEntry3.asp?p=" + nsPw + "\">" + link_title + "</a>";
    return strLink;
}

function showHideHowHear(ddlHear, txtHowDidYouHear, txtColleagueName) {
    var strSelectedOption = "";

    strSelectedOption = ddlHear.options[ddlHear.selectedIndex].value;

    // make sure we close them first
    $("#" + txtHowDidYouHear).hide("fast");
    $("#" + txtColleagueName).hide("fast");
    
    if (strSelectedOption == "Other") {
        $("#" + txtHowDidYouHear).show("slow");
    }   //end if

    else if (strSelectedOption == "Colleague Referral") {
        $("#" + txtColleagueName).show("slow");
    }   //end if

}   //end function

