﻿/*

              HELLO NERD! =)

              Nice of you to drop by. Im just assuming you are a nerd since you found this file :)
              So, you are here to either steal my code or just learn something. First of all, there are 
              better resources on the interwebs to learn something about javacript than this file.
              If you are here just to steal my code, i will hack your site :)
*/

$(document).ready(function () {
    //    $('#facebook-share').click(function () {
    //        window.location = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(document.location) + "&;t=" + encodeURIComponent(document.title);
    //    });
    //    $('#twitter-share').click(function () {
    //        window.open('http://twitter.com?status=' + encodeURIComponent(document.title + ' - ' + document.location));
    //    });
    var _amount = 1;
    var _baseCurrency = "SEK";
    var _otherCurrency = "EUR";
    var _multiplier;

    function getConversionRate() {
        //Get all the values
        _amount = $('#amount').val();
        //        _baseCurrency = $('#baseCurrency').val();
        //        _otherCurrency = $('#otherCurrency').val();

        var data =
                {
                    amount: 1,
                    baseCur: _baseCurrency,
                    otherCur: _otherCurrency
                }
        //Make data string


        $.ajax({
            type: "POST",
            url: "/Currency/Convert",
            data: data,

            success: function (data) {
                //Show results div
                _multiplier = data.result;
                $('#result-amount').show();
                var conversionResult = getResultByMulti(_multiplier, _amount);
                //Put received response into result div
                $('#result-amount').html(conversionResult);
                populateHelpers();
            }

        });
    }
    $('#convert').click(function () {
        getConversionRate();

    });


    $('#amount').keyup(function () {
        calculate();
    });

    function calculate() {
        _amount = $('#amount').val();
        var conversionResult = getResultByMulti(_multiplier, _amount);
        $('#result-amount').html(conversionResult);

    }
    function getResultByMulti(e, a) {
        var temp = a * e;
        return Math.round(temp * 1000) / 1000;
    }
    function getResultByDiv(e, a) {
        var temp = a / e;
        return Math.round(temp * 1000) / 1000;
    }
    $('#current-base-currency').click(function () {
        $('#country-base-list').slideToggle("fast");
    });
    $('#current-other-currency').click(function () {
        $('#country-other-list').slideToggle("fast");
    });

    function runFirst() {
        _baseCurrency = $('#current-base-currency').html();
        _otherCurrency = $('#current-other-currency').html();
        var data =
                {
                    amount: _amount,
                    baseCur: _baseCurrency,
                    otherCur: _otherCurrency
                }
        $.ajax({
            type: "POST",
            url: "/Currency/Convert",
            data: data,

            success: function (data) {
                //Show results div
                _multiplier = data.result;
                $('#result-amount').show();
                var conversionResult = getResultByMulti(_multiplier, _amount);
                //Put received response into result div
                $('#result-amount').html(conversionResult);
                populateHelpers();

            }

        });
    }

    function populateHelpers() {
        $('.current-other-currency').each(function () {
            $(this).html(_otherCurrency);
        });
        $('.base-currency-name').html(_baseCurrency);
        $('#base-1').html(getResultByDiv(_multiplier, 1).toString());
        $('#base-5').html(getResultByDiv(_multiplier, 5).toString());
        $('#base-10').html(getResultByDiv(_multiplier, 10).toString());
        $('#base-25').html(getResultByDiv(_multiplier, 25).toString());
        $('#base-50').html(getResultByDiv(_multiplier, 50).toString());
        $('#base-100').html(getResultByDiv(_multiplier, 100).toString());

        $('#other-1').html(getResultByMulti(_multiplier, 1).toString());
        $('#other-5').html(getResultByMulti(_multiplier, 5).toString());
        $('#other-10').html(getResultByMulti(_multiplier, 10).toString());
        $('#other-25').html(getResultByMulti(_multiplier, 25).toString());
        $('#other-50').html(getResultByMulti(_multiplier, 50).toString());
        $('#other-100').html(getResultByMulti(_multiplier, 100).toString());
    }
    function addClickEventToBaseList() {
        $('#country-base-list li').each(function () {
            $(this).click(function () {
                var cur = $(".currency-code", this).html();
                $('#current-base-currency').empty();
                $('#current-base-currency').html(cur);
                _baseCurrency = cur;
                $('#country-base-list').hide();
                getConversionRate();
            });
        });
    }
    function addClickEventToOtherList() {
        $('#country-other-list li').each(function () {
            $(this).click(function () {
                var cur = $(".currency-code", this).html();
                $('#current-other-currency').empty();
                $('#current-other-currency').html(cur);
                _otherCurrency = cur;
                $('#country-other-list').hide();
                getConversionRate();
            });
        });
    }
    function switchCurrencies() {

        var tempBase = _baseCurrency;
        _baseCurrency = _otherCurrency;
        _otherCurrency = tempBase;
        $('#current-other-currency').html(_otherCurrency);
        $('#current-base-currency').html(_baseCurrency);
        _multiplier = (1 / _multiplier);
        calculate();
    }
    $('#switch-currencies').click(function () {
        switchCurrencies();
    });

    runFirst();
    addClickEventToBaseList();
    addClickEventToOtherList();
});
