﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />
(function()
{
	BSC.UI.Ws.Event = function()
	{
		/// <summary>
		/// @Author		: Rasmus Tomi-Gottschau (rtg@beautifulpeople.com)
		/// @Created	: 
		/// 
		/// @History
		///				2008-11-10	: (RTG) Created.
		/// @Description
		///				Binds ProfileID on tabs
		/// </summary>
		var basebind = function(p1)
		{
			var guid = p1;
			var w = BSC.UI.Ws.Get(guid);
			var jQ = BSC.U.ParseQuery(w.query);
			if (jQ.id)
			{
				$("#" + guid + "_tabs_content_ul li a").map(function()
				{
					var h = this.href;
					// Replace with new id
					if (h.indexOf("/?id=") > -1)
						this.href = h.replace(/\/\?id=(.*)/i, "/?id=" + jQ.id);
					else
						this.href = h + "/?id=" + jQ.id
				});
			}
		};

		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var load = function(p1)
		{
			var guid = p1;
		};

		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var bind = function(p1)
		{
			var guid = p1;
			basebind(guid);
		};

		/// <summary>
		/// Unbind events to ui elements
		/// </summary>
		var unbind = function(p1)
		{
			var guid = p1;
		};

		return {
			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Load: load,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Bind: bind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Basebind: basebind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Event.Load, null, "event");
BSC.E.Subscribe("bind", BSC.UI.Ws.Event.Bind, null, "event");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Event.Unbind, null, "event");


/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />
(function()
{
	BSC.UI.Ws.Event.Attendees = function()
	{
		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var load = function(p1)
		{
			var guid = p1;
			var w = BSC.UI.Ws.Get(guid);
			var data = BSC.U.ParseQuery(w.query);

			// Load Attendees
			$("#" + guid + "_loader").hide();
			data.view = (data.view) ? data.view : (BSC.D.P.Setting("event.main.attendees")) ? BSC.D.P.Setting("event.main.attendees") : "big";
			BSC.D.AjaxGet("/Event/AttendeesList", data, function(res)
			{
				// Html
				$("#" + guid + "_result").html(res);
				$("#" + guid + "_loader").hide();
				$("#" + guid + "_result").show();
				// Event bind
				BSC.E.MapHrefs();
				var json = $("#" + guid + "_pagejson").getValue();
				var pagingData = BSC.D.JSON.Parse(json);
				BSC.UI.ProfilePaging.Bind("/Event/AttendeesList", guid, data, pagingData, "event.main.attendees", { view: "big" });
				BSC.UI.HideStatus(guid, 200);
			}, "html", guid);
		};

		/// <summary>
		/// Bind events to ui elements
		/// </summary>
		var bind = function(p1)
		{
			var guid = p1;

			BSC.UI.Ws.Event.Basebind(guid);
		};

		/// <summary>
		/// Unbind events to ui elements
		/// </summary>
		var unbind = function(p1)
		{
			var guid = p1;
		};

		return {
			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Load: load,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Bind: bind,

			/// <summary></summary>
			/// <param name="data"></param>
			/// <return></return>
			Unbind: unbind
		};
	} ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Event.Attendees.Load, null, "event.attendees");
BSC.E.Subscribe("bind", BSC.UI.Ws.Event.Attendees.Bind, null, "event.attendees");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Event.Attendees.Unbind, null, "event.attendees");



/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />
(function()
{
    BSC.UI.Ws.Event.Details = function()
    {
        /// <summary>
        /// Bind events to ui elements
        /// </summary>
        var load = function(p1)
        {
            var guid = p1;
        };

        /// <summary>
        /// Bind events to ui elements
        /// </summary>
        var bind = function(p1)
        {
            var guid = p1;

            BSC.UI.Ws.Event.Basebind(guid);

            if (BSC.D.P.IsLoggedIn())
            {

                $("#" + guid + "_friends").bind("change", guid, function(e)
                {
                    var numberoffriends = $(this).val();

                    $("#" + guid + "_friendsnames").html("");
                    if (numberoffriends == 0)
                        return;

                    var html = "";
                    for (i = 1; i <= numberoffriends; i++)
                    {
                        html = html + "Friend " + i + " name : <input id=\"" + guid + "_friend_" + i + "\" name=\"friendsnames\" value=\"\" /><br/>";
                    }

                    $("#" + guid + "_friendsnames").html(html);
                });


                $("#" + guid + "_joinevent").bind("click", guid, function(e)
                {
                    var guid = e.data;

                    var friendscount = $("#" + guid + "_friends").val();

                    var names = [];

                    for (i = 1; i <= friendscount; i++)
                    {
                        var name = $("#" + guid + "_friend_" + i).val();
                        if (!name || name.length == 0)
                        {
                            $("#" + guid + "_friend_" + i).trigger("focus");
                            alert("Please enter all your friends names");
                            return false;
                        }
                        names.push(name);
                    }

                    var w = BSC.UI.Ws.Get(guid);
                    var data = BSC.U.ParseQuery(w.query);
                    var eventid = data.id;

                    BSC.D.AjaxPost("/event/AttendEvent", { friendscount: friendscount, eventid: eventid, names:  names }, function(r)
                    {
                        BSC.UI.W.Reload(guid);
                    }, "json", guid);

                    return false;
                });

                $("#" + guid + "_leaveevent").bind("click", guid, function(e)
                {
                    var guid = e.data;
                    var eventid = ($(this).children())[0].id;

                    BSC.D.AjaxPost("/event/UnAttendEvent", { eventid: eventid }, function(r)
                    {
                        BSC.UI.W.Reload(guid);
                    }, "json", guid);

                    return false;
                });

                $("#" + guid + "_remove").bind("click", guid, function(e)
                {
                    var guid = e.data;
                    var w = BSC.UI.Ws.Get(guid);
                    var jQ = BSC.U.ParseQuery(w.query); //, { id: 'string?' }
                    var eventid = jQ.id;

                    BSC.UI.Alert("Yes", "Cancel", "Are you sure ?", "", function(r)
                    {
                        BSC.D.AjaxPost("/event/remove", { eventid: eventid }, function(r)
                        {
                            if (!r.success) return;

                            GO("/event");
                        }, "json", guid);
                    }, null, "info");



                    return false;
                });
            }
        };

        /// <summary>
        /// Unbind events to ui elements
        /// </summary>
        var unbind = function(p1)
        {
            var guid = p1;
        };

        return {
            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Load: load,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Bind: bind,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Unbind: unbind
        };
    } ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Event.Details.Load, null, "event.details");
BSC.E.Subscribe("bind", BSC.UI.Ws.Event.Details.Bind, null, "event.details");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Event.Details.Unbind, null, "event.details");


/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />
(function()
{
    BSC.UI.Ws.Event.Edit = function()
    {
        /// <summary>
        /// Bind events to ui elements
        /// </summary>
        var load = function(p1)
        {
            var guid = p1;
        };

        /// <summary>
        /// Bind events to ui elements
        /// </summary>
        var bind = function(p1)
        {
            var guid = p1;

            BSC.UI.Ws.Event.Basebind(guid);

           

            var settings = {
                button_image_url: BSC.BASE_STATIC_PATH + "beautifulpeoplecdn/images/buttons/blue/flash_btn.png",
                button_placeholder_id: guid + "_selectpicture",
                button_width: 115,
                button_height: 18,
                button_text_top_padding: 0,
                button_text: '<span class="btnupl">' + BSC.T.Get("JS.BSC.UI.Ws.Groups.Createphoto") + '</span>',
                button_text_style: '.btnupl { font-family: Verdana; font-weight:bold; color:#ffffff; font-size: 11px; line-height:18px; text-align:center; }',
                file_types_description: "Select event picture file",
                file_upload_limit: 0,
                file_queue_limit: 0,
                file_types: "*.jpg;*.gif;*.png;*.bmp",
                button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
                upload_success_handler: function(p1, p2)
                {
                    setTimeout(function()
                    {
                        var file = p1;
                        var data = BSC.D.JSON.Parse(p2);
                        var img = $("#" + data.guid + "_eventpicture");
                        img.attr("src", BSC.PicturePath(data.id, "medium"));
                        $("#" + data.guid + "_eventpicture_loader").hide();
                        img.fadeIn("fast", function() { });
                        $("#" + data.guid + "_pictureid").val(data.id);
                    }, 100);
                    //                    try
                    //                    {
                    //                        

                    //                    } catch (ex)
                    //                    {
                    //                        this.debug(ex);
                    //                    }
                    this.setButtonDisabled(false);

                },
                file_queued_handler: function(p1)
                {
                    var file = p1;
                    $("#" + guid + "_eventpicture").hide().attr("src", "");
                    $("#" + guid + "_pictureid").val("");
                    $("#" + guid + "_eventpicture_loader").show();
                },
                upload_start_handler: function(p1, p2)
                {
                    // PostParams
                    var postParams = {
                        //p1: null,
                        p2: (BSC.D.P.GetData()).TicketID,
                        //p3: null,
                        p4: guid,
                        p5: 5
                    };
                    this.setPostParams(postParams);
                    this.setButtonDisabled(true);
                }
            };

            BSC.F.GetUpload(guid, settings);

            $("#" + guid + "_save").bind("click", guid, function(e) { $("#" + e.data + "_eventedit").trigger("submit", e.data); });
            $("#" + guid + "_eventedit").bind("submit", guid, function(e)
            {
                var guid = e.data

                var options = {
                    beforeSubmit: function(formData, jqForm, options)
                    {
                        // formData is an array of objects representing the name and value of each field 
                        // that will be sent to the server;  it takes the following form: 
                        // 
                        // [ 
                        //     { name:  username, value: valueOfUsernameInput }, 
                        //     { name:  password, value: valueOfPasswordInput } 
                        // ] 
                        // 
                        // To validate, we can examine the contents of this array to see if the 
                        // username and password fields have values.  If either value evaluates
                        // to false then we return false from this method.

                        if (!jqForm.context.title.value || jqForm.context.title.value.length == 0)
                        {
                            $(jqForm.context.title).trigger("focus");
                            alert("Please enter a title");
                            return false;
                        }

                        if (!jqForm.context.place.value || jqForm.context.place.value.length == 0)
                        {
                            $(jqForm.context.place).trigger("focus");
                            alert("Please enter a place name");
                            return false;
                        }

                        var y = jqForm.context.year.value;
                        var mo = jqForm.context.month.value;
                        var d = jqForm.context.day.value;
                        var h = jqForm.context.time.value.split(":")[0];
                        var mi = jqForm.context.time.value.split(":")[1];

                        var time = new Date(y, mo, d, h, mi, 0);
                        //var time = d + "-" + mo + "-" + y + " " + jqForm.context.time.value;
                        if (time == "Invalid Date" || time == "0-0-0 0" || y == 0 || mo == 0 || d == 0 || jqForm.context.time.value == 0)
                        {
                            $(jqForm.context.day).trigger("focus");
                            alert("Invalid Date");
                            return false;
                        }
                        if (time < new Date())
                        {
                            $(jqForm.context.day).trigger("focus");
                            alert("Date must be in the feature");
                            return false;
                        }
                        formData.push({ name: "hour", value: h });
                        formData.push({ name: "minute", value: mi });

                        if (!jqForm.context.countrycode.value || jqForm.context.countrycode.value.length == 0)
                        {
                            $(jqForm.context.countrycode).trigger("focus");
                            alert("Please select a country");
                            return false;
                        }

                        if (!jqForm.context.pictureid.value || jqForm.context.pictureid.value.length == 0)
                        {
                            $(jqForm.context.pictureid).trigger("focus");
                            alert("Please upload a picture");
                            return false;
                        }
                    },
                    success: function(r)
                    {
                        GO("/event/details/?id=" + r.id);
                    },
                    dataType: "json"
                    // other available options: 
                    //url:       url         // override for form's 'action' attribute 
                    //type:      type        // 'get' or 'post', override for form's 'method' attribute 
                    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
                    //clearForm: true        // clear all form fields after successful submit 
                    //resetForm: true        // reset the form after successful submit 

                    // $.ajax options can be used here too, for example: 
                    //timeout:   3000 
                };
                $(this).ajaxSubmit(options);


                return false;
            });
        };

        /// <summary>
        /// Unbind events to ui elements
        /// </summary>
        var unbind = function(p1)
        {
            var guid = p1;
            BSC.UI.ProfilePaging.Bind(guid);
        };

        return {
            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Load: load,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Bind: bind,

            /// <summary></summary>
            /// <param name="data"></param>
            /// <return></return>
            Unbind: unbind
        };
    } ();
})();
BSC.E.Subscribe("load", BSC.UI.Ws.Event.Edit.Load, null, "event.edit");
BSC.E.Subscribe("bind", BSC.UI.Ws.Event.Edit.Bind, null, "event.edit");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Event.Edit.Unbind, null, "event.edit");


BSC.E.Subscribe("load", BSC.UI.Ws.Event.Edit.Load, null, "event.create");
BSC.E.Subscribe("bind", BSC.UI.Ws.Event.Edit.Bind, null, "event.create");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Event.Edit.Unbind, null, "event.create");

