﻿
var basketContent = "div.basket";
var basketIsVisible = false;
var basketDiv = "div.printbasket";


var shareContent = "div.share";
var shareIsVisible = false;
var shareDiv = "div.sharelinks";



setBasketCount = function (selector, block) {

    if (block) {
        $.blockUI.defaults.css = {};
        $(selector).block({
            message: "<img src='/css/images/load.gif' />",
            css: {},
            overlayCSS: {
                backgroundColor: '#FFF',
                opacity: 0.8
            }
        });
    }

    $.ajax(
        {
            type: "POST",
            url: '/usercontrols/WRDev.Apax.Umbraco/printbasket.asmx/Count',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var r = data.d;
                $(selector + " a span.none").html(" (" + r + ")");
                if (block) {
                    $(selector).unblock();
                }
            }
        });
};

addToPrintBasket = function (nodeId) {
        $.ajax(
        {
            type: "POST",
            url: '/usercontrols/WRDev.Apax.Umbraco/printbasket.asmx/Add',
            data: "{nodeId:" + nodeId + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var r = data.d;
                if (r) {
                    setBasketCount(basketContent, true);
                    if (basketIsVisible)
                        updateList();
                }
                $("div.add a").unblock();
            }
        })
    };

    removeFromPrintBasket = function (nodeId) {
        $.ajax(
        {
            type: "POST",
            url: '/usercontrols/WRDev.Apax.Umbraco/printbasket.asmx/Remove',
            data: "{nodeId:" + nodeId + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var r = data.d;
                //alert(r);
                if (r) {
                    setBasketCount(basketContent, true);
                    updateList();
                }
                $("div.add a").unblock();
            }
        })
    };

    togglePrintBasketView = function () {
        //alert(basketIsVisible);
        if (!basketIsVisible) {
            // show the basket
            $(basketContent).addClass("active");

            // call the ajax method to get the contents and render
            updateList();

            $(basketDiv).show();

            shareIsVisible = false;
            $(shareContent).removeClass("active");
            $(shareDiv).hide();
        }
        else {
            // hide the basket
            $(basketContent).removeClass("active");
            $(basketDiv).hide();
        }
        basketIsVisible = !basketIsVisible;
    }


    updateList = function () {
        $.ajax(
            {
                type: "POST",
                url: '/usercontrols/WRDev.Apax.Umbraco/printbasket.asmx/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) {
                            $(basketDiv).children(".printbasketcontent").html("<p>Your basket is currently empty</p>");
                        }
                        else {
                            var $content = $(basketDiv).children(".printbasketcontent");
                            $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) {
                                    removeMe(event.data.id, event.data.selector);
                                });
                                
                                var $span = $("<span />").addClass("delete").append($delete);

                                //$li.append($hidden);
                                $li.append($a);
                                $li.append($span);

                                $ul.append($li);
                            }
                            $content.append($ul);

                        }
                    }
                }
            });
    }

    removeMe = function (id, selector) {
        var $li = $(selector).parent("span").parent("li");

        $.blockUI.defaults.css = {};
        $li.block({
            message: "<img src='/css/images/load.gif' />",
            css: {},
            overlayCSS: {
                backgroundColor: '#FFF',
                opacity: 0.8
            }
        });

        removeFromPrintBasket(id);


        /*$.ajax(
        {
        type: "POST",
        url: '/usercontrols/WRDev.Apax.Umbraco/printbasket.asmx/Remove',
        data: "{nodeId:" + nodeId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
        var r = data.d;
        if (r) {
        setBasketCount(basketContent, true);
        updateList();
        }
        //$li.unblock();
        }
        })*/
    }


    sendToPrinter = function (nodeId) {
        $.ajax(
        {
            type: "POST",
            url: '/usercontrols/WRDev.Apax.Umbraco/printbasket.asmx/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;
                    }
                }
            }
        });
    }

    toggleShareView = function () {
        //alert(basketIsVisible);
        if (!shareIsVisible) {
            // show the basket
            $(shareContent).addClass("active");
            $(shareDiv + " .sharelinksleft").empty().share({
                services: ['twitter', 'facebook', 'digg', 'linkedin']
            });
            $(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");
            $(shareDiv).show();
            //alert("delay");

            // hide the basket
            basketIsVisible = false;
            $(basketContent).removeClass("active");
            $(basketDiv).hide();
        }
        else {
            $(shareContent).removeClass("active");
            $(shareDiv).hide();
        }
        shareIsVisible = !shareIsVisible;
    }

    // onload
    $(function() {
         setBasketCount(basketContent);
     });


     /* jquery.share.js */

     /* original script by http://lab.fancydesign.de */
     /* modified by webreality */

     (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);
