var ihsg_timeout;
var index_timeout;

function load_data(u, s, c, e) {
    if(typeof s == "undefined") {
        s = function() {};
    }
	
    if(typeof c == "undefined") {
        c = function() {};
    }
	
    if(typeof e == "undefined") {
        e = function() {};
    }
	
    jQuery.ajax({
        dataType: 'json',
        url: u,		
        success: s,		
        error: e,
        complete: c
    });
}


function ShowLoading() {
    document.getElementById('content_stok').style.opacity=0;
//document.getElementById("content_stok").innerHTML="<div align='center'><img src='images/loading.gif' width=20 align=middle></div>";
}
function label_bold(lb){
    if(lb=='ma'){
        krt_ma='<strong>Most Active</strong>';
    }else{
        krt_ma='<a href=\'javascript:most_active(\"ma\");\'>Most Active</a>';
    }
    if(lb=='tg'){
        krt_tg='<strong>Top Gainer</strong>';
    }else{
        krt_tg='<a href=\'javascript:top_gainer(\"tg\");\'>Top Gainer</a>';
    }
    if(lb=='tv'){
        krt_tv='<strong>Top Value</strong>';
    }else{
        krt_tv='<a href=\'javascript:top_value(\"tv\");\'>Top Value</a>';
    }
    if(lb=='tl'){
        krt_tl='<strong>Top Loser</strong>';
    }else{
        krt_tl='<a href=\'javascript:top_looser(\"tl\");\'>Top Loser</a>';
    }
    document.getElementById("ma").innerHTML=krt_ma;
    document.getElementById("tg").innerHTML=krt_tg;
    document.getElementById("tv").innerHTML=krt_tv;
    document.getElementById("tl").innerHTML=krt_tl;
}

function most_active(lb){
    //label_bold(lb);
    load_indexes();
    document.getElementById("ma").style.display="";
    document.getElementById("tg").style.display="none";
    document.getElementById("tv").style.display="none";
    document.getElementById("tl").style.display="none";
    document.getElementById("ma_index").style.display="";
    document.getElementById("tg_index").style.display="none";
    document.getElementById("tv_index").style.display="none";
    document.getElementById("tl_index").style.display="none";
//ShowLoading();
//$('#content_stok').load('ajax_most_active.php');
}
function top_gainer(lb){
    load_indexes();
    document.getElementById("ma").style.display="none";
    document.getElementById("tg").style.display="";
    document.getElementById("tv").style.display="none";
    document.getElementById("tl").style.display="none";
    document.getElementById("ma_index").style.display="none";
    document.getElementById("tg_index").style.display="";
    document.getElementById("tv_index").style.display="none";
    document.getElementById("tl_index").style.display="none";
//ShowLoading();
//$('#content_stok').load('ajax_top_gainer.php');
}
function top_value(lb){
    load_indexes();
    document.getElementById("ma").style.display="none";
    document.getElementById("tg").style.display="none";
    document.getElementById("tv").style.display="";
    document.getElementById("tl").style.display="none";
    document.getElementById("ma_index").style.display="none";
    document.getElementById("tg_index").style.display="none";
    document.getElementById("tv_index").style.display="";
    document.getElementById("tl_index").style.display="none";
//ShowLoading();
//$('#content_stok').load('ajax_top_value.php');
}
function top_looser(lb){
    load_indexes();
    document.getElementById("ma").style.display="none";
    document.getElementById("tg").style.display="none";
    document.getElementById("tv").style.display="none";
    document.getElementById("tl").style.display="";
    document.getElementById("ma_index").style.display="none";
    document.getElementById("tg_index").style.display="none";
    document.getElementById("tv_index").style.display="none";
    document.getElementById("tl_index").style.display="";
//ShowLoading();
//$('#content_stok').load('ajax_top_looser.php');
}

