Namespace = new Object();
Namespace.register = function(fullNS)
{
    var nsArray = fullNS.split('.');
    var sEval = "";
    var sNS = "";
    for (var i = 0; i < nsArray.length; i++)
    {
        if (i != 0) sNS += ".";
        sNS += nsArray[i];
        sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS + " = new Object();"
    }
    if (sEval != "") eval(sEval);
}
Namespace.register("Common.Slide");
Namespace.register("Common.GetUrl");
Namespace.register("Common.Zoom");
Namespace.register("Common.ZoomPic");
Namespace.register("Common.Reflection");
Namespace.register("Common.WaterMark");
Namespace.register("Common.UI");
	$(document).ready(function(){	
	});	
	
function Slide_Control(dir){
	if(dir=="left"){
		obj.Move_Slide("left");		
	}
	else{
		obj.Move_Slide("right");		
	}
}
	
	
Common.Slide.Slide_Move=function(slidecontent,page_count,UL_Width,num_count,i,move_count){
	this.slidecontent=slidecontent;
	this.page_count=page_count; 
	this.UL_Width=UL_Width;
	this.num_count=num_count;
	this.i=i;
	this.move_count=move_count;
}
Common.Slide.Slide_Move.prototype.BindClick=function(popdiv,bgdiv,imgContainer){
	
	this.popdiv=popdiv;
	this.bgdiv=bgdiv;
	this.imgContainer=imgContainer;
	for(var n=0;n<this.num_count;n++){
		$("#"+this.slidecontent+" li").eq(n).bind("click",function(event){ Common.Slide.show_imgs(this,popdiv,bgdiv);} );
		$("#"+this.slidecontent+" li").eq(n).bind("mouseover",function(event){ Common.Slide.OverImg(this,imgContainer);} );

	}
}

Common.Slide.OverImg=function(li,slidebody){
	this.slidebody=slidebody;
	this.li=li;
	var imgs=li.getElementsByTagName('img')[0];
	 $("#"+slidebody).append("<div id=newBody  style='position:absolute;left:0px;top:0px;'></div>");
	var s=imgs.src;
	s=s.substring(s.lastIndexOf("/")+1);
	$("#"+slidebody+" img").animate({opacity:'1'}, 0 )
	.animate({opacity:'0'}, 2300 );
	$("#newBody").html("<img src=images/"+ s +">");
	$("#newBody img").animate({opacity:'0'}, 0 )
	.animate({opacity:'1'},1500);
	function setHTML(){
		$("#"+slidebody).html("<img src=images/"+ s +">");
		$("#newBody").remove();
	}
	setTimeout(setHTML,1500);
}


Common.Slide.show_imgs=function(li,popdiv,bgdiv){
		this.li=li;
		this.popdiv=popdiv;
		this.bgdiv=bgdiv;
		var imgs = li.getElementsByTagName('img')[0];
		$("#"+popdiv).empty();
		$("#"+bgdiv).css("visibility","visible");
		$("#"+popdiv).css("visibility","visible");
		var s=imgs.src.substring(imgs.src.lastIndexOf("/")+1);
		var hh=(document.body.clientHeight);
		$("#"+this.bgdiv).height(hh);
		
		var	s_hei=(document.documentElement.clientHeight -$("#"+this.popdiv).height())/2;
		var	s_wid=(document.documentElement.clientWidth-$("#"+this.popdiv).width())/2;
		$("#"+this.popdiv).css("top", s_hei+document.documentElement.scrollTop);
		$("#"+this.popdiv).css("left",s_wid);
		$(window).scroll(function () { 
				var new_hei=document.documentElement.scrollTop+s_hei;
				$("#new_modle").css("top",new_hei);
				$("#new_modle")
				.animate({top:new_hei},90);

		});
		
		$("#"+popdiv).html("<p><img src=images/"+ s +"></p>");
		$("#"+popdiv+" img").bind("click",function(event){ Common.Slide.clearpop(popdiv,bgdiv);} );
		$("#"+popdiv).animate({opacity:'0'}, 0 )
		.animate({opacity:'1'},1000);
	
}
Common.Slide.clearpop=function(popdiv,bgdiv){
	$("#"+popdiv).empty();
	$("#"+bgdiv).css("visibility","hidden");
	$("#"+popdiv).css("visibility","hidden");
}


