var today;
var serverId,currId,lcId,ggId,gwURL;//版本，理财ID，公告ID，官网URL
var page = 1;
var isEnd = false;//数据是否加载完
var isloading = false;//是否正在加载数据
var $todayBox,$historyBox;
var menu;

$(function(){
    $todayBox = $('#todayBox');
    $historyBox = $('#historyBox');
    var index = parseInt(getQueryString("id"))-1; 
    $('#menu a:eq('+index+')').click();  
    init();
    $(window).resize(function() {
        init();
    });
    //滚动条事件
    var $scroll = $('.left-list');     
    $scroll.scroll(function(){  
        var height = $(this)[0].offsetHeight;
        var scrollHeight = $(this)[0].scrollHeight;
        var scrollTop = $(this).scrollTop(); 
        if(height + scrollTop >= scrollHeight -20){
            if(!isEnd && !isloading) {
                isloading = true;
                getData(getQueryString("id"));
            }
        }
    });
})

function init(){
  var h = $(this).height() - 33;        
  $('.voices-wrap').css('height', h+'px')
}
Date.prototype.format = function(format) {
       var date = {
              "M+": this.getMonth() + 1,
              "d+": this.getDate(),
              "h+": this.getHours(),
              "m+": this.getMinutes(),
              "s+": this.getSeconds(),
              "q+": Math.floor((this.getMonth() + 3) / 3),
              "S+": this.getMilliseconds()
       };
       if (/(y+)/i.test(format)) {
              format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
       }
       for (var k in date) {
              if (new RegExp("(" + k + ")").test(format)) {
                     format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
              }
       }
       return format;
}
function list(cId, obj){
    isEnd = false;
    page = 1;
    $todayBox.hide();
    var $iframe = $('#iframe_layout');
    var $list = $('#list_layout');
    var isIframe = false;

    $('#menu .stop-li').removeClass('stop-li');
    $('#menu .stop-li2').removeClass('stop-li2');
    if($(obj).parent().attr('class') == 'nav0411'){
        $(obj).parent().addClass('stop-li2');
    }else{
        $(obj).parent().addClass('stop-li');
    }
  
        currId = cId;  
    var $loading = $('.loading'); 
    $loading.css('margin-bottom','0px'); 

    if(isIframe){
        $iframe.show();
        $list.hide();
    }else{
        $iframe.hide();
        $list.show();

        getData(cId);
    }

    $('.left-list').animate({ "scrollTop": 0 }, 100);
   
}
function getData(serverId){
 // if(currId == 'tanchuang'){
    getAlertList(serverId, page)
  //}else{
   // content(currId, page);
  //}
}
function showLoading(){
    $('.loading').html('<img src="images/load.gif"/>正在加载...').show();
}
function hideLoading(){
    $('.loading').hide();
}
function showloaded(){
    $('.loading').html('已加载全部数据').show();
}

function content(id) {  
	url='http://newoem.nbj.cc/newoem/appimp.php/Appimp/getcpcontent/callback/a/id/'+id; 
    $.get(url, function(datas) {  
		if(datas.length>0){ 
		$("#title").html(datas[0].title);
		var d=datas[0].addtime*1000;
		var newDate = new Date();
		newDate.setTime(d);
		$("#dt").html(newDate.format('yyyy-MM-dd hh:mm:ss'));
		$("#content").html(datas[0].neirong);
		}
    },"jsonp");
	return false;
}

function getAlertList(serverId, p) {
  showLoading(); 
 url='http://newoem.nbj.cc/newoem/appimp.php/Appimp/getcpnews/callback/a/type/'+serverId;
    $.get(url, function(datas) {   
		 var tHtm = [];  
		for(var i=0; i<datas.length;i++){
			var c = datas[i];   
			if(i==0)
				content(c.id);
			var d=c.addtime*1000;
		var newDate = new Date();
          newDate.setTime(d);
 
			var str = li(c.id, c.title, newDate, "MM-dd");
			  tHtm.push(str); 
		 }
		 $('#historyBox').html(tHtm.join(''));
		 $('#historyBox').find("li").click(function(){content($(this).attr("lid"));});
		  hideLoading();
  },"jsonp");
}
 
function li(id, title, date, format){
	// onclick="javascript:content('+id+')"
  return '<li lid='+id+'><div>'+title+'</div><p>'+new Date(date).format(format)+'</p></li>';
}

function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}