var $j = jQuery.noConflict();

function set_sign_tab(obj_id) {
    var user_id = 'ENEWSID';
    if( (window.location.href.indexOf("tw") > 0 && window.location.href.indexOf("tw") < 20 ) || 
        (window.location.href.indexOf("cn") > 0 && window.location.href.indexOf("cn") < 20 ) ||  window.location.href.indexOf("cttnet")  > 6 ){
        user_id = 'CNEWSID';
    }
    if($j.cookie(user_id) && $j.cookie(user_id) != "null" ){
        $j('#sign'+obj_id).hide();
        $j('#signed'+obj_id).show();
        $j('#user_id'+obj_id).html($j.cookie(user_id));
    }else{
        $j('#sign'+obj_id).show();
        $j('#signed'+obj_id).hide();
    }
}

function set_tab(p_class,c_class,select_index) {
    $j(function(){
        var _showTab = select_index;
        $j(p_class).eq(_showTab).addClass('now').siblings('.now').removeClass('now');
        $j(c_class).hide().eq(_showTab).show();
        $j(p_class).mouseover(function() {
            var $this = $j(this),
            _index = $this.index();
            $this.addClass('now').siblings('.now').removeClass('now');
            $j(c_class).eq(_index).stop(false, true).fadeIn().siblings().hide();
            return false;
        }).find('a').focus(function(){
            this.blur();
        });
    });
}

var chk_tab_menu_flag = 0;
function chk_tab_menu(p_class,c_class,select_index) {
    if(chk_tab_menu_flag == 1){
        chk_tab_menu_flag = 2;
        window.clearTimeout();
        window.setTimeout(function () {
            if(chk_tab_menu_flag == 2){
                set_tab_menu(p_class,c_class,select_index)
            }
        }, 500);
    }
}
function set_tab_menu(p_class,c_class,select_index) {
    $j(function(){
        var _showTab = select_index;
        $j(p_class).eq(_showTab).addClass('now').siblings('.now').removeClass('now');
        $j(c_class).mouseover(function() {
            chk_tab_menu_flag = 1;
        }).mouseout(function() {
            chk_tab_menu(p_class,c_class,select_index);
        }).hide().eq(_showTab).show();
        $j(p_class).mouseover(function() {
            var $this = $j(this),
            _index = $this.index();
            $this.addClass('now').siblings('.now').removeClass('now');
            $j(c_class).eq(_index).stop(false, true).fadeIn().siblings().hide();
            chk_tab_menu_flag = 1;
            return false;
        }).mouseout(function() {
            chk_tab_menu(p_class,c_class,select_index);
        }).find('a').focus(function(){
            this.blur();
        });
    });
}

function set_roll(c_class,p_class,item_num,rollSpeed,wait_time){
	$j(function(){
		// 先取得 div#abgne_marquee ul
		// 接著把 ul 中的 li 項目再重覆加入 ul 中(等於有兩組內容)
		// 再來取得 div#abgne_marquee 的高來決定每次跑馬燈移動的距離
		// 設定跑馬燈移動的速度及輪播的速度
		var $marqueeUl = $j(c_class),
			$marqueeli = $marqueeUl.append($marqueeUl.html()).children(),
			_height = $j(p_class).height() * -1,
			scrollSpeed = rollSpeed,
			timer,
			speed = wait_time + scrollSpeed;
		// 幫左邊 $marqueeli 加上 hover 事件
		// 當滑鼠移入時停止計時器；反之則啟動
		$marqueeli.hover(function(){
			clearTimeout(timer);
		}, function(){
			timer = setTimeout(showad, speed);
		});
		// 控制跑馬燈移動的處理函式
		function showad(){
			var _now = $marqueeUl.position().top / _height;
			_now = (_now + 1) % $marqueeli.length;
			// $marqueeUl 移動
			$marqueeUl.animate({
				top: _now * _height
			}, scrollSpeed, function(){
				// 如果已經移動到第二組時...則馬上把 top 設 0 來回到第一組
				// 藉此產生不間斷的輪播
				if(_now >= $marqueeli.length / (2*item_num)){
					$marqueeUl.css('top', 0);
				}
			});
			// 再啟動計時器
			timer = setTimeout(showad, speed);
		}
		// 啟動計時器
		timer = setTimeout(showad, speed);
		$j('a').focus(function(){
			this.blur();
		});
	});
}

function set_auto_tab(p_class,c_class,stop_class,max_num,now_num,auto_tab) {
    if(now_num == -1){
        $j(stop_class).mouseover(function() {
            $j(auto_tab).val("0");
        }).mouseout(function() {
            $j(auto_tab).val("1");
        });
        $j(p_class).mouseover(function() {
            $j(auto_tab).val("0");
        }).mouseout(function() {
            $j(auto_tab).val("1");
        });
        $j(c_class).mouseover(function() {
            $j(auto_tab).val("0");
        }).mouseout(function() {
            $j(auto_tab).val("1");
        });
    }
    window.clearTimeout();
    window.setTimeout(function () {
        now_num += 1;
        if(max_num == now_num){
            now_num = 0;
        }
        if($j(auto_tab).val() == "1"){
//            set_tab(p_class,c_class,now_num);
	    $j(p_class).eq(now_num).addClass('now').siblings('.now').removeClass('now');
	    $j(c_class).hide().eq(now_num).show();
        }
        set_auto_tab(p_class,c_class,stop_class,max_num,now_num,auto_tab);
    }, 2000);
}


function chg_gt_lt(c_class){
    $j(c_class).each(function(i) {
        var $a = $j(this);
        $a.html($a.html().replace(/\&lt;/g, "<").replace(/\&gt\;/g, ">")); 
    }); 
}

function newOnlineSupplier_home(){
    $j("div#comCon div ul li").each(function(i) {
        var $a = $j(this);
        if( $a.index() == 7 || $a.index() == 16){
            $a.after("<BR>");
        }
    }); 
    $j("div#comCon").html($j("div#comCon").html().replace(/\<BR\>/ig, '</ul></div><div><ul>')); 

}