function ShowLoading_index() {
    document.getElementById('content_index').style.opacity=0;
//document.getElementById("content_index").innerHTML="<div align='center'><img src='images/loading.gif' width=20 align=middle></div>";
}
function label_bold_index(lb){
    if(lb=='wi'){
        krt_wi='<strong>World indices</strong>';
    }else{
        krt_wi='<a href=\'javascript:world_indices(\"wi\");\'>World indices</a>';
    }
    if(lb=='ii'){
        krt_ii='<strong>IDX indices</strong>';
    }else{
        krt_ii='<a href=\'javascript:idx_indices(\"ii\");\'>IDX indices</a>';
    }
    if(lb=='cr'){
        krt_cr='<strong>Currency</strong>';
    }else{
        krt_cr='<a href=\'javascript:currency(\"cr\");\'>Currency</a>';
    }
    if(lb=='cm'){
        krt_cm='<strong>Commodity</strong>';
    }else{
        krt_cm='<a href=\'javascript:comodity(\"cm\");\'>Commodity</a>';
    }
    document.getElementById("wi").innerHTML=krt_wi;
    document.getElementById("ii").innerHTML=krt_ii;
    document.getElementById("cr").innerHTML=krt_cr;
    document.getElementById("cm").innerHTML=krt_cm;
}
function world_indices(lb){
    load_indexes();
    document.getElementById("wi").style.display="";
    document.getElementById("ii").style.display="none";
    document.getElementById("cr").style.display="none";
    document.getElementById("cm").style.display="none";
    document.getElementById("wi_index").style.display="";
    document.getElementById("ii_index").style.display="none";
    document.getElementById("cu_index").style.display="none";
    document.getElementById("co_index").style.display="none";
//ShowLoading_index();
//$('#content_index').load('ajax_world_indices.php');
}
function idx_indices(lb){
    load_indexes();
    document.getElementById("wi").style.display="none";
    document.getElementById("ii").style.display="";
    document.getElementById("cr").style.display="none";
    document.getElementById("cm").style.display="none";
    document.getElementById("wi_index").style.display="none";
    document.getElementById("ii_index").style.display="";
    document.getElementById("cu_index").style.display="none";
    document.getElementById("co_index").style.display="none";
//label_bold_index(lb);
//ShowLoading_index();
//$('#content_index').load('ajax_idx_indices.php');
}
function currency(lb){
    load_indexes();
    document.getElementById("wi").style.display="none";
    document.getElementById("ii").style.display="none";
    document.getElementById("cr").style.display="";
    document.getElementById("cm").style.display="none";
    document.getElementById("wi_index").style.display="none";
    document.getElementById("ii_index").style.display="none";
    document.getElementById("cu_index").style.display="";
    document.getElementById("co_index").style.display="none";
//ShowLoading_index();
//$('#content_index').load('ajax_currency.php');
}
function comodity(lb){
    //label_bold_index(lb);
    load_indexes();
    document.getElementById("wi").style.display="none";
    document.getElementById("ii").style.display="none";
    document.getElementById("cr").style.display="none";
    document.getElementById("cm").style.display="";
    document.getElementById("wi_index").style.display="none";
    document.getElementById("ii_index").style.display="none";
    document.getElementById("cu_index").style.display="none";
    document.getElementById("co_index").style.display="";
//ShowLoading_index();
//$('#content_index').load('ajax_comodity.php');
}

function ShowLoadingIHSG() {
    document.getElementById("load_isi_ihsg").innerHTML="<div class=\"ihsg_value2\"><div class='ihsg_left2'>Get Latest Quotes</div> <div class='ihsg_right2'><form onsubmit=\"load_ihsg()\"><input class=\"textbox100\" id=\"isi_ihsg\" type=\"text\" value=\"\" /> </form><a href=\"javascript:load_ihsg();\" class=\"arr_go\"></a></div></div><div class=\"ihsg_today_detil_new\"><div class=\"ihsg_title2_new\"></div><div class=\"ihsg_value3_new\"><div class=\"ihsg_dcols_new\"> <div class='ihsg_left3'>Last</div> <div class='ihsg_right3'>:</div></div><div class=\"ihsg_dcols_new\"><div class='ihsg_left3'>Change</div> <div class='ihsg_right3'>:</div></div><div class=\"ihsg_dcols_new\"> <div class='ihsg_left3'>Volume</div> <div class='ihsg_right3'>: </div> </div><div class=\"ihsg_dcols_new\"> <div class='ihsg_left3'>Value</div> <div class='ihsg_right3'>:</div> </div><div class=\"ihsg_dcols_new\"><div class='ihsg_left3'>Open</div><div class='ihsg_right3'>:</div></div><div class=\"ihsg_dcols_new\"><div class='ihsg_left3'>High</div><div class='ihsg_right3'>:</div></div><div class=\"ihsg_dcols_new\"><div class='ihsg_left3'>Low</div><div class='ihsg_right3'>:</div></div><div class=\"ihsg_dcols_new\"> <div class='ihsg_left3'>Updated</div> <div class='ihsg_right3'>: </div> </div></div></div>";
//document.getElementById("load_isi_ihsg").innerHTML="<div align='center'><img src='images/loading.gif' width=20 align=middle></div>";
}

function load_ihsg_auto(field){
    ShowLoadingIHSG();
    $('#load_isi_ihsg').load('aajax_ihsg.php?id='+field);
}

function ajax_load_ihsg(field) {
    clearTimeout(ihsg_timeout);
    
    load_data('aajax_ihsg.php?id='+field, function(data) {
        for(var k in data) {
            var html = k == 'nama' ? "" : ": ";
            html += data[k];
            jQuery('#ihsg_'+k+'_val').html(html);
        }
    }, function(){
        ihsg_timeout = setTimeout("ajax_load_ihsg('"+field+"')", 60000);
    });
}

function load_ihsg(e){
    if(e!=null) {
        e.preventDefault();
        e.stopPropagation();
    }
	
    var field = document.getElementById('isi_ihsg').value;
    document.getElementById('isi_ihsg').value = field.toUpperCase();
	
    if(field==""){
        alert('Fill text');
    }else{
        ajax_load_ihsg(field);	
    }
    
    jQuery('#isi_ihsg').focus();
    jQuery('#isi_ihsg').select();
	
    return false;
}

function load_indexes() {
    clearTimeout(index_timeout);
    
    load_data('ajax_json.php?'+((new Date()).getTime()), function(data) {
        for(var k in data) {
            jQuery('#'+k+'_index_updated_val').html(data[k]['updated']);
            
            for(var l in data[k]['list']) {
                for(var m in data[k]['list'][l]) {
                    jQuery('#'+k+'_index_'+l+'_'+m+'_val').html(data[k]['list'][l][m]);
                }
            }
        }
    }, function(){
        index_timeout = setTimeout("load_indexes", 60000);
    });
}

$(function() {                
    ihsg_timeout = setTimeout("ajax_load_ihsg('AALI')", 60000);
    index_timeout = setTimeout("load_indexes", 60000);
});

