var Widgets = Widgets ? Widgets : function() {

    var private = {

        host: null,
        container: null,
        callback: null,
        propertyFriendly: null,
        layout: null,
        width: null,
        colourTheme: null,
        anchorDetails: null,
        optionalReviewID: null,

        jsonpcall: function(fn, paramArray, callbackFn) {
            // Create list of parameters in the form (http get format):
            // paramName1 = paramValue1 & paramName2 = paramValue2 &
            var paramList = '';
            if (paramArray.length > 0) {
                for (var i = 0; i < paramArray.length; i += 2) {
                    paramList += paramArray[i] + '=' + paramArray[i + 1] + '&';
                }
            }
            $.getJSON('http://' + private.host + '/Widgets/' + fn + '?' + paramList + 'method=?', callbackFn);
        },

        loadhtml: function(container, urlraw, propertyFriendly, layout, width, colourTheme, anchorDetails, callback) {
            var urlselector = (urlraw).split(" ", 1);
            var url = urlselector[0];
            var selector = urlraw.substring(urlraw.indexOf(' ') + 1, urlraw.length);
            private.container = container;
            private.callback = callback;
            private.jsonpcall('Service.ashx', ['downloadurl', escape(url), 'propertyFriendly', propertyFriendly, 'layout', layout, 'width', width, 'colourTheme', colourTheme, 'anchorDetails', anchorDetails],
                function(msg) {
                    // gets the contents of the Html in the 'msg'
                    // todo: apply selector
                    private.container.html(msg);
                    if ($.isFunction(private.callback)) {
                        private.callback();
                    }
                });
        },

        // wire widget after it's loaded
        Reviews_Init: function(i) {
            /* load fancybox 
            $("a#inline").fancybox({
            'hideOnContentClick': true
            });*/
            // this function is OnClick event for the review paging link
            $('a.ChangePage').click(function() {
                var widget = $('div#reviews');
                widget.find('img#loadingjpg').hide();
                widget.find('img#loading').show();
                widget.find("span#error").html("");
                var PageID, ReviewCount;
                if (private.layout == 'review_Nav') {
                    PageID = widget.find('input#hdNavPageID')[0].value;
                    ReviewCount = widget.find("input#hdNavReviewCount").val();
                }
                else {
                    PageID = widget.find('input#hdBodyPageID')[0].value;
                    ReviewCount = widget.find("input#hdBodyReviewCount").val();
                }
                private.jsonpcall('Reviews/Service.svc/BuildPage', ['PageID', PageID, 'propertyFriendly', private.propertyFriendly, 'layout', private.layout, 'width', private.width, 'colourTheme', private.colourTheme, 'reviewCount', ReviewCount],
                    function(result) {
                        if (result.Error == null) {
                            widget.find("div.guestReviewsBox").html(result.Value);
                        } else {
                            widget.find("span#result").html("Error");
                            widget.find("span#error").html("Error: " + result.Error);
                        }
                        setTimeout("$('img#loading').hide(); $('img#loadingjpg').show();", 500);
                    });
                return false;
            });

            // initializing the widget.
            if ((private.optionalReviewID > 0) && (i == undefined)) {
                // call ChangeViewerPage
                Widgets.LoadReview(0, 0, 0, 0);
            }
        }
    };

    var public = {

        // load widget into 'container' from 'host'
        Reviews: function(container, host, propertyFriendly, layout, width, colourTheme, optionalReviewID) {
            if ($("#reviewsLink").length > 0) {
                private.host = host;
                private.propertyFriendly = propertyFriendly;
                private.layout = layout;
                private.width = width;
                private.colourTheme = colourTheme;
                private.anchorDetails = $("#reviewsLink").text() + '|' + $("#reviewsLink").attr("href");
                // check for reviewID
                if (optionalReviewID == undefined) {
                    private.optionalReviewID = 0;
                }
                else {
                    private.optionalReviewID = optionalReviewID;
                }
                private.loadhtml(container, 'http://' + private.host + '/Widgets/Reviews/Default.aspx', private.propertyFriendly, private.layout, private.width, private.colourTheme, private.anchorDetails, private.Reviews_Init);
            }
            else {
                $("div#reviews").html("Reviews Widget Error: Link missing, please replace.");
            }
        },

        // load review viewer into widget
        LoadReview: function(reviewIndex, pageindex, pageSize, reviewCount) {
            var widget = $('div#reviews');
            widget.find("div#reviewWindowTemp").html(widget.find("div#reviewWindow").html());
            Widgets.ChangeViewerPage($('div#reviews'), reviewIndex, pageindex, pageSize, reviewCount);
        },
        // load review viewer into nav widget
        LoadReview_Nav: function(reviewIndex, pageindex, pageSize, reviewCount) {
            Widgets.ChangeViewerPage($('div#ViewerBody'), reviewIndex, pageindex, pageSize, reviewCount);
        },

        // close viewer and re-load
        CloseReview: function() {
            var widget = $('div#reviews');
            widget.find("div#reviewWindow").html(widget.find("div#reviewWindowTemp").html());
            private.Reviews_Init(1);
        },

        // OnClick event for the viewer paging link
        ChangeViewerPage: function(widget, reviewIndex, pageindex, pageSize, reviewCount) {
            widget.find('img#loadingViewerjpg').hide();
            widget.find('img#loadingViewer').show();
            widget.find("span#error").html("");

            private.jsonpcall('Reviews/Service.svc/BuildViewerPage', ['ReviewIndex', reviewIndex, 'propertyFriendly', private.propertyFriendly, 'pageindex', pageindex, 'pageSize', pageSize, 'reviewCount', reviewCount, 'layout', private.layout, 'theme', private.colourTheme, 'optionalReviewID', private.optionalReviewID],
            function(result) {
                if (result.Error == null) {
                    widget.find("div#reviewWindow").html(result.Value);
                } else {
                    widget.find("span#result").html("Error");
                    widget.find("span#error").html("Error: " + result.Error);
                }
                setTimeout("$('img#loadingViewer').hide(); $('img#loadingViewerjpg').show();", 500);
                if (private.layout == 'review_Nav') {
                    $("#hidden_link").fancybox({ 'hideOnContentClick': false }).trigger('click');
                }
            });
            private.optionalReviewID = 0;
        }

    }

    return public;
} ();

