var socialTemplate = "<div class='sociable'>"
    + "<div class='sociable_tagline'>"
    + "    Share this event: "
    + "      </div>"
    + "      <ul>"
    + "            <li><a title='Digg' href='http://digg.com/submit?phase=2&amp;url=_url_&amp;title=_title_'"
    + "    target='_blank' rel='nofollow'>"
    + "      <img alt='Digg' title='Digg' src='/content/digg_32.png' /></a></li>"
    + "    <li><a title='Share on twitter' href='http://twitter.com/home?status=_title_ _url_'"
    + "          target='_blank' rel='nofollow'>"
    + "            <img alt='TwitThis' title='TwitThis' src='/content/twitter_32.png' /></a></li>"
    + "          <li><a title='Facebook' href='http://www.facebook.com/sharer.php?u=_url_&amp;t=_title_'"
    + "                target='_blank' rel='nofollow'>"
    + "  <img alt='Facebook' title='Facebook' src='/content/facebook_32.png' /></a></li>"
    + "<li><a title='E-mail this story to a friend!' href='mailto:?subject=_title_&amp;body=_url_'"
    + "                target='_blank' rel='nofollow'>"
    + "                <img alt='E-mail this to a friend!' title='E-mail this to a friend!' src='/content/email_32.png' /></a></li>"
    + "</ul>"
    + "</div>";

$(function() {

    $("#copyrighttext p").text($("#copyrighttext p").text().replace('_year_', new Date().getFullYear()));

    $('#flyingman').cycle({
        fx: 'scrollDown',
        speedIn: 2000,
        speedOut: 500,
        easeIn: 'bounceout',
        easeOut: 'backin',
        delay: -4000
    });

    $("#reserveNow").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 550,
        width: 450,
        modal: true,
        buttons: {
            'Reserve Now': function() {
                $(this).find("form").submit();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });

    $("#lnkEnterNowDiv").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 400,
        width: 350,
        modal: true,
        buttons: {
            'Enter Now': function() {
                $(this).find("form").submit();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });

    $("#lnkFaqDiv").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 450,
        width: 550,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });

    $("#thankYouReserveNow").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 230,
        width: 350,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });

    $("#thankYouEnterNow").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 230,
        width: 350,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });


    $("#lnkAboutUsDiv").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 450,
        width: 550,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });

    $('#moreEventsDiv').dialog({
        bgiframe: true,
        autoOpen: false,
        height: 550,
        width: 700,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });

    $('#mapPopup').dialog({
        bgiframe: true,
        autoOpen: false,
        height: 650,
        width: 700,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            location.hash = '';
        }
    });


    $('#lnkEnterNow').click(function() {
        $('#lnkEnterNowDiv').dialog('open');
    });

    $('#lnkAboutUs').click(function() {
        $('#lnkAboutUsDiv').dialog('open');
    });

    $('#lnkFaq').click(function() {
        $('#lnkFaqDiv').dialog('open');
    });

    $('#lnkReserveNow').click(function() {
        $('#reserveNow').dialog('open');
    });

    $('#lnkMoreEvents').click(function() {
        $('#moreEventsDiv').dialog('open');
    });

    $('#lnkFourthAveEvent').click(function() {
        $('#fourthAveEventDiv').dialog('open');
    });


    var calendarDiv;

    if (location.hash && location.hash.length > 0) {
        calendarDiv = $(location.hash);
        if (calendarDiv.length > 0)
            openMapPopup(calendarDiv[0]);
    } else if ($("#hdnSelectedId").val().length > 0) {
        calendarDiv = $("#" + $("#hdnSelectedId").val());
        if (calendarDiv.length > 0)
            openMapPopup(calendarDiv[0]);
    }

});

function playVideo() {

    $("#playbutton").hide();
    $("#itsamazingtext").hide();
    $("#videopreview").hide();

    $("#flashMovieDiv").show();


}

function onYouTubePlayerReady() {
    $("#flashMovie")[0].playVideo();
}


function showAddress(address) {
    if (!GBrowserIsCompatible()) return;
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var geocoder = new GClientGeocoder();

    geocoder.getLatLng(
						address,
						function(point) {
						    if (!point) { }
						    else {
						        map.clearOverlays()
						        map.setCenter(point, 14);
						        var marker = new GMarker(point, { draggable: true });
						        map.addOverlay(marker);
						    }
						});
}

function openMapPopup(parentDiv) {
    $('#mapPopup #when').html("<b>When:</b> " + $(parentDiv).find(".event-time").attr("title"));
    $('#mapPopup #where').html("<b>Where:</b> " + $(parentDiv).find(".event-where").val());

    var description = $(parentDiv).find(".event-descr").val().replace(/(https?:\/\/[^ ;|\\*'"!,()<>]+\/?)/g, '<a href="$1">$1</a>');

    $('#mapPopup #description').html("<b>Description:</b> " + description);
    $('#mapPopup').dialog('option', 'title', $(parentDiv).find(".event-title").html()).dialog('open');

    var id = $(parentDiv).attr('id');
    if (!id || id.length == 0) {
        id = $(parentDiv).parents("div.day:eq(0)").attr('id');
        location.hash = id;
    }
    else
        location.hash = id;

    showAddress($(parentDiv).find(".event-where").val());

    var socialTemplateReplaced = socialTemplate.replace(/_url_/g, "http://" + location.host + "/" + id)
                                               .replace(/_title_/g, escape($(parentDiv).find(".event-title").text().replace(/\n|\s{2,}/g, "")));

    $('#mapPopup').find('div.sociable').remove();
    $('#mapPopup').find('div#description').after($(socialTemplateReplaced));

}

