﻿// JScript File
var concertDate;
var currentVideoID;
var firstTimePlay = false;
var host = 'gigpromoter';
var skip;
var fullShareURL;

var CPL = function() {
    var concerts = new Array();
    var ytplayer;
    var currentIndex = 0;
    var currentMarker;
    var map;
    var geoCoder;
    var popupMarginBottom = 20;
    var firstLoad = true;

    var months = new Array("Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul", "Aug", "Sept",
    "Oct", "Nov", "Dec");

    var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

    return {
        Point: null,
        CurrentLocation: null,

        ChangeLocation: function() {
            var location = $('#txtZip').val();

            geoCoder = new GClientGeocoder(null);
            geoCoder.reset();
            //geoCoder.setBaseCountryCode("USA");
            geoCoder.getLatLng(location, function(point) {

                if (point) {
                    CPL.Point = point;

                    if (!isLocalized) {
                        createCookie("defaultLocation", location, 90);
                        createCookie("defaultPoint", CPL.Point, 90);
                    }

                    map.panTo(point);

                    CPL.CloseCurrentConcert();
                    currentIndex = -1;
                    CPL.GetConcerts();
                } else {
                    $('#txtZip').val('Sorry we couldn\'t find this address. Please try again');
                }
            });
        },

        //load concerts data
        GetConcerts: function() {
            $('#concertContainer').empty();

            if (firstLoad) {
                $('#divPlayerContainer').css('visibility', 'hidden')
                $('#divDataLoading').css('display', 'block')
            }
            else {
                var html = '<div style="margin-left: 100px; margin-top: 30px; width: 240px;">';
                html += '<img src="/content/images/thefilter_logo2.jpg" title="" alt="" style="margin-left: 10px; margin-bottom: 20px"  />';
                html += '<div style="text-align: center;">';
                html += '<img src="/content/images/tiny_red.gif" title="" alt="" /> Loading concert data. Please wait.....';
                html += '</div></div>';
                $('#concertContainer').append(html);
                html = '';
            }

            createCookie("defaultDateRange", $('#selDateRange')[0].selectedIndex, 90);

            concerts = new Array();

            if ($('#selDateRange').val() == 'specificdate') {
                concertDate = $("#datepicker").datepicker('getDate');
            } else {
                concertDate = new Date();
            }

            currentIndex = -1;

            var displayLocation = $('#txtZip').val();

            CPL.CurrentLocation = displayLocation;

            $.get(
                '/Data?token=' + (new Date()).getUTCMilliseconds(),
                { point: CPL.Point, dateRange: $('#selDateRange').val(), date: (concertDate.getMonth() + 1) + "-" + concertDate.getDate() + "-" + concertDate.getFullYear(), location: displayLocation },
                function(data) {
                    var xml = CPL.ParseXML(data);
                    var currentDate = null;
                    var html = "";
                    var concertIndex = 0;
                    currentIndex = 0;

                    //var location = $(xml).find('Results_Title').eq(0).text();
                    var location = 'New York';

                    if (location.length > 0) {
                        var displayLocation = $('#txtZip').val();
                        //$('#divLocation').html(displayLocation);
                    }
                    else {
                        //alert($(xml).text());
                        $('#txtZip').val('Unknown location - enter a valid zip');
                        $('#divDataLoading').css('display', 'none')
                        $('#divPlayerContainer').css('visibility', 'visible')
                        CPL.ShowChangeLocation();
                    }

                    $(xml).find('Event').each(function() {

                        var event_id = $(this).attr('eventId');
                        var event_date = $(this).find('EventDate').text();
                        var ticket_url = $(this).attr('ticketUrl');
                        var venue = CPL.GetVenueInfo($(this).find('Venue'));

                        $(this).find('Artist').each(function() {
                            var artist_id = $(this).attr('artistId');
                            var artist_name = $(this).find('Name').text();

                            var concert = new CPL.Concert(event_id, event_date, artist_id, artist_name, venue, ticket_url);
                            concert.videoID = $(this).find('Video').attr('id');

                            //right now we can't handle muzu vids
                            if (concert.videoID.length > 0) {
                                concerts.push(concert);

                                if (CPL.FormatDate(CPL.GetConcertDateAsDate(concert)) != currentDate) {
                                    if (currentDate != null) {
                                        //alert(concert.date);
                                        html += '</div>'
                                        /*$('#concertContainer').append(html);
                                        html = "";*/
                                    }

                                    html += '<div class="date_header">' + CPL.FormatDate(CPL.GetConcertDateAsDate(concert)) + '</div><div class="concerts">';
                                    currentDate = CPL.FormatDate(CPL.GetConcertDateAsDate(concert));
                                }

                                html += '<div id="liConcert_' + concertIndex + '" class="unselected">';

                                //html += '<div style="border: 0px; padding: 0px; margin-top: 2px; float: right; margin-right: 10px;">
                                html += '<a href="' + concert.ticketURL + '?affil_code=TheFilter" onclick="logEvent(\'ClickTicketURL\', \'' + concert.ticketURL + '\');" target="_blank"><img src="/content/images/buyticketsbutton.gif" style="height: 32px; float: right; margin-right: 12px;" title="Buy Tickets" onmouseover="$(this).toggleClass(\'opaque\');" onmouseout="$(this).toggleClass(\'opaque\');" /></a>';
                                //html += ' <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26ref%255F%3Dnb%255Fss%255Fdmusic%26field-keywords%3D' + escape(concert.artistName) + '%26url%3Dsearch-alias%253Ddigital-music&tag=gigpromoter-20&linkCode=ur2&camp=1789&creative=390957" onclick="logEvent(\'ClickAmazonLink\', \'' + concert.artistName + '\');" target="_blank" style="margin-left: 5px;"><img src="/content/images/amazon-a-dl-ico.png" title="Buy music on Amazon" style="width: 32px; height: 32px;" onmouseover="$(this).toggleClass(\'opaque\');" onmouseout="$(this).toggleClass(\'opaque\');" /></a>';
                                //html += ' <a href="http://www.ticketmaster.com" target="_blank" style="margin-left: 5px;"><img src="/content/images/users.png" title="Share" onmouseover="$(this).toggleClass(\'opaque\');" onmouseout="$(this).toggleClass(\'opaque\');" />'
                                //html += '</div>';

                                html += '<a id="aConcert_' + concertIndex + '" name="aConcert_' + concertIndex + '" href="#" onclick="CPL.PlayConcert(' + concertIndex + '); return false;">';
                                html += '<span class="artistName">' + concert.artistName + '</span></a><br />';
                                html += concert.venue.name;

                                html += '</div>';

                                if (event_id == defaultEvent && artist_id == defaultArtist) {
                                    currentIndex = concertIndex;
                                }

                                concertIndex++;
                            }
                        }
                        ); //end each for 'artist_name'
                    }
                    ); //end each for 'event'

                    if (concerts.length > 0) {
                        //close out the final unordered list
                        html += '</div>';
                        html += '<div style="font-weight: bold; margin-top: 20px; margin-left: 30px;">Powered By</div><div style="margin-left: 30px; margin-bottom: 20px;"><a href="http://thefilter.com" title="The Filter - Entertainment Optimized" target="_blank"><img src="/content/images/thefilter_logo2.jpg" alt="The Filter"/></a><a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=332205002&mt=8" title="Follow your favorite artists on BandStalker - Exclusively on the iPhone" target="_blank"><img src="/content/images/bandstalker.jpg" style="margin-left: 10px; height: 70px; width: 70px;" alt="BandStalker available exclusively on the iPhone" /></a></div>';
                        //then add in our new html
                        $('#concertContainer').empty();
                        $('#concertContainer').append(html);
                        html = "";

                        if (firstLoad) {
                            $('#divDataLoading').css('display', 'none')
                            $('#divPlayerContainer').css('visibility', 'visible')
                            setInterval(RefreshAds, 60000);
                            firstLoad = false;
                        }
                        CPL.PlayConcert(currentIndex);
                    } else {

                        if (firstLoad) {
                            $('#divDataLoading').css('display', 'none')
                            $('#divPlayerContainer').css('visibility', 'visible')
                            setInterval(RefreshAds, 60000);
                            firstLoad = false;
                        }

                        html = '<div style="font-size: 20px; font-weight: bold; margin-left: 5px; margin-top: 40px; text-align: center;">Sorry! No shows found for this day.';
                        html += '<br />Try a different date range';
                        html += '<br />or change your location</div>';
                        $('#concertContainer').empty();
                        $('#concertContainer').append(html);
                        CPL.RedrawAds(null);

                        //map.panTo(CPL.Point);
                    }
                }
            );

            return false;
        },

        ParseXML: function(xml) {
            if (window.ActiveXObject && window.GetObject) {
                var dom = new ActiveXObject('Microsoft.XMLDOM');
                dom.loadXML(xml);
                return dom;
            }
            if (window.DOMParser)
                return new DOMParser().parseFromString(xml, 'text/xml');
            throw new Error('No XML parser available');
        },

        GetVenueInfo: function(xml) {
            var name = $(xml).find('Name').text();
            var address = $(xml).find('Address').text();
            var country = $(xml).find('Country').text();
            //var zip = $(xml).find('venue_zip').text();
            var longitude = $(xml).find('Longitude').text();
            var latitude = $(xml).find('Latitude').text();

            return new CPL.Venue(name, address, country, longitude, latitude);
        },

        Venue: function(name, address, country, longitude, latitude) {
            this.name = name;
            this.address = address;
            this.country = country;
            this.point = new GLatLng(latitude, longitude);
        },

        Concert: function(id, date, artistID, artistName, venue, ticketURL) {
            this.id = id;
            this.date = date;
            this.artistID = artistID;
            this.artistName = artistName;
            this.venue = venue;
            this.eventURL = null;
            this.videoID = null;
            this.ticketURL = ticketURL;
        },

        BodyUnload: function() {
            CPL.CloseCurrentConcert();
            GUnload();
        },

        BodyOnLoad: function() {
            if (GBrowserIsCompatible()) {
                map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl());
                map.addControl(new GMapTypeControl());
                map.setCenter(new GLatLng(0, 0), 13);

                currentIndex = 0;

            }
        },

        CloseCurrentConcert: function() {
            try {
                if (concerts.length == 0)
                    return;

                if (currentIndex == -1)
                    return;

                if (currentMarker) {
                    currentMarker.closeInfoWindow();
                    map.removeOverlay(currentMarker);
                    //currentMarker = null;
                }

                var concert = concerts[currentIndex];
                $('#liConcert_' + currentIndex).removeClass("selected");
                $('#liConcert_' + currentIndex).addClass("unselected");


            } catch (e)
            { logEvent('error', 'CloseCurrentConcert'); }
        },

        NextDay: function() {
            var myDate = new Date($("#datepicker").datepicker('getDate'));
            myDate.setDate(myDate.getDate() + 1);
            $("#datepicker").datepicker('setDate', myDate);

            if ($('#txtZip').val() != CPL.CurrentLocation) {
                CPL.ChangeLocation();
            } else {
                CPL.GetConcerts();
            }

            return false;
        },

        PreviousDay: function() {
            var myDate = new Date($("#datepicker").datepicker('getDate'));
            myDate.setDate(myDate.getDate() - 1);
            $("#datepicker").datepicker('setDate', myDate);

            if ($('#txtZip').val() != CPL.CurrentLocation) {
                CPL.ChangeLocation();
            } else {
                CPL.GetConcerts();
            }

            return false;
        },

        PlayNext: function() {
            try {
                if (concerts.length == 0) {
                    /*var myDate = new Date($("#datepicker").datepicker('getDate'));
                    myDate.setDate(myDate.getDate() + 1);
                    $("#datepicker").datepicker('setDate', myDate);
                    CPL.GetConcerts();*/
                    return false;
                }

                if (currentIndex == concerts.length - 1) {
                    //CPL.PlayConcert(0);
                    /*CPL.CloseCurrentConcert();
                    var myDate = new Date($("#datepicker").datepicker('getDate'));
                    myDate.setDate(myDate.getDate() + 1);
                    $("#datepicker").datepicker('setDate', myDate);
                    CPL.GetConcerts();*/
                } else {
                    CPL.PlayConcert(currentIndex + 1);
                }
                return false;
            }
            catch (e) { logEvent('error', 'PlayNext'); }
        },

        PlayPrevious: function() {
            if (concerts.length == 0)
                return false;

            if (currentIndex == 0) {
                //CPL.PlayConcert(concerts.length - 1);
                /*var myDate = $("#datepicker").datepicker('getDate');
                myDate.setDate(myDate.getDate() - 1);
                $("#datepicker").datepicker('setDate', myDate);
                CPL.CloseCurrentConcert();
                CPL.GetConcerts();*/
            } else {
                CPL.PlayConcert(currentIndex - 1);
            }
            return false;
        },

        PlayConcert: function(index) {
            clearTimeout(skip);

            CPL.CloseCurrentConcert();
            currentIndex = index;

            var concert = concerts[index];

            ytplayer = document.getElementById("youtubeplayer");
            /*if (ytplayer) {
            if (ytplayer.playVideo)
            ytplayer.playVideo();
            }*/

            //analytics
            logEvent('ViewVideo', concert.videoID);

            if (index > 0) {
                $('#prev_show').html('<a style="margin-right: 5px;" href="#" onclick="CPL.PlayPrevious();" title="play previous show"><img src="/content/images/back.png" alt="" onmouseover="$(this).toggleClass(\'opaque\');" onmouseout="$(this).toggleClass(\'opaque\');" /></a>');
            } else {
                $('#prev_show').html('<img src="/content/images/back.png" alt="" class="disabled" />');
            }

            if (index < concerts.length - 1) {
                $('#next_show').html('<a href="#" onclick="CPL.PlayNext();" title="play next show"><img src="/content/images/next.png" alt="" onmouseover="$(this).toggleClass(\'opaque\');" onmouseout="$(this).toggleClass(\'opaque\');" /></a>');
            } else {
                $('#next_show').html('<img src="/content/images/next.png" alt="" class="disabled" />');
            }


            if (ytplayer) {
                ytplayer.loadVideoById(concert.videoID, 0, 'small');
            } else {
                CreateVideo(concert.videoID);
            }

            currentMarker = CPL.CreateMarker(concert.venue.point, concert.videoID);
            map.addOverlay(currentMarker);

            //var concertDate = new Date($("#datepicker").datepicker('getDate'));
            var concertDate = CPL.GetConcertDateAsDate(concert);
            var formattedDate = (concertDate.getMonth() + 1) + '-' + concertDate.getDate() + '-' + concertDate.getFullYear();
            var location = encodeURIComponent($('#txtZip').val());

            GEvent.addListener(currentMarker, "infowindowopen", function() {
                addthis.button('.addthis_button', {},
                { url: shareURL + '/shows/' + location + '/' + formattedDate + '/' + concert.id + '/' + concert.artistID, title: '#livemusic : ' + concert.artistName + ' @ ' + concert.venue.name + ', ' + concert.venue.address + ' on ' + CPL.FormatShortDate(CPL.GetConcertDateAsDate(concert)) });
            });

            GEvent.addListener(map, "moveend", function() {
                currentMarker.openInfoWindowHtml(CPL.ConcertHTML(index));
                $('#liConcert_' + index).removeClass("unselected");
                $('#liConcert_' + index).addClass("selected");
                $('#aConcert_' + index).focus();
                //$('#concertContainer').scrollTop($('#liConcert_' + index).offset().top);
                //alert($('#liConcert_' + index).offset().top);
                GEvent.clearListeners(map, "moveend");
            }
            );


            map.panTo(concert.venue.point);
            CPL.RedrawAds(concert);

        },

        ConcertHTML: function(index) {
            var concert = concerts[index];

            var concertDate = CPL.GetConcertDateAsDate(concert);
            var formattedDate = (concertDate.getMonth() + 1) + '-' + concertDate.getDate() + '-' + concertDate.getFullYear();
            var location = encodeURIComponent($('#txtZip').val());
            var title = encodeURIComponent(concert.artistName + ' playing ' + concert.venue.name + ', ' + concert.venue.address + ' on ' + CPL.FormatShortDate(CPL.GetConcertDateAsDate(concert)) + ' #livemusic via @gigpromoter');
            fullShareURL = shareURL + '/shows/' + location + '/' + formattedDate + '/' + concert.id + '/' + concert.artistID + '?title=' + encodeURIComponent(title);


            var html = '<div style="width: 325px;">';

            html += '<div style="float: left; width: 175px;"><div><b>' + CPL.FormatDate(CPL.GetConcertDateAsDate(concert)) + '</b></div><div><b>' + concert.artistName + '</b></div>';
            html += '<div>' + concert.venue.name + '</div>';
            html += '<div>' + concert.venue.address + '</div>';
            html += '<a href="' + concert.ticketURL + '?affil_code=TheFilter" target="_blank" title="Buy Tickets Now!">Tickets and Info</a>';
            html += '</div>';

            html += '<div style="float: left ; margin-right: 10px;" width:200px; >';
            //html += '<img src="/content/images/buyticketsbutton.gif" style="margin-left: 20px; width: 74px; height: 56px;" alt="" /></a>';
            //html += '<div style="margin-top: 10px; margin-bottom: 5px;"><a class="addthis_button"></a></div>';
            html += '<iframe src="/tweet?url=' + fullShareURL + '&amp;title=' + title + '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:80px; margin-top: 10px; float: right" allowTransparency="true"></iframe>';
            html += '</div>';
            html += '<iframe src="http://www.facebook.com/plugins/like.php?href=' + fullShareURL + '&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px; margin-top: 10px;" allowTransparency="true"></iframe>';
            html += '</div>';
            //html += '<div style="clear: both;height: 5px;">&nbsp;</div>';
            return html;
        },

        GetConcertDateAsDate: function(concert) {
            return (new Date(concert.date.substring(0, 4), concert.date.substring(5, 7) - 1, concert.date.substring(8, 10)));
        },

        FormatDate: function(date) {
            return (days[date.getDay()] + ', ' + months[date.getMonth()] + " " + date.getDate() + " " + date.getFullYear());
        },

        FormatShortDate: function(date) {
            return (days[date.getDay()] + ', ' + months[date.getMonth()] + " " + date.getDate());
        },

        CreateMarker: function(point, video) {
            var marker = new GMarker(point);
            GEvent.addListener(marker, "infowindowopen", function() {
                //alert("opened");
                //CPL.CreateVideo(video);
            });

            GEvent.addListener(marker, "infowindowbeforeclose", function() {
                //alert('close window');
                //CPL.ClearVideo();
            });

            return marker;
        },

        ShowChangeLocation: function() {
            $('#lnkChangeLocation').css('display', 'none');
            $('#divChangeLocation').css('display', 'inline');

            return false;
        },

        RedrawAds: function(concert) {
            $('#ad').empty();
            $('#ad').html("<iframe src='http://rya.rockyou.com/ams/ad.php?placeguid=43DD338511&type=MediumRectangle' style='border:0px;' width='300' height='250' scrolling='no' frameborder='0'/></iframe>");


            /*
            var url = '/adsense';
            if (concert) {
            url += '?date=' + encodeURIComponent(concert.date) + '&artistName=' + encodeURIComponent(concert.artistName) + '&ticketURL=' + encodeURIComponent(concert.ticketURL);
            url += '&venueName=' + encodeURIComponent(concert.venue.name) + '&venueCity=' + encodeURIComponent(concert.venue.city);
            }*/

            //$('#adsense').attr('src', url);
        },

        CurrentConcert: function() {
            return concerts[currentIndex];
        },

        CheckDateRange: function() {

            var range = $('#selDateRange').val();
            if (range == 'specificdate') {
                $('#specificDate').removeClass('invisible');
            } else {
                $('#specificDate').addClass('invisible');
            }
        }
    };
} ();

