﻿/// <reference path="jquery-vsdoc.js" />
//*************************************************************************************
// File     : skag_functions.js
// Requires : jquery.js (version 1.3.2+), braingnat.js (version 0.4.0+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : Mar 03, 2010
// Modified : Apr 20, 2010
//*************************************************************************************

// Global variables
var thisYear = 0;
var lastYear = 0;

$(document).ready(function() {
    //BrainGnat.checkForLocal();
    BrainGnat.slideshow.ajaxLoadCrossfade('/ridge_httpdocs/Controls/imageList/imageList.aspx?dir=/ridge_httpdocs/uploads/slideshow/', '#wrapper', 'img', 8000, 1000, false);
    toggleImage();
    setYears();
    alignDates();
    evenThingsOut();
});

var endOfMonth = new Array();
endOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var now = new Date();
var month = now.getMonth();
var day = now.getDate();
var year = now.getFullYear();

function toggleImage() {
    if ($('.toggle').length > 0) {
        $('.switch').click(function(event) {
            $('.toggle').attr('src', $(this).attr('href'));
            return false;
        });
    }
}

function evenThingsOut() {
    var difference = $(window).height() - ($('#header').outerHeight() + $('#footer').outerHeight() + 40);
    if ($('#content').height() < difference) {
        $('#content').css('min-height', difference + 'px');
    }
}

function setYears() {
    var d = new Date();
    thisYear = d.getFullYear();
    lastYear = thisYear - 1;
}

function alignDates() {
    // 1. set initial date to today and tomorrow.
    // set the year
    $('#inyear option:eq(' + (year - thisYear) + ')').attr('selected', 'selected');
    $('#outyear option:eq(' + (year - thisYear) + ')').attr('selected', 'selected');
    // set the month
    $('#inmonth option:eq(' + month + ')').attr('selected', 'selected');
    $('#outmonth option:eq(' + month + ')').attr('selected', 'selected');
    // set the day
    $('#inday option:eq(' + (day - 1) + ')').attr('selected', 'selected');
    if (day >= endOfMonth[month]) {
        if ((month + 1) > 11) {
            $('#outyear option:eq(' + (year - lastYear) + ')').attr('selected', 'selected');
            $('#outmonth option:eq(0)').attr('selected', 'selected');
            $('#outday option:eq(0)').attr('selected', 'selected');
        } else {
            $('#outmonth option:eq(' + (month + 1) + ')').attr('selected', 'selected');
            $('#outday option:eq(0)').attr('selected', 'selected');
        }
    } else {
        $('#outday option:eq(' + day + ')').attr('selected', 'selected');
    }

    // 2. Event handlers for when the date is altered. Prevent the date from going before today, or the out date from preceding the in date.
    $('#inmonth').change(function() { maintainDates(); });
    $('#inday').change(function() { maintainDates(); });
    $('#inyear').change(function() { maintainDates(); });
    $('#outmonth').change(function() { maintainDates(); });
    $('#outday').change(function() { maintainDates(); });
    $('#outyear').change(function() { maintainDates(); });

}

function maintainDates() {
    // 1. Collect day/month/year for in and out.
    id = $('#inday option').index($('#inday option:selected'));
    im = $('#inmonth option').index($('#inmonth option:selected'));
    iy = $('#inyear option').index($('#inyear option:selected')); 0
    od = $('#outday option').index($('#outday option:selected'));
    om = $('#outmonth option').index($('#outmonth option:selected'));
    oy = $('#outyear option').index($('#outyear option:selected'));
    // 2. Make sure in date is not before today, if it is, set it to today.
    if (iy < (year - thisYear)) {
        iy = (year - thisYear);
    } else if (iy == (year - thisYear)) {
        if (im < month) {
            iy = (year - lastYear);
        } else if (im == month) {
            if (id < (day - 1)) {
                im = month + 1;
                if (im > 11) {
                    im = 0;
                    iy = (year - lastYear);
                }
            }
        }
    }
    // 3. Make sure out date is not before in date. If it is, set it after in date.
    if (oy < iy) {
        oy = iy;
    }
    if (oy == iy) {
        if (om < im) {
            om = im;
        }
        if (om == im) {
            if (od < (id + 1)) {
                od = (id + 1);
            }
            if (od > endOfMonth[month]) {
                od = 0;
                om = om + 1;
                if (om > 11) {
                    om = 0;
                    oy = oy + 1;
                }
            }
        }
    }
    // 4. Update the dates
    $('#inday option:eq(' + id + ')').attr('selected', 'selected');
    $('#inmonth option:eq(' + im + ')').attr('selected', 'selected');
    $('#inyear option:eq(' + iy + ')').attr('selected', 'selected');
    $('#outday option:eq(' + od + ')').attr('selected', 'selected');
    $('#outmonth option:eq(' + om + ')').attr('selected', 'selected');
    $('#outyear option:eq(' + oy + ')').attr('selected', 'selected');
}
