﻿var Turn = {
    pre: function(obj) {
        if (obj.current == 0) {
            return
        } else {
            this.goto(obj, obj.current - 1)
        }
    },
    next: function(obj) {
        var max = $("#" + obj.div).find("ul").size() - 1;
        if (obj.current >= max) {
            return
        } else {
            this.goto(obj, obj.current + 1)
        }
    },
    goto: function(obj, index) {
        if (obj.current == index) {
            return
        }
        var span = -index;
        obj.current = index;
        $("a[id^=" + obj.a + "]").removeClass("currA");
        $("#" + obj.a + index).addClass("currA");
        if (obj.step > 0) {
            $("#" + obj.div).stop().animate({
                marginLeft: span * obj.step + "px"
            },
            {
                queue: false,
                duration: obj.speed
            })
        }
    }
};

/*通用tab构建函数*/
/***
 * 参数:
 * tabdiv:可传入id,class，或者jquery对象
 * action:mouseover/click
 * html构建时，需要自行在相应元素内添加tabscontainer/tabsmenu/tabmenu/tabcontent类
 **/
var TAB = function(tabdiv,action,onclass,offclass,start) {
  tabdiv = (typeof(tabdiv) == 'string')?$(tabdiv):tabdiv;
  tabdiv = $(tabdiv).eq(0);
  if(!tabdiv || tabdiv.size() == 0) return;

  //设定初始menu
  if (!start || start < 0 || start > tabdiv.find(".tabsmenu .tabmenu").size())start=0;
  tabdiv.find(".tabsmenu .tabmenu").removeClass(onclass).addClass(offclass).eq(start).removeClass(offclass).addClass(onclass);
  tabdiv.find(".tabsmenu .tabmenu").css("cursor","pointer");
  tabdiv.find(".tabcontent").eq(start).show().siblings(".tabcontent").hide();

  action = action?action:'';
  if (action.toLowerCase() == 'click') {
    tabdiv.find(".tabsmenu .tabmenu").click(function(){
      var idx = $(".tabsmenu .tabmenu").index($(this));
      
      tabdiv.find(".tabsmenu .tabmenu").removeClass(onclass).addClass(offclass);
      $(this).removeClass(offclass).addClass(onclass);
      tabdiv.find(".tabcontent").eq(idx).show().siblings(".tabcontent").hide();
    });
  } else {
    tabdiv.find(".tabsmenu .tabmenu").mouseover(function(){
      var idx = tabdiv.find(".tabsmenu .tabmenu").index($(this));

      tabdiv.find(".tabsmenu .tabmenu").removeClass(onclass).addClass(offclass);
      $(this).removeClass(offclass).addClass(onclass);
      tabdiv.find(".tabcontent").eq(idx).show().siblings(".tabcontent").hide();
    });
  }
}

/**通用焦点大小图切换构建函数**/
/*
 * 
 * 需要自行设定类/feature_list/feature_tabs/feature_tab/feature_out
 * */
var FeatureList = function(fobj,options) {
  function feature_slide(nr) {
    if (typeof nr == "undefined") {
      nr = visible_idx + 1;
      nr = nr >= total_items ? 0 : nr;
    }

    tabs.removeClass(onclass).addClass(offclass).filter(":eq(" + nr + ")").removeClass(offclass).addClass(onclass);
    output.stop(true, true).filter(":visible").hide(speed);
	    output.filter(":eq(" + nr + ")").show(speed,function() {
	    visible_idx = nr;  
    });
  }

  fobj = (typeof(fobj) == 'string')?$(fobj):fobj;
  fobj = $(fobj).eq(0);
  if(!fobj || fobj.size() == 0) return;

  //轮询间隔，默认2S
  var options      = options || {};
  var visible_idx  = options.startidx || 0;
  var onclass = options.onclass || "current";
  var offclass = options.offclass || "";
  var speed = options.speed || 50;
  options.pause_on_act = options.pause_on_act || "click";
  options.interval  = options.interval  || 2000;

  var tabs = fobj.find(".feature_tabs .feature_tab");
  var output = fobj.find(".feature_out");
  var total_items = tabs.length;
 
  //初始设定
  output.hide().eq( visible_idx ).show();
  tabs.eq( visible_idx ).addClass(onclass);

  if (options.interval > 0) {
    var timer = setInterval(function () {
      feature_slide();
    }, options.interval);
    output.mouseenter(function() {clearInterval( timer );}).mouseleave(function() {clearInterval( timer );timer = setInterval(function () {feature_slide();}, options.interval);});
    if (options.pause_on_act == "mouseover") {
        tabs.mouseenter(function() {
        clearInterval( timer );
        
        var idx = tabs.index($(this));
        feature_slide(idx);
      }).mouseleave(function() {
        clearInterval( timer );
        timer = setInterval(function () {
          feature_slide();
        }, options.interval);
      });
    } else {
        tabs.click(function() {
        clearInterval( timer );
        var idx = tabs.index($(this));
        feature_slide(idx);
      });
    }
  }
}

