/*------------------------------------------------------------*/
/*			
/*			Eshop店铺模版系统常用功能js
/*			
/*------------------------------------------------------------*/
function mod_tab(o){
	o = $(o);
	o.parent().find('span.focus').removeClass('focus');
	o.addClass('focus');
	o.parent().parent().find('div.wg_cont').hide();
	o.parent().parent().find('div.wg_cont').eq(o.parent().find('span').index(o)).show();
};
/*---幻灯片展示---*/
var slide = function(o){
	var _this = this;
	o.find('ol').each(function(){
		_this.num = 0;
		_this.self = $(this);
		_this.self.after('<div class="slide_num">');
		_this.nums = _this.self.next();
		_this.lis = _this.self.find('li');
		_this.lis.each(function(i){
			var span = $('<span>');
			span.html(i+1).attr('rel',i).hover(function(){
				clearInterval(_this.t)
				_this.run($(this).attr('rel'))
			},function(){
				_this.timer()
			});
			span.appendTo(_this.nums);
		});
		_this.nums.find('span').eq(0).addClass('hover');
	});
	this.timer();
};
slide.init = function(){
	$('div.slide').each(function(){
		return new slide($(this));
	})
};
slide.prototype = {
	run:function(i){
		this.nums.find('span').eq(this.num).removeClass('hover');
		this.nums.find('span').eq(i).addClass('hover');
		this.num = i;
		this.lis.eq(0).stop();
		this.lis.eq(0).animate({marginTop:'-'+this.lis.eq(0).height()*this.num+'px'});
	},
	timer:function(){
		var _this = this;
		this.t =  setInterval(function(){
			if(_this.num == _this.lis.size()-1){
				_this.i = 0;
			}else{
				_this.i = _this.num-1+2;
			}
			_this.run(_this.i);
		},2500)
	}
};
$(slide.init);
/*---商品分类列表---*/
function sideCate(){
    //默认折叠方式
    $('h3.expand,h3.sub-cate').click(function(){
    	if ($(this).hasClass('sub-cate')) {
    		$(this).removeClass('sub-cate').addClass('expand').next('div').show();
    	}
    	else {
    		$(this).removeClass('expand').addClass('sub-cate').next('div').hide();
    	}	
    });
    $('h3.sub-cate a').click(function(){
    	window.location = $(this).attr('href');
    	return false;
    });
    if(!$('h3.sub-cate a.current').parent().parent().hasClass('cate_pop')){
        $('h3.sub-cate a.current').parent().removeClass('sub-cate').addClass('expand').next('div').show();
    }
   
    //popup弹出方式
    $('div.cate_pop h3').hover(function(){
        $('.cate_pop h3').removeClass('hover');
        $(this).addClass('hover');
        $('.cate_pop div').hide();
        if($(this).next().is('div')){
            var position = $(this).position();
            var h3width = $(this).width();
            $objn = $(this).next();
            $objn.css({'left':position.left+h3width,'top':position.top,'position':'absolute','width':h3width})
		    $objn.show();
        }
	},function(){
        flag = 0;
        $this = $(this);
        if($(this).next().is('div')){
            var $obj = $(this).next();
            $obj.hover(function(){
                flag=1;$(this).show();
                },function(){
                flag=0;$(this).hide();
                $this.removeClass('hover');
            });
            setTimeout(function(){
                if(flag==0){$obj.hide();$this.removeClass('hover');}
            },600);
        }
    });
};
/*---导航菜单下拉菜单---*/
function menuHover(){
$('div.menu_more').each(function(){
		var _self = $(this);
		_self.hover(function(){
            _self.addClass('current');
			_self.find('ul').slideDown('fast')
		},function(){
            _self.removeClass('current');
			_self.find('ul').slideUp('fast')
		})
	});
	$('div.widget').each(function(){
    	if($(this).attr('hover')){
    		$(this).find('div.wg_title').find('span').mouseover(function(){
	    		$(this).click();
	    	})
    	}
    });
};
/*---随页面加载运行代码---*/
$(function(){
    sideCate();
    menuHover();
});

var replaceAll = function(_str,s1,s2)
{
    return _str.replace(new RegExp(s1,"gm"),s2);
};
var _location = document.location.href;
_location = new String(_location);
_location = replaceAll(_location,'&','%26');

function setdiv(obj, type){
    obj.style.display = type;
};

