(function($){
    jQuery.fn.countdown = function(options){
        var defaults = {
            date: {
                from: new Date(),
                to: new Date()
            },
            label: {
                from: "00:00",
                to: "23:59"
            },
            prepare: function(){
            },
            run: function(){
            },
            complete: function(){
            },
            updateTime: 250,
            sync: false,
            gmt: +2
        };
        var opts = $.extend({}, defaults, options),
            msPerDay = 24 * 60 * 60 * 1000,
            run = false,
            self = this;
            
        if (opts.sync) {
            $.ajax({
                url: opts.sync,
                complete: function(XMLHttpRequest, textStatus){
                    var headerString = XMLHttpRequest.getAllResponseHeaders(),
                        pattern = /Date:(.*?)GMT/g;
                    startCountdown(pattern.exec(headerString)[1]);
                },
                error: function(){
                    startCountdown(false);
                }
            });
        }
        else startCountdown(false);
       
        
        function startCountdown(syncDate){
            self.each(function(){
                var t = $(this),
                    d = t.find('.day strong'),
                    h = t.find('.hour strong'),
                    m = t.find('.minute strong'),
                    s = t.find('.second strong'),
                    label = t.find('.alertWebshop'),
                    gmt = opts.gmt*60*60*1000,
                    td = syncDate!=false ? new Date(new Date(syncDate).getTime()+gmt) : new Date(),
                    dateDif = syncDate!=false ? new Date()-td : 0,
                    tdd = new Date(new Date().getTime() - dateDif),
                    from = new Date(tdd.getFullYear() + '/' + (tdd.getMonth() + 1) + '/' + tdd.getDate() + ' ' + opts.label.from),
                    to = new Date(tdd.getFullYear() + '/' + (tdd.getMonth() + 1) + '/' + tdd.getDate() + ' ' + opts.label.to),
                    CountdownFromDate = new Date(opts.date.from),
                    CountdownDate = new Date(opts.date.to),
                    dl, hl, ml, sl;
                 
                /*if (td <= CountdownDate && td >= CountdownFromDate) {
                    t.children().show();
                }*/
                
                if (td < CountdownFromDate) {
                    opts.prepare();
                }
                
                var intval = setInterval(function(){
                    var TodaysDate = new Date(new Date().getTime() - dateDif);
                    if (TodaysDate <= CountdownDate && TodaysDate >= CountdownFromDate) {
                        t.show();
                        if (!run) {
                            opts.run();
                            run = true;
                        }
                        var timeLeft = (CountdownDate.getTime() - TodaysDate.getTime()),
                            e_daysLeft = timeLeft / msPerDay,
                            daysLeft = Math.floor(e_daysLeft),
                            e_hrsLeft = (e_daysLeft - daysLeft) * 24,
                            hrsLeft = Math.floor(e_hrsLeft),
                            minsLeft = Math.floor((e_hrsLeft - hrsLeft) * 60),
                            e_minsleft = (e_hrsLeft - hrsLeft) * 60,
                            secLeft = Math.floor((e_minsleft - minsLeft) * 60);
                            
                        if (dl != daysLeft) {
                            d.hide().html((String(daysLeft).length == 1 ? '0' : '') + daysLeft + ':').show();
                            dl = daysLeft;
                        }
                        if (hl != hrsLeft) {
                            h.hide().html((String(hrsLeft).length == 1 ? '0' : '') + hrsLeft + ':').show();
                            hl = hrsLeft;
                        }
                        if (ml != minsLeft) {
                            m.hide().html((String(minsLeft).length == 1 ? '0' : '') + minsLeft + ':').show();
                            ml = minsLeft;
                        }
                        if (sl != secLeft) {
                            s.hide().html((String(secLeft).length == 1 ? '0' : '') + secLeft).show();
                            sl = secLeft;
                        }
                    }
                    else {
                        d.html('00:');
                        h.html('00:');
                        m.html('00:');
                        s.html('00');
                        if (TodaysDate >= CountdownDate) {
                            opts.complete();
                            clearInterval(intval);
                        }
                    }
                    
                    if (TodaysDate >= from && TodaysDate <= to && run) {
                        label.show();
                    }
                    else 
                        label.hide();
                    
                }, opts.updateTime);
            });
        }   
        return this;
    };
})(jQuery);
