﻿

// states
var basketIsVisible = false;
var shareIsVisible = false;



// jquery extension
(function ($) {

    //$.extend({

    $.printandshare = function (options) {



        //Default settings
        var defaults = {

            // Selectors
            basketView: "div.basket",
            basketViewCount: "div.basket a span.none",
            basketViewActiveClass: "active",
            basketDropdown: "div.printbasket",
            basketDropdownContent: "div.printbasket div.printbasketcontent",
            addToBasketLink: "div.add a",
            shareContent: "div.share",
            shareDiv: "div.sharelinks",

            // UI options
            block: true, // requires the jquery plugin blockUI  http://jquery.malsup.com/block/


            // media
            loadingImage: '/css/images/load.gif',
            overlayCSS: { backgroundColor: '#FFF', opacity: 0.8 },

            // services
            printBasketWebserviceUrl: "/usercontrols/services/printbasket.asmx"
        };

        var settings = $.extend(defaults, options);


        var functions = {

            setBasketCount: function () {

                if (settings.block) {
                    $.blockUI.defaults.css = {};

                    if (settings.block) {
                        $(settings.basketView).block({
                            message: "<img src='" + settings.loadingImage + "' />",
                            css: {},
                            overlayCSS: settings.overlayCSS
                        });
                    }
                }

                // start ajax
                $.ajax({
                    type: "POST",
                    url: settings.printBasketWebserviceUrl + '/Count',
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var r = data.d;
                        $(settings.basketViewCount).html(" (" + r + ")");
                        if (settings.block) {
                            $(settings.basketView).unblock();
                        }
                    }
                }); // end ajax
            },

            addToPrintBasket: function (nodeId) {

                if (settings.block) {
                    $(settings.addToBasketLink).block({
                        message: "<img src='/css/images/load.gif' />",
                        css: {},
                        overlayCSS: {
                            backgroundColor: '#FFF',
                            opacity: 0.8
                        }
                    });
                }

                $.ajax(
                {
                    type: "POST",
                    url: settings.printBasketWebserviceUrl + '/Add',
                    data: "{nodeId:" + nodeId + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var r = data.d;
                        if (r) {
                            functions.setBasketCount(settings.basketViewCount, true);
                            if (basketIsVisible)
                                functions.updateList();
                        }
                        if (settings.block) {
                            $(settings.addToBasketLink).unblock();
                        }
                    }
                });
            },


            removeFromPrintBasket: function (nodeId) {
                $.ajax({
                    type: "POST",
                    url: settings.printBasketWebserviceUrl + '/Remove',
                    data: "{nodeId:" + nodeId + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var r = data.d;
                        //alert(r);
                        if (r) {
                            functions.setBasketCount(settings.basketContent, true);
                            functions.updateList();
                        }
                        if (settings.block) {
                            $(settings.addToBasketLink).unblock();
                        }
                    }
                });
            },


            togglePrintBasketView: function () {
               // alert(basketIsVisible);
                if (!basketIsVisible) {
                    // show the basket
                    $(settings.basketView).addClass(settings.basketViewActiveClass);

                    // call the ajax method to get the contents and render
                    functions.updateList();

                    $(settings.basketDropdown).show();

                    shareIsVisible = false;
                    $(settings.shareContent).removeClass(settings.basketViewActiveClass);
                    $(settings.shareDiv).hide();
                }
                else
                {
                    // hide the basket
                    $(settings.basketView).removeClass(settings.basketViewActiveClass);
                    $(settings.basketDropdown).hide();
                }
                basketIsVisible = !basketIsVisible;
               // alert(basketIsVisible);
            },



            updateList: function () {
                $.ajax({
                    type: "POST",
                    url: settings.printBasketWebserviceUrl + '/BasketContent',
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var r = data.d;
                        if (r) {
                            // alert(r.length);
                            if (r.length === 0) {
                                $(settings.basketDropdownContent).html("<p>Your basket is currently empty</p>");
                            }
                            else {
                                var $content = $(settings.basketDropdownContent);
                                $content.html("<p>You have " + r.length + " page" + (r.length > 1 ? "s" : "") + " in <br/>your basket");
                                var $ul = $("<ul />");
                                for (x = 0; x < r.length; x++) {
                                    var c = r[x];
                                    var $li = $("<li />");
                                    //var $hidden = $("<input type='hidden' />").html(c.id);
                                    var $a = $("<a />").attr("href", c.url).html(c.name);
                                    var $delete = $("<a />").attr("href", "javascript:;").html("Delete");
                                    $delete.bind("click", { id: c.id, selector: $delete }, function (event) {
                                        functions.removeMe(event.data.id, event.data.selector);
                                    });

                                    var $span = $("<span />").addClass("delete").append($delete);

                                    $li.append($a);
                                    $li.append($span);

                                    $ul.append($li);
                                }

                                $content.append($ul);

                            } // end if(r)
                        } // end success()
                    }
                });
            },



            removeMe: function (id, selector) {
                var $li = $(selector).parent("span").parent("li");

                if (settings.block) {
                    $.blockUI.defaults.css = {};
                    $li.block({
                        message: "<img src='" + settings.loadingImage + "' />",
                        css: {},
                        overlayCSS: settings.overlayCSS
                    });
                }

                functions.removeFromPrintBasket(id);
            },


            sendToPrinter: function (nodeId) {
                $.ajax({
                    type: "POST",
                    url: settings.printBasketWebserviceUrl + '/PrinterUrl',
                    data: "{nodeId:" + nodeId + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data) {
                            var r = data.d;
                            //alert(r);
                            if (r) {
                                window.location = r;
                            }
                        }
                    }
                });
            },


            // leave this for now...
            toggleShareView: function () {
                //alert(basketIsVisible);
                if (!shareIsVisible) {
                    // show the basket
                    $(settings.shareContent).addClass("active");
                    $(settings.shareDiv + " .sharelinksleft").empty().share({
                        services: ['twitter', 'facebook', 'digg', 'linkedin']
                    });
                    $(settings.shareDiv + " .sharelinksright").empty().share({
                        services: ['google', 'yahoo', 'stumbleupon', 'delicious']
                    });
                    // should just be show() but needs hack for ie7
                    //$(shareDiv).css("backgroundColor", "#D2D2D3").css("position", "relative").show().css("backgroundColor", "#ffffff");
                    $(settings.shareDiv).show();
                    //alert("delay");

                    // hide the basket
                    basketIsVisible = false;
                    $(settings.basketView).removeClass("active");
                    $(settings.basketDropdown).hide();
                }
                else {
                    $(settings.shareContent).removeClass("active");
                    $(settings.shareDiv).hide();
                }
                shareIsVisible = !shareIsVisible;
            }
        };



        return functions;

    }
    // });



})(jQuery);




