/**
 * marquee
 *
 * @author: 吴烈
 * @website: http://www.cnnetlab.com
 */
(function($){
$.fn.extend({
	marquee: function(pause, speed) {
		$(this).each(function(){
			var current_id = 0;
			var element = $(this).find('.images');
			var ctrl = $(this).find('.ctrl');
			var childrens = element.children();
			var item_count = childrens.size()-1;
			var item_width = childrens.first().width();
			var timer = null;
			var pageno = 1;
			var pagecount = Math.ceil(item_count / 5);
			init = function(){
				element.width((item_count+1) * item_width);
				ctrls(0);
				
				// timer
				timer = setInterval('auto()', pause);
				ctrl.mouseover(function(){
					clearInterval(timer);
				}).mouseout(function(){
					timer = setInterval('auto()', pause);
				});
			};
			
			ctrls = function(cid){
				ctrl.empty();
				if(cid != undefined){
					// auto page
					pageno = Math.ceil((cid+1) / 5);
				}
				
				if(pageno > 1){
					ctrl.append('<a class="prev" href="javascript:void(0);" rel="' + (pageno-1)+ '">&lt;</a>');
				}
				
				for(var i = 0; i < 5; i ++){
					var cid_t = (pageno-1) * 5 + i;
					if(cid_t == item_count){
						break;
					}
					
					var c_name = 'num';
					//alert(cid +'->' + cid_t)
					if(cid == cid_t){
						c_name = 'num current';
					}
					ctrl.append('<a class="'+ c_name +'" href="javascript:void(0);">'+(cid_t+1)+'</a>');
				}
				
				if(pageno < pagecount){
					ctrl.append('<a class="next" href="javascript:void(0);" rel="' + (pageno-1)+ '">&gt;</a>');
				}
				
				ctrl.find('a.prev').each(function(){
					$(this).click(function(){
						pageno --;
						if(pageno <= 0){
							pageno = 1;
						}
						
						ctrls();
					})
				});
				ctrl.find('a.next').each(function(){
					$(this).click(function(){
						pageno ++;
						if(pageno >= 0){
							pageno = pagecount;
						}
						
						ctrls();
					})
				});
				ctrl.find('a.num').each(function(){
					$(this).click(function(){
						move($(this).text()-1);
					})
				});
			}
			
			auto = function(){
				var sid = current_id+1;
				
				move(sid);
			};
			
			move = function(tid){
				if(tid == item_count){
					var  marginLeft = -1* tid * item_width;
					element.animate({marginLeft: marginLeft}, speed, function(){
						element.css({marginLeft: 0})
					});
					current_id = tid = 0;
					ctrls(tid);
				} else {
					var  marginLeft = -1* tid * item_width;
					element.animate({marginLeft: marginLeft}, speed);
					current_id = tid;
					ctrls(tid);
				}
			}
			
			init();
		});
	}
})
})(jQuery);

/**
 * loadthumb
 *
 * jQuery图片预加载插件
 * 
 * @author: CssRain
 * @website: http://www.CssRain.cn
 */
(function($){
$.fn.extend({
	loadthumb: function(options) {
		options = $.extend({
			 src : ""
		},options);
		var _self = this;
		_self.hide();
		var img = new Image();
		
		$(img).load(function(){
			
			_self.attr("src", options.src);
			_self.fadeIn("slow");
		}).attr("src", options.src);  //.atte("src",options.src)要放在load后面，
		return _self;
	}
})
})(jQuery);


/**
 * fashion
 * 
 * @author: 吴烈
 * @website: http://www.cnnetlab.com/
 */
(function($){
$.fn.extend({
	fashion: function(pause, speed) {
		$(this).each(function(){
			var current_id = 0;
			var element = $(this).find('.images');
			var ctrl = $(this).find('.ctrl');
			var childrens = element.children();
			var item_count = childrens.size();
			var item_width = childrens.first().width();
			var timer = null;
			var srcs = new Array();
			var pageno = 1;
			var pagecount = Math.ceil(item_count / 5);
			init = function(){
				// 获取所有图片的src
				element.children().each(function(){
					srcs.push($(this).attr('src'));
				});
				element.empty();
				element.html('<img class="pic" height="400" src="' + srcs[0] +'" />');
				
				// ctrl fisrt time
				ctrls(0);
				
				// timer
				timer = setInterval('auto()', pause);
				ctrl.mouseover(function(){
					clearInterval(timer);
				}).mouseout(function(){
					timer = setInterval('auto()', pause);
				});
			};
			
			ctrls = function(cid){
				ctrl.empty();
				if(cid != undefined){
					// auto page
					pageno = Math.ceil((cid+1) / 5);
				}
				
				if(pageno > 1){
					ctrl.append('<a class="prev" href="javascript:void(0);" rel="' + (pageno-1)+ '">&lt;</a>');
				}
				
				for(var i = 0; i < 5; i ++){
					var cid_t = (pageno-1) * 5 + i;
					if(cid_t == item_count){
						break;
					}
					
					var c_name = 'num';
					//alert(cid +'->' + cid_t)
					if(cid == cid_t){
						c_name = 'num current';
					}
					ctrl.append('<a class="'+ c_name +'" href="javascript:void(0);">'+(cid_t+1)+'</a>');
				}
				
				if(pageno < pagecount){
					ctrl.append('<a class="next" href="javascript:void(0);" rel="' + (pageno-1)+ '">&gt;</a>');
				}
				
				ctrl.find('a.prev').each(function(){
					$(this).click(function(){
						pageno --;
						if(pageno <= 0){
							pageno = 1;
						}
						
						ctrls();
					})
				});
				ctrl.find('a.next').each(function(){
					$(this).click(function(){
						pageno ++;
						if(pageno >= 0){
							pageno = pagecount;
						}
						
						ctrls();
					})
				});
				ctrl.find('a.num').each(function(){
					$(this).click(function(){
						move($(this).text()-1);
					})
				});
			}
			
			auto = function(){
				var sid = current_id+1;
				if(sid == item_count){
					sid = 0;
				}
				
				move(sid);
			};
			
			move = function(tid){
				if(srcs[tid]){
					element.find('img').loadthumb({src: srcs[tid]});
					//element.find('img').attr('src', srcs[tid]);
					current_id = tid;
					ctrls(tid);
				}
			}
			
			init();
		});
	}
})
})(jQuery);


