﻿/// <reference path="jquery-vsdoc.js" />
//*************************************************************************************
// File     : usma_functions.js
// Requires : jquery.js (version 1.3.2+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : December 28, 2009
// Modified : January 08, 2010
//*************************************************************************************

window.onload = function() {
    footerPush();
    equalColumns();
    $('#schedule .previous').bind('click', function() { slideSchedule('left'); $(this).blur(); });
    $('#schedule .next').bind('click', function() { slideSchedule('right'); $(this).blur(); });
    $('#schedule .schedule').bind('click', function() { window.location = "/Schedule.aspx"; });
    displayScheduleButtons();
    $('.gallery a').lightBox({ fixedNavigation: true });
    googleMap();
};

function equalColumns() {
    // This function equalizes the visual height of the primary and secondary columns so their backgrounds line up with one another.
    var h = $('#header').height();
    var c = $('#content').css('padding-top').substr(0, $('#content').css('padding-top').length - 2) * 1;
    var f = $('#footer').height();
    var p1 = $('#primary').height();
    var s1 = $('#secondary').height();
    var p2 = ($('#primary').css('padding-top').substr(0, $('#primary').css('padding-top').length - 2) * 1) + ($('#primary').css('padding-bottom').substr(0, $('#primary').css('padding-bottom').length - 2) * 1);
    var s2 = ($('#secondary').css('padding-top').substr(0, $('#secondary').css('padding-top').length - 2) * 1) + ($('#secondary').css('padding-bottom').substr(0, $('#secondary').css('padding-bottom').length - 2) * 1);
    var p = p1 + p2;
    var s = s1 + s2;
    var ptotal = h + c + f + p2;
    var stotal = h + c + f + s2;
    var diff = $(window).height();
    //alert(p + ' ' + diff + ' ' + ptotal);
    //alert(s + ' ' + diff + ' ' + stotal);
    if (p1 < (diff - ptotal) && s1 < (diff - stotal)) {
        $('#primary').css('min-height', (diff - ptotal) + 'px');
        $('#secondary').css('min-height', (diff - stotal) + 'px');
    } else {
        if (p > s) { $('#secondary').css('min-height', (p1 + (p2 - s2)) + 'px'); } else { $('#primary').css('min-height', (s1 + (s2 - p2)) + 'px'); }
    }
}

function slideSchedule(dir) {
    // This function slides the schedule (on the homepage) left/right.
    $('#schedule ol').stop();
    var slide = 0;
    var wide = 0;
    for (i = 0; i < $('#schedule li').length; i++) {
        wide = wide + $('#schedule li:eq(' + i + ')').width() + 20;   
    }
    var margin = $('#schedule ol').css('margin-left').substr(0, $('#schedule ol').css('margin-left').length - 2)*1;
    if (dir == "left") {
        slide = 480;
        margin += slide;
        if (margin > 0) {
            margin = 0;
        }
    } else if (dir == "right") {
        slide = -480;
        margin += slide;
        if (margin < (($('#schedule .wrapper').width() * 1) - wide)) {
            margin = (($('#schedule .wrapper').width() * 1) - wide);
            var i = 0;
            var result = 0;
            while (result >= margin) {
                i++;
                result = -(i * 160);
            }
            i--;
            margin = -(i * 160);
        } else {
        }
    }
    $('#schedule ol').animate({ marginLeft: margin + 'px' }, "normal", 'linear', function() { displayScheduleButtons(); });
}

function displayScheduleButtons() {
    // function causes the schedule previous/next buttons to show/hide depending on the state of the slider
    if ($('#schedule').html()) {
        var wide = 0;
        for (i = 0; i < $('#schedule li').length; i++) {
            wide = wide + $('#schedule li:eq(' + i + ')').width() + 20;
        }
        var margin = ($('#schedule ol').css('margin-left').substr(0, $('#schedule ol').css('margin-left').length - 2) * 1);

        if (margin == 0) {
            $('#schedule .previous').css('background-image', 'none');
        } else {
            $('#schedule .previous').css('background-image', '');
        }
        if (Math.abs(margin - (($('#schedule .wrapper').width() * 1) - wide)) < 30) {
            $('#schedule .next').css('background-image', 'none');
        } else {
            $('#schedule .next').css('background-image', '');
        }
    }
}

function googleMap() {
    // this function loads a google map showing the gym's location.
    BrainGnat.Google.map.load("#map");
    BrainGnat.Google.map.loadFromHCard("#secondary .vcard", "#map", 13, false);
}

function footerPush() {
    // this function will measure the height of the page body. If it's smaller than the window's height, it'll stretch it.
    var b = $('body');
    var w = $(window);
    var diff = $(window).height() - (b.css("padding-bottom").substr(0, b.css("padding-bottom").length - 2) * 1);
    if (b.height() < diff) {
        b.css("height", diff + "px");
    }
}