﻿if (typeof WK == "undefined") {
    var WK = {};
}

WK.addBookmark = function(site, delay, opt) {
    /*
    * $param site [string] 'wk' / 'outnet', defines the share links based on site
    * $param delay [int] delay between mouse out and hiding the tool panel
    * $param opt [object] optional parameters (selector, addString), custom usage
    *
    * HTML required for bookmarking:
    * <div id="add-bookmark-link"><a href="#">Share this page</a></div>
    *
    */

    // get some page information and set some default values for absent params
    var arrurl = new Array();
    arrurl = document.Form1.action.split("/")
    var url = encodeURIComponent('http://www.worldkitchen.co.uk/' + arrurl[arrurl.length - 1]),
		title = encodeURIComponent(document.title),
		delay = delay || 400,
		opt = opt || {};
    
    opt.selector = opt.selector || "#add-bookmark-link";
    opt.addString = opt.addString || "";

    /*
    * $var BookmarkData [object]
    * Reference object for all the available bookmark targets
    *
    * bookmarkName[string] = {
    * 	   title: bookmark display name [string],
    * 	   url: the bookmarking url of the target site [string]. can contain the vars {url} and {title} defined above,
    * 	   windowWidth: width of the window to be opened [int],
    * 	   windowHeight: height of the window to be opened [int],
    * 	   site: comma delimited list of the sites to which the bookmark applies [string]. should match param site
    * }
    *
    */

    var bookmarkData = {
        "delicious": {
            title: "<img src='images/delicious.gif' title='delicious'>",
            url: "http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=" + url + opt.addString + "&amp;title=" + title,
            windowWidth: 630,
            windowHeight: 500,
            site: "wk,outnet,shoptalk",
            scroll: 0
        },
        "facebook": {
            title: "<img src='images/facebook.gif' title='facebook'>",
            url: "http://www.facebook.com/sharer.php?u=" + url + opt.addString + "&t=" + title,
            windowWidth: 630,
            windowHeight: 500,
            site: "wk,outnet,shoptalk",
            scroll: 0
        },
        "google": {
            title: "<img src='images/google.gif' title='google'>",
            url: "http://www.google.com/bookmarks/mark?op=add&bkmk=" + url + opt.addString + "&title=" + title,
            windowWidth: 700,
            windowHeight: 600,
            site: "wk,outnet,shoptalk",
            scroll: 1
        },
        "twitter": {
            title: "<img src='images/twitter.gif' title='twitter'>",
            url: "http://twitter.com/home?status=" + url + opt.addString,
            windowWidth: 800,
            windowHeight: 600,
            site: "wk,outnet,shoptalk",
            scroll: 1
        }
    };

    var thisItemHTML,
		bookmarks = [];

    for (var key in bookmarkData) {
        var thisItem = bookmarkData[key];
        if (thisItem.site.toLowerCase().indexOf(site.toLowerCase()) != -1) {
            thisItemHTML = '<div class="' + key + ' bookmark-link" onmouseover="WK.bookmarkUtils.hilite(this)" onmouseout="WK.bookmarkUtils.lolite(this)" onclick="WK.bookmarkUtils.bookmarkThis(\'' + key + '\')"><span>' + thisItem.title + '</span></div>';
            bookmarks.push(thisItemHTML);
        }
    }
    var html = ['<div id="add-bookmark-container" style="position:absolute;">',
					'<div id="add-bookmark-content">',
						'<div class="add-bookmark-list">',
							bookmarks.join(""),
						'</div>',
					'</div>',
				'</div>'].join("");

    function hideTool() {
        $("#add-bookmark-container").remove();
    }

    var hideInterval;

    $(opt.selector).hover(
		function() {
		    // onmouseover
		    clearInterval(hideInterval);
		    $(html).appendTo($(this));
		},
		function() {
		    // onmouseout
		    hideInterval = setInterval(hideTool, delay);
		}
	).click(function(event) {
	    event.preventDefault();
	});

    $(".bookmark-link").click(function(event) {
        event.stopPropagation();
        hideTool();
    });

    // lend some functions
    WK.bookmarkUtils = {
        hilite: function(element) {
            element.style.backgroundColor = "#F2F2F2";
        },
        lolite: function(element) {
            element.style.backgroundColor = "transparent";
        },
        bookmarkThis: function(type) {
            var thisItem = bookmarkData[type];
            window.open(thisItem.url, "_blank", "scrollbars=" + thisItem.scroll + ",toolbar=0,status=0,width=" + thisItem.windowWidth + ",height=" + thisItem.windowHeight);
        },
        showHelp: function() {
            // show the 'what's this' help dialog
        },
        hideHelp: function() {
            // hide the 'what's this' help dialog
        }
    };
}
