var mopTipCount=0;
var startOpenMopTip=false;
var mopTipWindW,mopTipWindH,mopTipStyle;
var mopTipOpened=false;
var mopTipW,mopTipH;
var mopTipFunc,mopTipPin=false,mopTipContent;
var mopTipScrollX,mopTipScrollY,mopTipMouseX,mopTipMouseY;
var mopTipxOffset,mopTipyOffset;
var mopTipUa=navigator.userAgent,mopTipBrowser,mopTipIe=false;
var mopTipLeft,mopTipTop;
var mopTipHover=false;
var mopTipYoff;
var mopTipNoHeight=false;
var overClickCheck=false;
var closeBtnClick=false;

/*line of changing shape from page bottom*/
var mopTipChangeY=200;
jQuery.fn.extend({
	mopTip:function(setting){
		$("#mopTip01").hide();
		$("#mopTip01 .mop_close").hide();
		mopTipH=null;
		if($(this).attr("href")=="#"){
			$(this).attr({href:"#?moptip"});
		};
		$(this).css({cursor:"pointer"});

		if(setting.act == "click") {

		$(this).click(function(evt){
			$("#mopTip01 .mop_close").hide(); 
			$("#mopTip01 .mop_content").html("");
			if(mopTipCount==0){
				mopTipFunc.tipInit();
				mopTipCount+=1;
			};
			if(mopTipUa.indexOf("MSIE")>-1){
					mopTipIe=true;
			};
			mopTipW=setting.w;
			mopTipH=setting.h;
			mopTipStyle=setting.style;
			mopTipContent=$(setting.get).html();
			mopTipYoff=setting.yOff;
			
			if(mopTipH==null){
				mopTipNoHeight=true;
				if(mopTipYoff==null){
					mopTipYoff=-150;
				}
			}else{
				mopTipNoHeight=false;
			}
			$("#mopTip01 .mop_tip").css({width:mopTipW+"px"});
			if(mopTipH!=null){
				$("#mopTip01 .mop_tip").css({height:mopTipH+"px"});
			}else{
				mopTipH==null
				$("#mopTip01 .mop_tip").css({height:"0px"});
			}
			$("#mopTip01 .mop_content").html(mopTipContent);
			mopTipFunc.findPosi(evt);
			$("#mopTip01 table").hover(
				function(){mopTipHover==true;},
				function(){mopTipHover==false;}
			);
		});

		} else {

		$(this).mouseover(function(evt){
			$("#mopTip01 .mop_close").hide(); 
			$("#mopTip01 .mop_content").html("");
			if(mopTipCount==0){
				mopTipFunc.tipInit();
				mopTipCount+=1;
			};
			if(mopTipUa.indexOf("MSIE")>-1){
					mopTipIe=true;
			};
			mopTipW=setting.w;
			mopTipH=setting.h;
			mopTipStyle=setting.style;
			mopTipContent=$(setting.get).html();
			mopTipYoff=setting.yOff;
			
			if(mopTipH==null){
				mopTipNoHeight=true;
				if(mopTipYoff==null){
					mopTipYoff=-150;
				}
			}else{
				mopTipNoHeight=false;
			}
			$("#mopTip01 .mop_tip").css({width:mopTipW+"px"});
			if(mopTipH!=null){
				$("#mopTip01 .mop_tip").css({height:mopTipH+"px"});
			}else{
				mopTipH==null
				$("#mopTip01 .mop_tip").css({height:"0px"});
			}
			$("#mopTip01 .mop_content").html(mopTipContent);
			mopTipFunc.findPosi(evt);
			$("#mopTip01 table").hover(
				function(){mopTipHover==true;},
				function(){mopTipHover==false;}
			);
		});
		};
		$(this).mouseout(function(){
			if(mopTipStyle=="overOut"){
				mopTipFunc.tipClose();
			}
			startOpenMopTip=false;
			mopTipStyle=false;
			mopTipPin=false;
		});
		$(this).click(function(){
			if(mopTipOpened==true){
				mopTipPin=true;
			}
		});
		$(this).mousemove(function(evt){
				$(".dForIe").hide();			   
				$(".dForIe").html(mopTipMouseX+mopTipxOffset);
				if((mopTipStyle!=false)&&(mopTipPin==false)){
					mopTipFunc.findPosi(evt);
				}
		});
		mopTipFunc={
			tipInit:function(){
				$("body").append(
					'<div class="dForIe"></div>'+
					'<div id="mopTip01">'+
					  '<table border="0" cellspacing="0" cellpadding="0">'+
						'<tr>'+
						  '<td class="mop_leftTop">&nbsp;</td>'+
						  '<td><table class="mop_arrowSet" border="0" cellspacing="0" cellpadding="0">'+
							'<tr>'+
							  '<td class="mop_arrow">&nbsp;</td>'+
							  '<td class="mop_top">&nbsp;</td>'+
							'</tr>'+
						  '</table></td>'+
						  '<td class="mop_rightTop">&nbsp;</td>'+
						'</tr>'+
						'<tr>'+
						  '<td class="mop_left">&nbsp;</td>'+
						  '<td class="mop_tip"><div class="mop_content"></div></td>'+
						  '<td class="mop_right">&nbsp;</td>'+
						'</tr>'+
						'<tr>'+
						  '<td class="mop_leftBottom">&nbsp;</td>'+
						  '<td><table class="mop_arrowSetBottom" border="0" cellspacing="0" cellpadding="0">'+
							'<tr>'+
							  '<td class="mop_arrowBottom">&nbsp;</td>'+
							  '<td class="mop_bottom">&nbsp;</td>'+
							'</tr>'+
						  '</table></td>'+
						  '<td class="mop_rightBottom"></td>'+
						'</tr>'+
					  '</table>'+
					  '<div class="mop_close">&nbsp;</div>'+
					'</div>'
				);
				/*close button click*/
				$("#mopTip01 .mop_close").click(function(){
					closeBtnClick=true;	 
					mopTipFunc.tipClose();
				});
			},
			findPosi:function(evt){
				/*position*/
				if(mopTipIe==true){
					mopTipScrollX = document.documentElement.scrollLeft;
					mopTipScrollY = document.documentElement.scrollTop;
					mopTipMouseX=window.event.clientX+mopTipScrollX;
					mopTipMouseY=window.event.clientY+mopTipScrollY;
				}else{
					mopTipScrollX = window.pageXOffset;
					mopTipScrollY = window.pageYOffset;
					mopTipMouseX=evt.pageX;
					mopTipMouseY=evt.pageY;
				}
				mopTipWindW=document.documentElement.clientWidth;
				mopTipWindH=document.documentElement.clientHeight;
				var windXsc=mopTipWindW+mopTipScrollX;
				var windYsc=mopTipWindH+mopTipScrollY;
				var checkX=windXsc-mopTipMouseX;
				var checkY=windYsc-mopTipMouseY;
				
				var mopTipCheckHeight=mopTipChangeY;
				var preCheckHeight=mopTipH+40+40;

				
				if(mopTipNoHeight==false){
					if(mopTipCheckHeight<preCheckHeight){
						mopTipCheckHeight=preCheckHeight;
					}
				}else{
					mopTipCheckHeight=mopTipChangeY;
				}
				mopTipLeft=mopTipMouseX+mopTipxOffset;
				mopTipTop=mopTipMouseY+mopTipyOffset;
				if((checkX>mopTipW)&&(checkY>mopTipCheckHeight)){
					/*shape left top*/
					mopTipxOffset=-50;
					mopTipyOffset=30;
					$("#mopTip01 .mop_arrowSet tr").html('<td class="mop_arrow">&nbsp;</td><td class="mop_top">&nbsp;</td>');
					$("#mopTip01 .mop_top").css({width:mopTipW-60+"px",height:"20px"});
					$("#mopTip01 .mop_arrowSet").css({width:mopTipW+"px"});
					$("#mopTip01 .mop_arrowSetBottom tr").html('<td class="mop_bottom">&nbsp;</td>');
					$("#mopTip01 .mop_bottom").css({width:mopTipW+"px",height:"20px"});
				}else if((checkX<=mopTipW)&&(checkY>mopTipCheckHeight)){
					/*shape right top*/
					mopTipxOffset=-mopTipW-10;
					mopTipyOffset=30;
					$("#mopTip01 .mop_arrowSet tr").html('<td class="mop_top">&nbsp;</td><td class="mop_arrow">&nbsp;</td>');
					$("#mopTip01 .mop_top").css({width:mopTipW-60+"px",height:"20px"});
					$("#mopTip01 .mop_arrowSet").css({width:mopTipW+"px"});
					$("#mopTip01 .mop_arrowSetBottom tr").html('<td class="mop_bottom">&nbsp;</td>');
					$("#mopTip01 .mop_bottom").css({width:mopTipW+"px",height:"20px"});
				}else if((checkX>mopTipW)&&(checkY<=mopTipCheckHeight)){
					/*shape left bottom*/
					mopTipxOffset=-50;
					if(mopTipNoHeight==false){
						mopTipyOffset=-(mopTipH+40+40);
					}else{
						mopTipyOffset=mopTipYoff;
					}
					$("#mopTip01 .mop_arrowSet tr").html('<td class="mop_top"></td>');
					$("#mopTip01 .mop_top").css({width:mopTipW+"px",height:"20px"});
					$("#mopTip01 .mop_arrowSet").css({width:mopTipW+"px"});
					$("#mopTip01 .mop_arrowSetBottom tr").html('<td class="mop_arrowBottom">&nbsp;</td><td class="mop_bottom">&nbsp;</td>');
					$("#mopTip01 .mop_bottom").css({width:mopTipW-60+"px",height:"20px"});
				}else if((checkX<=mopTipW)&&(checkY<=mopTipCheckHeight)){
					/*shape right bottom*/
					mopTipxOffset=-mopTipW-10;
					if(mopTipNoHeight==false){
						mopTipyOffset=-(mopTipH+40+40);
					}else{
						mopTipyOffset=mopTipYoff;
					}
					$("#mopTip01 .mop_arrowSet tr").html('<td class="mop_top"></td>');
					$("#mopTip01 .mop_top").css({width:mopTipW+"px",height:"20px"});
					$("#mopTip01 .mop_arrowSet").css({width:mopTipW+"px"});
					$("#mopTip01 .mop_arrowSetBottom tr").html('<td class="mop_bottom">&nbsp;</td><td class="mop_arrowBottom">&nbsp;</td>');
					$("#mopTip01 .mop_bottom").css({width:mopTipW-60+"px",height:"20px"});
				}
				/*pngFix*/
				$("#mopTip01").pngFix();
				mopTipFunc.setPosi();
			},
			setPosi:function(){
				$("#mopTip01").css({
					position: "absolute",
					width:mopTipW+40+"px",
					left:mopTipMouseX+mopTipxOffset+"px",
					top:mopTipMouseY+mopTipyOffset+"px"
				});
				mopTipFunc.openTimeout();
			},
			openTimeout:function(){
				startOpenMopTip=true;
				if(mopTipStyle!="overOut"){
					$("#mopTip01 .mop_close").show();
				}
				setTimeout("mopTipFunc.tipOpen()",500);
			},
			tipOpen:function(){
				if(startOpenMopTip==true){
					/*put content*/
					$("#mopTip01").show();
					mopTipOpened=true;
					closeBtnClick=false;
				}
			},
			tipClose:function(){
				startOpenMopTip=false;
				mopTipOpened=false;
				mopTipH=null;
				setTimeout("mopTipFunc.tipClosing()",100);
			},
			tipClosing:function(){
				//$("#mopTip01 table").focus();
				$("#mopTip01 .mop_close").hide();
				$("#mopTip01").hide();
			} 
		}
	}
});