function listtab(){
	$('.settings .tabcont').hide();
	$('.settings .tabtitle li a').toggle(function(){
		$('.settings .tabtitle li').removeClass('current');
		$(this).parent().addClass('current');
		$('.settings .tabcont').hide();
		$('.settings .tabcont').eq($('.settings .tabtitle li').index($(this).parent())).show()
	},function(){
		$('.settings .tabtitle li').removeClass('current');
		$('.settings .tabcont').hide();
	})
};

$(document).ready(function(){
  listtab();
  $('.tabcont li a').click(function(){
  	$(this).parent().parent().hide();
  	$('.tabtitle li.current a').addClass('cancel').text('撤销');
	$('.tabtitle li a.cancel').click(function(){
		$('.tabtitle li.current').removeClass('current');
		$('.settings .tabcont').eq($('.settings .tabtitle li').index($(this).parent())).hide()
  	$(this).text('请选择').removeClass('cancel');
	$(this).parent('li').find('strong').remove();
	listtab();
  });
	if($('.tabtitle li.current strong').length>0){
		
		$('.tabtitle li.current strong').text($(this).text());
	}else{
		$('.tabtitle li.current a').before('<strong>'+$(this).text()+'</strong>');
	}
	$('.tabtitle li.current').removeClass('current');	
  });
});

/*---Mbox---*/
var popup = {
	box:'<div id="moverlay"></div><div id="mshadow"></div><div id="mbox"><div id="mdel"><a href="javascript:void(0);"></a></div><div id="mcontent"></div></div>',
	ctrlbox:'<div id="mctrl"><label class="button5"><button id="msave">保 存</button></label><label class="button5"><button id="mcal">取 消</button></label></div>',
	iframelay:'<iframe src="javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px;height:0;width:0;z-index:-1; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe>',
	init:function(data){
		$(popup.box).appendTo('body');
		if(data.ctrl){$('#mcontent').after(popup.ctrlbox)};
		if(data.del == false){
			$('#mdel').remove();
		}else{
			$('#mdel a,#mcal').click(function(){
				popup.quit();
			});
		};
		if(data.url){
			$('#mcontent').load(data.url,function(){
				popup.fx(data.norm);data.callback?data.callback.call():'';
			});
		};
		if(data.cont){
			$('#mcontent').append(data.cont.hide().show());
			data.callback?data.callback.call():'';
			popup.fx(data.norm);
		};
		$('#mshadow').css({opacity:0.3});
	},
	fx:function(n){
		var l = $('#mbox').width()/2;
		$('#mbox').css({top:popup.getTop()-1+151}).addClass('norm'+n);
		$('#mshadow').height($('#mbox').outerHeight()).width($('#mbox').outerWidth()-1).css({
			top:popup.getTop()-1+155,
			marginLeft:'-'+(($('#mbox').outerWidth()-12)/2)+'px'
		}).addClass('norm1shadow'+n);
		$('#mdel').width($('#mbox').width());
		$('#moverlay').height(Math.max($('body').height(),$(window).height()));
		if (!window.XMLHttpRequest) {
			$('#mshadow,#mbox,#moverlay').append(popup.iframelay);
			$('#mbox iframe,#mshadow iframe').width($('#mbox').outerWidth()-1+30).height($('#mbox').outerHeight()-1+30);
			$('#moverlay iframe').width('100%').height($('#moverlay').height());
		};
	},
	quit:function(){
		$('#moverlay,#mshadow').animate({opacity:'-0'},100,function(){
			$('#mbox,#moverlay,#mshadow').remove();
		});
	},
	getTop:function (){
        if (document.documentElement && document.documentElement.scrollTop) {
            return document.documentElement.scrollTop;
        }
        else 
            if (document.body) {
                return document.body.scrollTop;
            }
    }
};
/*---SetCookies---*/ 
(function(){
function SetCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString())+";path=/";        
}
window['RootCookies'] = {};
window['RootCookies']['SetCookie'] = SetCookie;
})();

/*---Get cookies Value---*/
var cookies = document.cookie.split("; ");
function GetCookieValue(cookieName) {
    var ret = null;
    for (var i = 0; i < cookies.length; i++) {
        var leeiiocookies = cookies[i].split("=");
        if (cookieName == leeiiocookies[0]) {
            ret = unescape(decodeURI(leeiiocookies[1]))
        }
    }
    return ret
};