/**
 * page
 * 
 * @author: 吴烈
 * @website: http://www.cnnetlab.com/
 */
(function($){
$.fn.extend({
	page: function(speed) {
		$(this).each(function(){
			var current_id = 0;
			var element = $(this).find('.inner');
			var ctrl = $(this).find('.ctrl');
			var item_height = $(this).find('.text').height();
			var page_count = Math.ceil(element.height() / item_height);
			var min_marginTop = -1 * (element.height() - item_height);
			var auto_up_timer, auto_down_timer;
			
			page_up = function(){
				var tid = current_id - 1;
				if(tid == -1){
					return;
				}
				
				move_to_page(tid);
			}
			
			page_down = function(){
				var tid = current_id + 1;
				if(tid == page_count){
					return;
				}
				
				move_to_page(tid);
			}
			
			line_move = function(sp){
				down_sp = sp;
				if(page_count != 1){
					var  marginTop = parseInt(element.css("margin-top")) + down_sp;
					if(marginTop < 0 && (marginTop > min_marginTop)){
						element.css("margin-top", marginTop);
					} else {
						return; 
					}
				}
				
				if(down_sp == 0){
					return;
				}
				
				setTimeout(function(){line_move(down_sp)}, 20);
			}
			
			move_to_page = function(tid){
				current_id = tid;
				var  marginTop = -1* tid * item_height;
				element.animate({marginTop: marginTop}, speed);
			}
			
			ctrl.find('a.up').mouseover(function(){
				line_move(3);
			}).mouseout(function(){
				line_move(0);
			});
			ctrl.find('a.down').mouseover(function(){
				line_move(-3);
			}).mouseout(function(){
				line_move(0);
			});
			
			ctrl.find('a.up').click(function(){
				line_move(5);
			});
			ctrl.find('a.down').click(function(){
				line_move(-5);
			});
		});
	}
})
})(jQuery);

function nav_fix(){
	if ($.browser.mozilla) {
		$('#nav-ch').find('a').each(function(){
			$(this).mouseover(function(){
				var pos = $(this).css('background-position');
				var pos_a = pos.split(' ');
				pos_a[1] = '-34px';
				$(this).css('background-position', pos_a.join(' '));
			}).mouseout(function(){
				var pos = $(this).css('background-position');
				var pos_a = pos.split(' ');
				pos_a[1] = '-22px';
				$(this).css('background-position', pos_a.join(' '));
			})
		});	

		$('#nav-en').find('a').each(function(){
			$(this).mouseover(function(){
				var pos = $(this).css('background-position');
				var pos_a = pos.split(' ');
				pos_a[1] = '-11px';
				$(this).css('background-position', pos_a.join(' '));
			}).mouseout(function(){
				var pos = $(this).css('background-position');
				var pos_a = pos.split(' ');
				pos_a[1] = '0';
				$(this).css('background-position', pos_a.join(' '));
			})
		});	
	}
}

(function($){
$.fn.extend({
	tabs: function(curId) {
		$(this).each(function(){
			var element = $(this);
			if(element.size() == 0){
				return;
			}
			
			var ctrl = element.find('ul.tabs-ctrl');
			var tag = 1;
			var activeID = curId;
			var count = ctrl.find('a').size();
			

			
			ctrl.find('li').each(function(){
				$(this).mouseover(function(){
					// content
					var contentId = $(this).find('a').attr('rel');
					if(contentId != undefined && contentId != ''){
						element.children('.tabs-contents').children('div').each(function(){
							$(this).hide();
						});
						$(contentId).fadeIn(1000);
						
						// ctrl
						ctrl.children('li').each(function(){
							$(this).removeClass('sel');
						});
						$(this).addClass('sel');
					}
				});
			});
		});
	}
})
})(jQuery);

$().ready(function(){
	$('#top-tab').tabs();
	$('#slider1').easySlider({controlsShow: false, auto: true, continuous: true, vertical: false});
})