/**通用tab自动往左滚动的函数**/
/*
 * 
 var ScrollBigPic = new Array() ;
ScrollBigPic[0] = "" ;

var MovieRecom={					
      				bigpic:"#SwitchBigPic",	//大图DIV之ID通用部分
      				step:924,
      				smallpic:"#big_pic_nav",//小图或者小按钮之ID通用部分
					smallpictag:"a",//小图或者小按钮之ID下的标签
      				selectstyle:"num_blue",	//小图或者小按钮被选中之后的CSS
					unselectstyle:"num_gary",//小图或者小按钮未选中之后的CSS
      				pictxt:"",	//配套图片文字
      				totalcount:7,				//图片数量
      				autotimeintval:5000,
      				objname:"MovieRecom",	//对象名称
					prebtn:"",//上一个按钮
					nextbtn:"",//下一个按钮
					eventname:"click" //事件名称
      			};
      BigNews.init(MovieRecom);
 * */
 
//向左滚动
ScrollCrossLeft = {
    interval: 0,
    count: 0,
    duration: 0,
    step: 0,
    srcObj: null,
    callback: null
};
ScrollCrossLeft.doit = function(obj, b, c, d) {
    BigNews.OnScrolling = true;
    var s = ScrollCrossLeft;
    obj.css('marginLeft', cpu(s.count, b, c, d) + 'px');
    s.count++;
    if (s.count == d) {
        clearInterval(s.interval);
        s.count = 0;
        obj.css('marginLeft', b + c + 'px');
        s.callback();
        BigNews.OnScrolling = false;
    }
    function cpu(t, b, c, d) {
        return c * ((t = t / d - 1) * t * t + 1) + b;
    };
}
var B = BigNews = {
    current: 0,
    next: 0,
    scrollInterval: 0,
    autoScroller: 0,
    s: {},
    OnScrolling: false
};
BigNews.turn = function(index, obj) {
    if (BigNews.OnScrolling) {
        return false;
    }
    clearInterval(BigNews.autoScroller);
    BigNews.scroll(index, obj);
}
BigNews.scroll = function(index, obj) {

    var count = 0;
    var step = obj.step;
    var duration = 50;
    var b = BigNews;
    b.next = index;
    if (index != b.current && count > duration / 8) {
        return;
    }
	if (BigNews.OnScrolling) {
        return;
    }
    try {
        clearInterval(BigNews.s.interval);
    } catch(e) {}

    if (obj.pictxt != null && obj.pictxt != "") $(obj.pictxt + "_" + index).css('display', "block");
    var span = -index + b.current;
    var begin_value = $(obj.bigpic).scrollLeft;
    var chang_in_value = span * step + (b.current * step - begin_value);

    //var s=ScrollCrossLeft;
    BigNews.s.duration = duration;
    BigNews.s.callback = cb;
    var beign = parseInt($(obj.bigpic).css("marginLeft")) || 0;
    // load images
    if ($("#bic_pic_" + index).attr('src') == '') {
        try {
            $("#bic_pic_" + index).attr('src', ScrollBigPic[index]);
        } catch(e) {}

    }
    BigNews.s.interval = setInterval(function() {
        BigNews.s.doit($(obj.bigpic), beign, step * span, duration)
    },
    10);

    function cb() {
        BigNews.current = index;
        showTitles(index);
    }
    function showTitles(index) {
        for (i = 0; i < obj.totalcount; i++) {
            if (i == index) {
                $("#big_pic_title_desc_" + i).css('display', "block");
                $("#big_pic_nav_" + i).attr('class', obj.selectstyle);
            } else {
                $("#big_pic_title_desc_" + i).css('display', "none");
                $("#big_pic_nav_" + i).attr('class', obj.unselectstyle);
            }
        }
        // next & pre btn
        if (obj.prebtn != null && obj.prebtn != "" && obj.nextbtn != null && obj.nextbtn != "") {
            if (index == 0) {
                $(obj.prebtn).attr('class', obj.selectstyle);
            } else {
                $(obj.prebtn).attr('class', obj.unselectstyle);
            }
            if (index == obj.totalcount - 1) {
                $(obj.nextbtn).attr('class', obj.selectstyle)
            } else {
                $(obj.nextbtn).attr('class', obj.unselectstyle);
            }
        }
    }
}
BigNews.auto = function(obj) {
    clearInterval(BigNews.autoScroller);
    BigNews.autoScroller = setInterval(function() {
        BigNews.scroll(BigNews.current == (obj.totalcount - 1) ? 0 : BigNews.current + 1, obj);
    },
    obj.autotimeintval);
}
BigNews.pauseSwitch = function() {
    clearTimeout(BigNews.autoScroller);
}
BigNews.showNext = function(current, obj) {
    if (current >= MovieRecom.totalcount - 1) {
        document.body.focus();
        return false;
    } else {
        BigNews.pauseSwitch();
        BigNews.turn(current + 1, obj);
        BigNews.auto(obj);
        document.body.focus();
    }

}
BigNews.showPre = function(current, obj) {
    if (current <= 0) {
        document.body.focus();
        return false;
    } else {
        BigNews.pauseSwitch();
        BigNews.turn(current - 1, obj);
        BigNews.auto(obj);
        document.body.focus();
    }

}
BigNews.init = function(obj) {
    BigNews.s = ScrollCrossLeft;
    $(obj.bigpic).onmouseover = new Function("BigNews.pauseSwitch();");
    $(obj.bigpic).onmouseout = new Function("BigNews.auto(" + obj.objname + ");");
    
    if(obj.eventname==null || obj.eventname=="")
    	obj.eventname="click";
    	
    if (obj.smallpic != null && obj.smallpic != "") {
        $(obj.smallpic + ' ' + obj.smallpictag).each(function(intIndex) {
            $(this).bind(obj.eventname,
            function() {
                setTimeout('BigNews.pauseSwitch();BigNews.turn(' + intIndex + ',' + obj.objname + ');BigNews.auto(' + obj.objname + ');document.body.focus();', 50);
            });
        });
    }
    if (obj.prebtn != null && obj.prebtn != "" && obj.nextbtn != null && obj.nextbtn != "") {
        $(obj.prebtn).bind('click',
        function() {
            setTimeout('BigNews.showPre(BigNews.current,' + obj.objname + ');', 50);
        });
        $(obj.nextbtn).bind('click',
        function() {
            setTimeout('BigNews.showNext(BigNews.current,' + obj.objname + ');', 50);
        });
    }
    BigNews.auto(obj);
}

