use zUtil;

sub barchartsStockHandler
{
    $html = shift;
    $symbol = shift;
    my( @rValue ) = ();
    my( $table, $infoRow, $value, $change, $row );
    my( $done, $date, $link, $headline, $source );
    my( %results, $results );
    my( $headlineIndex, $stockIndex );

    
    print "------------------ barchartsStockHandler ($symbol) --------------------\n";
    print "$html\n";
    #sleep 5;
    #exit 1;
           
    # http://www.jarloo.com/real-time-google-stock-api/
    # look up symbols here, i.e. for nasdaq  https://www.google.com/finance?q=NASDAQ

    $change = "n/a";
    $last = "n/a";


    $close = zUtil::getNthItemNew(1, $html,"<lastPrice","</lastPrice>");
    $close =~ s|<.+?>||g;
    $open = zUtil::getNthItemNew(1, $html,"<open","</open>");
    $open =~ s|<.+?>||g;

    print "open: $open\n";
    print "close: $close\n";
     
    if( $open > 0 ) {
        $close = sprintf("%.2f", $close);
        $open = sprintf("%.2f", $open);

        $change_value = $close - $open;
        if( $change_value >= 0) {
            $change_percent = sprintf("%.2f", (($close - $open)/$close)*100);
            $change = sprintf("+%.2f (%s)", $change_value, $change_percent);
        } else {
            $change_percent = sprintf("%.2f", (($open - $close)/$close)*100);
            $change = sprintf("%.2f (%s)", $change_value, $change_percent);
        }
    } else {
        $change_value = 0;
        $change_percent = 0;
        $change = sprintf("%.2f (%s)", $change_value, $change_percent);
    }
          
    print "change: $change\n";

    push @rValue,
    {
        'change' => $change,
        'last' => $close
    };
    undef( $change );
    undef( $value );

    #exit 1;
    
    return @rValue;
}

return 1;

