﻿var DealTimer = function (d, h, m, s) {
    var timerinterval;
    var that = this;
    var first = true;
    window.globalTimerArray = [];

    var dt = {
        days: isNaN(d) ? 0 : d,
        hours: isNaN(h) ? 0 : h,
        minutes: isNaN(m) ? 0 : m,
        seconds: isNaN(s) ? 0 : s,
        getTens: function (num) {
            return parseInt(num / 10);
        },
        getOnes: function (num) {
            return (num - (parseInt(num / 10) * 10));
        },
        getTimeLeft: function () {
            return (new Date(0, 0, dt.days, dt.hours, dt.minutes, dt.seconds));
        }
    };

    dt.decrement = function () {
        if (dt.days == 0 && dt.hours == 0 && dt.minutes == 0 && dt.seconds == 1)
            clearInterval(timerinterval);
        var timeLeft = new Date(dt.getTimeLeft() - 1000);
        //dt.hours = timeLeft.getHours() + (dt.days * 24);  // if we're displaying days
        dt.hours = timeLeft.getHours();
        dt.minutes = timeLeft.getMinutes();
        dt.seconds = timeLeft.getSeconds();
    };

    dt.setDigitValues = function (specifier) {
        specifier = specifier || "";
        if (dt.days > 0) {
            var dayString = dt.days.toString() + ' day' + (dt.days == 1 ? '' : 's');
            $("#digits").addClass('withDays');
            $(".digit-container.days").show().find(".days-digit.ones" + specifier).html(dayString);
        } else {
            $("#digits").removeClass('withDays');
            $(".digit-container.days").hide();
        }
        $(".hours-digit.tens" + specifier).html(dt.getTens(dt.hours));
        $(".hours-digit.ones" + specifier).html(dt.getOnes(dt.hours));
        $(".minutes-digit.tens" + specifier).html(dt.getTens(dt.minutes));
        $(".minutes-digit.ones" + specifier).html(dt.getOnes(dt.minutes));
        $(".seconds-digit.tens" + specifier).html(dt.getTens(dt.seconds));
        $(".seconds-digit.ones" + specifier).html(dt.getOnes(dt.seconds));
    };

    dt.digitsThatChanged = function () {
        var backElements = $(".back");
        var differingElements = [];

        $.each(frontElements, function (k, v) {
            v = $(v);
            if (v.html() != $(backElements[k]).html()) {
                differingElements.push(v);
            }
        });

        return differingElements;
    };

    dt.animateCountdown = function () {
        dt.decrement();
        dt.setDigitValues(".back");
        if (first) {
            $("#digits").show();
            first = false;
        }
    };

    dt.init = new function () {
        dt.setDigitValues();

        //if timer recieves all zeros then don't count down
        if (dt.days < 1 && dt.hours < 1 && dt.minutes < 1 && dt.seconds < 1) {
            console.log('error parsing the timer');
            clearInterval(timerinterval);
            $("#digits").show();
        } else {
            globalTimerArray.push(dt);
            globalTimerArray[0].animateCountdown();
            timerinterval = setInterval('globalTimerArray[0].animateCountdown()', 1000);
        }
    };
    return dt;
};

