var nv = new Object();

V = function(o){ return typeof(o) == 'number' ? true : !(o == undefined || o == null || o == ''); }
T = function(f,t){ return window.setTimeout(f,t); }
E = function(s){ return eval(s); }
RN = function() { return Math.ceil(Math.random() * 1000) * 1000;}

var data = new Array();

$ = function(objectID)
{
    if(document.getElementById && document.getElementById(objectID)) return document.getElementById(objectID);
    else if (document.all && document.all(objectID)) return document.all(objectID);
    else if (document.layers && document.layers[objectID]) return document.layers[objectID];
    else return false;
}

nv.Contents = function(name, id_txt, id_img, loop_element, start_idx)
{
    this.name       = name;
    this.data       = null;
    this.image_idx  = 0;
    this.idx        = V(start_idx) ? (V(start_idx[1])?start_idx[1]:start_idx[0]) : 0;
    this.image_idx	= V(start_idx) ? start_idx[0] : 0;
    this.items      = new Object();
    this.parent_id  = id_txt;
    this.list_tag   = loop_element;
    this.isBound    = false;
    this.url        = "";
    this.binddelay  = 400;
    this.xmlhttp 	= null;
    
    this.callback_func_panel = function(){};
    
    this.__create = function() {
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e1)
        {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e2) 
            {
                try {
                    this.xmlhttp = new XMLHttpRequest();
                } catch(e3) {
                    this.xmlhttp = false;
                }
            }
        }
        
        return this.xmlhttp;
    }
    
    this.__bind = function(target,data) {
        try {
            $(target).innerHTML = data;
        } catch(e) 
        {
            alert(e.description);
        }
    }
    
    this.load = function(url, target, callback_func) {
        if(!V(callback_func)){callback_func = this.__bind;}
        
        var xmlhttp = this.xmlhttp = this.__create();
        
        xmlhttp.onreadystatechange = function() {
            if(xmlhttp.readyState == 4) {
                if(xmlhttp.status == 200) {
                    var contents = xmlhttp.responseText;
                    callback_func(target, contents);
                }
            }
        }
        
        url = url + "?" + RN();
        this.xmlhttp.open("GET", url, true);
        this.xmlhttp.send(null);
    }
    
    this.callback_func_item = function(obj, data){ obj.innerHTML = data[0]; }
    
    this.callback_func_json = function(obj, responseText) {
        try {
            obj.data = E(responseText);
            obj.items_image = new Array();
            
            if(obj.data[0].IMAGE != undefined || obj.data[0].IMAGE != null) {
                for(var i = 0 ; i < obj.data[0].IMAGE.length ; i++){
                    obj.items_image[i] = obj.data[0].IMAGE[i];
                }
            }
            
            obj.items = new Array();
            
            for(var i = ((obj.name == 'nvvideo') ? 0 : 1),k=0 ; i < obj.data.length ; i++,k++) {
                //alert(obj.data[i].DATA);
                //alert(k);
                obj.items[k] = obj.data[i];
            }
            
        } catch(e) {
            alert("error : " + e.description);
        }
    }
    
    this.addurl = function(url)
    {
        if(!this.isBound && V(url)) {
            this.url = url;
            this.load(url, this, this.callback_func_json);
            this.isBound = true;
        }
    }
    
    this.next = function() {
        if(!this.isBound)
        {
            this.addurl(this.url);
            T(this.name+".next()", this.binddelay);
            return;
        }
        
        this.idx = (this.idx >= (this.items.length - 1)) ? 0 : ++this.idx;
        this.display(this.idx);
        
        try {
            this.image_idx = (this.image_idx >= (this.items_image.length - 1)) ? 0 : ++this.image_idx;
            $(id_img).innerHTML = this.items_image[ this.image_idx ] ;
        } catch(e) 
        {
            
        }
    }
    
    this.prev = function() {
        if(!this.isBound) {
            this.addurl(this.url);
            T(this.name+".prev()", this.binddelay);
            return;
        }
        
        this.idx = (this.idx <= 0) ? (this.items.length - 1) : --this.idx;
        this.display(this.idx);
        
        try {
            this.image_idx = (this.image_idx <= 0) ? this.items_image.length - 1 : --this.image_idx;
            $(id_img).innerHTML = this.items_image[ this.image_idx ] ;
        } catch(e) 
        {
        
        }
    }
    
    this.seturl = function(url) { 
        this.url = url;
    }
    
    this.show = function() { 
         T(this.name + ".display(" + this.name +".idx)", this.delay); 
    }
    
    this.display = function(idx) {
        try {
            idx = (!V(idx) || idx >= this.items.length || idx < 0) ? 0 : idx;
            
            this.callback_func_panel(this.items[idx]);
            
            var parent = $(this.parent_id);
            var data_idx = 0;
            
            if(!V(parent)) return;
            
            for(var i = 0 ; i < parent.childNodes.length ; i++) {
                if(parent.childNodes[i].tagName == this.list_tag) {
                    var element = parent.childNodes[i];
                    this.callback_func_item(element, this.items[idx].DATA[data_idx++]);
                    
                }
            }
        } catch(e) {  
            //alert('#error : ' + e.description + "\n" + e);
        }
    }
}

var nvsense;
var nvtalk;
var nvstory;
var nvvideo;

function nvcontents(s1, s2, s3, s4)
{
    nvsense	= new nv.Contents("nvsense","nvSenseR","nvSenseI","LI",s1);
    nvtalk	= new nv.Contents("nvtalk","nvTalkR","nvTalkI","LI",s2);
    nvstory = new nv.Contents("nvstory","nvStoryR","nvStoryI","LI",s3);
    nvvideo = new nv.Contents("nvvideo","nvVideo","","DD",s4);
    
    nvsense.seturl("/2008/ajax/center1.json.php");
    nvtalk.seturl("/2008/ajax/center2.json.php");
    nvstory.seturl("/2008/ajax/center3.json.php");
    nvvideo.seturl("/2008/ajax/center4.json.php");
    
    nvsense.show();
    nvtalk.show();
    nvstory.show();
    nvvideo.show();
}

function onLoadComplete()
{
    nvcontents(nvsense_sidx,
               nvtalk_sidx);
}
/*
20100702 backup
function onLoadComplete()
{
    nvcontents(nvsense_sidx,
               nvtalk_sidx,
               nvstory_sidx,
               nvvideo_sidx);
}*/