/**
* 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 $.fn.cooki('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.fn.cooki('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.fn.cooki('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.fn.cooki('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 $.fn.cooki
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given key.
*
* @example $.fn.cooki('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 $.fn.cooki
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
(function ($) {$.fn.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);

/*!
* 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
		containerElement : '.header', // element that html will be prepended to
		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
		dragAlt : 'Drag me!', // default drag icon alt text
		elementId : 'pinning-discoverability', // Id of banner container that will be injected
		logoPath : 'http://www.esecurityplanet.com/img/esp-32.png', // Path to image to use for logo. 32px x 32px suggested.
		message : 'To pin this site, drag the logo to the Windows Taskbar.', // default message
		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 class="toast" id="{{elementId}}"><div class="pinning-content"><img src="{{logoPath}}" alt="{{dragAlt}}" class="msPinSite" /><div class="pinning-main-text">{{message}}</div><a href="#close" class="pinning-hide" title="{{closeTitle}}">close</a></div></div>',
			
			pushUp: '<div class="push-up" id="{{elementId}}"><div class="pinning-content"><div class="pinning-bg"><img src="http://buildmypinnedsite.com/{{logoPath}}" class="msPinSite" alt="{{dragAlt}}" /></div><div class="pinning-desc" id="pinning-desc">{{message}}</div></div><a href="#close" class="pinning-hide" title="{{closeTitle}}">close</a></div>',
			
			topHat: '<div class="top-hat" id="{{elementId}}"><div class="pinning-alignment"><div class="pinning-content"><img style="cursor: move;" src="{{logoPath}}" class="msPinSite" alt="{{dragAlt}}" /><span class="pinning-desc">{{message}}</span><a id="close" href="#close" class="pinning-hide" title="{{closeTitle}}">close</a></div></div></div>'
		};

	var methods = {
		init: function (callerSettings) {
			settings = $.extend(settings, callerSettings); 
			settings.cookieValue = 'hidden';

			return methods.showBanner();

		},
		showBanner: function() {

			if (methods.shouldDisplay()) { // Inject HTML

				if (discStyles.hasOwnProperty(settings.discStyle)) {

					var html = eval('discStyles.' + settings.discStyle),
					html = html.replace('{{message}}', settings.message),
					html = html.replace('{{dragAlt}}', settings.dragAlt),
					html = html.replace('{{closeTitle}}', settings.closeTitle),
					html = html.replace('{{logoPath}}', settings.logoPath),
					html = html.replace('{{elementId}}', settings.elementId),
					$html;

					$html = $(html); 
					$(settings.containerElement).prepend($html);
					$html.slideToggle(settings.slideDuration).find('.pinning-hide').bind('click', hideBanner);
					return $html;

				} else {
					$.error("Cannot find discoverability style.");
					return;
				}

			}

			function hideBanner(event) {
				event.preventDefault();

				$html.slideToggle(settings.slideDuration).remove();
				// set cookie
				if (settings.useCookie) {
					$.fn.cookie(settings.cookieName, settings.cookieValue, { expires: settings.cookieLength });
				}

			}
		},
		shouldDisplay: function() {

			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 ($.fn.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;
			}
		}
	};

	$.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.pinable');
		}
	}

})(jQuery);