//set the ready event
$(document).ready(
    function() {
        //eraseCookie("firstTimeCheck");

        var firstTimeCheck = readCookie("firstTimeCheck")

        if (getParameterByName('host').length > 0)
            host = getParameterByName('host');

        CPL.BodyOnLoad();

        defaultLocation = readCookie("defaultLocation");
        defaultPoint = readCookie("defaultPoint");

        if (defaultEvent > 0) {
            $('#selDateRange')[0].selectedIndex = 1;

        } else {
            if (readCookie("defaultDateRange")) {
                $('#selDateRange')[0].selectedIndex = readCookie("defaultDateRange");
            }
        }

        CPL.CheckDateRange();

        if (!defaultPoint | isLocalized) {
            $('#txtZip').val(urlDefaultLocation);

            geoCoder = new GClientGeocoder(null);
            geoCoder.reset();
            //geoCoder.setBaseCountryCode("USA");
            geoCoder.getLatLng(urlDefaultLocation, function(point) {

                if (point == null) {
                    $('#txtZip').val("Enter your location");
                    point = "(37.3857, -122.026, 0)";
                }

                if (!firstTimeCheck) {
                    createCookie("defaultPoint", point, 90);
                    createCookie("defaultLocation", urlDefaultLocation, 90);
                    createCookie("firstTimeCheck", 'true', 90);
                    //document.location = '/firsttime';
                }

                CPL.Point = point;

                $("#datepicker").datepicker({ defaultDate: +1,
                    onClose: function() {
                        CPL.CloseCurrentConcert();
                        CPL.GetConcerts();
                    }
                }
                );
                $("#datepicker").datepicker('setDate', new Date(defaultDate));
                CPL.GetConcerts();
            });
        } else {
            CPL.Point = defaultPoint;
            $('#txtZip').val(defaultLocation);

            $("#datepicker").datepicker({ defaultDate: +1,
                onClose: function() {
                    if ($('#txtZip').val() != CPL.CurrentLocation) {
                        CPL.ChangeLocation();
                    } else {
                        CPL.GetConcerts();
                    }
                }
            }
            );

            $("#datepicker").datepicker('setDate', new Date(defaultDate));
            CPL.GetConcerts();

        }

        $("#txtZip").keypress(function(e) {
            if (e.which == 13) {
                CPL.ChangeLocation();
                return false;
            }
        });

    }
);

