/*
 ********************** Style Platform Copyright(c) 2011, artron.net *********************
 ************************************* Author: 殷文利 *************************************
 ***************************** CreateTime: 2011/10/9 *************************************
 ***************************** Version: 1.0 **********************************************
 **************************** Copyright: artron.net **************************************
 *****************************************************************************************
 ********************************************************************** QQ:237720089 *****
 */
 
SFO = {sfo:this.SFO};
String.prototype.trim = function(arr){
	arr = arr.slice() || [];
	arr[arr.length] = '　';
	var r = new RegExp('(^(\\s|' + arr.join('|') + ')+)|((\\s|' + arr.join('|') + ')+$)', 'g');
	return this.replace(r,'');
};
SFO.ua = (function(){
	var _ua = navigator.userAgent.toLowerCase();
	return {
		ie: /msie/.test(_ua),
		ie6: /msie 6/.test(_ua)
	};
})();
/*
SFO.scroll({
	pre: $('.scr_w .up'),	//上一条
	next: $('.scr_w .down'),//下一条
	area: $('.scr_w .inf_w'),//滚动区
	ra: $('.scr_w .list'),	//重复区
	mouseArea: $('.scr_w'),	//鼠标区
	sp: 3000,				//间隔
	num: 0,					//滚动个数,0为滚屏
	dir: 'ud',				//滚动方向ud,lr
	cscroll: false,			//匀速滚动
	auto: true				//自动滚
});
*/
SFO.scroll = function(op){
	var dur = 500, h, t, hn = 0, vn = 0, $p, $a, $cs, $c, n, htm;
	var init = function(){
		if(!op.area)throw "滚动区未定义";
		op.ra = op.ra || op.area;
		
		$p = op.area;
		$a = op.ra;
		$cs = $a.children();
		$c = $cs.eq(0);
		n = $cs.length;
		htm = $a.html();
		
		op.mouseArea = op.mouseArea || op.area;
		op.sp = op.sp || 3000;
		op.num = op.num || 0;
		op.dir = op.dir || 'ud';
		op.auto = op.auto ? op.auto : true;
		op.cscroll = op.cscroll || false;
		//op.vari = op.vari || false;
		if(op.cscroll){
			op.sp = 20; op.num = 1; dur = 0; h = 1;
		}
		if(!calc())return 0;
	
		$a.append(htm + htm);
		$p.scrollLeft(0).scrollTop(0);
		return 1;
	},
	calc = function(){
		if(op.dir == 'lr'){
			if(op.cscroll){
				$cs.each(function(){
					hn += $(this).outerWidth(true);  //滚动区  总宽度
				});
			}else{
				h = $c.outerWidth(true); 
				vn = Math.ceil($p.width() / h);
				hn = h * n;
			}
			
			//alert($p.width());
			//if(hn<$p.width())return 0;
			$a.width(hn*3);
		}
		else{
			if(op.cscroll){
				$cs.each(function(){
					hn += $(this).outerHeight(true);
				});
			}else{
				h = $c.outerHeight(true);
				vn = Math.ceil($p.height() / h);
				hn = h * n;
			}
			if(hn<$p.height())return 0;
		}
		return 1;
	},
	auto = function(d,c){
		//if(op.vari)op.cscroll ? csr() : nc();
		var _h = h;
		c = c || op.num || vn;
		switch(d){
			case 'right':
				_h = -h;
				if($p.scrollLeft()<=hn*2+_h*c)$p.scrollLeft($p.scrollLeft()+hn);
				break;
			case 'down':
				_h = -h;
				if($p.scrollTop()<=hn*2+_h*c)$p.scrollTop($p.scrollTop()+hn);
				break;
			default:;
		}
		if(op.dir == 'ud'){
			$p.animate({scrollTop: $p.scrollTop() + _h * c}, dur, function(){if($p.scrollTop()>=hn)$p.scrollTop($p.scrollTop()-hn);});
		}else{
			$p.animate({scrollLeft: $p.scrollLeft() + _h * c}, dur, function(){if($p.scrollLeft()>=hn)$p.scrollLeft($p.scrollLeft()-hn);
			});
		}
	},
	addE = function(){
		op.pre && op.pre.click(function(){
			$p.stop(1,1);
			op.dir == 'ud' ? auto('down') : auto('right');
		});
		op.next && op.next.click(function(){
			$p.stop(1,1);
			op.dir == 'ud' ? auto('up') : auto('left');
		});
	},
	go = function(){
		var fn = function(){
			op.dir == 'ud' ? auto('up', 1) : auto('left', 1);
		};
		t = window.setInterval(fn, op.sp);
		//t = SFO.Timer.add(fn,op.sp);
		op.mouseArea.mouseover(function(){
			window.clearInterval(t);
			//SFO.Timer.remove(t);
		}).mouseout(function(){
			t = window.setInterval(fn, op.sp);
			//t = SFO.Timer.add(fn,op.sp);
		});
	};
	if(!init())return;
	addE();
	op.auto && go();
};
SFO.TextArea = (function(){
	var o = {};
	o.chgFC = function($obj,n_c,hy){//变换前景色
		var o_c;
		hy = hy == null ? 1 : hy;
		$obj.focus(function(){
			var o = this;
			o_c  = $(this).css('color');
			$(this).css('color',n_c);
			if(this.value == this.defaultValue){
				if(document.all){
					this.createTextRange().select();
				}else{
					window.setTimeout(function(){
						o.setSelectionRange(0, o.value.length);
					}, 100);
				}
			}
		}).blur(function(){
			$(this).css('color',o_c);
			if(this.value == '' && hy)
				this.value = this.defaultValue;
		});
	};
	return o;
})();
SFO.App = {
	Jump: function(url, time){//404跳转
		var $sec = $('#sec'),
			t = window.setInterval(countDown, 1000);
		$sec.html(time = time || 5);
		function countDown(){
			$sec.html(--time);
			if(time<=0){
				window.clearInterval(t);
				location.replace(url);
			}
		}
	}
};

