$.fn.AutoImages		= function(options){
	var setting		= $.extend({
		width		: 160,
		height		: 120
	},options);
	this.each(function(){
		$(this).AutoImage(setting);
	});
}
$.fn.AutoImage		= function(options){
	var obj			= this;
	var setting		= $.extend({
		width		: 160,
		height		: 120,
		obj			: obj.parent()
	},options);
	var img			= new Image();
	img.onload		= function(){
		var _tmp	= {
			w		: this.width,
			h		: this.height
		}
		_tmp.p		= _tmp.h/_tmp.w;
		var _size	= {
			width	: setting.width,
			height	: setting.height
		}
		if(_tmp.w>_size.width){
			_tmp.w	= _size.width; _tmp.h = _size.width * _tmp.p;
		}
		if(_tmp.h>_size.height){
			_tmp.w	= _size.height/_tmp.p; _tmp.h = _size.height;
		}
		obj.css({width:_tmp.w,height:_tmp.h});
	}
	img.src = obj.attr('src');
	if(setting.obj.find('img.align').size()<1){
		setting.obj.append($('<img class="align" />').css({
			width	: '0px',
			height	: '100%',
			border	: 'none'
		}));
	}
	return img;
}
var AutoImage		= function(options){
	var setting		= $.extend({
		width		: 160,
		height		: 120,
		src			: ''
	},options);
	var img			= new Image();
	img.onload		= function(){
		var _tmp	= {
			w		: this.width,
			h		: this.height
		}
		_tmp.p		= _tmp.h/_tmp.w;
		var _size	= {
			width	: setting.width,
			height	: setting.height
		}
		if(_tmp.w>_size.width){
			_tmp.w	= _size.width; _tmp.h = _size.width * _tmp.p;
		}
		if(_tmp.h>_size.height){
			_tmp.w	= _size.height/_tmp.p; _tmp.h = _size.height;
		}
		this.width	= _tmp.w;
		this.height	= _tmp.h;
	}
	img.src = setting.src;
	return {width:img.width,height:img.height};
}
String.prototype.format = function($json){
	if(typeof $json != 'object'){
		var args = Array.prototype.slice.call(arguments,0);
		return this.replace(/\{(\d+)\}/g,function(m,i){
			return args[i];
		});
	}else{
		return this.replace(/\{(\w+)\}/g,function(m,i){
			return $json[i];
		});
	}
}; 

function megaHoverOver(){
	$(this).find(">ul").stop().slideDown('fast');
}

function megaHoverOut(){ 
  $(this).find(">ul").stop().slideUp('fast');
}
jQuery(function(){
	//$('.TopMenu ul').TopMenu();
	$(".TopMenu ul li:has(ul)").each(function(){
		var _this	= $(this);
		var _ul		= _this.find(">ul");
		var _li		= _ul.find(">li");
		var _size	= _li.size();
		var _width	= _li.filter(':first').outerWidth();
		_ul.css({width:_width*_size,left:(_width*_size-$(this).outerWidth())/2*-1});
	});
	var config = {    
		 sensitivity	: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval		: 100, // number = milliseconds for onMouseOver polling interval    
		 over			: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout		: 500, // number = milliseconds delay before onMouseOut    
		 out			: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	$(".TopMenu ul li:has(ul)").hoverIntent(config).css({position:'relative'}).find('>ul').hide();

//	$('.body .left ul li').hover(function(){
//		$(this).addClass("hover");
//	},function(){
//		$(this).removeClass("hover");
//	});
	$('.scroll-pane').jScrollPane({showArrows:true,scrollbarWidth:11});
	$('.products.body .content .items').scrollable({
		size     : 4
	}).mousewheel();
	$('.products.body .content .nodes').scrollable({
		size     : 4
	}).mousewheel();
//	//$('.products-item .img a img').AutoImages({width:220,height:165});
//	$('.product .images .imglist .items ul li a img').AutoImages({width:120,height:90});
//	$(".product .images .imglist .items ul li a").bind('click',function(){ 
//		var obj		= $(this);
//		var img		= AutoImage({width:480,height:640,src:obj.attr('href')});
//		$('.product .images .img').html($('<img class="align" />').css({
//			width	: '1px',
//			height	: 640,
//			border	: 'none'
//		})).append($('<img />').css({
//			width	: img.width,
//			height	: img.height,
//			border	: 'none'
//		}).attr('src',obj.attr('href')));
//		return false;
//	}).filter(":first").click();
})