//set the unload event
$(document).unload(
    function() {
        CPL.BodyUnload();
    }
);

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function currentConcert() {
    return CPL.CurrentConcert();
}

function playNext() {
    CPL.PlayNext();
}


function CreateVideo(videoID) {
    var params = { allowScriptAccess: "always", wmode: "transparent" };
    var atts = { id: "youtubeplayer" };
    var autoplay = "1";

    if (firstTimePlay) {
        autoplay = "0";
        firstTimePlay = false;
    }

    currentVideoID = videoID;
    try {
        //swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3&playerapiid=myytplayer",
        //               "ytvideo", "300", "250", "8", null, null, params, atts);
        
        swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "&rel=0&enablejsapi=1&playerapiid=myytplayer&autoplay=" + autoplay,
                       "ytvideo", "300", "250", "8", null, null, params, atts);
        
    }
    catch (e) { logEvent('error', 'CreateVideo'); }
}

function PauseVideo() {
    ytplayer = document.getElementById(concert.videoID);
    if (ytplayer) {
        if (ytplayer.pauseVideo)
            ytplayer.pauseVideo();
    }
}

function PlayVideo() {
    ytplayer = document.getElementById(concert.videoID);
    if (ytplayer) {
        if (ytplayer.playVideo)
            ytplayer.playVideo();
    }
}

