jQuery(document).ready(function () {
    tamountC = jQuery("#total-amount").val();
    jQuery("#currencyAmount").html(tamountC);
    var amount = jQuery('#amount').val();
    var from = jQuery('#fromCurrency').val();
    var to = "";
    total = jQuery("div.currency").find("input").size();
    for (i = 1; i <= total; i++) {
        to += jQuery('#toCurrency' + i).val();
        if (i != total) {
            to += "~";
        }
    }
    var dataString = "amount=" + amount + "&from=" + from + "&to=" + to + "&total=" + total;
    jQuery.ajax({
        type: "POST",
        url: "http://gapyearsouthafrica.com/wp-content/themes/gysa/incl/ajax_converter.php",
        data: dataString,
        success: function (data) {
            jQuery('#results').show();
            jQuery('#results').html(data);
            for (i = 0; i < total; i++) {
                jQuery("#rcurrency" + i).css("display", "none");
            }
            currencyRate();
            clickFunction();
        }
    });
});

function currencyRate() {
    count = jQuery("div.data").find(".currency_count").size();
    for (i = 0; i < count; i++) {
        USD = jQuery("#rcurrency0").val();
        CAD = jQuery("#rcurrency1").val();
        AUD = jQuery("#rcurrency2").val();
        ZAR = jQuery("#rcurrency3").val();
        EUR = jQuery("#rcurrency4").val();
        GBP = jQuery("#amount").val();
        tamount = jQuery("#total-amount" + i).val();
        USD = Math.round(USD * tamount);
        EUR = Math.round(EUR * tamount);
        GBP = Math.round(GBP * tamount);
        ZAR = Math.round(ZAR * tamount);
        CAD = Math.round(CAD * tamount);
        AUD = Math.round(AUD * tamount);
        jQuery("#flag" + i + " #flag1 a img").attr("alt", AUD);
        jQuery("#flag" + i + " #flag2 a img").attr("alt", CAD);
        jQuery("#flag" + i + " #flag3 a img").attr("alt", EUR);
        jQuery("#flag" + i + " #flag4 a img").attr("alt", ZAR);
        jQuery("#flag" + i + " #flag5 a img").attr("alt", USD);
        jQuery("#result" + i + " #currencyAmount").html(GBP);
        jQuery("#result" + i + " #currencyFlag img").attr("alt", GBP);
    }
}

function clickFunction() {
    jQuery("#mainHref ul li a").click(function () {
        cFlagImage = jQuery(this).parent().attr("id");
        var count = jQuery("div.data").find(".currency_count").size();
        for (i = 0; i < count; i++) {
            var amount = jQuery("div#flag" + i + " li#" + cFlagImage + " a img").attr("alt");
            var symbol = jQuery("div#flag" + i + " li#" + cFlagImage + " a img").attr("class");
            jQuery("#result" + i + " #currencyAmount").html(amount);
            jQuery("#result" + i + " #currencyType").html(symbol);
            clickImageParent = jQuery(this).parent().attr("id");
            clickImage = jQuery("div#flag" + i + " li#" + clickImageParent + " a").html();
            currentImage = jQuery("#result" + i + " #currencyFlag").html();
            parentId = clickImageParent;
            jQuery("div#flag" + i + " li#" + parentId + " a").html(currentImage);
            jQuery("#result" + i + " #currencyFlag").html(clickImage);
        }
    });
}

