
/*
	[DUCEDIS] (C)2007-2008 Ducedis.com
    This is a freeware, use is subject to license terms
    Script filename：global.js

    Support: www.ducedis.com

*/

/*
查询字符串中是否存在某字符 
fore = 1 表示字符串开头
*/
var $_find = function ( needle, str, fore ){
	var pos=needle?(str+'').indexOf(needle):-1;
	if (pos!=-1) {
		if(fore==1)
			return pos===0?true:false;

		return true
	}
	return false
}

/*
获取字符串中某字符最后出现处起的字符串, 
length = -1 表示前面到某字符最后出现处的字符串
length > 0 表示某字符最后出现处起的字符串截取 length 个字符
length = undefined||0 表示某字符最后出现处起的字符串
*/
var $_findend = function ( needle, str, length ) {
	var pos=$_find(needle,str)?(str+'').lastIndexOf(needle):-1;
	if ( pos != -1 ) {
		if ( length > 0 )
			return str.substr( pos + needle.length, length );

		if ( length == -1 )
			return str.substr( 0, pos );

		return str.substr( pos + needle.length )
	}
	return str
}

/*截获商店URL地址最后出现"/"起的字符串*/
var $_findext = function ( src ) {
	if(!src) return '';
	return $_findend('/', src).replace(/^(index\.php)?(\?)?/i, '').replace(/%2C/ig, ',')
}

var $_IMGDIR = $_findend('/', $('ducedis_r').src, -1); /*模板图片URL目录*/
var $_INADMIN = parent.SHOPADMINDIR ? true : false;
var $_SHOPBASE = $_IMGDIR.replace(/\/themes\/[a-z0-9_\-]+\/images([\/]+)?$/, '/');
var $_LOCAL = location.href.replace(/%2C/ig, ',');

var $_DUCEDIS = {


	initHead: function () {

		this.showItemSet = false;
		this.showItemChild('.navbar .GoodsDropNavs .n-cat-depth-1');
		var _this = this;
		window.addEvent('domready',function(){
			_this.fixGoodsImgSize($$('.goodpic','.goodsImg','.dComment .pic','.goods-detail-pic-thumbnail a'));
			_this.fixGoodsImgSize($$('.goods-detail-pic'),'middle');
		});

		window.addEvent('domready', function(){
			var hotcat = $E('.hotcatsWrap');
			if ( hotcat ) {
			hotcat.getElements('ul').each(function(ul){
				ul.addEvents({
					'mouseenter': function(){this.addClass('current');},
					'mouseleave': function(){this.removeClass('current');}
				})
			});
			}
		});


	},
	

	/* 初始化左边栏商品分类列表 */
	showItemChild: function(spec){
		if(this.showItemSet&&!spec){
			return;
		}
		
		var cats = $ES(spec?spec:'.border_item .c-cat-depth-1');
		if(!cats[0]) return;

		var _this=this,
			u=this.furl($_LOCAL),
			n,
			all=false;

		if(!spec){
			this.showItemSet = true;
			all=$E('.border_item li.current')?false:true;
		}

		var reid = 0;
		var config = $_CONFIG['cats']||{};

		cats.each(function(cat){
			var a = $ES('a', cat);
			var show = all||false;
			if(reid || !cat.hasClass('cat-custom')) {
				show = show||$_find(config[reid], $_LOCAL)||u==_this.furl(a[0].href);
				if( config[reid]){a[0].href = ($_find('http://',config[reid],1)?'':$_SHOPBASE) + config[reid];}
				reid++;
			}
			if(spec){return;}
			if(show){
				if(a[1]) cat.addClass('current');
			}
			if(a[1]){
				new Element('cite', {
					'title': $_CONFIG['lang'][cat.hasClass('current')?'to_close':'to_open'],
					'events': {
						'click': function(){
							if( this.getParent().hasClass('current') ) {
								this.title = $_CONFIG['lang']['to_open'];
								this.getParent().removeClass('current');
							}else{
								this.title = $_CONFIG['lang']['to_close'];
								this.getParent().addClass('current');
							}
						}
					}
				}).injectBefore(a[0]);
				if ( window.ie6 )
					cat.setStyle('padding-top', '1px');
			}
		});

	},

	//修正tools.js中的getTrueWidth和getTrueHeight错误
	getPadding: function(c){
		return {
			x:(c.getStyle("padding-left").toInt()||0)+(c.getStyle("padding-right").toInt()||0),
			y:(c.getStyle("padding-top").toInt()||0)+(c.getStyle("padding-bottom").toInt()||0)
		}
	},

	getMargin: function(c){
		return {
			x:(c.getStyle("margin-left").toInt()||0)+(c.getStyle("margin-right").toInt()||0),
			y:(c.getStyle("margin-top").toInt()||0)+(c.getStyle("margin-bottom").toInt()||0)
		}
	},
	


	zoomImg: function(p,b,c,o){
		var z=o&&o!='middle'?true:false;
		if(z&&o.retrieve('size')){
			var f=o.retrieve('size');
		}else{
			var f=this.getTrueSize(c,p);
			if(z){o.store('size',f);}
			if(f.y<p.getSize().y&&!c.getStyle('height').toInt()){
				f.y = p.getSize().y;
			}
		}
		if(f.x<1||f.y<1){
			return
		}
		p.store('resized',true);
		var m={x:b.get("width"),y:b.get("height")};

		var s = {'width':m.x,'height':m.y};
		if(m.x>f.x){
			m.y=(f.x*m.y/m.x).toInt();
			s['width']=m.x=f.x;
			s['height']=window.ie6?m.y:'auto';
		}
		if(m.y>f.y){
			m.x=(f.y*m.x/m.y).toInt();
			s['width']=window.ie6?m.x:'auto';
			s['height']=m.y=f.y;
		}
		if(m.y<f.y&&o!='middle'){
			s['margin-top']=((f.y-m.y)/2).toInt();
		}
		/*if(img.getParent('.ducerank-hot')) {
			alert(f.x+'-'+f.y);
			//alert(this.getImgWard(img).x+'-'+this.getImgWard(img).y);
		}*/
		p.setStyles(s);
	},

	fixGoodsImgSize: function(ss,o){
		//return;
		var _this=this;
		ss.each(function(c,i){
			var p=c.getElement('img');
			var u=p.get("init_src")||p.get("src");
			if(!u||p.retrieve('resized')){return}
			new Asset.image(u, {
				onload:function(){
					//var g=p.getParent();
					if(!this||!this.width){
						return //g.adopt(p)
					}
					//if(!i) alert(this.get("height"));
					_this.zoomImg(p,this,c,o);
				}
			})
		});
	},


	
	extend: function(){
		var target = arguments[0] || {}, i = 1, length = arguments.length, options;
		if(typeof target != "object" && typeof target != "function")
			target = {};
		if(length == i){
			target = this;
			--i;
		}
		for ( ; i < length; i++ )
			if ( (options = arguments[i]) != null )
				for ( var name in options ) {
					var src = target[name], copy = options[name];
					if ( target === copy )
						continue;
					if ( copy !== undefined )
						target[ name ] = copy;
				}
		return target;
	}

};

