﻿/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given key and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String key The key of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given key.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String key The key of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

/*!
* jQuery pinable Plugin v1.2
* http://buildmypinnedsite.com
*
* Copyright 2011, Microsoft
* Licensed under MS-PL.
*
* Date: Thursday June 16 2011 14:54:29 -04
* Legal Notice: Any code provided on this site is "sample code" subject to the MSDN Terms of Use (http://msdn.microsoft.com/en-us/cc300389.aspx). Using this code means you agree to it.
*/
(function ($) {

	var settings = {
		closeTitle : 'Close', // default close title
		cookieLength : 30, // in days
		cookieName  : 'iePinning_' + document.domain, // appending domain to keep unique
		debug : false, // debug mode; forces the banner to show
		discStyle : 'toast', // discoverability style; options are toast, topHat and pushUp
		slideDuration : 'slow', // change the speed the banner animates in; options are same as jQuery slideToggle options
		useCookie : true // check for a cookie.
	},
		discStyles = {
			toast: '<div style="outline: 0px; left: 53px; top: 24px; width: auto; height: auto; display: block; z-index: 1002;" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-popover ui-popover-top" role="dialog" tabIndex="-1" aria-labelledby="ui-dialog-title-1"><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span id="ui-dialog-title-1" class="ui-dialog-title">Pin This Site</span><a class="pinning-hide ui-dialog-titlebar-close ui-corner-all" role="button" href="#close"><span class="ui-icon ui-icon-closethick">close</span></a></div><div style="width: auto; height: auto; min-height: 0px;" class="ui-dialog-content ui-widget-content" scrollTop="0" scrollLeft="0">Drag this icon to your Windows<br/> taskbar for quicker access</div><div class="ui-popover-arrow"></div></div>',
		};

	function hideBanner(event) {
		event.preventDefault();

		settings.banner.slideToggle(settings.slideDuration);

		// set cookie
		if (settings.useCookie) {
			$.cookie(settings.cookieName, settings.cookieValue, { expires: settings.cookieLength });
		}

	}

	function shouldDisplay() {
    
		if (settings.debug) {
			return true; // debug mode
		}

		try {
			if (!window.external.msIsSiteMode()) { // not in pinned mode, but it's available
				if (settings.useCookie) {
					// check cookie
					if ($.cookie(settings.cookieName) !== settings.cookieValue) {
						return true; // no cookie
					} else {
						return false; // cookie; don't show
					}
				} else {
					return true; // not using cookies; just display
				}
			} else {
				// we are in pinned mode
				return false;
			}
		} catch (ex) { // browsers does not support pinning; don't show
			return false;
        }
	}

    function showBanner() {
		if (shouldDisplay()) { // Inject HTML

			if (discStyles.hasOwnProperty(settings.discStyle)) {

				var html = eval('discStyles.' + settings.discStyle);

				settings.banner = $(html);

				settings.element.append(settings.banner);

				$('.pinning-hide').bind('click', hideBanner);
			} else {
				$.error("Cannot find discoverability style.");
			}

		}
    }

	var methods = {
		init: function (callerSettings) {
			settings = $.extend(settings, callerSettings);
			settings.element = this;
			settings.cookieValue = 'hidden';
			settings.banner = undefined;

			showBanner();

		},
		destroy: function () {
			settings.element.find('.pinning-hide').unbind('click');
			settings.element = undefined;
			settings.banner.remove();
			settings.banner = undefined;
		}
	};

	$.fn.pinable = function (method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.tooltip');
		}
	}

})(jQuery);
