$(function(){
    iframeHeight=162;  
    page=0;
    pageNum=6;
    $iframeName=$("iframe[name='newsIframe']");
    iframeLoadObject=new iframeLoad($iframeName,__initURL__);
    iframeLoadObject.initIframe(__initURL__); 
          
    started=false;
    /**
    * start up and down button
    */
    var overf=function(){
       $(this).attr("src","/news/index/scroll_"+$(this).attr("title")+"_green.gif");        
    };
    
    var outf=function(){
       $(this).attr("src","/news/index/scroll_"+$(this).attr("title")+"_gray.gif");    
    }
    
    _timeInterval_up=_timeInterval_down=null;
    var outFun=function(){
        if(_timeInterval_up){
            clearInterval(_timeInterval_up);
            _timeInterval_up=null;
        }
    }
    
    $("img[title='up']").hover(overf,outf).bind("mousedown",
        function(){
            iframeLoadObject.moveUp();
            //_timeInterval_up=setInterval("iframeLoadObject.moveUp()",200);
        }
    ).bind("mouseup",outFun).bind("mouseout",outFun);

    
    outFun=function(){
        if(_timeInterval_down){
            clearInterval(_timeInterval_down);
            _timeInterval_down=null;
        }
    };
    
    $("img[title='down']").hover(overf,outf).bind("mousedown",
        function(){
            iframeLoadObject.moveDown();
            //_timeInterval_down=setInterval("iframeLoadObject.moveDown()",200);
        }
    ).bind("mouseup",outFun).bind("mouseout",outFun);
    /**
    * end up and down button
    */
    $("#new_ul_header li[action='extend'] img").click(function(){
        if($iframeName.height()>0){
            if($iframeName.height()==162){
                $(this).attr("src","/news/index/action_remove.gif");
                iframeLoadObject.expandIframeHeight(324);    
            }else{
                $(this).attr("src","/news/index/action_add.gif");
                iframeLoadObject.expandIframeHeight(162);
            }
        }
    });    
    
    
    $("#new_ul_header li[action!='extend']").click(function(){
        $this=$(this);  
        if($this.hasClass("toggle_overed")){
            var action= $this.attr("action");
            closeTab(action);
        }else{
            var action= $this.removeClass().addClass("toggle_overed").attr("action");
            $this.siblings("li[action!='extend']").removeClass().addClass("toggle_out").each(function(){
                //console.log($(this).attr("action"),this);
            });
            $("#"+action).addClass("divcolor").show().siblings("div[id]").hide();
            started=true;  
        }
    }).hover(
        function(){
           $this=$(this); 
           if(!$this.hasClass('toggle_overed')){
               if(started){ 
                   $this.removeClass("toggle_out");
                   $this.addClass("toggle_over");  
               }else{
                   $this.removeClass();
                   $this.addClass("initover");     
               }
  
           } 
        },
        function(){
           $this=$(this); 
           if(!$this.hasClass('toggle_overed')){
               if(started){
                   $this.removeClass("toggle_over");
                   $this.addClass("toggle_out");  
               }else{
                   $this.removeClass(); 
                   $this.addClass("init");
               }
           } 
        }
    )
    
    
    //连接点击
    $("li span[url]").click(function(){
        $this=$(this);
        var url=$this.attr("url");
        var parentDivId=$this.parents("div[id]").attr("id");
        $(".header li[action='"+parentDivId+"']").html($this.html()).attr("param",url);
        gotoURL(parentDivId);
    });  

    function gotoURL(id){
       var params=$(".header li[action][action!='extend']").removeClass().addClass("init").map(function(i,e){
           var param=$(e).attr("param"); 
           if(typeof param!=undefined){
              return param; 
           }    
       }).get().join('&');
       closeTab(id);
       page=0;
       iframeLoadObject.initIframe(__URL__+"&"+params); 
    }
    
    function closeTab(id){
      $(".header li[action!='extend']").removeClass().addClass("init");
      started=false;
      $("#"+id).hide();  
    }
});
//class
function iframeLoad($iframeName,initURL){
    this.initUrl=initURL;
    this.iframeName=$iframeName;
    this.initIframe=function(){
        var url=this.initUrl;
        if(this.initIframe.arguments.length>0){
            url=this.initIframe.arguments[0];
        }
/*        var data=$("body:first").data(url);
        if(typeof data=="string"){
            this.iframeName.contents().find("body").html(data);
            return;
        }*/
        var thisobj=this;
        this.iframeName.height(0).attr("src",url).bind("load",function(){
            $(this).height(iframeHeight);
            $(this).next("div").hide();
            iframeHeight==324 && thisobj.expandWidth();
            $(this).unbind("load");
            /*
            var data=$.trim($(this).contents().find("body").html());
            if(data){
                $("body:first").data(url,data);   
            }
            */
        }).next("div").show();
    }
    
    this.expandWidth=function(){
        var addwidth=237;//+3
        this.liWidth= iframeHeight==324?(17+addwidth):17;
        this.bgWidth= iframeHeight==324?(522+addwidth):522;
        this.tableWidth= iframeHeight==324?(502+addwidth):500;
        this.tdWidth= iframeHeight==324?(364+addwidth-2):364;
        this.divWidth= iframeHeight==324?(520+addwidth):520;
        $("#newsParent").width(iframeWidth+17);
        $("li.extend").width(this.liWidth);
        $("div.bg").width(this.bgWidth);
        $("div.bg div[id]").width(this.divWidth);
        
        this.iframeName.contents().find("table[page='true']").width(this.tableWidth);
        this.iframeName.contents().find("table[page='true'] tr td[width='364']").each(function(i,e){
            var $a=$("a",this);
            var temp=$a.attr('title');
            $a.attr('title',$a.html());
            $a.html(temp);
        }).width(this.tdWidth);
    }
    
    this.expandIframeHeight=function(height){
        iframeHeight= height==324?324:162;
        iframeWidth= height==324?743:500;
        

        /*
        if(iframeHeight==324){
           $(".update").hide(); 
        }else{
           $(".update").show(); 
        }
        */
        
        this.iframeName.height(iframeHeight);
        this.iframeName.width(iframeWidth);
        this.expandWidth();
    }  
    
    this.moveUp=function(){
        var iframe1=this.iframeName.contents().find("body")[0];
        page--;
        if(page<0){
           page=0; 
        } 
        var wheretr=page*pageNum; 
        var size=  this.iframeName.contents().find("table[page='true'] tr").size(); 
        if(wheretr<size&&wheretr>=0){ 
            iframe1.scrollTop=this.iframeName.contents().find("table[page='true'] tr").eq(wheretr).offset().top;  
        } 
    } 
    
    this.moveDown=function(){
        var iframe1=this.iframeName.contents().find("body")[0];  
        page++;    

        var size=  this.iframeName.contents().find("table[page='true'] tr").size();
        var totalPage=Math.ceil(size/pageNum)-1;
        if(page>totalPage){
            page=totalPage;
        } 
        var wheretr=page*pageNum; 
        if(wheretr<size&&wheretr>=0){
           iframe1.scrollTop=this.iframeName.contents().find("table[page='true'] tr").eq(wheretr).offset().top;      
        }
        
    }
}
