﻿/// <reference path="/Scripts/jquery-1.4.1-vsdoc.js"/>

var Aumio = {};

Aumio.Countdown = function () {
    var _FORMAT = '<li>dd</li><li>HH</li><li>mm</li><li>ss</li>';
    var _intervalID;
    var _elements;

    this.Start = function () {
        _elements = $('.countdown');

        if (_intervalID == null) {
            _intervalID = setInterval(function () {
                $(_elements).each(function () {
                    var endsIn = $(this).attr('rel');
                    endsIn--;

                    if (endsIn >= 0) {
                        var html = _FORMAT.replace('dd', Calculate(endsIn, 86400, 100000)).replace('HH', Calculate(endsIn, 3600, 24))
                        .replace('mm', Calculate(endsIn, 60, 60)).replace('ss', Calculate(endsIn, 1, 60));

                        $(this).find('.countdown-values').html(html);
                        $(this).attr('rel', endsIn);
                    } else {
                        $(this).parents('.deal-controls').hide(0);
                    }
                });
            }, 1000);
        }
    };

    this.Stop = function () {
        clearTimeout(_intervalID);
        _intervalID = null;
        _elements = null;
    };

    var Calculate = function (endsIn, num1, num2) {
        var value = ((Math.floor(endsIn / num1)) % num2).toString();
        if (value.length < 2) {
            value = "0" + value;
        }
        return value;
    };
};

Aumio.Facebook = function () {
    this.Initialized = false;

    this.Initialize = function () {
        FB.init({ apiKey: Aumio._API_KEY, cookie: true, status: true, xfbml: true });
        this.Initialized = true;
    };

    this.Login = function (success) {
        if (!FB.getAuthResponse()) {
            FB.login(function (response) {
                if (response.authResponse) {
                    $.cookie('access_token', response.authResponse.accessToken, { path: '/' });
                    success();
                }
            }, { scope: 'publish_stream' });
        } else {
            success();
        }
    };

    this.Logout = function (success) {
        if (FB.getAuthResponse()) {
            FB.logout(function (response) {
                $.cookie('access_token', null, { path: '/' });
                success();
            });
        } else {
            success();
        }
    };
};

Aumio.GetSatisfaction = function () {
    $.getScript('http://s3.amazonaws.com/getsatisfaction.com/javascripts/feedback-v2.js', function () {
        var options = {};
        options.display = 'overlay';
        options.company = 'aumio';
        options.placement = 'left';
        options.color = '#F173AC';
        options.style = 'idea';
        options.container = 'getsatisfaction';

        var widget = new GSFN.feedback_widget(options);
    });
};

Aumio.Helpers = function () {
    this.Notify = function (message) {
        $('body').queue(function () {
            $(this).append(['<div id=notbar-wrapper><div id=notbar-content>', message, '</div></div>'].join(''));
            $('#notbar-wrapper').slideDown('fast').delay(5000).slideUp('fast', function () {
                $(this).remove();
                $('body').dequeue();
            }).click(function () {
                $(this).dequeue();
            });
        });
    };
};

