/*
作者: 深山老熊 qq:81090 email:bjx2008@gmail.com
*/

/*首页选项卡，简单写一下*/
(function($){
	$.fn.indextabs  = function(){
		return this.each(function(){
			//重设内容块高度 ， 取最高的一个内容块
			/*
			var divs = $('.i_tab_s_b',$(this));
			var height = 0;
			for (var i = 0,c=divs.length ;i<c ;i++ )
			{
				height = height > divs.eq(i).height() ? height : divs.eq(i).height();
			}
			divs.height(height);
			*/
			//选项卡
			var li = $('>.i_tabs>ul:first>li',$(this));
			li.bind('mouseover',function(){
				$(this).addClass('cur').siblings().removeClass('cur');
				$('#catid_'+$(this).attr('catid')).show().siblings().hide();
			});
		});
	};
})(jQuery);


/*焦点图*/
(function($){
	$.fn.tctzFocus = function(s){
		s = $.extend({speed:4000,duration:500},s||{});
		return this.each(function(){
			var big   = $('.big_img li',$(this)),
				cur   = 0,
				preid = 0,
				len   = $('a',big).length,
				w     = $(this).width();

			//只有一张图片是，不作动画处理
			if (len <= 1) {return};

			var smallCode = function(){
				var s = [];
				s.push('<div class="thumb_img">');
				big.each(function(i){
					s.push("<a" + (i==0?' class="cur"':'') + "><img src='" + $('img',$(this)).attr('small') +"' /></a>");				
				});
				s.push('</div>');
				return s.join('');
			};
			$(this).prepend($(smallCode()));
			var small = $('.thumb_img a',$(this));

			//鼠标移动到小图时，显示当前大图
			small.mouseover(function(){
				show($(this));				
			});
			var show = function(i){
				if (typeof i == 'object') i = small.index(i);
				cur = i;
				small.eq(cur).addClass("cur").siblings().removeClass("cur");
			//	big.eq(cur).stop(true,true).fadeIn(2000).siblings("li").fadeOut(1000);

			//	big.eq(cur).show().siblings("li").hide();
				big.eq(preid).stop(true,true).fadeOut(400);
				big.eq(cur).stop(true,true).fadeIn(600);
				preid = cur;
			};
			//自动播放
			var autoshow = function(){
				if (cur == len - 1)
					show(small.eq(0));
				else 
					show(small.eq(cur).next());
			};			
			var myTime = setInterval(autoshow,s.speed);		
			
			$(this).hover(function(){
				if(myTime) clearInterval(myTime);
			},function(){
				myTime = setInterval(autoshow,s.speed);
			});

		});
	};
})(jQuery);

(function($){
	$.fn.Navigation  = function(){
		var _this   = $(this),
			rootDiv = $('.menu_root',_this),
			rootA   = $('a',rootDiv),
			childDiv= $('.menu_child',_this);

			childDiv.addClass( $.browser.msie ? 'menu_child_IE' : 'menu_child_shadow' );

			//子菜单的宽度没有A宽时，调整宽度
			rootA.each(function(){
				var o = $('#' + $(this).attr('id').replace('rootM_','childM_'));
				if (o.length == 0 ) return true;

				var minWidth = $(this).width() + 30;

				if ( minWidth > o.width() )
				{
					o.width(minWidth);
				}	
				

			});

			//子菜单最后一列不显示虚线
			childDiv.each(function(){
				$('td:last',$(this)).addClass('last');
			});

			//一级菜单
			rootA.hover(function(){
					var o = $('#' + $(this).attr('id').replace('rootM_','childM_'));
					if (o.length == 0){
						//当前没有子菜单，隐藏所有子菜单
						childDiv.hide('fast');
					}else{
						//定位操作
						var cw    = o.width(),              //子菜单宽度
							ah    = 40,                     //当前一级菜单 A高度  $(this).height() 用了line-height ie6有问题
							aw    = $(this).width(),        //当前一级菜单 A宽度
							of    = $(this).offset(),
							aleft = of.left,                //当前一级菜单 A 相对屏幕left
							atop  = of.top ,                //当前一级菜单 A 相对屏幕top
							rleft = rootDiv.offset().left,  //一级菜单最左边 位置
							rw    = rootDiv.width();        //一级菜单总宽度

						var top = atop + ah,left = 0;

						//在右侧可以显示完
						if ( cw + aleft < rleft + rw )
						{
							left = aleft - ($.browser.msie ? 2 : 0); //向左移2个像素 border
							//left = aleft - ($.browser.msie ? 0 : 0); //向左移2个像素 border
						}else{
							//按当前栏目位置 居中
							left = aleft - parseInt(cw / 2) + parseInt(aw / 2);
							//按当前栏目位置 居中 （左边超出了） 改成 相对屏幕居中
							if ( left < rleft || cw > rw * 0.67)
							{
								left = rleft + parseInt((rw - cw) / 2) ; 
							}
						}
						o.css({top:top,left:left + 7});
						o.show().siblings('.menu_child').hide();
					}
					$(this).addClass('hover').data('ishover',true).siblings().removeClass('hover');
				},function(){
					$(this).data('ishover',false);	
					if ($('#' + $(this).attr('id').replace('rootM_','childM_')).length == 0)
					{
						$(this).removeClass('hover');
					}
			});

			//鼠标在二级菜单里时，置当前一级菜单Hover状态 
			//离开时隐藏菜单
			childDiv.bind('mouseleave',function(){
					var o = $('#' + $(this).attr('id').replace('childM_','rootM_'));
					o.removeClass('hover');
					if ( ! o.data('ishover')) $(this).hide();			
			}).hover(function(){
				$(this).data('ishover',true);	
			},function(){
				$(this).data('ishover',false);	
			});
		return this;
	};
})(jQuery);