/* 多张图片自动轮换 **/
/* ex,
	var adobj={scrollbox:'#scrollbox',scrollboxTag:'a',scrollH:240,scrollInterval:3000};						   
	adscroll.init(adobj);
*
**/
var  adscroll={current: 0,count:0,autoScroller: 0,direction:"up"};
adscroll.scroll = function (obj){
	var height=-(obj.scrollH*adscroll.current) + 'px';

	$(obj.scrollbox).animate({marginTop:height},{
		duration:1000,
		complete:function(){
			var tmp = adscroll.current;
			if(adscroll.direction == "up")
				tmp = adscroll.current+1;
			else
				tmp = adscroll.current-1;
			if(tmp<0)
			{
				adscroll.direction = "up";
				adscroll.current = adscroll.current+1;
				}
			else if(tmp>=adscroll.count)
			{
				adscroll.direction = "down";
				adscroll.current = adscroll.current-1;
			}
			else
			{
					adscroll.current = tmp;
			}
			}
	});
};

adscroll.init = function(obj) {
	adscroll.count = $(obj.scrollbox+' '+obj.scrollboxTag).length;
	adscroll.autoScroller = setInterval(function() {adscroll.scroll(obj)},obj.scrollInterval);
	$(obj.scrollbox).hover(function(){
		clearInterval(adscroll.autoScroller);
	},function(){
		adscroll.autoScroller = setInterval(function() {adscroll.scroll(obj)},obj.scrollInterval);
	});
	
};

