﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />
(function()
{
    BSC.UI.Ws.Rating = function()
    {
        // Properties
        var localdata = {};
        var current = null;

        // Private
        var ShowApplicant = function(p1, p2, p3)
        {
            var guid = p1;
            var id = p2;
            var updateUri = (p3 == null) ? true : p3;
            var pdata = localdata[guid].applicants[id];

            if (!pdata) { return; }

            // Render Applicant details
            $("#" + guid + "_profilename").html(pdata.n);
            $("#" + guid + "_title").html("Rating: " + pdata.n);
            $("#" + guid + "_appname").html(pdata.n);
            $("#" + guid + "_picture").attr("src", BSC.PicturePath(pdata.pic, "profile")).unbind().bind("click", pdata.pic, function(e)
            {
                var pictureID = e.data;
                GO("/picture/?id=" + pictureID);
            });
            // country
            $("#" + guid + " .flag div").removeClass().addClass(pdata.cc);
            localdata[guid].AID = id;

            // Set Window title
            BSC.P.SetTitle(guid, "Rating : " + name);

            var w = BSC.UI.Ws.Get(guid);

            BindProfileLinks(guid);

            // Build query
            if (updateUri)
            {
                BSC.UI.Ws.Get(guid).uri = "/rating/" + id;
                BSC.P.ChangeURI(BSC.UI.Ws.Get(guid).uri);
            }
        }

        var BindProfileLinks = function(p1)
        {
            var guid = p1;
            // Report Applicant link event bind
            $("#" + guid + "_report").unbind().bind("click", guid, function(e)
            {
                var data =
				{
				    reportType: "Profile",
				    reasons: [8],
				    additionalComments: ""
				};

                BSC.UI.Alert("Ok", "Cancel", "Is this an unserious or fake profile?", "Click OK to report the profile.", function()
                {
                    BSC.D.AjaxPost("/Feedback/ReceiveReport/" + localdata[e.data].AID, data, function(res)
                    {
                        GiveRank(guid, 1, true);
                        //				        // Html
                        //				        if (res.success)
                        //				        {
                        //				            BSC.UI.W.Close(guid);
                        //				            BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.UserReport.OK"), null, BSC.T.Get("JS.BSC.UI.Ws.UserReport.Report.Success.Title"), BSC.T.Get("JS.BSC.UI.Ws.UserReport.Report.Success.Desc"), null, guid, "info");
                        //				        }
                        //				        else
                        //				        {
                        //				            BSC.UI.HideStatus(guid, 200);
                        //				            BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.UserReport.OK"), null, BSC.T.Get("JS.BSC.UI.Ws.UserReport.Error.Title"), BSC.T.Get("JS.BSC.UI.Ws.UserReport.Error.Desc"), null, guid, "info");
                        //				        }
                        //				        if (data.reportType == "Applicant")
                        //				        { GO("/Rating"); return; }
                    }, "json", guid);
                }, null, "info");


                //                if (localdata[e.data].AID)
                //                    GO("/feedback/reportapplicant/" + localdata[e.data].AID);
            });

            // Report Applicant link event bind
            $("#" + guid + "_seeprofile").unbind().bind("click", guid, function(e)
            {
                if (localdata[e.data].AID)
                    GO("/" + localdata[e.data].AID);
            });

        };

        var GiveRank = function(p1, p2, p3)
        {
            var guid = p1;
            var score = parseInt(p2);
            var report = p3;

            var applicantID = localdata[guid].AID;
            var applicant = localdata[guid].applicants[applicantID];

            // Profile info
            $("#" + guid + "_ratedname").html("" + applicant.n);
            $("#" + guid + "_age span.value").html("" + applicant.age);
            $("#" + guid + "_country span.value").html("" + applicant.cn);
            $("#" + guid + "_ratedpicture").show().attr("src", BSC.PicturePath(applicant.pic, "big"));
            $("#" + guid + "_ratedprofile").unbind().attr("href", "/" + applicantID).bind("click", { guid: guid, id: applicantID }, function(e) { GO("/" + e.data.id); return false; });

            // Show Loader
            if (report)
            {
                $("#" + guid + "_reported").show();
                $("#" + guid + "_ratingscore").hide();
            }
            else
            {
                $("#" + guid + "_reported").hide();
                $("#" + guid + "_ratingscore").show();
            }

            $("#" + guid + "_ratedstatus").removeClass();
            $("#" + guid + "_loading").show();
            
            $("#" + guid + "_prev").show();
            $("#" + guid + "_result").css({ width: 0 })

            // Load next applicant
            LoadNextApplicant(guid, applicantID);

            $("#" + guid + "_slider1").css({ width: 0 });
            $("#" + guid + "_slider2").css({ width: 0 });
            $("#" + guid + "_slider3").css({ width: 0 });
            $("#" + guid + "_slider4").css({ width: 0 });

            // Sent score
            $.post("/rating/givescore", { id: applicantID, value: score, cc : applicant.cc }, function(r)
            {
                $("#" + guid + "_loading").hide();
                if (!report)
                {
//                    var scoreWidth = r.score * 22;
//                    scoreWidth = Math.min(scoreWidth, (5 * 22));

//                    $("#" + guid + "_result").css({ width: 0 }).animate({ width: scoreWidth, duration: 200 });

//                    var l = localdata[guid].rated.length;
//                    for (i = 0; i < l; i++)
//                    {
//                        if (localdata[guid].rated[i].id == r.id)
//                        {
//                            localdata[guid].rated[i].r = r.score;
//                            break;
//                        }
                    //                    }

                    var max = 159;
                    $("#" + guid + "_slider1").css({ width: 0 }).animate({ width: Math.min((r.v1 * 1.59), max) });
                    $("#" + guid + "_slider2").css({ width: 0 }).animate({ width: Math.min((r.v2 * 1.59), max) });
                    $("#" + guid + "_slider3").css({ width: 0 }).animate({ width: Math.min((r.v3 * 1.59), max) });
                    $("#" + guid + "_slider4").css({ width: 0 }).animate({ width: Math.min((r.v4 * 1.59), max) });
                    ////

                    
                    var inout = (r.gettingin) ? "positive" : "negative";
                    $("#" + guid + "_ratedstatus").removeClass().addClass("status " + inout);
                }
            }, "json");
        };


        var LoadNextApplicant = function(p1, p2)
        {
            var guid = p1;
            var applicantID = p2;

            // Add Current to rated applicants
            localdata[guid].rated.push(localdata[guid].applicants[applicantID]);
            localdata[guid].ratedIndex = localdata[guid].rated.length - 1;
            var currentApplicant = localdata[guid].applicants[applicantID];
            delete localdata[guid].applicants[applicantID];

            // Get Next applicant from list
            var nextApplicant = null;
            for (var key in localdata[guid].applicants)
            {
                nextApplicant = localdata[guid].applicants[key];
                break;
            }


            // Count Statistic
            localdata[guid].AC = localdata[guid].AC - 1;
            var procentLeft = localdata[guid].AC * 100 / localdata[guid].TAC;
            $("#" + guid + "_applicantsleft").html(localdata[guid].AC);

            // No more applicants
            if (!nextApplicant)
            {
                if (localdata[guid].AC > 0)
                {
                    GO("/rating");
                    return;
                }
                $.get("/rating/nomoreprofiles", null, function(res) { $("#" + guid + "_content").html(res); BSC.E.MapHrefs(); }, "html");
                return;
            }

            ShowApplicant(guid, nextApplicant.id, true);

            // Reload banner
            if (document.getElementById(guid + "_banner"))
                document.getElementById(guid + "_banner").src = document.getElementById(guid + "_banner").src;
        };

        var ShowApplicantPreview = function(p1, p2, p3)
        {
            var guid = p1;
            var applicant = p2;
            var duration = (p3) ? p3 : 800;

            if (!applicant) return;

            // Rating score
            $("#" + guid + "_prev").show();
            $("#" + guid + "_ratedname").html("" + applicant.n);
            $("#" + guid + "_age span.value").html("" + applicant.age);
            $("#" + guid + "_country span.value").html("" + applicant.cn);

            $("#" + guid + "_ratedpicture").show().attr("src", BSC.PicturePath(applicant.pic, "big"));

            var scoreWidth = applicant.r * 22;
            scoreWidth = Math.min(scoreWidth, (5 * 22));
            $("#" + guid + "_result").css({ width: 0 }).animate({ width: scoreWidth, duration: 200 });

            $("#" + guid + "_ratedpicture").show().attr("src", BSC.PicturePath(applicant.pic, "big"));
            $("#" + guid + "_ratedprofile").unbind().attr("href", "/" + applicant.id).bind("click", { guid: guid, id: applicant.id }, function(e)
            {
                GO("/" + e.data.id);
                return false;
            });

        };


        ///***************************************************
        ///***************************************************
        ///***************************************************
        ///***************************************************        



        // Public Implementation
        var loaddata = function(p1)
        {
            var guid = p1;
            current = guid;

            var w = BSC.UI.Ws.Get(guid);
            var json = $("#" + guid + "_json").getValue();

            //if (!json) return;
            localdata[guid] = { AC: 0 };
            if (json) localdata[guid] = BSC.D.JSON.Parse(json);
            localdata[guid].TAC = localdata[guid].AC;
            localdata[guid].rated = [];
            localdata[guid].ratedIndex = 0;



            // Get Current Applicant ID && remove from GUI
            if (w.data.key)
            {
                localdata[guid].AID = w.data.key;
            }
            else
            {
                var applicant = null;
                for (var key in localdata[guid].applicants)
                {
                    applicant = localdata[guid].applicants[key];
                    break;
                }
                if (applicant)
                {
                    ShowApplicant(guid, applicant.id);
                }
            }
        };


        var bind = function(p1)
        {
            var guid = p1;

            $("#" + guid + "_content").css("overflow", "hidden");

            // Hide the bookmark icon in the topmenu
            $("#" + guid + "_bookmark").hide();


            BindProfileLinks(guid);

            $("#" + guid + "_browseRated").bind("click", guid, function(e)
            {
                var guid = e.data;
                var el = BSC.E.GetTarget(e, "div", "a");
                if ($(el).hasClass("right"))
                {
                    localdata[guid].ratedIndex++;
                }
                else if ($(el).hasClass("left"))
                {
                    localdata[guid].ratedIndex--;
                }

                if (localdata[guid].ratedIndex > localdata[guid].rated.length - 1)
                    localdata[guid].ratedIndex = 0;
                else if (localdata[guid].ratedIndex < 0)
                    localdata[guid].ratedIndex = localdata[guid].rated.length - 1;

                var applicant = localdata[guid].rated[localdata[guid].ratedIndex];

                ShowApplicantPreview(guid, applicant, 200);
            });

//            $("#" + guid + "_ranking a").unbind().bind("mouseover", guid, function(e)
//            {
//                var guid = e.data;
//                $("#" + guid + "_rankingtext").html($(this).attr("title"));
//            }).bind("click", guid, function(e)
//            {
//                var guid = e.data;
//                var value = $(this).text();
//                var applicantID = localdata[guid].AID;  //jQ.id;
//                GiveRank(guid, value);
//            });

            $("#" + guid + "_buttons button").unbind().bind("click", guid, function(e)
            {
                var guid = e.data;
                var value = $(this).text();
                var applicantID = localdata[guid].AID;  //jQ.id;
                GiveRank(guid, value);
            });

//            $("#" + guid + "_ranking").unbind().bind("mouseout", guid, function(e)
//            {
//                var guid = e.data;
//                $("#" + guid + "_rankingtext").html("");
//            });

        };



        var unbind = function(p1)
        {
            var guid = p1;
            delete localdata[guid];
        };



        var updateView = function(p1, p2)
        {
            var applicantID = p1;
            var guid = current;
            var value = p2;

            if (!guid) return;

            //            var activeApplicantID = localdata[guid].AID

            //            if (activeApplicantID == applicantID)
            //            {
            //                updateApplicantDetails(guid, applicantID, value, false);
            //            }
            //            else
            //            {
            //                // Count Statistic
            //                localdata[guid].AC = localdata[guid].AC - 1;
            //                var procentLeft = localdata[guid].AC * 100 / localdata[guid].TAC;
            //                $("#" + guid + "_progress").animate({ width: procentLeft + "%" }); //.css("width", procentLeft + "%");
            //                $("#" + guid + "_applicantsleft").html(localdata[guid].AC);
            //                localdata[guid].rated.push(localdata[guid].applicants[applicantID]);
            //                var currentApplicant = localdata[guid].applicants[applicantID];
            //                delete localdata[guid].applicants[applicantID];
            //                localdata[guid].resulttimer = setTimeout(function()
            //                {
            //                    currentApplicant.values = currentApplicant["i" + value];
            //                    ShowApplicantPreview(guid, currentApplicant);
            //                }, 200);
            //            }
        };


        // Public Interface
        return {
            Loaddata: loaddata,
            Bind: bind,
            Unbind: unbind,
            UpdateView: updateView
        };
    } ();
})();
// Subscribe to events
BSC.E.Subscribe("load", BSC.UI.Ws.Rating.Loaddata, null, "rating");
BSC.E.Subscribe("bind", BSC.UI.Ws.Rating.Bind, null, "rating");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Rating.Unbind, null, "rating");