/* 取消a href=#的默认行为 */
SFO.App.a_default = {
	off: function(){
		$('a[href=#]').click(function(e){
			e.preventDefault();
		});
	}
};
SFO.App.returnTop = function($obj){//返回顶部
	if($obj.length < 1)return;
	var h = document.documentElement,
		b = document.body;
	$(window).scroll(function(){
		Math.max(h.scrollTop, b.scrollTop) > 0 ? $obj.fadeIn(100) : $obj.fadeOut(100);
	});
	$(window).scroll();
	$obj.click(function(e){
		var m = h.scrollTop > b.scrollTop ? h : b;
		$(m).animate({
			scrollTop: 0
		}, 150);
		e.preventDefault();
	});
};
SFO.App.showMenu = function($src, $tar){//搜索选项下拉
	if($src.length < 1 )return;
	var n;
	//$tar.css('left',$('.ss_frm').offset().left - $('.menu').offset().left + 1 + 'px');
	$src.keyup(function(){
		var v = $(this).val();
		if(v.length>0){
			$tar.animate({height:'80px',display:'block'},500);
		}else{
			$tar.stop(1,1);
			$tar.height(0);
			$tar.hide();
		}
		$tar.find('li em').each(function(){
			$(this).text(v);
			SFO.TextArea.limitWidth($(this), 92);
		});
	});
	$tar.find('li:not(.title)').hover(function(){
		$(this).addClass('cur');
	},function(){
		$(this).removeClass('cur');
	});
	$(document).click(function(){
		$tar.stop(1,1);
		$tar.height(0);
		$tar.hide();
	});
};

function chgP(op){
	var t, ch = op.area.children(), n = ch.length,i=0,pre=0,cur=0;
	op.time = op.time || 5000;
	op.title = op.title || 0;
	op.num = op.num || 0;
	function b(i){
		$('.chgP .num li').removeClass('cur').eq(i).addClass('cur');
		op.title && $('.chgP .title').children().hide().eq(i).show();
	}
	function a(){
		ch.eq(i).fadeOut(500,function(){
			i = ++i % n;
			ch.eq(i).fadeIn(500);
			op.num && b(i);
			pre = i;
		});
	}
	if(op.num){
		$('.chgP .num li').click(function(){
			cur = $('.chgP .num li').index(this);
			ch.stop(1,1).eq(pre).fadeOut(500,function(){
				ch.eq(cur).fadeIn(500);
				op.num && b(cur);
			});
			i = pre = cur;
		});
	}
	$('.chgP').hover(function(){
		window.clearInterval(t);
	},function(){
		t = window.setInterval(a,op.time);
	});
	t = window.setInterval(a,op.time);
}

/* ****************************************** */
$(function(){
	SFO.App.a_default.off();//取消a href=#的默认行为
	SFO.TextArea.chgFC($('input[type=text]'), '#333');//输入框的文本色
	SFO.App.returnTop($('a[href=#top]'));//返回顶部
	SFO.App.showMenu($('#m_key'), $('.menu .ss_opt'));//搜索选项下拉
});