Common.Slide.Slide_Move.prototype.Move_Slide=function(direction){
	this.direction=direction;
	for(var j=0;j<this.num_count;j++){
		$("#"+this.slidecontent+" img").eq(j).removeClass();
	}
	if(direction=="left"){
		this.i+=1;
		if(this.i<=this.page_count){
			$("#"+this.slidecontent).animate({marginLeft:-this.move_count*this.i},1500);
		}
		else{
			$("#"+this.slidecontent).animate({marginLeft:0},1500);
			this.i=0;
		}
		$("#"+this.slidecontent+" img").eq(this.i).addClass("current");
	}
	else{
		if(this.i-1>=0){
			this.i=this.i-1;
			$("#"+this.slidecontent+" img").eq(this.i).addClass("current");
			$("#"+this.slidecontent).animate({marginLeft: -this.move_count*this.i}, 1000);
		}
	}
}
Common.Slide.Slide_Move.prototype.Inalization=function(){
	$("#"+this.slidecontent).width(this.UL_Width);
}
Common.Slide.MoveSlide=function (slidecontent,page_count,UL_Width,num_count,i,move_count){
	Common.Slide.Slide_Move.call(this,slidecontent,page_count,UL_Width,num_count,i,move_count);
};
Common.Slide.MoveSlide.prototype=new Common.Slide.Slide_Move();

Common.GetUrl.getArgs=function(){
    var args = new Object();
    var query = location.search.substring(1);      // Get query string
    var pairs = query.split("&");                  // Break at ampersand
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');           // Look for "name=value"
        if (pos == -1) continue;                   // If not found, skip
        var argname = pairs[i].substring(0, pos); // Extract the name
        var value = pairs[i].substring(pos + 1);     // Extract the value
        value = decodeURIComponent(value);         // Decode it, if needed
        args[argname] = value;                     // Store as a property
    }
    return args;                                   // Return the object
}
Common.GetUrl.showUrl=function(){
    var a = window.location.href;
    var b = a.split("/");
    var c = b.slice(b.length - 1, b.length).toString(String).split(".");
    var d = c.slice(0, 1);
    return d;
}

Common.Zoom = function(o){
        var d = document,db=document.body,timgs=d.getElementsByTagName('img'),ie = !+'\v1',isStrict = d.compatMode == "CSS1Compat",
			opt = {
				width:300,
				height:140,
				offset:10,
				float:'rigth',
                minWidth:100
			},
			div = d.createElement('div'),divup = d.createElement('div'),
			getXY = function(el){
				var r={t:0,l:0},
					ua = navigator.userAgent.toLowerCase(),
					add = function(t,l){r.l+=l,r.t+=t},
					p = el;
				if(el&&el!=db){
					if(el.getBoundingClientRect){
						var b = el.getBoundingClientRect();
						add(b.top + Math.max(d.body.scrollTop,d.documentElement.scrollTop),
							b.left+Math.max(d.body.scrollLeft,d.documentElement.scrollLeft));
						isStrict?add(-d.documentElement.clientTop,-d.documentElement.clientLeft):add(-1,-1)
					}else{
						var dv = d.defaultView;
						while(p){
							add(p.offsetTop,p.offsetLeft);
							p = p.offsetParent;
						}
						p = el.parentNode;
						while (p && p != db) {
							add(-p.scrollTop,-p.scrollLeft);
							p = p.parentNode;
						}
					}
				}
				return r;
			},
			extend = function(t,s){
				for(var p in s){
					t[p] = s[p];
				};
			};
			div.id='zoomdiv',divup.id = 'zoomup';
			div.innerHTML = '<img id="bigimg" src="" />';
			db.appendChild(div);
			extend(opt,o);
			function leave(){
				div.style.display = divup.style.display = 'none';
				db.onmousemove = null;
			};
			for(var i=0,ci;ci=timgs[i++];){
				if(ci.className=='zoom'){
					ci.onmouseover = function(e){
						this.parentNode.appendChild(divup);
						var bimg=d.getElementById('bigimg'),bwidth,bheight,sx,sy,
							width = this.offsetWidth,height = this.height,top=getXY(this).t,left = getXY(this).l,tWidth,tLeft,sWidth;
						bimg.onload = function(){
                            div.style.display = 'block';
                            bwidth = bimg.width,bheight = bimg.height,
							sx = bwidth/width,sy = bheight/height;
                            tLeft =  opt.float=='rigth'?opt.offset+width+left:left-opt.offset-width*opt.width*sx,
                            sWidth = window.innerWidth||(isStrict?d.documentElement.clientWidth:db.clientWidth);
                            if(tLeft+opt.width+5>sWidth){
                                tWidth = sWidth - 5 - tLeft;
                                tWidth<opt.minWidth&&(tLeft = left-opt.offset-opt.width,tWidth=0);
                            };
                            extend(div.style,{
								left:tLeft +'px',
								top:top+'px',
								width:(tWidth||opt.width)+'px',
								height:opt.height+'px'
							});
							extend(divup.style,{
								width:(tWidth||opt.width)/sx+'px',
								height:opt.height/sy+'px',
                                display:'block'
							})
							db.onmousemove = function(e){
								var e = e || event,x=e.pageX||(e.clientX+(d.documentElement.scrollLeft||db.scrollLeft)),y=e.pageY||(e.clientY+(d.documentElement.scrollTop||db.scrollTop)),
									scrolly = y - divup.offsetHeight/2 - top,
									scrollx = x - divup.offsetWidth/2 - left;
								scrolly = y - divup.offsetHeight/2 < top ? 0 : y + divup.offsetHeight/2>height+top ? height - divup.offsetHeight  : scrolly;
								scrollx = x - divup.offsetWidth/2 < left ? 0 : x + divup.offsetWidth/2>width+left ? width - divup.offsetWidth  : scrollx;
								div.scrollTop = scrolly*sy;
								div.scrollLeft =  scrollx*sx;
								extend(divup.style,{top:scrolly+'px',left:scrollx+'px'});
							}

						}
                        bimg.src = this.getAttribute('big');
					};
					ci.parentNode[ie?'onmouseleave':'onmouseout'] = ie?leave:function(e){
						!(this===e.relatedTarget||(this.contains?this.contains(e.relatedTarget):this.compareDocumentPosition(e.relatedTarget)==20))&&leave();
					}
				}
			};    
    }