(function ($) {
    $.fn.share = function (options) {
        var defaults = {
            url: window.location, 							// url of social bookmark, default is current url, use 'http://www.example.com' for individual url	
            title: $.trim($('title').text()), 							// title of social bookmark, default is window title, use 'example' for individual title	
            description: $.trim($('meta[name=description]').attr("content")), // description of social bookmark, default is <meta>-description, use 'example' for individual description							
            tags: $.trim($('meta[name=keywords]').attr("content")), 	// tags of social bookmark, default is <meta>-keywords, use 'example' for individual tags
            services: ['all'], 										// 'all' for [all] services, or ['facebook', 'delicious', ..] for specific services	
            img_size: 16, 											// width and height of <img>-tag (px)
            img_alt: '$service', 									// alt-text of <img>-tag, $service will be replaced by the name of the service
            a_target: '_blank', 									// target-attribute of <a>-tag
            a_title: 'share on $service'								// title-attribute of <a>-tag, $service will be replaced by the name of the service							
        };
        var options = $.extend(defaults, options);
        return this.each(function () {
            var target = $(this);
            var services = {
                facebook: ['http://www.facebook.com/sharer.php?u=' + escape(options.url) + '&t=' + options.title, 'http://static.ak.fbcdn.net/rsrc.php/z7/r/5875srnzL-I.ico', 'Facebook'],
                twitter: ['http://twitter.com/home?status=' + options.title + ':+' + options.url, 'http://twitter.com/phoenix/favicon.ico', 'Twitter'],
                delicious: ['http://del.icio.us/post?url=' + options.url + '&title=' + options.title + '&tags=' + options.tags + '&notes=' + options.description, 'http://www.delicious.com/favicon.ico', 'Delicious'],
                digg: ['http://digg.com/submit?phase=2&url=' + options.url + '&title=' + options.title, 'http://cdn1.diggstatic.com/img/favicon.a015f25c.ico', 'Digg'],
                google: ['http://www.google.com/bookmarks/mark?op=add&bkmk=' + options.url + '&title=' + options.title + '&labels=' + options.tags + '&annotation=' + options.description, 'http://www.google.com/favicon.ico', 'Google'],
                yahoo: ['http://bookmarks.yahoo.com/toolbar/savebm?u=' + options.url + '&t=' + options.title + "&d=" + options.description, 'http://l.yimg.com/i/i/eu/aut/favic1.ico', 'Yahoo'],
                misterwong: ['http://www.mister-wong.com/index.php?action=addurl&bm_url=' + options.url + '&bm_description=' + options.title + '&bm_tags=' + options.tags + '&bm_notice=' + options.description, 'http://www.mister-wong.de/favicon.ico', 'Mister Wong'],
                netvibes: ['http://www.netvibes.com/share?url=' + options.url + '&title=' + options.title, 'http://cdn.netvibes.com/favicon.ico', 'Net Vibes'],
                linkedin: ['http://www.linkedin.com/shareArticle?mini=true&url=' + options.url + '&title=' + options.title + '&source=&summary=' + options.description, 'http://www.linkedin.com/img/favicon_v2.ico', 'Linked In'],
                stumbleupon: ['http://www.stumbleupon.com/submit?url=' + options.url + '&title=' + options.title, 'http://cdn.stumble-upon.com/favicon.ico', 'StumbleUpon']
            }


            if (options.services == "all") {
                options.services = new Array();
                for (n in services) options.services.push(n);
            }
            $.each(options.services, function (index, service) {
                if (services[service] != undefined) {
                    var content = '<div class="service"><a href="' + services[service][0] + '"';
                    if (options.target != "") content += ' target="' + options.a_target + '"';
                    if (options.title != "") content += ' title="' + (options.a_title).replace(/\$service/g, this) + '"';
                    content += '><img width="' + options.img_size + '" height="' + options.img_size + '" border="0" src="' + services[service][1] + '"';
                    if (options.alt != "") content += ' alt="' + (options.img_alt).replace(/\$service/g, this) + '"';
                    content += ' /><span class="service_text">' + services[service][2] + '</span></a></div>';
                    target.append(content);
                }
            });
        });
    };
})(jQuery);









