$(document).ready(function(){
    //Light Box function
    $('a.lightbox').lightBox(); // Select all links with lightbox class

    //Update menu function
    $("#menu li a").click(function() {
        $("#menu li a.current").removeClass("current");
        $(this).addClass("current");
    });

    // Email send clean up
    $("#sendmessage").click(function() {
        $("#menu li a.current").removeClass("current");
        $("#contact-link").addClass("current");
    });

    //This Day in History
    $('#dayHistory').click(function(){
        $('#currentText').append('<img src="images/horizontalStripesLoading.gif" class="loaderIcon" alt="Loading..." />');
        /*
        Get the current time for the day and month settings on the client side
        to pass it into a PHP function
        */
        var currentTime = new Date();
        var dataArray = [
        {
            name: "day",
            value: currentTime.getDate()
        },{
            name: "month",
            value: (currentTime.getMonth()+1)
        }
        ];
        /* Pass the dataArray variable to a PHP file */
        $.ajax({
            type: 'POST',
            url: 'include/dayInHistory.php',
            data: dataArray,
            dataType: 'json',
            success: function(data){
                generateTable(data);
            }, //end success function
            error: function(data){
                $('#currentText').html(data);
            }  //end error function
        }) //end ajax call
        $('#currentText img.loaderIcon').fadeOut(1000);
    });  //end click function

    function generateTable(data) {
        var greyRow = 1; // Used to stripe rows
        count = data.length;
        /*
        console.log(data);
        console.log('count=' + count);
        */
        var output = '<br /><br /><h4>This Day In History';
        output = output + '&nbsp;&nbsp;' + returnTime() + ' </h4>';
        output = output + '<br /> ' + count + '  records found';
        //Set up Table
        output = output + "<table width='650' class='timeTable' border='1'>";
        output = output + "<tr>";
        output = output + "<th bgcolor='#F8BFA3'><font face='Verdana, Arial, sans-serif, sans-serif' width='120px'>DATE</font></th>";
        output = output + "<th bgcolor='#F8BFA3'><font face='Verdana, Arial, sans-serif'>DESCRIPTION</font></th>";
        output = output + "</tr>";
        for(i=2; i < count; i++){
            if( greyRow == 1 ) {
                output = output + " <tr>";
                output = output + "<td valign='top' align='left' width='120px' >";
                output = output + '<rHistory>' + data[i].date  + '</rHistory>';
                output = output + "</td>";
                output = output + "<td valign='top' align='left' >";
                output = output + '<rHistory>' + data[i].description + '</rHistory>';
                output = output + "</td>";
                output = output + "</tr>";
                $greyRow = 0;
            }
            else {
                output = output + "<tr>";
                output = output + "<td valign='top' align='left' width='120px' >";
                output = output + data[i].date;
                output = output + "</td>";
                output = output + "<td valign='top' align='left'  >";
                output = output + data[i].description;
                output = output + "</td>";
                output = output + "</tr>";
                greyRow = 1;
            }
            output = output + "</tr>";
        }
        output = output + "</table>";
        $('#currentText').html(output);

        $(".timeTable tr").mouseover(function() {
            $(this).addClass("over");
        }).mouseout(function() {
            $(this).removeClass("over");
        });

        $(".timeTable tr:even").addClass("alt");
        $(".timeTable tr:odd").addClass("odd");
    }

    function returnTime(){
        var currentTime = new Date()
        var month = currentTime.getMonth() + 1
        var day = currentTime.getDate()
        var year = currentTime.getFullYear()
        var returnVar = month + "/" + day + "/" + year;
        return returnVar;
    }

    // Quote of the Day
    $('#randomQuote').click(function(){
        $('#currentText').append('<img src="images/horizontalStripesLoading.gif" class="loaderIcon" alt="Loading..." />');
        $.ajax({
            url: 'include/randomQuote.php',
            success: function(data){
                var outStr = formatQuote(data);
                $('#currentText').html(outStr);
            }, //end success function
            error: function(data){
                $('#currentText').html(data);
            }  //end error function
        }) //end ajax call
        $('#currentText img.loaderIcon').fadeOut(1000);
    });

    function formatQuote(data){
        var tempStr = new Array();
        tempStr = data.split('|');
        var retStr = "<rAuthor>" + tempStr[0] + ':</rAuthor><br />';
        retStr = retStr + "<rQuote>" + tempStr[1] + '</rQuote>';
        return retStr;
    }

    // Frequent Links load functions
    $('#frequentLinks').click(function(){
        $('#currentText').append('<img src="images/horizontalStripesLoading.gif" class="loaderIcon" alt="Loading..." />');
        $('#currentText').load('include/frequentlinks.php', function() {
            $("tr:even").addClass("even");
            $("tr:odd").addClass("odd");
            $("table tr").hover(function() {
                $(this).toggleClass("over")
            });
        });
        $('#currentText img.loaderIcon').fadeOut(1000);
    });

    // Resume display
    $('#resumeLinks').click(function(){
        $('#currentText').load('include/resumeMain.php', function() {
            });
    });


    // Clears the text area for displays
    $('#clearCurrentArea').click(function(){
        $('#currentText').html("");
        $('#currentText img.loaderIcon').fadeOut(1000);
    });
    
    // Bring up a Google Map of my location
    $('#gMapLinks').click(function(){
        $('#currentText').html("");
        $('<div id="map_canvas">').appendTo('#currentText');
        var latLng = new google.maps.LatLng(44.961455,-93.243006);
        var latLngMark = new google.maps.LatLng(44.961455,-93.243006);
        var latLngPowMark = new google.maps.LatLng(44.945118,-93.259077);
        var myOptions = {
            zoom: 17,
            center: latLng,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var marker = new google.maps.Marker({
            position: latLngMark,
            map: map,
            title:"My New Home Location!"

        });
       /*
       var markerPP = new google.maps.Marker({
            position: latLngPowMark,
            map: map,
            title:"Powderhorn Park!"
        });
        */
    });
});


