﻿
; (function ($) {
    
    $.eventBtn = function(container, options) {
        var b = new eventButton();
        return b.init(container, options);
    };
    $.fn.eventBtn = function (options) {
    var b = new eventButton();
        return b.init(this, options);

    };

    function eventButton () {
        this.eventId = null;
        this.attendButton = null;
        this.notAttendButton = null;
        this.speakButton = null;
        this.waitImg = null;
        this.speakerLabel = null;

        this.init = function(container, options){
                var s = this;
                this.eventId = options.eventId;
                
                if (options.speakerLabel)
                    this.speakerLabel = options.speakerLabel;
            
                s.attendButton = $("<img id='" + options.eventId + "_att' src='/images/Layout/Attending.gif' />").appendTo(container);            
                s.notAttendButton = $("<img id='" + options.eventId + "_natt' src='/images/Layout/Not_Attending.gif' />").appendTo(container);
                    
                if (options.speakingAvailable) {
                    $("<br />").appendTo(container);
                    s.speakButton = $("<img id='" + options.eventId + "_att' src='/images/Layout/EventSpeak.gif' />").appendTo(container);            
                    s.speakButton.click(function() {
                        if (!confirm("Are you sure you want to sign up to speak?  If you change your mind, you must contact One Business Connection to be removed from the schedule.")) {
                              return false;
                        } 
                         
                        s.setSpeaking(s, options.speakButtonValue); 
                        return true;
                    });
                }
                else if (options.showDisabledSpeakButton) {
                    $("<br />").appendTo(container);
                    s.speakButton = $("<img id='" + options.eventId + "_att' src='/images/Layout/EventSpeakDisabled.gif' />").appendTo(container);            
                }
                    
            
                s.waitImg = $("<img src='/images/Layout/wait28trans.gif' style='position: absolute; z-index: 99;' />").hide().appendTo(container);
                s.attendButton.click(function() { s.setAttending(s, true); });
                s.notAttendButton.click(function() { s.setAttending(s, false); });

                if (options.status) {
                    s.attendButton.attr("src", "/images/Layout/Attending_Selected.gif");
                }
                else {
                    s.notAttendButton.attr("src", "/images/Layout/Not_Attending_Selected.gif");
                }
            };

          this.setAttending = function (s, isAttending) {
                s.waitImg.show();
                gcal.setAttending(s.eventId, isAttending, function(data) { s.onSaved(s, data); });
            };
        
          this.setSpeaking = function (s, isSpeaking) {
                s.waitImg.show();
                gcal.setSpeaking(s.eventId, isSpeaking, function(data) { s.onSaved(s, data); });
            };

          this.onSaved = function (s, data) {
                s.waitImg.hide();
                if (data[0].SaveSuccess) {
                    if (data[0].IsAttending){    
                        s.attendButton.attr("src", "/images/Layout/Attending_Selected.gif");
                        s.notAttendButton.attr("src", "/images/Layout/Not_Attending.gif");
                    }
                    else {
                        s.attendButton.attr("src", "/images/Layout/Attending.gif");
                        s.notAttendButton.attr("src", "/images/Layout/Not_Attending_Selected.gif");
                    }
                    
                    if (data[0].IsSpeaking) {
                        if (s.speakButton) {
                            s.speakButton.attr("src", "/images/Layout/EventSpeakDisabled.gif");
                            s.speakButton.unbind("click");
                        }
                        
                        if (s.speakerLabel && data[0].SpeakerName)
                            s.speakerLabel.html(data[0].SpeakerName);
                        
                    }
                }
                else{
                    alert("Error saving response.  Please try again.  \n" + data[0].ErrorResponse);
                }
            };
        };

})(jQuery);

var gcal = {
    PostResponse: null,
    setAttending: function (eventId, isAttending, callback) {
        $.post("/usercontrols/OneBC.Umbraco/EventRegistrationHandler.ashx", eventId + "=" + isAttending.toString(), callback);
    },
    
    setSpeaking: function (eventId, isSpeaking, callback) {
        $.post("/usercontrols/OneBC.Umbraco/EventRegistrationHandler.ashx", "s_"+eventId + "=" + isSpeaking.toString(), callback);
    }
};


var gmap = {

    geocoder: null,
    map: null,
    marker: null,

    returnMapUrl: function (GLatLngObj) {
        var point = GLatLngObj || map.getCenter();
        return "http://maps.google.com/maps?q=" + point.lat() + ',' + point.lng();
    },

    SetMap: function (mapCanvas, address, latLong) {
        if (address == "" && latLong == "") { return; }

        $(mapCanvas).html("");
        //MAP
        var options = {
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUi: true,
            draggable: false,
            mapTypeControl: false,
            overviewMapControl: false
        };

        map = new google.maps.Map(document.getElementById(mapCanvas), options);

        google.maps.event.addListener(map, "click", function (overlay, latlng) {
            var point = map.getCenter();

            window.open(returnMapUrl(latlng));
        });

        if (latLong != null && latLong != "") {
            var latlngStr = latLong.split(",", 2);

            var lat = parseFloat(latlngStr[0]);
            var lng = parseFloat(latlngStr[1]);

            var point = new google.maps.LatLng(lat, lng);

            map.setCenter(point);
            var marker = new google.maps.Marker({
                map: map,
                position: point
            });
            return;
        }

        //GEOCODER
        geocoder = new google.maps.Geocoder();

        marker = new google.maps.Marker({
            map: map,
            draggable: true
        });

        geocoder.geocode({ 'address': address }
                , function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);

                        var marker = new google.maps.Marker({
                            map: map,
                            position: results[0].geometry.location
                        });
                    }
                    else {
                    }
                });




    }
};


