Number.prototype.limit = function(min, max){
	return Math.min(max,Math.max(min, this));
}
Date.prototype.format = function(_format,lng){
	var _week  = [
		{ch:'星期日',en:'Sunday'},
		{ch:'星期一',en:'Monday'},
		{ch:'星期二',en:'Tuesday'},
		{ch:'星期三',en:'Wednesday'},
		{ch:'星期四',en:'Thursday'},
		{ch:'星期五',en:'Friday'},
		{ch:'星期六',en:'Saturday'}
	];
	var _month = [
		{ch:'一月',en:'January'},
		{ch:'二月',en:'February'},
		{ch:'三月',en:'March'},
		{ch:'四月',en:'April'},
		{ch:'五月',en:'May'},
		{ch:'六月',en:'June'},
		{ch:'七月',en:'July'},
		{ch:'八月',en:'August'},
		{ch:'九月',en:'September'},
		{ch:'十月',en:'October'},
		{ch:'十一月',en:'November'},
		{ch:'十二月',en:'December'},
	];
	var _quarter = [
		{ch:'春季',en:'Spring'},
		{ch:'夏季',en:'Summer'},
		{ch:'秋季',en:'Fall'},
		{ch:'冬季',en:'Winter'}
	];
	_format   = _format || 'YYYY/MM/DD hh:nn:ss';
	var $this = this;
	var $date = {
		y : $this.getYear(),
		m : $this.getMonth()+1,
		d : $this.getDate(),
		h : $this.getHours(),
		n : $this.getMinutes(),
		s : $this.getSeconds(),
		ms: $this.getMilliseconds(),
		quarter : _quarter[Math.floor(($this.getMonth()+3)/3)-1][lng||'en'],
		week    : _week[$this.getDay()][lng||'en'],
		month   : _month[$this.getMonth()][lng||'en']
	}
	return _format.replace(/yyyy|yy|mm|month|m\d|m|dd|d|hh|h|nn|n|ss|s|week|w|q/ig,function(idx){
		var result;
		switch(idx.toLowerCase()){
			case 'ms'    : result = $date['ms'];break;
			case 'm3'    : result = String($date['month']).replace(/^(.{3}).*$/,'$1');break;
			case 'month' : result = $date['month'];break;
			case 'yyyy'  : result = $date['y'];break;
			case 'yy'    : result = String($date['y']).replace(/^\d{2}(\d{2})$/,'$1');break;
			case 'mm'    : result = String($date['m']).replace(/^(\d)$/,'0$1');break;
			case 'm'     : result = $date['m'];break;
			case 'dd'    : result = String($date['d']).replace(/^(\d)$/,'0$1');break;
			case 'd'     : result = $date['d'];break;
			case 'hh'    : result = String($date['h']).replace(/^(\d)$/,'0$1');break;
			case 'h'     : result = $date['h'];break;
			case 'nn'    : result = String($date['n']).replace(/^(\d)$/,'0$1');break;
			case 'n'     : result = $date['n'];break;
			case 'ss'    : result = String($date['s']).replace(/^(\d)$/,'0$1');break;
			case 's'     : result = $date['s'];break;
			case 'week'  : result = $date['week'];break;
			case 'w'     : result = String($date['week']).replace(/^(.{3}).*$/,'$1');break;
			case 'q'     : result = $date['quarter'];break;
		};
		return result;
	});
}
function basket_update(data){
	$(this).parent().prev().html(data.price);
	$(this).parent().next().html(data.subtotal);
	$(this).parent().next().next().html(data.weights);
	$(this).parent().parent().parent().next().find('tr:eq(0) td:last').html(data.totalweights);
	$(this).parent().parent().parent().next().find('tr:eq(1) td:last').html(data.total);
}
function quantity_update(data){
	if(data.success){
		$(this).parent().next().find('span').html(data.subtotal);
	}else{
		alert(data.msg)
	}
}
//$.fn.AjaxLoad	= function(id){
//	var obj		= this;
//	var __loading		= $('<div class="loading-content"><div class="overlay"></div><div class="loading"></div></div>');
//	__loading.find('.overlay').css({
//		opacity			: 0.1
//	});
//	var __load	= function($href){
//		__loading.appendTo(obj).show();
//		window.setTimeout(function(){
//			obj.load($href+' '+id,function(){
//				_load();
//			});
//		},100);
//	}
//	var _load	= function(){
//		obj.find('.pagination a').click(function(){	
//			var _href	= this.href;
//			__load(_href);
//			return false;
//		});
//		var _go		= obj.find('.pagination button').attr('onClick');
//		var _max	= String(_go).replace(/^.*limit\(\d+\,(\d+)\)\;$/i,'$1');
//		obj.find('.pagination button').attr('onClick','').click(function(){
//			__load('index.asp?controller=Default&action=index&page='+Number($(this).prev().val()).limit(1,_max));
//			return false;
//		});
//	}
//	_load();
//}
$.fn.AjaxLoad	= function(id){
	return this.each(function(){
		var _this	= $(this);
		var overlay	= $('<div class="loading-content"><div class="overlay"></div><div class="loading"></div></div>').hide();
		overlay.find('.overlay').css({
			opacity			: 0.1
		});
		(function(){
			var fn	= arguments.callee;
			_this.find('.pagination a').click(function(){
				overlay.appendTo(_this).show();
				_this.load(this.href+' '+id,function(){
					overlay.hide();
					fn();
				});
				return false;
			});
		})();
	});
}
$.fn.SubmitAjax	= function(options){
	var setting			= $.extend({
		'event'			: 'click',
		'dataType'		: 'json',
		'type'			: 'post',
		'readerTo'		: null,
		'success'		: null
	},options);
	return this.bind(setting['event'],function(){
		var _this		= $(this.form);
//		var _mask		= setting['readerTo']||_this;
//		var _overlay	= $('<div class="overlay" />');
//		var _loading	= $('<div class="loading" />');
		$.ajax({
			cache 		: false,
			url			: setting['url']||_this.attr('action'),
			dataType 	: setting['dataType'],
			type		: setting['type'],
			data 		: _this.serializeArray(),
			beforeSend 	: function(){
//				var _pos	= {
//					top		: 0,
//					left	: 0
//				};
//				_mask.css({position:'relative'});
//				_overlay.css(_pos).css({opacity:0.4}).appendTo(_mask);
//				_loading.css(_pos).appendTo(_mask);
			},
			success		: function(data){
				if($.isFunction(setting['success'])){
					setting['success'].call(this,data);
				}else{
					if(data.success){
						alert(data.msg);
					}else
						alert(data.msg);
				}
			},
			complete	: function(){
//				_mask.find('.overlay,.loading').css({position:'static'});
			}
		});
		return false;
	});
}
var __URI__		= location.pathname.replace(/^(.*?\.asp).*?$/i,'$1');
$(function(){
//	$('#basket').Template({
//		tpl			: 'basket.tpl',
//		//tpl			: '{#template MAIN}<table cellpadding="0" cellspacing="0" border="0" class="grid"><thead><tr>{#foreach $P.options.header as row}<th{$P.functions.getWidth($T.row.width)}>{$T.row.name}</th>{#/for}</tr></thead><tbody>{#foreach $T.rows as row}{#param name=index value=($P.index||0)+1}<tr>{#include ROW root=$T.row}</tr>{#else}<tr><td colspan="{$P.options.header.length}" class="empty">{$P.options.empty}</td></tr>{#/for}</tbody></table>{#/template MAIN}{#template ROW}{#foreach $P.options.header as row}<td{$P.functions.getAlign($T.row.align)}>{$P.functions.dataIndex($T.row.dataIndex,$T,$P)}</td>{#/for}{#/template ROW}',
//		action		: __URI__+'?controller=Basket&action=json',
//		empty		: 'No Product'
//	});
	$('.grid tbody tr td a.edit').Dialog({
		'width'		: 300,
		'height'	: 107,
		'title'		: 'Change color and language',
		'action'	: 'index.asp?controller=Basket&action=editsave',
		'callback'	: function(o,me){
			this.load(me.attr('href'));
		},
		'success'	: function(data,me){
			me.parent().parent().find('td:eq(3)').html(data.color+'<br/>'+data.lng);
		}
	});
	$('fieldset.form form select').SubmitAjax({
		'event'			: 'change',
		'dataType'		: 'html',
		'type'			: 'post',
		'url'			: 'index.asp?controller=Basket&action=calculation',
		'readerTo'		: $('fieldset.form form'),
		'success'		: function(data){
			$('table.grid tfoot tr.calculation').remove();
			$('table.grid tfoot').append($('table.grid tfoot tr.calculation',data));
		}
	});
	$('fieldset.form form select:eq(2)').change(function(){
		$(this).next('.text').remove();
		if(this.value==3){
			$(this).after('<span class="text">(need pay more middle bank fee)</span>');
		}
	});
	$('.newproduct').AjaxLoad('.newproduct');
	$('.recommend').AjaxLoad('.recommend');
	$('.Hotsale').AjaxLoad('.Hotsale');
	$('.dlg>dd').AjaxLoad('.dlg>dd');

//	window.setInterval(function(){
//		$('.date').html(new Date().format());
//	},1);
	$('.date').html(new Date().format());
	$('#country').dropdown({
		callback	: function(n){
			$('#shipment').dropdown({
				url		: 'index.asp?controller=Basket&action=shipment&time='+Date.parse(new Date()),
				load	: true,
				data	: {country:n}
			});
		}
	});
	$("#KinSlideshow").KinSlideshow({
			moveStyle:"right",
			isHasTitleBar : false,
			titleBar:{titleBar_height:30,titleBar_bgColor:"#08355c",titleBar_alpha:0.5},
			btn:{btn_bgColor:"#FFFFFF",btn_bgHoverColor:"#CC0000",btn_fontColor:"#000000",btn_fontHoverColor:"#FFFFFF",btn_borderColor:"#cccccc",btn_borderHoverColor:"#FF0000",btn_borderWidth:1}
	});
	$(".scrollable").scrollable({circular:true, mousewheel:true}).autoscroll({
		size:2,
		interval: 3000
	});
	$('.product #images .item').click(function(){
		var obj = $('.product #images #image a');
		obj.attr('href',this.href);
		$('.product #images .item').removeClass('on');
		$(this).addClass('on');
		var obj = obj.find('img').attr('src',this.href);
		
		var img		= new Image();
		img.onload = function(){
			var _tmp = {
				w	: this.width,
				h	: this.height
			}
			_tmp.p	= _tmp.h/_tmp.w;
			var _size = {
				width	: 400,
				height	: 300
			}
			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');
		return false;
	}).filter(':first').click();
	$('.product #images a.item img').each(function(){
		var obj = $(this);
		var img		= new Image();
		img.onload = function(){
			var _tmp = {
				w	: this.width,
				h	: this.height
			}
			_tmp.p	= _tmp.h/_tmp.w;
			var _size = {
				width	: 100,
				height	: 75
			}
			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');
//		var _tmp = {
//			w	: obj.width(),
//			h	: obj.height()
//		}
//		_tmp.p	= _tmp.h/_tmp.w;
//		var _size = {
//			width	: 100,
//			height	: 100
//		}
//		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});
	});
	$('#body').find('.layout').layoutHeight({
		data    : [
			{index:0,height:-38},
			{index:1,id:'.location'}
		]
	});
//	$("#country").autocomplete(country,{
//		minChars: 0,
//		width: 310,
//		matchContains: "word",
//		autoFill: false,
//		formatItem: function(row, i, max) {
//			return row.abbr + " " + row.ename;
//		},
//		formatResult: function(row) {
//			return row.ename;
//		}
//	});
	$('.shipment li a').click(function(){
		window.open(this.href,'m','width=400,height=300, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=n o, status=no');
		return false;
	});
});
