var _product;
function closeHandler(){
	_product.closeWin();
}
function iframeOnLoad(){
	_product.iframeOnLoad();
}
if(typeof ssy == "undefined"){ssy={}}
 ssy.mouseCoords = function(e){
	e = e || window.event
	if(e.pageX){ 
        return {x:e.pageX, y:e.pageY}; 
    } 
    return { 
        x:e.clientX + document.documentElement.scrollLeft+ document.body.scrollLeft,
		y:e.clientY + document.documentElement.scrollTop+ document.body.scrollTop
    }; 
}
Product =function(inConfig){
	var _serviceMin = new ServiceMin({openWinFn:openWinHandler});
	var _win = new WinPanel();
	function openWinHandler(e){
		_win.show(e);
	}
	function _closeWin(){
		_win.close();
	}
	function _dragIframe(e){
		_win.drag(e);
	}
	function _iframeOnLoad(){
		_win.iframeOnLoad();
	}
	return{
		closeWin:_closeWin,
		iframeOnLoad:_iframeOnLoad
	}
}

ServiceMin=function(inConfig){
	var _body = $('<div class="service_min"><div>').appendTo("body");
	_body.click(function(){ inConfig.openWinFn() });
	return{
		jq:_body
	}
}
WinPanel=function(){
	var _body = $('<div class="maskPanel hide"><div>').appendTo("body");
	var _iframe = $("<iframe class='iframe' style='left:-5000px' width='770px' height='453px' frameborder='0' scrolling='no' src='/box.aspx'></iframe>").appendTo("body");
    var _openYN = false;
    _body.click( _close );
	
    $(window).resize(function(){
		if(_openYN){
			_body.css("width", $("body").outerWidth()+ "px");
		    _body.css("height", $("body").outerHeight()+ "px");
		}
	});
	var _extraX=0;_extraY=0;
	var _bodyW=0;_bodyH=0;
	var _iframeDargBar = null;
	var _iframeInitiYN = false;
	function _iframeOnLoadHandler(){
		if ( $.browser.msie && !_iframeInitiYN){
			_iframeInitiYN = true;
			_iframe.get(0).contentWindow.window.location.reload();
			return;
		}
		_iframeDargBar = $( _iframe.get(0).contentWindow.document.getElementById('wtitle') );
		_iframeDargBar.mousedown(function(e){ 
		   if($.browser.msie){
				e = this.document.parentWindow.event;
		   }
		   var _mouseCoords = ssy.mouseCoords(e);
		   if ( $.browser.msie){
		      	_iframeDargBar.get(0).setCapture(false);
		   }else{
		     	_iframe.get(0).contentWindow.window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		   }
		   if($.browser.msie || navigator.userAgent.indexOf("Chrome")>0 ){
		   	_extraY =  _mouseCoords.y  - _iframeDargBar.position().top + document.body.scrollTop;
		    _extraX =  _mouseCoords.x  - _iframeDargBar.position().left +  document.body.scrollLeft;
		   }else{
		   	_extraY =  _mouseCoords.y  - _iframeDargBar.position().top + document.documentElement.scrollTop;
		    _extraX =  _mouseCoords.x  - _iframeDargBar.position().left + document.documentElement.scrollLeft;
		   }
		   _iframe.get(0).contentWindow.document.onmousemove =dragHandler;
		   _iframe.get(0).contentWindow.document.onmouseup = dragEndHandler;
		   
		   _bodyW = $("body").outerWidth();
		   _bodyH = $("body").outerHeight();
		});
	}
	function dragHandler(e){
		if($.browser.msie){
			e = _iframeDargBar.get(0).document.parentWindow.event;
		}
		 var _mouseCoords = ssy.mouseCoords(e);
		 var t_x = _iframe.position().left + _mouseCoords.x - _extraX;
		 t_x = t_x<0?0:t_x;
		 t_x = t_x +770 > _bodyW?_bodyW-770: t_x;
		 var t_y = _iframe.position().top + _mouseCoords.y - _extraY;
		 t_y = t_y<0?0:t_y;
		 t_y = t_y +453 > _bodyH?_bodyH-453: t_y;
		 
		_iframe.css("left",t_x + "px");
		_iframe.css("top", t_y + "px");
	}
	function dragEndHandler(e){
		_iframe.get(0).contentWindow.document.onmousemove = null;
		_iframe.get(0).contentWindow.document.onmouseup = null;
		if($.browser.msie){
		  _iframeDargBar.get(0).releaseCapture();
		  return;	
		}
		_iframe.get(0).contentWindow.window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	}
	function _show(e){
		_openYN = true;
		var t_width = 0;
		var t_height = 0;
		t_width = document.body.scrollLeft + document.documentElement.scrollLeft+ (document.documentElement.clientWidth-_iframe.width())/2;
		t_height =document.body.scrollTop + document.documentElement.scrollTop+ ( document.documentElement.clientHeight-_iframe.height())/2;
		
		_iframe.css("left", t_width<0?0:t_width  + "px");
		_iframe.css("top", t_height<0?0:t_height + "px");
		_body.css("width", $("body").outerWidth()+ "px");
		_body.css("height", $("body").outerHeight()+ "px");
		_body.removeClass("hide");
		
	}
	function _close(e){
		_openYN = false;
		_body.addClass("hide");
		_iframe.css("left","-5000px");
	}

	return{
		jq:_body,
		show:_show,
		close:_close,
		iframeOnLoad:_iframeOnLoadHandler
	}
	
}

$(function(){_product=new Product() ;});
