/* Just for you IE6/7 */
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


function isNull(a) {
    return typeof a == 'object' && !a;
}

function addCommas(nStr){
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

jQuery(document).ready(function(){jQuery('.selector').show();});

jQuery(document).ready(function(){
    if (!isNull(getCookie('currency'))) {
        convert(getCookie('currency'));
    } else {
        convert('NZD');
    }
});
var reqcurr = [];

function convert(currency){
    jQuery('.currency').each(function(){
        var from = jQuery(this).html();
        //var reg = new RegExp("^\\s*(USD|NZD|GBP|EUR|AUD|RAND|ZAR)?(?:\$|&#8364;|R)?([0-9\\.,]+)( *\\([a-z]{0,3} ?[0-9,]+\\))?( *\\(converting\\.\\.\\.\\))?\\s*$", 'img');
        //var fromcurr = from.replace(reg, "$1");
        //from = from.replace(reg, "$2");
        //alert(from);
        fromcurr = from.replace(/^\s*(USD|NZD|GBP|EUR|AUD|RAND|ZAR)?(?:\$|&#8364;|&#163;|R|£|€| )?([0-9\.,]+)( *\([a-z]{0,3}(?:\$|&#8364;|&#163;|R|£|€| )?[0-9,]+\))?( *\(converting\.\.\.\))?\s*$/ig, "$1");
        from     = from.replace(/^\s*(USD|NZD|GBP|EUR|AUD|RAND|ZAR)?(?:\$|&#8364;|&#163;|R|£|€| )?([0-9\.,]+)( *\([a-z]{0,3}(?:\$|&#8364;|&#163;|R|£|€| )?[0-9,]+\))?( *\(converting\.\.\.\))?\s*$/ig, "$2");
        
        if (fromcurr == '') {fromcurr = 'NZD';}
        if (fromcurr == 'RAND') {fromcurr = 'ZAR';}
        
        if (fromcurr == 'EUR') {
            //symbol = "&#8364";
            symbol = "€";
        } else if (fromcurr == 'GBP') {
            //symbol = "&#163";
            symbol = "£";
        } else if (fromcurr == 'ZAR') {
            symbol = '';
        } else {
            symbol = '$';
        }
        //alert(fromcurr+' - '+from);
        if (reqcurr.indexOf(fromcurr) == -1) {reqcurr.push(fromcurr);}
        var formatted = fromcurr+symbol+from+' (converting...)';
        //alert(fromcurr+' - '+from);
        //alert(formatted);
        jQuery(this).html(formatted);
    });
    
    /* run this for each currency in use */

    for(i=0;i<reqcurr.length;i++) {
        jQuery.getJSON("http://www.africansafaris.co.nz/currency/convert.php", { from: reqcurr[i], to: currency },
            
        function(rate){
        /*get conversion rate*/

            var r = rate.conversion_rate;
            /*
            if(!r.match(/^[0-9\.]+$/)) {
                alert('Currency conversion is not avaliable at this time');
                jQuery('.currency').each(function(){
                var from = jQuery(this).html();
                var reg = new RegExp("^\\s*(USD|NZD|GBP|EUR|AUD|RAND|ZAR)?(?:\$|&#8364;|R)?([0-9\\.,]+)( *\\([a-z]{0,3}(?:\$|&#8364;|&#163;|R| )?[0-9,]+\\))?( *\\(converting\\.\\.\\.\\))?\\s*$", 'img');
                fromcurr = from.replace(reg, "$1");
                from = from.replace(reg, "$2");
                var formated = fromcurr+''+from;
                jQuery(this).html(formated);
                })
                return false;
            }
            */
            jQuery('.currency').each(function(){
                var from = jQuery(this).html();
                //alert("^\\s*" + rate.from + rate.from_symbol_regex+ "([0-9\\.,]+)( *\\([a-z]{0,3}(?:\$|&#8364;|&#163;|R|£|€| )?[0-9,]+\\))?( *\\(converting\\.\\.\\.\\))?\\s*$");
                var reg = new RegExp("^\\s*" + rate.from + "(?:\\\$|&#8364;|&#163;|R|£|€| )?" + "([0-9\\.,]+)( *\\([a-z]{0,3}(?:\$|&#8364;|&#163;|R|£|€| )?[0-9,]+\\))?( *\\(converting\\.\\.\\.\\))?\\s*$", 'img');//rate.from_symbol_regex+
                
                if (from.match(reg)) {
                //alert(rate.from+' to '+rate.to+' '+rate.conversion_rate);
                    from = from.replace(reg, "$1");
                
                    var to = from.replace(/,/g, "") * rate.conversion_rate;
                    to = Math.round(to);
                    if (rate.from == rate.to) {
                        var formatted = rate.from+rate.from_symbol+from;
                    } else {
                        var formatted = rate.from+rate.from_symbol+from+' ('+currency+rate.to_symbol+addCommas(to)+')';
                    }
                    setCookie('currency', currency);
                    setCookie('rate_'+currency, rate.conversion_rate);
                    jQuery(this).html(formatted);
                }
            }
            )
        });
    }
    }