﻿
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function FSMachine(b) {
    for (var a in b) {
        if (a == "machine" || a == "states" || a == "events" || a == "parameters") {
            this[a] = b[a]
        }
    }
    this.transitions = {}
}
$.fn.bindFSMachine = function (a) {
    var b = a;
    return this.each(function () {
        if (typeof (this.$FSM) == "undefined") {
            this.$FSM = {};
            var d = [];
            for (var c in b.events) {
                d.push("fsm:" + b.machine + ":" + b.events[c])
            }
            $(this).bind(d.join(" "), function (f) {
                var e = f.type.split(":");
                if (e.length == 3) {
                    if (typeof (this.$FSM[e[1]]) != "undefined") {
                        if (!this.$FSM[e[1]].locked) {
                            this.$FSM[e[1]].locked = true;
                            if (typeof (this.$FSM[e[1]].transitions[e[2] + "||" + this.$FSM[e[1]].currentState]) != "undefined") {
                                this.$FSM[e[1]].transitions[e[2] + "||" + this.$FSM[e[1]].currentState].transition(this, e[1], f.fsmparams)
                            }
                            this.$FSM[e[1]].locked = false
                        }
                    }
                }
            })
        }
        this.$FSM[b.machine] = {
            events: b.events,
            states: b.states,
            transitions: b.transitions,
            locked: false,
            currentState: "uninited"
        };
        for (var c in b.parameters) {
            this.$FSM[b.machine][b.parameters[c]] = null
        }
        $(this).trigger("fsm:" + b.machine + ":init")
    })
};
FSMHeroCarousel = new FSMachine({
    machine: "carousel",
    states: ["default", "autoplay", "animated"],
    events: ["init", "goto", "autoincrement", "end"],
    parameters: ["index", "size", "timer", "width"]
});
FSMHeroCarousel.transitions = {
    "init||uninited": {
        transition: function (a, c, b) {
            a.$FSM[c].index = parseInt($(a).attr('startindex'));
            a.$FSM[c].size = $(".carousel-card", a).length;
            a.$FSM[c].width = $(a).width();
            $(".carousel-card:eq(" + a.$FSM[c].index + ")", a).css({
                left: 0
            });
            if (a.$FSM[c].size > 1) {
                a.$FSM[c].timer = window.setInterval(function () {
                    $(".hero-carousel").trigger("fsm:carousel:autoincrement")
                }, 6000);
                a.$FSM[c].timer = window.setTimeout(function () {
                    var $card = $(".carousel-card:eq(" + $(".hero-carousel")[0].$FSM['carousel'].index + ")", $(".hero-carousel")[0]);
                    if ($card.is('[flashloaded]')) {
                        $card[0].startAnimation();
                    } else {
                        $card.attr('initflashonload', 'true');
                    }

                }, 1000);
                $(".hero-carousel-dot:eq(" + a.$FSM[c].index + ")", a).addClass("active");

                a.$FSM[c].currentState = "autoplay"
            }
        }
    },
    "autoincrement||autoplay": {
        transition: function (b, d, c) {

            if ($('[carouselanimated=true]').length == 0 && !$(b).is('[tainted=true]')) {
                var a = {
                    left: -b.$FSM[d].width,
                    top: 0
                };
                $(".carousel-card:eq(" + b.$FSM[d].index + ")", b).animate(a, 1200, "swing");
                b.$FSM[d].index = (b.$FSM[d].index + 1 >= b.$FSM[d].size) ? 0 : b.$FSM[d].index + 1;
                a.left = 0;

                $(".carousel-card:eq(" + b.$FSM[d].index + ")", b).css({
                    left: b.$FSM[d].width
                }).animate(a, 1200, "swing", function () {
                    $(".hero-carousel").trigger("fsm:carousel:end");

                    var $card = $(".carousel-card:eq(" + $('.hero-carousel')[0].$FSM.carousel.index + ")");
                    if ($card.is('[flashloaded]')) {
                        $card[0].startAnimation();
                    } else {
                        $card.attr('initflashonload', 'true');
                    }
                });

                $(".hero-carousel-dot", b).removeClass("active").filter(":eq(" + b.$FSM[d].index + ")").addClass("active");
                b.$FSM[d].currentState = "animated"
            }
        }
    },
    "goto||autoplay": {
        nextState: "animated",
        transition: function (c, e, d) {

            $('[carouselanimated=true]').each(function () {
                var $flash = $(this).find('object')[0];
                

                $flash.killAnimation();
                $flash.reactivateHotspots();

            });

            window.clearInterval(c.$FSM[e].timer);
            c.$FSM[e].timer = null;

            if (typeof (d.index) != "undefined" && $(".carousel-card:eq(" + d.index + ")", c).length == 1) {

                if (d.index != c.$FSM[e].index) {
                    var a = (d.index > c.$FSM[e].index) ? true : false;
                    var b = {
                        left: (a ? "-" : "") + c.$FSM[e].width,
                        top: 0
                    };
                    $(".carousel-card:eq(" + c.$FSM[e].index + ")", c).animate(b, 1200, "swing");
                    c.$FSM[e].index = d.index;
                    b.left = 0;
                    $(".carousel-card:eq(" + c.$FSM[e].index + ")", c).css({
                        left: (a ? "" : "-") + c.$FSM[e].width + "px"
                    }).animate(b, 1200, "swing", function () {
                        $(".hero-carousel").trigger("fsm:carousel:end");

                        var $card = $(".carousel-card:eq(" + $('.hero-carousel')[0].$FSM.carousel.index + ")");
                        if ($card.is('[flashloaded]')) {
                            $card[0].startAnimation();
                        } else {
                            $card.attr('initflashonload', 'true');
                        }
                    });

                    $(".hero-carousel-dot", c).removeClass("active").filter(":eq(" + c.$FSM[e].index + ")").addClass("active");
                    c.$FSM[e].currentState = "animated"
                }
            }
        }
    },
    "end||animated": {
        transition: function (a, c, b) {
            a.$FSM[c].currentState = (a.$FSM[c].timer != null) ? "autoplay" : "default"
        }
    }
};
FSMHeroCarousel.transitions["goto||default"] = FSMHeroCarousel.transitions["goto||autoplay"];

