/*!
 * jQuery xmlDOM Plugin v1.0
 * http://outwestmedia.com/jquery-plugins/xmldom/
 *
 * Released: 2009-04-06
 * Version: 1.0
 *
 * Copyright (c) 2009 Jonathan Sharp, Out West Media LLC.
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 */

//------------------------------
    //XML DOM
    //------------------------------
(function($) {
  // IE DOMParser wrapper
  if ( window['DOMParser'] == undefined && window.ActiveXObject ) {
    DOMParser = function() { };
    DOMParser.prototype.parseFromString = function( xmlString ) {
      var doc = new ActiveXObject('Microsoft.XMLDOM');
          doc.async = 'false';
          doc.loadXML( xmlString );
      return doc;
    };
  }
  
  $.xmlDOM = function(xml, onErrorFn) {
    try {
      var xmlDoc   = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
      if ( $.isXMLDoc( xmlDoc ) ) {
        var err = $('parsererror', xmlDoc);
        if ( err.length == 1 ) {
          throw('Error: ' + $(xmlDoc).text() );
        }
      } else {
        throw('Unable to parse XML');
      }
    } catch( e ) {
      var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
      if ( $.isFunction( onErrorFn ) ) {
        onErrorFn( msg );
      } else {
        $(document).trigger('xmlParseError', [ msg ]);
      }
      return $([]);
    }
    return $( xmlDoc );
  };
})(jQuery);    

  //------------------------------
    //Cross Domain Call
    //------------------------------
    // Accepts a url and a callback function to run.  
    function requestCrossDomain( site, callback ) {  
      
        // If no url was passed, exit.  
        if ( !site ) {  
            alert('No site was passed.');  
            return false;
        }  
      
       // Take the provided url, and add it to a YQL query. Make sure you encode it!  
       var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';  
     
       // Request that YSQL string, and run a callback function.  
       // Pass a defined function to prevent cache-busting.  
       $.getJSON( yql, cbFunc );  
     
       function cbFunc(data) {  
       // If we have something to work with...  
       if ( data.results[0] ) {  
           // Strip out all script tags, for security reasons.  
           // BE VERY CAREFUL. This helps, but we should do more.  
           data = data.results[0]//.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');  
     
           // If the user passed a callback, and it  
           // is a function, call it, and send through the data var.  
           if ( typeof callback === 'function') {  
               callback(data);  
           }  
       }  
       // Else, Maybe we requested a site that doesn't exist, and nothing returned.  
       else throw new Error('Nothing returned from getJSON.');  
       }  
   };

    //------------------------------
    //refresh Gold Price
    //------------------------------
  function refreshGoldPrice(){
    if($("#goldChart").css("display")=="none"){$("#goldChart").css("display","block");}
    var img = new Image();
    $(img).load(function () {
    $('#chartLoader').empty();
      $(this).hide();
      $('#chartLoader').removeClass('loading').append(this);
      $(this).fadeIn();
    })
    .error(function () {$("#goldChart").css("display","none");})
    .attr('src', 'http://www.kitconet.com/images/live/s_gold.gif');
    var currentDate = new Date();
    var currentTime=currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
    $("#currentTime").html(currentTime);
    var t=setTimeout(refreshGoldPrice,120000);
  };

    //------------------------------
    //currency Row
    //------------------------------
                       
 function currencyRow(item){
  var returnName;
  var reverse=false;
  var returnValue="";
  var name= $(item).find("title").text();
  var price=$(item).find("description").text().split(" ")[4];
  switch(name)
  {
    case "EUR/USD":
      returnName="یورو به دلار آمریکا"
      reverse=true; 
    break;
    case "GBP/USD":
      returnName="پوند به دلار آمریکا"
      reverse=true; 
    break;
    case "AUD/USD":
      returnName="دلار استرالیا به دلار آمریکا"
      reverse=true; 
    break;
    case "NZD/USD":
      returnName="دلار نیوزلند به دلار آمریکا"
      reverse=true; 
    break;
    case "CAD/USD":
      returnName="دلار آمریکا به دلار کانادا";
      reverse=false;
    break;
    case "CHF/USD":
      returnName="دلار آمریکا به فرانک سوئیس"
      reverse=false;
    break;
    case "JPY/USD":
      returnName="دلار آمریکا به ین ژاپن";
      reverse=false; 
    break;
    case "RMB/USD":
      returnName="دلار آمریکا به یوان چین";
      reverse=false;
    break;
    case "INR/USD":
      returnName="دلار آمریکا به روپیه هند"
      reverse=false;
    break;
    case "CNY/USD":
      returnName="یوان چین به دلار آمریکا"
      reverse=true;
    break;
    case "SGD/USD":
      returnName="دلار آمریکا به دلار سنگاپور"
      reverse=false;
    break;
    case "HKD/USD":
      returnName="دلار آمریکا به دلار هنگ کنگ"
      reverse=false;
    break;
    case "NOK/USD":
      returnName="دلار آمریکا به کرون نروژ"
      reverse=false;
    break;
    case "SEK/USD":
      returnName="دلار آمریکا به کرون سوئد "
      reverse=false;
    break;
    case "DKK/USD":
      returnName="دلار آمریکا به کرون دانمارک"
      reverse=false;
    break;
    case "ZAR/USD":
      returnName=" دلار آمریکا به رند آفریقای جنوبی"
      reverse=false;
    break;
    case "MXN/USD":
      returnName="دلار آمریکا به پزوی مکزیک"
      reverse=false;
    break;
    case "TRY/USD":
      returnName="دلار آمریکا به لیر ترکیه"
      reverse=false;
    break;
    default:
        returnName=""
    break;
  }
  if(returnName!=""){
    var tname=name.split("/");
    var cc=tname[0].substr(0,2).toLowerCase();
    if (reverse==true){
      price=1/price;
      price=Math.round(price*10000)/10000;
    }else{
      name=tname[1]+"/"+tname[0];
    };
    returnValue="<td class='flags'><img src='/data/bexResources/Images/blank.gif' width='16' height='11' class='flag , flag-" + cc + "' /></td><td class='name'>" + returnName + "</td><td class='symb'>" + name + "</td><td class='price'>" + price + "</td>"
    }
  return returnValue;
   }
    
    //------------------------------
    //end of currency Row
    //------------------------------
    
    //------------------------------
    //refresh Gold Price
    //------------------------------
  function refreshCurrencyPrice(){
  var mytable="<table class='miniPriceTable' cellspacing='0' cellpadding='0'>";//<thead><tr><th>عنوان</th><th>نشان ارز</th><th>قیمت نسبت به دلار</th></tr></thead><tbody>";
    
  requestCrossDomain('http://themoneyconverter.com/USD/rss.xml', function(xml) {
   var oxml=$.xmlDOM(xml);
    var i=1;
    $(oxml).find("item").each(function(){
      var newRow=currencyRow($(this));
      if (newRow!=""){
        var myclass = "odd";
        if(i%2==0) {myclass = "even"};
        mytable += "<tr class='"+ myclass +"'>"+ newRow + "</tr>";
        i++;
      };
      
      
      });
      mytable +="</table>";
      $("#currencyTable").html(mytable);
    });
      var t=setTimeout(refreshGoldPrice,180000);
  };
                       
   $(function(){
      refreshGoldPrice();
      refreshCurrencyPrice();
     
     $("#nav a").hover(
      function(){
        $(this).stop(true, true).animate({backgroundPosition: '10%'},500);  
      },
      function(){
        $(this).stop(true, true).animate({backgroundPosition: '100%'},500);  
      }
    )
   })

