$(window).load(function(){
    var $rates = new Rates(24);
    $rates.Init();
});

//Rates component on the home page
function Rates($rateSearchStringLength){
	this.$rateSearchStringLength = $rateSearchStringLength;
	var $self = this;
    var $ajaxURI = '/main/rates_ajax.php';
    var $selectSource = $("#selRateSource");
    var $selectDestination = $("#selRateDestination");
    var $divBestRatesSearch = $("#divBestRatesSearch");
    
    var $ajaxLoader = $(document.createElement("img")).attr({src:'/images/myaccount/ajax-loader.gif', border:'0'});
    $ajaxLoader.addClass("ajaxloader");
    
    this.Init = function(){
        this.HandleSelectSourceChange();
        this.HandleSelectDestinationChange();
    };
    
    this.HandleSelectSourceChange = function(){
        $selectSource.change(function(){
            var $source = $selectSource.children('option:selected').val();
            var $destination = $selectDestination.children('option:selected').val();
            
            $self.HandleBestRatesSelectionChange($source, $destination, $divBestRatesSearch);           
        });
    };
    this.HandleSelectDestinationChange = function(){
        $selectDestination.change(function(){
            var $source = $selectSource.children('option:selected').val();
            var $destination = $selectDestination.children('option:selected').val();
            
            $self.HandleBestRatesSelectionChange($source, $destination, $divBestRatesSearch);           
        });
    };
    
    this.HandleBestRatesSelectionChange = function($source, $destination, $target){
        if($destination != 0){
            $self.GetBestRates($source, $destination, $target);
        }else{
            return "Invalid Destination!";
        }
    };
    
    this.GetBestRates = function($sourceId, $destinationId, $target){
        $.ajax({
            type:'GET',
            url:$ajaxURI,
            data:({
                func:'getBestRates',
                source:$sourceId,
                destination:$destinationId
            }),
            beforeSend:function(){
                this.tempLoader = $ajaxLoader;
                $target.append(this.tempLoader);
            },
            complete:function(){
                this.tempLoader.remove();
            },
            cache:false,
            success:function(data){
                var $obj = jQuery.parseJSON(data);
                delete(data);
                $ratesDiv = "";
                
                if($obj!=null){
                    var $countryCode = "";
                    var $destination = "";
                    if($.isArray($obj)){
                        $.each($obj, function(i,item){
                            if($countryCode == "") $countryCode = item.country_code;
                            if($destination == "") $destination = item.destination;
                            $ratesDiv += "<div class=\"ratesrow\">";
                            $ratesDiv += "<a class=\"rateslink\" href=\"/rates/" + $destination.replace(/\s*/g, '') + "/\">";
                            $ratesDiv += "<span class=\"flag-20 " + $countryCode + "20\"></span>";
                            if(item.destination.length < $self.$rateSearchStringLength){
                            	$ratesDiv += item.destination;
                            }else{
                                $tempDestination = item.destination.substring(0, $self.$rateSearchStringLength);
                                $nextSpace = $tempDestination.lastIndexOf(' ');
                                $ratesDiv += $tempDestination.substring(0, $nextSpace);    
                            }
                            $ratesDiv += "</a>";
                            $ratesDiv += "<span class=\"ratecol\">&#36;" + item.rate + "</span>";
                            $ratesDiv += "</div>";
                        });
                    }else{
                        if($countryCode == "") $countryCode = $obj.country_code;
                        if($destination == "") $destination = $obj.destination;
                        $ratesDiv += "<div class=\"ratesrow\">";
                        $ratesDiv += "<a class=\"rateslink\" href=\"/rates/" + $destination.replace(/\s*/g, '') + "/\">";
                        $ratesDiv += "<span class=\"flag-20 " + $countryCode + "20\"></span>";
                        $ratesDiv += $obj.destination;
                        $ratesDiv += "</a>";
                        $ratesDiv += "<span class=\"ratecol\">&#36;" + $obj.rate + "</span>";
                        $ratesDiv += "</div>";
                    }
                }
                $target.html($ratesDiv);
            }
        });
    };
};