/*表格换色*/
(function($){
	$.fn.hovertable = function(){		
		$('tbody>tr:even',$(this)).addClass('even');
		return this;
	};
})(jQuery);


$(function(){

	//下拉菜单
	$('#nav').Navigation();

	//首页选项卡
	$('.i_cyzc').indextabs();
	
	//调整内容块高度
	try
	{
		if ( $('#container_sidebar').height() > $('#container_main').height() )
		{
			$('#container_main').height($('#container_sidebar').height());
		}
	}
	catch (e)
	{

	}

	//加入收藏
	$("#add-favourites").click(function(){
		var title = '深圳市天成投资集团有限公司',url = 'http://www.tctz.com/';
		if ( $.browser.msie )
		{
			window.external.addFavorite(url,title);
		}
		else if ( $.browser.mozilla )
		{
			window.sidebar.addPanel(title, url, '');
		}
	});

	//简繁切换
	$('#yxcms_lang').click(function(){
		$.post("/?ajax/gbbig5",function(data){location.reload()});
	});

	//表格换色

	$('table.hover').hovertable();


	//搜索
	$('#dosearch').click(doSearch);
	$('#keywords').bind('keydown',function(e){
		var key = e.which;
		if (key == 13) {
			doSearch();
		};
	});

	//搜索关键字
	$('#keywords').focus(function(){
		if ( $(this).val() == $(this).attr('defaultval') )
		{
			$(this).val('');
		}
	}).blur(function(){
		if ( $(this).val() == '' )
		{
			$(this).val($(this).attr('defaultval'));
		}
	});


});

var doSearch = function(){
	var key = $('#keywords').val();
	if ( !(/.?[^\s]+/.test(key)) || $('#keywords').val() == $('#keywords').attr('defaultval') ){
		alert('请输入关键字！');
		return false;
	}else{
		location.href='/search/cn/products/' + encodeURIComponent(key) + '.html';
	}
};


(function($) {                                          // Compliant with jquery.noConflict()
$.fn.jCarouselLite = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
        btnGo: null,
        mouseWheel: false,
        auto: null,

        speed: 200,
        easing: null,

        vertical: false,
        circular: true,
        visible: 3,
        start: 0,
        scroll: 1,

        beforeStart: null,
        afterEnd: null
    }, o || {});

    return this.each(function() {                           // Returns the element collection. Chainable.

        var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
        var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

        if(o.circular) {
            ul.prepend(tLi.slice(tl-v-1+1).clone())
              .append(tLi.slice(0,v).clone());
            o.start += v;
        }

        var li = $("li", ul), itemLength = li.size(), curr = o.start;
        div.css("visibility", "visible");

        li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
        ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
        div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});

        var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
        var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
        var divSize = liSize * v;                           // size of entire div(total length for just the visible items)

        li.css({width: li.width(), height: li.height()});
        ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));

        div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images

        if(o.btnPrev)
            $(o.btnPrev).click(function() {
                return go(curr-o.scroll);
            });

        if(o.btnNext)
            $(o.btnNext).click(function() {
                return go(curr+o.scroll);
            });

        if(o.btnGo)
            $.each(o.btnGo, function(i, val) {
                $(val).click(function() {
                    return go(o.circular ? o.visible+i : i);
                });
            });

        if(o.mouseWheel && div.mousewheel)
            div.mousewheel(function(e, d) {
                return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
            });

        if(o.auto)
            setInterval(function() {
                go(curr+o.scroll);
            }, o.auto+o.speed);

        function vis() {
            return li.slice(curr).slice(0,v);
        };

        function go(to) {
            if(!running) {

                if(o.beforeStart)
                    o.beforeStart.call(this, vis());

                if(o.circular) {            // If circular we are in first or last, then goto the other end
                    if(to<=o.start-v-1) {           // If first, then goto last
                        ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
                        curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
                    } else if(to>=itemLength-v+1) { // If last, then goto first
                        ul.css(animCss, -( (v) * liSize ) + "px" );
                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
                        curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
                    } else curr = to;
                } else {                    // If non-circular and to points to first or last, we just return.
                    if(to<0 || to>itemLength-v) return;
                    else curr = to;
                }                           // If neither overrides it, the curr will still be "to" and we can proceed.

                running = true;

                ul.animate(
                    animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
                    function() {
                        if(o.afterEnd)
                            o.afterEnd.call(this, vis());
                        running = false;
                    }
                );
                // Disable buttons when the carousel reaches the last/first, and enable when not
                if(!o.circular) {
                    $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
                    $( (curr-o.scroll<0 && o.btnPrev)
                        ||
                       (curr+o.scroll > itemLength-v && o.btnNext)
                        ||
                       []
                     ).addClass("disabled");
                }

            }
            return false;
        };
    });
};

function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};
function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
function height(el) {
    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};

})(jQuery);