/***
 * 树形菜单展示,参数:
 * tabdiv:可传入id,class，或者jquery对象
 * action:mouseover/click
 * tway:wink/slide
 * tspeed:fast/slow
 * start:从第几个默认开启
 * html构建时，需要自行在相应元素内添加treescont/treesmenu/treemenu/treecontent类
 **/
var TREES = function(tabdiv,action,onclass,offclass,tway,tspeed,start) {
  var tabString = tabdiv;
  tabdiv = (typeof(tabdiv) == 'string')?$(tabdiv):tabdiv;
  tabdiv = $(tabdiv).eq(0);
  if(!tabdiv || tabdiv.size() == 0) return;
  //设定初始menu
  if (!start || start < 0 || start > tabdiv.find(".treesmenu .treemenu").size())start=0;
  if (!tway)tway="wink";
  if (!tspeed)tspeed="fast";
  tabdiv.find(".treesmenu .treemenu").removeClass(onclass).addClass(offclass).eq(start).removeClass(offclass).addClass(onclass);
  //tabdiv.find(".treesmenu .treemenu").css("cursor","pointer");
  tabdiv.find(".treecontent").eq(start).show().siblings(".treecontent").hide();
  action = action?action:'';
  if (action.toLowerCase() == 'click') {
    tabdiv.find(".treesmenu .treemenu").click(function(){
      var idx = $(tabString+" .treesmenu .treemenu").index($(this));
      tabdiv.find(".treesmenu .treemenu").removeClass(onclass).addClass(offclass);
      $(this).removeClass(offclass).addClass(onclass);
	  if(tway=="wink"){tabdiv.find(".treecontent").hide();tabdiv.find(".treecontent").eq(idx).show();}else{tabdiv.find(".treecontent").slideUp(tspeed);tabdiv.find(".treecontent").eq(idx).slideDown(tspeed);}
    });
  } else {
    tabdiv.find(".treesmenu .treemenu").mouseover(function(){
      var idx = tabdiv.find(".treesmenu .treemenu").index($(this));
      tabdiv.find(".treesmenu .treemenu").removeClass(onclass).addClass(offclass);
      $(this).removeClass(offclass).addClass(onclass);
      if(tway=="wink"){tabdiv.find(".treecontent").hide();tabdiv.find(".treecontent").eq(idx).show();}else{tabdiv.find(".treecontent").slideUp(tspeed);tabdiv.find(".treecontent").eq(idx).slideDown(tspeed);}
    });
  }
}


