/* 
 * jCountdown 1.3.1 jQuery Plugin
 * Copyright 2011 Tom Ellis http://www.webmuse.co.uk, MIT Licensed (license.txt)
 */
(function (l) {
    l.fn.countdown = function (q) {
        var x = {
            date: new Date,
            updateTime: 1E3,
            htmlTemplate: '<div class="day-container">%{d}<span class="cd-time">days</span></div><div  class="hour-container">%{h}<span class="cd-time">hours</span></div><div  class="minute-container"> %{m} <span class="cd-time">mins</span></div><div class="second-container"> %{s} <span class="cd-time">sec</span></div>',
            minus: false,
            onChange: null,
            onComplete: null,
            onResume: null,
            onPause: null,
            leadingZero: false,
            offset: null,
            hoursOnly: false,
            direction: "down"
        },
            y = [].slice,
            j = Math.floor,
            s = /(%\{d\}|%\{h\}|%\{m\}|%\{s\})/g,
            m = /%\{d\}/,
            n = /%\{h\}/,
            o = /%\{m\}/,
            p = /%\{s\}/,
            t = function (d) {
                d = d || 0;
                var a = 0,
                    b = new Date;
                d *= 36E5;
                a = b.getTime() - -b.getTimezoneOffset() / 60 * 36E5;
                d = b.setTime(a + d);
                return new Date(d)
            },
            v = function () {
                var d, a, b, f, e, h, g;
                d = "";
                var c = this.data("jcdData");
                if (!c) return false;
                d = c.htmlTemplate;
                a = c.offset === null ? new Date : t(c.offset);
                b = new Date(c.date);
                f = (c.direction === "down" ? b.getTime() - a.getTime() : a.getTime() - b.getTime()) / 864E5;
                e = j(f);
                h = (f - e) * 24;
                f = j(h);
                g = j((h - f) * 60);
                h = j(((h - f) * 60 - g) * 60);
                if (c.hoursOnly) {
                    f += e * 24;
                    e = 0
                }
                if (c.leadingZero) {
                    if (e < 10 && !c.hoursOnly) e = "0" + e;
                    if (f < 10) f = "0" + f;
                    if (g < 10) g = "0" + g;
                    if (h < 10) h = "0" + h
                }
                if (c.direction === "down" && (a <= b || c.minus)) d = d.replace(m, e).replace(n, f).replace(o, g).replace(p, h);
                else if (c.direction === "up" && (b <= a || c.minus)) d = d.replace(m, e).replace(n, f).replace(o, g).replace(p, h);
                else {
                    d = d.replace(s, "00");
                    c.hasCompleted = true
                }
                this.html(d).trigger("change.jcdevt", [c]);
                if (c.hasCompleted) {
                    this.trigger("complete.jcdevt");
                    clearInterval(c.timer)
                }
            },
            w = {
                init: function (d) {
                    var a = l.extend({}, x, d),
                        b;
                    b = a.htmlTemplate;
                    return this.each(function () {
                        var f = l(this),
                            e = {},
                            h = a.offset === null ? new Date : t(a.offset),
                            g = new Date(a.date);
                        e = (a.direction === "down" ? g.getTime() - h.getTime() : h.getTime() - g.getTime()) / 864E5;
                        var c = j(e);
                        e = (e - c) * 24;
                        var i = j(e),
                            k = j((e - i) * 60),
                            r = j(((e - i) * 60 - k) * 60),
                            u = "";
                        if (a.hoursOnly) {
                            i += c * 24;
                            c = 0
                        }
                        if (f.data("jcdData")) {
                            f.countdown("changeSettings", d);
                            return true
                        }
                        a.onChange && f.bind("change.jcdevt", a.onChange);
                        a.onComplete && f.bind("complete.jcdevt", a.onComplete);
                        a.onPause && f.bind("pause.jcdevt", a.onPause);
                        a.onResume && f.bind("resume.jcdevt", a.onResume);
                        if (a.leadingZero) {
                            if (c < 10 && !a.hoursOnly) c = "0" + c;
                            if (i < 10) i = "0" + i;
                            if (k < 10) k = "0" + k;
                            if (r < 10) r = "0" + r
                        }
                        e = {
                            originalContent: f.html(),
                            date: a.date,
                            hoursOnly: a.hoursOnly,
                            leadingZero: a.leadingZero,
                            updateTime: a.updateTime,
                            direction: a.direction,
                            template: a.htmlTemplate,
                            htmlTemplate: a.htmlTemplate,
                            minus: a.minus,
                            offset: a.offset,
                            onChange: a.onChange,
                            onComplete: a.onComplete,
                            onResume: a.onResume,
                            onPause: a.onPause,
                            hasCompleted: false,
                            timer: 0
                        };
                        if (a.direction === "down" && (h <= g || a.minus)) u = b.replace(m, c).replace(n, i).replace(o, k).replace(p, r);
                        else if (a.direction === "up" && (g <= h || a.minus)) u = b.replace(m, c).replace(n, i).replace(o, k).replace(p, r);
                        else {
                            u = b.replace(s, "00");
                            e.hasCompleted = true
                        }
                        if (!e.hasCompleted) {
                            h = l.proxy(v, f);
                            e.timer = setInterval(h, e.updateTime)
                        }
                        f.data("jcdData", e).html(u);
                        if (e.hasCompleted) {
                            f.trigger("complete.jcdevt");
                            clearInterval(e.timer)
                        }
                    })
                },
                changeSettings: function (d) {
                    return this.each(function () {
                        var a = l(this),
                            b, f, e, h, g, c, i, k;
                        if (!a.data("jcdData")) return true;
                        b = l.extend({}, a.data("jcdData"), d);
                        f = b.htmlTemplate;
                        e = b.offset === null ? new Date : t(b.offset);
                        h = new Date(b.date);
                        g = (b.direction === "down" ? h.getTime() - e.getTime() : e.getTime() - h.getTime()) / 864E5;
                        c = j(g);
                        i = (g - c) * 24;
                        g = j(i);
                        k = j((i - g) * 60);
                        i = j(((i - g) * 60 - k) * 60);
                        if (b.hoursOnly) {
                            g += c * 24;
                            c = 0
                        }
                        a.unbind(".jcdevt");
                        clearInterval(b.timer);
                        b.onChange && a.bind("change.jcdevt", b.onChange);
                        b.onComplete && a.bind("complete.jcdevt", b.onComplete);
                        b.onPause && a.bind("pause.jcdevt", b.onPause);
                        b.onResume && a.bind("resume.jcdevt", b.onResume);
                        if (b.direction === "down" && (e <= h || b.minus)) f.replace(m, c).replace(n, g).replace(o, k).replace(p, i);
                        else if (b.direction === "up" && (h <= e || b.minus)) f.replace(m, c).replace(n, g).replace(o, k).replace(p, i);
                        else {
                            f.replace(s, "00");
                            b.hasCompleted = true
                        }
                        f = l.proxy(v, a);
                        b.timer = setInterval(f, b.updateTime);
                        a.data("jcdData", b);
                        f();
                        if (b.hasCompleted) {
                            a.trigger("complete.jcdevt");
                            clearInterval(b.timer)
                        }
                    })
                },
                resume: function () {
                    return this.each(function () {
                        var d = l(this),
                            a, b, f, e, h, g, c, i, k;
                        b = "";
                        a = d.data("jcdData");
                        if (!a) return true;
                        f = l.proxy(v, d);
                        b = a.htmlTemplate;
                        e = a.offset === null ? new Date : t(a.offset);
                        h = new Date(a.date);
                        g = (a.direction === "down" ? h.getTime() - e.getTime() : e.getTime() - h.getTime()) / 864E5;
                        c = j(g);
                        i = (g - c) * 24;
                        g = j(i);
                        k = j((i - g) * 60);
                        i = j(((i - g) * 60 - k) * 60);
                        if (a.hoursOnly) {
                            g += c * 24;
                            c = 0
                        }
                        if (a.direction === "down" && (e <= h || a.minus)) b = b.replace(m, c).replace(n, g).replace(o, k).replace(p, i);
                        else if (a.direction === "up" && (h <= e || a.minus)) b = b.replace(m, c).replace(n, g).replace(o, k).replace(p, i);
                        else {
                            b = b.replace(s, "00");
                            a.hasCompleted = true
                        }
                        a.timer = setInterval(f, a.updateTime);
                        d.data("jcdData", a).trigger("resume.jcdevt").html(b).trigger("change.jcdevt");
                        if (a.hasCompleted) {
                            d.trigger("complete.jcdevt");
                            clearInterval(a.timer)
                        }
                    })
                },
                pause: function () {
                    return this.each(function () {
                        var d = l(this),
                            a = d.data("jcdData");
                        if (!a) return true;
                        clearInterval(a.timer);
                        d.trigger("pause.jcdevt")
                    })
                },
                complete: function () {
                    return this.each(function () {
                        var d = l(this),
                            a = d.data("jcdData");
                        if (!a) return true;
                        clearInterval(a.timer);
                        a.hasCompleted = true;
                        d.data("jcdData", a).trigger("complete.jcdevt").unbind(".jcdevt")
                    })
                },
                destroy: function () {
                    return this.each(function () {
                        var d = l(this),
                            a;
                        a = d.data("jcdData");
                        if (!a) return true;
                        clearInterval(a.timer);
                        d.unbind(".jcdevt").removeData("jcdData").html(a.originalContent)
                    })
                },
                getSettings: function (d) {
                    var a;
                    if (a = l(this).data("jcdData")) if (d) {
                        if (a.hasOwnProperty(d)) return a[d]
                    } else return a
                }
            };
        if (w[q]) return w[q].apply(this, y.call(arguments, 1));
        else if (typeof q === "object" || !q) return w.init.apply(this, arguments);
        else l.error("Method " + q + " does not exist in the jCountdown Plugin")
    }
})(jQuery);