function flashCallbackRollover() {
    $('.hero-carousel').attr('tainted', 'true');
}

function flashCallbackStop() {
    $('[carouselanimated=true]').attr('carouselanimated', 'false');
}

function flashLoadedDG() {
    $('#hero-card-dg').attr('flashloaded', 'true');
    if ($('#hero-card-dg').is('[initflashonload]')) {
        $('#hero-card-dg')[0].startAnimation();
    }
}

function flashLoadedRH() {
    $('#hero-card-rh').attr('flashloaded', 'true');
    if ($('#hero-card-rh').is('[initflashonload]')) {
        $('#hero-card-rh')[0].startAnimation();
    }
}

$(document).ready(function () {

    var _start = getQuerystring("c", "sy");

    // remove dinner gaurantee
    if (_start != "dg") {
        $('#hero-card-dg').remove();
    }

    if ($('#hero-card-' + _start).length != 0) {
        _start = $('.carousel-card').index($('#hero-card-' + _start)[0])
    } else {
        _start = 0;
    }

    $('.hero-carousel').attr('startindex', _start);
    $('.carousel-card').css({ width: 946, height: 471, overflow: 'hidden', position: 'absolute', top: 0, left: -2000 });

    $('.carousel-card').each(function (i) {
        if ($(this).is('[carouselcardswf]')) {
        var params = { wmode: "transparent", allowscriptaccess: "always" };
        var attributes = { id: "CarouselCard" + i };
        var flashvars = {};
        $('span.flashvar', this).each(function () {
        flashvars[$(this).attr('paramname')] = $(this).attr('paramvalue');
        });
        $('span.flashvar', this).remove();
        $(this).append('<div id="' + attributes.id + '"></div>');
        swfobject.embedSWF($(this).attr('carouselcardswf'), attributes.id, "946", "471", "9.0.0", false, flashvars, params, attributes);
        }

        this.startAnimation = function () {

            if ($(this).find('object').length > 0 && !$(this).is('[carouselnoanimate]')) {

                /*var $flash = $('#' + $(this).attr('flash-id'))[0];
                if (navigator.appName != "Microsoft Internet Explorer") {
                    $flash = $(this).find('object:last')[0];
                }

                $flash.AnimationStart();*/

                $(this).find('object')[0].AnimationStart();

                $(this).attr('carouselanimated', 'true');
                $(this).attr('carouselnoanimate', 'true');
            }
        }
    });

    $('.hero-carousel').each(function () {
        if ($('.carousel-card', this).length > 1) {
            var _buttons = new Array();
            $('.carousel-card', this).each(function () {
                _buttons.push('<span class="hero-carousel-dot" style="width:17px;height:17px;cursor:pointer;display:inline-block;background-image:url(/images/hero-carousel/carousel-nav-dots.png);"></span>');
            });
            $(this).append('<div class="hero-carousel-nav" style="top:447px;right:12px;position:absolute;width:' + ($('.carousel-card', this).length * 17) + 'px;height:17px;">' + _buttons.join("") + '</div>');
        }
    });

    $('.hero-carousel-dot').click(function () {
        $('.hero-carousel').trigger({ type: 'fsm:carousel:goto', fsmparams: { index: $('.hero-carousel-dot').index(this)} });
    });

    $('.hero-carousel-dot').mouseenter(function () {
        $(this).addClass('hover');
    });

    $('.hero-carousel-dot').mouseleave(function () {
        $(this).removeClass('hover');
    });

    $('.hero-carousel').bindFSMachine(FSMHeroCarousel);
    $('.hero-carousel').css({ visibility: 'visible' });
});
           