function onYouTubePlayerReady(playerid) {

    try {
        ytplayer = document.getElementById("youtubeplayer");
        ytplayer.addEventListener("onStateChange", "onYTPlayerStateChange");
        ytplayer.addEventListener("onError", "onYTPlayerError");

        /*if (currentVideoID)
            ytplayer.loadVideoById(currentVideoID, 0, 'small');*/

    } catch (e)
    { logEvent('error', 'onYouTubePlayerReady'); }
}

function onYTPlayerStateChange(newState) {
    //alert(newState);

    if (newState == 0) {
        playNext();
    }
}

function onYTPlayerError(error) {
    //playNext();
    logEvent('BrokenVideo', currentVideoID);
    skip = setTimeout(playNext, 5000);
}


function openOverlay(url) {
    $("#overlay").show();
    $("#iframe_wrapper").show();
    frames["iframe_overlay"].location = url;
    return false;
}

function closeOverlay() {
    $("#iframe_wrapper").hide();
    $("#overlay").hide();
    frames["iframe_overlay"].location = "/blank";
    return false;
}

function logEvent(event, data) {

    if (document.createElement) {
        var img = new Image();
        img.src = '/analytics?source=' + host + '&event=' + event + '&data=' + data;
    }
}

function RefreshAds() {
    CPL.RedrawAds(CPL.CurrentConcert());
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}


var addthis_config =
{
    username: 'gigpromoter'
}

function buttonGo_clicked() {

    CPL.CloseCurrentConcert();
    if ($('#txtZip').val() != CPL.CurrentLocation) {
        CPL.ChangeLocation();
    } else {
        CPL.GetConcerts();
    }
}
