/* ------------------------------------------------------------------------
	Class: mpTooltip
	Use: A simple jQuery tooltip 
	Author: Martin Poucher
	Company: TBA
	Version: 1.0
	Created: 16th January 2010
------------------------------------------------------------------------- */

(function($){
    $.fn.mpTooltip = function(options) {
        settings = $.extend({
            pointerUrl: "/images/tooltip_arrow.gif",
            textAttribute: "title",
            height: 25,
            width: 70,
            id: "tooltip",
            arrowHeight: 6,
            borderWidth: 1,
            borderColour: "#999"
        }, options);

        var maxHeight = 0;
        return $.each($(this), function(i, n) {
            $(this).hover(function() {
                var thisTop = $(this).offset();
                var thisWidth = $(this).width();
                var tooltipText = $(this).children("span").html();
                
                $("body").append("<div id='" + settings.id + "'><div class='tooltipText'>" + tooltipText + "</div></div>");
                $("#" + settings.id).css({
                    "position": "absolute",
                    "top": thisTop.top - settings.height,
                    "left": thisTop.left + (thisWidth / 2) - (settings.width / 2),
                    "background-color": "transparent",
                    "background-image": "url(" + settings.pointerUrl + ")",
                    "background-position": "bottom center",
                    "background-repeat": "no-repeat",
                    "width": settings.width,
                    "height": settings.height,
                    "z-index": "99999"
                });
                $("#" + settings.id + " div.tooltipText").css({
                    "padding": "0",
                    "border": settings.borderWidth + "px solid " + settings.borderColour,
                    "background-color": "#fff",
                    "height": settings.height - settings.arrowHeight + "px",
                    "text-align": "center",
                    "width": "100%"
                });
            },
            function() {
                $("#" + settings.id).remove();
            });
        });
    };
})(jQuery);

$.mpAlert = function(options) {
    settings = $.extend({
        backgroundColour: "#FFEBE8",
        borderWidth: 3,
        borderColour: "#ff0000",
        text: "",
        width: 300,
        padding: 10
    }, options);

    var alertId = "alertPopup";
    var transitionSpeed = 100;
    if ($("#" + alertId).length == 0) {
        $("body").append("<div id='" + alertId + "' style='display:none;width:" + settings.width + "'>" + settings.text + "<br /><a href='#' class='closeAlert'>Close</a></div>");
    }
    
    var viewportHeight = $(window).height();
    var viewportWidth = $(window).width();
    var alertHeight = $("#" + alertId).height();
    var alertWidth = $("#" + alertId).width();
    
    $("#" + alertId).css({
        "top": (viewportHeight / 2) - (alertHeight / 2),
        "left": (viewportWidth / 2) - (alertWidth / 2),
        "position": "absolute",
        "background-color": settings.backgroundColour,
        "border": settings.borderWidth + "px solid " + settings.borderColour,
        "z-index": "99999",
        "padding": settings.padding + "px"
    }).fadeIn(transitionSpeed, function() {
        $("a.closeAlert").click(function() { $("#" + alertId).fadeOut(transitionSpeed); return false; });
    });
};

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 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
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