/***
 * 特殊焦点资讯构造函数,其中带更多切换
 * tabdiv:可传入id,class，或者jquery对
 * action:mouseover/click
 * onclass:选中的样式
 * offclass:未选中的样式
 * start:从第几个默认开启
 * tkey:需要特殊处理的位置
 * html构建时，需要自行在相应元素内添加focusNewtabs/focusNewtab/focusNewtab_b/tabcontent类
*/
var FOCUS = function(tabdiv,action,onclass,offclass,start,tkey) {
  tabdiv = (typeof(tabdiv) == 'string')?$(tabdiv):tabdiv;
  tabdiv = $(tabdiv).eq(0);
  if(!tabdiv || tabdiv.size() == 0) return;
		  
  //设定初始menu
  if (!start || start < 0 || start > tabdiv.find(".focusNewtabs .focusNewtab").size())start=0;
		  
  tabdiv.find(".focusNewtabs .focusNewtab").removeClass(onclass).addClass(offclass).eq(start).removeClass(offclass).addClass(onclass);
  if(typeof(tkey) != 'undefined' || tkey != '') {
    tabdiv.find(".focusNewtabs .focusNewtab").eq(tkey).siblings().removeClass(offclass);
  }
  tabdiv.find(".focusNewtabs .focusNewtab").css("cursor","pointer");
  tabdiv.find(".focusNewtabs .focusNewtab_b").eq(start).show().siblings(".focusNewtab_b").hide();
  tabdiv.find(".tabcontent").eq(start).show().siblings(".tabcontent").hide();
		  
  action = action?action:'';
  if(action.toLowerCase() == 'click') {
    tabdiv.find(".focusNewtabs .focusNewtab").click(function(){
      var idx = $(".focusNewtabs .focusNewtab").index($(this));
      
      tabdiv.find(".focusNewtabs .focusNewtab").removeClass(onclass).addClass(offclass);
      $(this).removeClass(offclass).addClass(onclass);
      if(typeof(tkey) != 'undefined' || tkey != '') {
        tabdiv.find(".focusNewtabs .focusNewtab").eq(tkey).siblings().removeClass(offclass);
      }
      tabdiv.find(".focusNewtabs .focusNewtab_b").eq(idx).show().siblings(".focusNewtab_b").hide();
      tabdiv.find(".tabcontent").eq(idx).show().siblings(".tabcontent").hide();
    });
  } else {
    tabdiv.find(".focusNewtabs .focusNewtab").mouseover(function(){
      var idx = tabdiv.find(".focusNewtabs .focusNewtab").index($(this));

      tabdiv.find(".focusNewtabs .focusNewtab").removeClass(onclass).addClass(offclass);
      $(this).removeClass(offclass).addClass(onclass);
      if(typeof(tkey) != 'undefined' || tkey != '') {
        tabdiv.find(".focusNewtabs .focusNewtab").eq(tkey).siblings().removeClass(offclass);
      }
      tabdiv.find(".focusNewtabs .focusNewtab_b").eq(idx).show().siblings(".focusNewtab_b").hide();
      tabdiv.find(".tabcontent").eq(idx).show().siblings(".tabcontent").hide();
    });
  }
}

/*
*首页热门专题js效果，speed为自动切换的间隔时间
*/
var FOCUSIMG = function(speed) {
	var len = $(".pic_box li").size();
	var speed = speed;
	var index = 1;
	$(".img_pre,.img_nex").hide();
	$(".img_nex").click(function(){
			index++;
			if(index>=len){
				index=0;
			}
			showimg(index);
		});
		
	$(".img_pre").click(function(){
			index--;			
			if(index<0){
				index=len-1;
			}
			showimg(index);
		});	
	$(".pic_box li,.img_nex,.img_pre").hover(function(){
		$(".img_pre,.img_nex").show();
		if (myTime){
			clearInterval(myTime);
		}
	});
		
	$(".pic_box li,.img_nex,.img_pre").mouseleave(function(){
		$(".img_pre,.img_nex").hide();
		myTime = setInterval(function(){
			index++;
			if(index>=len){
				index=0;
			}
			showimg(index);
		},speed);
	});
	//自动开始切换
	var myTime = setInterval(function(){
		showimg(index);
		index++;
		if (index>=len){
			index=0;
		}
	},speed);
	
	function showimg(i){
		$(".pic_box li").css("display","none");
		var ipre = i+1
		$("#pic"+ipre).css("display","block");
	}
}