if (!document.myGetElementsByClassName) {
	document.myGetElementsByClassName = function(className) {
		var children = document.getElementsByTagName('*') || document.all;
		var elements = new Array();
	  
		for (var i = 0; i < children.length; i++) {
			var child = children[i];
			var classNames = child.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j] == className) {
					elements.push(child);
					break;
				}
			}
		}
		return elements;
	}
}

var Reflection = {
	defaultHeight : 0.5,
	defaultOpacity: 0.5,
	
	add: function(image, options) {
		Reflection.remove(image);
		
		doptions = { "height" : Reflection.defaultHeight, "opacity" : Reflection.defaultOpacity }
		if (options) {
			for (var i in doptions) {
				if (!options[i]) {
					options[i] = doptions[i];
				}
			}
		} else {
			options = doptions;
		}
	
		try {
			var d = document.createElement('div');
			var p = image;
			
			var classes = p.className.split(' ');
			var newClasses = '';
			for (j=0;j<classes.length;j++) {
				if (classes[j] != "reflect") {
					if (newClasses) {
						newClasses += ' '
					}
					
					newClasses += classes[j];
				}
			}

			var reflectionHeight = Math.floor(p.height*options['height']);
			var divHeight = Math.floor(p.height*(1+options['height']));
			
			var reflectionWidth = p.width;
			
			if (document.all && !window.opera) {
				/* Fix hyperlinks */
                if(p.parentElement.tagName == 'A') {
	                var d = document.createElement('a');
	                d.href = p.parentElement.href;
                }  
                    
				/* Copy original image's classes & styles to div */
				d.className = newClasses;
				p.className = 'reflected';
				
				d.style.cssText = p.style.cssText;
				p.style.cssText = 'vertical-align: bottom';
			
				var reflection = document.createElement('img');
				reflection.src = p.src;
				reflection.style.width = reflectionWidth+'px';
				reflection.style.display = 'block';
				reflection.style.height = p.height+"px";
				
				reflection.style.marginBottom = "-"+(p.height-reflectionHeight)+'px';
				reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(options['opacity']*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(options['height']*100)+')';
				
				d.style.width = reflectionWidth+'px';
				d.style.height = divHeight+'px';
				p.parentNode.replaceChild(d, p);
				
				d.appendChild(p);
				d.appendChild(reflection);
			} else {
				var canvas = document.createElement('canvas');
				if (canvas.getContext) {
					/* Copy original image's classes & styles to div */
					d.className = newClasses;
					p.className = 'reflected';
					
					d.style.cssText = p.style.cssText;
					p.style.cssText = 'vertical-align: bottom';
			
					var context = canvas.getContext("2d");
				
					canvas.style.height = reflectionHeight+'px';
					canvas.style.width = reflectionWidth+'px';
					canvas.height = reflectionHeight;
					canvas.width = reflectionWidth;
					
					d.style.width = reflectionWidth+'px';
					d.style.height = divHeight+'px';
					p.parentNode.replaceChild(d, p);
					
					d.appendChild(p);
					d.appendChild(canvas);
					
					context.save();
					
					context.translate(0,image.height-1);
					context.scale(1,-1);
					
					context.drawImage(image, 0, 0, reflectionWidth, image.height);
	
					context.restore();
					
					context.globalCompositeOperation = "destination-out";
					var gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
					
					gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
					gradient.addColorStop(0, "rgba(255, 255, 255, "+(1-options['opacity'])+")");
		
					context.fillStyle = gradient;
					context.rect(0, 0, reflectionWidth, reflectionHeight*2);
					context.fill();
				}
			}
		} catch (e) {
	    }
	},
	
	remove : function(image) {
		if (image.className == "reflected") {
			image.className = image.parentNode.className;
			image.parentNode.parentNode.replaceChild(image, image.parentNode);
		}
	}
}

Common.Reflection.addReflections=function() {
	var rimages = document.myGetElementsByClassName('reflect');
	for (i=0;i<rimages.length;i++) {
		var rheight = null;
		var ropacity = null;
		
		var classes = rimages[i].className.split(' ');
		for (j=0;j<classes.length;j++) {
			if (classes[j].indexOf("rheight") == 0) {
				var rheight = classes[j].substring(7)/100;
			} else if (classes[j].indexOf("ropacity") == 0) {
				var ropacity = classes[j].substring(8)/100;
			}
		}
		
		Reflection.add(rimages[i], { height: rheight, opacity : ropacity});
	}
}
Common.WaterMark=function(id,text,color,bgcolor){
	this.id=id;
	this.text=text;
	this.color=color;
	var altText=document.getElementById(id).value=text;
	Init();
	function Init(){
		document.getElementById(id).style.color=color;
		document.getElementById(id).style.backgroundColor=bgcolor;
	}
	document.getElementById(id).onmousedown=function show(){
		document.getElementById(id).value="";
	}
	document.getElementById(id).onblur=function change(){
		if(document.getElementById(id).value=="" || document.getElementById(id).value==altText){
			altText=document.getElementById(id).value=text;
		}
	}
}
Common.ZoomPic=function(){
	var zoompos = {x:0,y:0};//定义一个对象，缓存x,y变量.
	var $pre = $("#preview");
	var $preimg = $("#preview img");
	var p_w  = parseInt( $preimg.css("width"),10);  //这里不能使用 $preimg.width(). width()方法对隐藏元素无效
	var p_h  = parseInt( $preimg.css("height"),10);
	$(".zoomple").bind("mouseenter",function(e){
		var $this  = $(this);
		var $zoom = $this.find(".zoomplePopup");
		$zoom.show();
		$pre.show();
		PositionPopupZoom( $this , $zoom , e.pageX , e.pageY , p_w , p_h );
		$this.bind("mousemove",function(e){
			setTimeout(function(){
				PositionPopupZoom( $this , $zoom , e.pageX , e.pageY , p_w , p_h);
			},10);
		});
	}).bind("mouseleave",function(){
		var $this  = $(this);
		var $zoom = $this.find(".zoomplePopup");
		$zoom.hide();
		$pre.hide();
		$this.unbind("mousemove");
	});
	function PositionPopupZoom( wrap , zoom , x , y , w , h){
		var wrapLeft = wrap.offset().left;
		var wrapTop =  wrap.offset().top;
		var zoomWidth = zoom.width();
		var zoomHeight = zoom.height();
		var wrapWidth = wrap.width();
		var wrapHeight = wrap.height();
		//计算半透明层的x,y.确定它不超出图片
		zoompos.x =  x -wrapLeft - (zoomWidth/2);
		zoompos.y =  y -wrapTop- (zoomHeight/2);
		if( zoompos.x <= 0 ){
			zoompos.x  =  0;
		}
		if( zoompos.y <= 0 ){
			zoompos.y  =  0;
		}
		if( zoompos.x + zoomWidth >= wrapWidth){
			zoompos.x  = wrapWidth  -  zoomWidth; 
		}
		if( zoompos.y + zoomHeight >= wrapHeight){
			zoompos.y  = wrapHeight  -  zoomHeight; 
		}
		//放大比例
		var xRatio = w / wrapWidth;  
		var yRatio = h / wrapHeight;
		//设置位置
		zoom.css({left:zoompos.x,top:zoompos.y});
		$preimg.css({left: -(zoompos.x * parseInt(xRatio, 10)) , top:  -(zoompos.y * parseInt(yRatio, 10))  }); 
	}
}
Common.UI.Tab=function(TabHead,TabContent){
	var Tab_Length=$("#"+TabHead+" li").length;
	for(var i=0;i<Tab_Length;i++){
		var j=i;
		$("#"+TabHead+" a").removeClass();
		$("#"+TabHead+" a").eq(i).bind("mouseover",{foo:j},ChangeTab );
		
	}
	function ChangeTab(id){
		var id=id.data.foo;
		for(var i=0;i<Tab_Length;i++){
			if(i==id){
				$("#"+TabContent+" li").eq(i).css("display","inline");
			}
			else{
				$("#"+TabContent+" li").eq(i).css("display","none");
			}
		}
	}
}
Common.UI.Hidden=function(DivId){
	
	$("#"+DivId).animate({opacity:1}, 0)
	.animate({opacity:0}, 1000)
	;
}
Common.UI.Show=function(DivId){
	
	$("#"+DivId).animate({opacity:0}, 0)
	.animate({opacity:1}, 1000)
	;
}
