var day = null;
/*overstate*/
$(document).ready(function () {

    var ie7 = (navigator.userAgent.indexOf("MSIE 7") != -1) ? true : false;


    $(".menuoverstate").each(function (i) {

        preload_image = new Image();
        preload_image.src = this.src.replace(".jpg", "_o.jpg");

        $(this).hover(
            function () {
                if (this.src.indexOf("_o.jpg") == -1) {
                    this.src = this.src.replace(".jpg", "_o.jpg");
                }

            },

            function () {
                this.src = this.src.replace("_o.jpg", ".jpg");

            });

    }
        );

    $(".menuoverstate2").each(function (i) {

        preload_image = new Image();
        preload_image.src = this.src.replace(".gif", "_o.gif");

        $(this).hover(
            function () {
                if (this.src.indexOf("_o.gif") == -1) {
                    this.src = this.src.replace(".gif", "_o.gif");
                }

            },

            function () {
                this.src = this.src.replace("_o.gif", ".gif");

            });

    }
        );


    /*overstates*/

    jQuery(".quicklinks img, .gallery img, #gallery-small img").hover(function () {
        jQuery(this).stop().animate({ opacity: 0.5 }, 200);
    },
        function () {
            jQuery(this).stop().animate({ opacity: 1.0 }, 200);
        });

    jQuery("table#pricing tbody tr:odd, table#specs tbody tr:odd, table#offer tbody tr:odd").addClass('odd');


    $('a.img-link').hover(function () {

        var src = $(this).find("img").attr("src");

        if (src != null) {

            $(this).find("img").attr("src", $(this).find("img").attr("alt"));

            $(this).find("img").attr("alt", src);
        }

    });

    try
    {
        loadCalendar($('#month-holder').html().split(':')[0] + "/" + $('#month-holder').html().split(':')[1]);
    }catch(e){}

    $('#calendar-prev').click(function () {
        var month = $('#month-holder').html().split(':')[0] * 1;
        var year  = $('#month-holder').html().split(':')[1] * 1; 

        if (month == 1) {
            month = 12;
            year--;
        }
        else {
            month--;
        }

        loadCalendar(month + "/" + year);
        return false;
    });

    $('#calendar-next').click(function () {
        var month = $('#month-holder').html().split(':')[0] * 1;
        var year = $('#month-holder').html().split(':')[1] * 1;

        if (month == 12) {
            month = 1;
            year++;
        }
        else {
            month++;
        }

        loadCalendar(month + "/" + year);
        return false;
    });




    if (ie7) {
        $(".news-habit").addClass("news-habit-IE7");
        $(".main-bottom-img > img").addClass("ie7-img-fix");
    }

});

function loadCalendar(date) {
    var m = new Array(12);
    var current = (day != null) ? "&day="+day : "";
    m[0] = "Jan"; m[1] = "Feb"; m[2] = "Mar"; m[3] = "Apr"; m[4] = "May"; m[5] = "Jun"; m[6] = "Jul"; m[7] = "Aug"; m[8] = "Sep"; m[9] = "Oct"; m[10] = "Nov"; m[11] = "Dec";
    $.ajax({
        url: "/app_pages/calendar.aspx?date=" + date + current,
        success: function (data, textStatus, jqXHR) {
            $('#calendar-wrapper').html(data);

            $('.calendar-event-hover').each(function () {
                //var letters = $(this).children('a').html().split('');

                $(this).width($(this).find('a span').width());
                $(this).hide();

            });

            $('.calendar-event').hover(function () {

                $(this).children(".calendar-event-hover").toggle();
            });

            $('#calendar-header-month').html(m[date.split("/")[0] - 1] + " '" + date.split("/")[1].substring(2, 4));
            $('#month-holder').html(date.replace("/", ":"));
        }
    });
}