/*---判断cookies的值---*/
//author:Leeiio(2009-08-15)
function popupOpen(obj,div){
switch(div){
	case 1:div = $('#popupTips1').clone();divtarget = 0;
	break;
	case 2:div = $('#popupTips2').clone();divtarget = 1;
	break;
	case 3:div = $('#popupTips3').clone();divtarget = 1;
	break;
	default:div = $('#popupTips1').clone();divtarget = 1;
	break;
}
	if ('no' == GetCookieValue('show_365tip')){
		window.open(url,'_blank');
		}else{
				popup.init({cont:div,norm:'1',ctrl:false,del:false,callback:function(){
					$('#mcontent button.nextStep').click(function(){
						if ($(this).parent().parent().find("input").attr('checked') == true) {
							RootCookies.SetCookie('show_365tip', 'no', 30);
							window.location.reload();
							}
						if (divtarget == 0) {
							window.location.href = $(obj).attr('href');
						}
						else {
							window.open($(obj).attr('href'), '_blank');
						}
						popup.quit();
						})
					}});
		}
};

var popupSystem = {
	open : function(id,title){
		var obj = $("#"+id);
		if (obj.size() > 0){
			var HTML = "<div id=\"popupSystemOverlay\"><iframe id=\"popupSystemIframe\"></iframe></div>";
			HTML += "<div id=\"popupSystemContent\">";
			HTML += "<div id=\"popupSystemTitle\"><span>" + title + "</span></div>";
			HTML += "<div id=\"popupSystemClose\"><a href=\"javascript:void(0)\" title=\"关闭\" onclick=\"popupSystem.close('" + id + "')\"></a></div>";
			HTML += "<div id=\"popupSystemInner\">" + obj.html() + "</div>";
			HTML += "</div>";
			obj.empty();
			$("body").append(HTML);
			$('#popupSystemOverlay').css({height:$(document).height(),'opacity':'0.2'});
			$('#popupSystemIframe').css({'height':$(document).height(),'width':'100%','background-color':'#fff','border':'none','position':'absolute','z-index':'99','top': '0','left': '0','filter':'alpha(opacity=0)','-moz-opacity': '0','opacity': '0'});
			$('#popupSystemContent').css({'margin-left':-$('#popupSystemContent').outerWidth(true)/2});
			$("#popupSystemOverlay,#popupSystemContent").show();
		}else{
			alert("调用程序出错，请检查参数！");
		}
	},
	close : function(id){
		var obj = $("#"+id);
		if (obj.size() > 0){
			obj.html($("#popupSystemInner").html());
			$("#popupSystemOverlay,#popupSystemContent").remove();
		}else{
			alert("调用程序出错，请检查参数！");		
		}
	}
};

/*---JS MAP---*/
//author:HuangFeng(2009-10-14)
function Map(){   
	var struct = function(key, value)
	{   
		this.key = key;   
		this.value = value;   
	}  
	var put = function(key, value)
	{   
		for (var i = 0; i < this.arr.length; i++)
		{   
		if (this.arr[i].key === key )
		{   
			this.arr[i].value = value;   
			return;   
		}   
		}   
		this.arr[this.arr.length] = new struct(key, value);   
	}   
	var get = function(key)
	{   
		for (var i = 0; i < this.arr.length; i++)
		{   
			if ( this.arr[i].key === key )
			{   
				return this.arr[i].value;   
			}   
		}   
 		return null;   
	}   
	var remove = function(key)
	{   
		var v;   
		for (var i = 0; i < this.arr.length; i++)
		{   
			v = this.arr.pop();   
			if ( v.key === key )
			{   
				continue;   
  			}   
  			this.arr.unshift(v);   
 		}   
	}    
	var size = function()
	{   
		return this.arr.length;   
	}   
   
	var isEmpty = function()
	{   
		return this.arr.length <= 0; 
	}   
 
	this.arr = new Array();   
	this.get = get;   
	this.put = put;   
	this.remove = remove;   
	this.size = size;   
	this.isEmpty = isEmpty;   
};

var b2c = {
select:function(oData){
		return{
			data:oData,
			process:function(){
				if(!this.data){
					return false;
				}else{
					for(var _i in this.data){
						alert(data[_i]);
					}
				}
			},
			init:function(){
				var data = this.data;
				var self = this;
				$("a",data.id).click(function(){
					if(data.isOnly){
						$('a',$(this).parents(data.id)).removeClass("current");
						$(this).toggleClass("current");
					}else{
						$(this).toggleClass("current");
					}
					var idRe = /\:(\d+)$/;
					$(this).attr("id").match(idRe);
					var store=RegExp.$1;
					//$(data.store).html(store); huangsui 2009-2-2
					var target = data.target;
					$(target).html("您已选择：");
					$(".current",data.id).each(function(){
						var sValue = "\""+$(this).text()+"\"";
						$(target).append("<strong>"+sValue+"</strong>");
					});
					return false;
				});
			}
		};
	}
};
