$(document).ready(function() {
    var dt = new Date();
    var activeMonth = dt.format('n') - 1;
    $("#eventAccordion").accordion({ header: "h3", autoHeight: false, active: activeMonth });
    if(activeMonth > 0)
        setFirstToInactive();
    else
        setFirstToActive();
});


var specialEventUrl = "special-events.aspx"
var EventsCalendar = Base.extend({

    constructor: function(container) {

        var outer = this;
        this.xmlData = null;

        // dom
        this.container = container;
        this.dateContainer = ".event-list h1:first";
        this.listingContainer = "#listing";
        this.dt;

        this.daylist = $(container).find(".days");
        this.dateList = $(container).find(".dates");
        this.eventList = $(container).find("#event-list");
        this.defaultEventHolder = $(container).find("#defaultEventHolder");
        this.currentDay = null;
        this.cover = null;


        //Load the xml and display
        $.ajax({
            type: "GET",
            url: "/services/getevents.ashx",
            //url: "/xml/eventlistings.xml",
            dataType: "xml",
            success: function(xml) {
                outer.xmlData = $(xml);

                // load todays selection
                dt = new Date()
                firstDayDate = new Date(dt.format('Y/m/') + "01");
                var day = firstDayDate.getDay() - 1 + dt.getDate();
                outer.currentDay = $(this.dateList).find("#" + dt.format('F').toLowerCase() + " li:nth-child(" + day + ")");
                outer.updateEventList(dt.format('Y/m/d'));

                $(this.dateList).find("li").each(function() {

                    // add the onclick
                    $(this).mousedown(function() {
                        var monthNumber = getMonthNumber($(this).parent().attr("id"));
                        //alert($(this).parent().attr("id"))
                        if ($(this).parent().attr("class") == "dates") {


                            if (outer.currentDay != null) {
                                outer.setDateImage($(outer.currentDay), false);
                            }

                            outer.currentDay = $(this);

                            if (outer.currentDay.html() != "&nbsp;") {
                                $(outer.eventList).fadeOut("fast", function() {
                                    outer.clearlist();
                                    outer.updateEventList("2010/0" + monthNumber + "/" + outer.currentDay.text());
                                    $(outer.eventList).fadeIn();
                                })
                            }

                        }

                    })



                })

            }
        });
    },

    updateEventList: function(date) {
        dt = new Date(date);
        var outer = this;

        this.updateDate(dt);

        this.showEvent(date);

        // format the days - turn everybody off
        $(this.daylist).find("li").each(function() {
            outer.setDayImage($(this), false);
        })

        var day = (dt.getDay() == 0) ? 7 : dt.getDay();
        // turn on the current day (mon etc)
        outer.setDayImage($(this.daylist[dt.format('n') - 1]).find("li:nth-child(" + day + ")"), true);

    },

    showCover: function(date) {

        var outer = this;


        var before = new Date("2010/02/15");
        var after = new Date("2010/04/15");
        var selected = new Date(date);

        var coverClass = "mod-calender-teaser";

        if (dates.compare(selected, before) == -1) {
            coverClass = "mod-calender-teaser-before";
        }

        if (dates.compare(selected, after) == 1) {
            coverClass = "mod-calender-teaser-after";
        }


        outer.removeCover();
        outer.cover = jQuery(document.createElement("div"));
        outer.cover.addClass(coverClass);

        outer.defaultEventHolder.append(outer.cover);
        $("#listing").css("display", "none");
        //$(outer.eventList).find("h1:first").css("display","none")


        $(outer.eventList).fadeOut("fast", function() {
            outer.clearlist();
            $(outer.cover).fadeIn();
            $(outer.eventList).fadeIn();
        })
        //}

    },

    removeCover: function() {

        var outer = this;

        $("#listing").css("display", "block");
        if (outer.cover) {
            outer.cover.remove();
        }







        //$(outer.eventList).find("h1:first").css("display","block")
        //$(".mod-calender-teaser").remove()
        //outer.cover = null


    },


    clearlist: function() {
        $("#listing").find(".event-list-item").each(function() {
            $(this).remove();
        })
    },


    setDayImage: function(item, highlight) {
        if (highlight) {
            $(item).addClass($(item).text().replace(" ", "") + "-active");
            $(item).attr("style", "background-position:bottom");
        } else {
            $(item).removeClass($(item).text().replace(" ", "") + "-active");
            $(item).attr("style", "background-position:top");
        }

    },

    setDateImage: function(item, highlight) {
        var activeName = "active-date";
        if (($(item)).is('.off')) {
            activeName += "-off";
        }
        if (highlight) {
            $(item).addClass(activeName);
            $(item).attr("style", "");
        } else {
            $(item).removeClass(activeName);
            $(item).attr("style", "background:none");
        }
    },


    updateDate: function(date) {
        var outer = this;
        dt = new Date(date);

        date = dt.format('l j{S} F Y');
        date = date.replace("{", "<sup>").replace("}", "</sup>");
        $("#event-list h1:first").html(date);
        outer.setDateImage($(outer.currentDay), true);
    },

    showEvent: function(date) {
        var url = specialEventUrl;
        dt = new Date(date);
        //url = url.replace("{0}", dt.format('F'))
        $("#all-events-link").attr("href", url);

        // find the 
        var xmlEvents = $("event", this.xmlData);
        var item;
        var dates;
        var listingItem;
        var count;
        var outer = this;


        date = date.replace(" ", "");
        count = $("index:contains('" + date + "')", this.xmlData);
        //count = 0

        //alert("hit")



        if (count.length > 0) {

            outer.removeCover();
            //$("#listing").scrollTo(0,0)

            xmlEvents.each(function() {
                dates = $("index", this);

                dates.each(function() {

                    if ($(this).text() == date) {




                        item = $(this).parent().parent();

                        listingItem = jQuery(document.createElement("div"));
                        listingItem.addClass("event-list-item");

                        htmlString = "<div class=\"event-thumb\"><div class=\"cover\"></div><img src='" + $("thumb", item).text() + "' width=\"95\" height=\"85\" alt='" + $("header", item).text() + "' title='" + $("header", item).text() + "'></div>";
                        var htmlStr = trunctate(stripHTML(($("details", item).text())), 50);
                        htmlStr = htmlStr.replace("Gourmet Traveller", "<i>Gourmet Traveller</i>");
                        htmlString += "<div class=\"list-text\"><h2>" + $("header", item).text() + "</h2><p>" + htmlStr + "</p>";



                        if ($("showMore", item).text() == "true");
                        //htmlString += "<a href='" + url + "#e" + $("id", item).text() + "' class='more-link'><span>more</span></a>"

                        htmlString += "<a href='" + $("more-info", item).text() + "' class='more-link'><span>more</span></a>";

                        htmlString += "</p></div>";

                        listingItem.html(htmlString);
                        $("#listing").append(listingItem);


                    }

                })


            })
        } else {

            outer.showCover(dt)
        }

    },

    setMoreLinks: function(url) {


        $(".more-link").each(function() {
            alert("asa");

        })

    }



})

function getMonthNumber(month) {
    var index = 6;
    switch (month) {
        case "january":
            index = 1;
            break;
        case "february":
            index = 2;
            break;
        case "march":
            index = 3;
            break;
        case "april":
            index = 4;
            break;
        case "june":
            index = 6;
            break;
        case "july":
            index = 7;
            break;
        case "august":
            index = 8;
            break;
    }
    return index;
}

function setFirstToInactive()
{
    var obj = $("#eventAccordion").find("h3:first");
    var obj2 = $("#eventAccordion").find("h4");
    
    obj.hide();
    obj2.show();
}

function setFirstToActive()
{
    var obj = $("#eventAccordion").find("h3:first");
    var obj2 = $("#eventAccordion").find("h4");
    
    obj.show();
    obj2.hide();
}