function fb_share(did) {
    u = "http://" + document.location.hostname + document.location.pathname.replace(/\/\d+$/, "");
    u += "/" + did;
    url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(u);
    var fb = window.open(url, 'fb_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function fb_subscribe_share() {
    url = "http://www.facebook.com/sharer/sharer.php?s=100&p[title]=I Just Subscribed to DealChicken!&p[url]=http%3A%2F%2F" + document.location.hostname + "&p[summary]=Enjoy up to 90% off local dining, shopping and more.&p[images][0]=http%3A%2F%2F" + document.location.hostname + "/Content/Images/DealChicken/ic-fbshare-subreg.png";
    var fb = window.open(url, 'fb_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function fb_register_share() {
    url = "http://www.facebook.com/sharer/sharer.php?s=100&p[title]=I Just Registered to DealChicken!&p[url]=http%3A%2F%2F" + document.location.hostname + "&p[summary]=Enjoy up to 90% off local dining, shopping and more.&p[images][0]=http%3A%2F%2F" + document.location.hostname + "/Content/Images/DealChicken/ic-fbshare-subreg.png";
    var fb = window.open(url, 'fb_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function fb_purchase_share(did) {
    u = "http://" + document.location.hostname + purchaseShareUrl;
    url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(u);
    var fb = window.open(url, 'fb_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function fb_sweepstakes_share(did) {
    u = "http://" + document.location.hostname + document.location.pathname.replace(/\/\d+$/, "");
    url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(u);
    var fb = window.open(url, 'fb_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function fb_clpromo_share(uniqueurl) {
    url = "http://www.facebook.com/sharer/sharer.php?s=100&p[title]=Get a little Chicken Love - $5 off&p[url]=http%3A%2F%2F" + document.location.hostname + "&p[summary]=I'm sharing the Chicken Love with my peeps. Click here for $5 off your first daily deals purchase: http://" + document.location.hostname + "/signup/" + uniqueurl + "&p[images][0]=http%3A%2F%2F" + document.location.hostname + "/Content/Images/DealChicken/ic-fbshare-subreg.png";
    var fb = window.open(url, 'fb_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function tweet() {
    u = document.location.href;
    t = $('.dealtitle').html();
    url = "http://twitter.com/intent/tweet?text=" + escape(t) + "&url=" + escape(u);
    var tw = window.open(url, 'tw_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function tweet_subscribe() {
    u = "http://" + document.location.hostname;
    t = "I Just Subscribed to DealChicken!";
    url = "http://twitter.com/intent/tweet?text=" + escape(t) + "&url=" + escape(u);
    var tw = window.open(url, 'tw_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function tweet_register() {
    u = "http://" + document.location.hostname;
    t = "I Just Registered to DealChicken!";
    url = "http://twitter.com/intent/tweet?text=" + escape(t) + "&url=" + escape(u);
    var tw = window.open(url, 'tw_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function tweet_purchase() {
    u = "http://" + document.location.hostname;
    t = "I Just Bought a Deal on DealChicken!";
    url = "http://twitter.com/intent/tweet?text=" + escape(t) + "&url=" + escape(u);
    var tw = window.open(url, 'tw_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function tweet_sweepstakes() {
    t = "Full year of free daily deals? Yes, please! I entered already, have you? Enter by 12/11 at http://bit.ly/t4KydG #feelingclucky";
    url = "http://twitter.com/intent/tweet?text=" + escape(t);
    var tw = window.open(url, 'tw_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}
function tweet_clpromo(uniqueurl) {
    u = "http://" + document.location.hostname + "/signup/" + uniqueurl;
    t = "#chickitout $5 off first purchase from DealChicken.com with this url-> ";
    url = "http://twitter.com/intent/tweet?text=" + escape(t) + escape(u);
    var tw = window.open(url, 'tw_share', 'toolbar=0,status=0,scrolling=0,width=626,height=426');
    return false;
}

(function ($) {
    $.fn.modal = function () {

        var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/); 
         
        if (this.attr('id').substr(0,2) != 'm_')
            var target = '#m_'+this.attr('id');
        else
            var target = '#'+this.attr('id');
        if ($(target).length === 0)
            $('div',this).first().attr('id','m_'+this.attr('id'));
        if($(target + '.modal').length === 0){
            $(target).addClass('modal');
            $(target).children().wrapAll('<div class="content"/>');
            $('.content',this).append('<span class="chicken"/>');
        }

        if (isMobile) {
            $.fancybox({              
                "href": target,
                "easingIn": "easeOutBack",
                "easingOut": "easeInBack",
                "hideOnContentClick": false,
                "overlayOpacity": 0.6,
                "overlayColor": "#000000",
                "scrolling": "no",
                "autoDimensions": true,
                "showCloseButton": true,
                "centerOnScroll":false,
                "resizeOnWindowResize": false,
                "onClosed":function(){$(target).unwrap();}
            });
        } else {
            $.fancybox({
                "href": target,
                "easingIn": "easeOutBack",
                "easingOut": "easeInBack",
                "hideOnContentClick": false,
                "overlayOpacity": 0.6,
                "overlayColor": "#000000",
                "scrolling": "no",
                "autoDimensions": true,
                "showCloseButton": true,
                "centerOnScroll":true,
                "onClosed":function(){$(target).unwrap();}
            });
        }
    };
})(jQuery);

$(function () {
    $('.trigger-locations').click(function () {
        $('#wrap-content').css('z-index', '30');
        $('#city-menu').slideDown('fast');
        return false;
    });
    $('#city-menu .hide').click(function () {
        $('#city-menu').slideUp('fast', function() {
            $('#wrap-content').removeAttr('style');
        });
    });
    $('.facebook').click(function () {
        new OmnitureTriggers().LinkTrack('body_facebookshare');
        var did = $(this).attr('rel');
        fb_share(did);
        return false;
    });
    $('.facebook-subscribe').click(function () {
        new OmnitureTriggers().LinkTrack('modal_facebookshare');
        fb_subscribe_share();
        return false;
    });
    $('.facebook-register').click(function () {
        new OmnitureTriggers().LinkTrack('modal_facebookregister');
        fb_register_share();
        return false;
    });
    $('.facebook-purchase').click(function () {
        new OmnitureTriggers().LinkTrack('modal_facebookpurchase');
        var did = $(this).attr('rel');
        fb_purchase_share(did);
        return false;
    });
    $('.facebook-sweepstakes').click(function () {
        var did = $(this).attr('rel');
        fb_sweepstakes_share(did);
        return false;
    });
    $('.facebook-clpromo').click(function () {
        var uniqueurl = $(this).attr('rel');
        fb_clpromo_share(uniqueurl);
        return false;
    });
    $('.twitter').click(function () {
        new OmnitureTriggers().LinkTrack('body_twittershare');
        tweet();
        return false;
    });
    $('.twitter-subscribe').click(function () {
        new OmnitureTriggers().LinkTrack('modal_twittershare');
        tweet_subscribe();
        return false;
    });
    $('.twitter-register').click(function () {
        new OmnitureTriggers().LinkTrack('modal_twitterregister');
        tweet_register();
        return false;
    });
    $('.twitter-purchase').click(function () {
        new OmnitureTriggers().LinkTrack('modal_twitterpurchase');
        tweet_purchase();
        return false;
    });
    $('.twitter-sweepstakes').click(function () {
        tweet_sweepstakes();
        return false;
    });
    $('.twitter-clpromo').click(function () {
        var uniqueurl = $(this).attr('rel');
        tweet_clpromo(uniqueurl);
        return false;
    });
    $('.sign-in').click(function () {
        new OmnitureTriggers().LinkTrack('body_signinnow');
        return false;
    });

    $('.signin').click(function () {
        new OmnitureTriggers().LinkTrack('header_signin');
        return true;
    });

    $('.newuser').click(function () {
        new OmnitureTriggers().LinkTrack('overlay_signup');
        return true;
    });

    $('.sign-in .signup').click(function () {
        new OmnitureTriggers().LinkTrack('header_signup');
        return true;
    });

    $('.joinButton').click(function () {
        new OmnitureTriggers().LinkTrack('inline_register');
        return true;
    });

    $('.registerButton').click(function () {
        new OmnitureTriggers().LinkTrack('footer_register');
        return true;
    });

    $('.sign-in-facebook').click(function () {
        new OmnitureTriggers().LinkTrack('body_facebookconnect');
        return true;
    });
    $('.gift').click(function () {
        new OmnitureTriggers().LinkTrack('body_senddealgift');
        return true;
    });
    $('.subscribe').click(function () {
        new OmnitureTriggers().LinkTrack('body_sendmedeals');
        $('#modal-subscribe input').val('');
        $('#modal-subscribe .error').attr('style', 'display:none;');
        $("#modal-subscribe").modal();
        return false;
    });

    $('.travel-subscribe').click(function () {
        new OmnitureTriggers().LinkTrack('body_travesubscribe');
        $('#modal-travel input:not(input[type=hidden])').val('').removeAttr('checked');
        $('#modal-travel .error').attr('style', 'display:none;');
        $("#modal-travel").modal();
        return false;
    });

    $('.email').click(function (e) {
        if (window.location.href.indexOf("admin") == -1 && window.location.href.indexOf("vendor") == -1) {
            new OmnitureTriggers().LinkTrack('body_emailshare');
            var funcName = $(this).attr('class');
            funcName = funcName.split('-');
            funcName = funcName[1];
            window[funcName]();
            e.preventDefault();
        }
    });
    $('.help-link').hover(
        function () {
            $(this).parent().find('.help-text').show('');
            $(this).parent().find('.help-arrow').show('');
        },
        function () {
            $(this).parent().find('.help-text').hide('');
            $(this).parent().find('.help-arrow').hide('');
        }
    );
    $('.swruleslink').click(function () {
        $("#modal-sweepstakes").modal();
        return false;
    });
    $('.previewgift').live('click', function () {
        $("#modal-previewgift").modal();
        return false;
    });
    if ($('#modal-email').length === 0) {
        $('form').append('<div id="modal-email"></div>');
    }
    $('a.goToDeal').click(function () {
        $("#modal-receiptnotification").modal();
        return false;
    });

    $('#user-name').hover(
        function () {
            $('#user-links').show();
        },
        function () {
            $('#user-links').hide();
        }
    );
});
