var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function $$(id) {
	return document.getElementById(id);
}
function doane(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}

var open_div_win_position = new Array();
function openDivWindow(action, width, height, link_href) {
	if ($$('open_div_win_layer') != null) {
		$$('open_div_win_layer').style.display = 'none';
		$$('open_div_win_layer').innerHTML = '';
	}
	var objs = document.getElementsByTagName("OBJECT");
	if(action == 'open') {
		for(i = 0;i < objs.length; i ++) {
			if(objs[i].style.visibility != 'hidden') {
				objs[i].setAttribute("oldvisibility", objs[i].style.visibility);
				objs[i].style.visibility = 'hidden';
			}
		}
		var clientWidth = document.body.clientWidth;
		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		/*if (win_type == 'small') {
			var open_div_win_width = 400;
			var open_div_win_height = clientHeight * 0.7;
		} else {
			var open_div_win_width = 800;
			var open_div_win_height = clientHeight * 0.8;
		}*/
		var open_div_win_width = width;
		var open_div_win_height = height;
		if(!$$('open_div_win_layer') || $$('open_div_win_layer').innerHTML == '') {
			if($$('open_div_win_layer') != null) {
				div = $$('open_div_win_layer');
			} else {
				div = document.createElement('div');div.id = 'open_div_win_layer';
			}
			div.style.width = open_div_win_width + 'px';
			div.style.height = open_div_win_height + 'px';
			div.style.left = ((clientWidth - open_div_win_width) / 2) + 'px';
			div.style.position = 'absolute';
			div.style.zIndex = '999';
			$$('append_parent').appendChild(div);
			$$('open_div_win_layer').innerHTML = '<div style="width: ' + open_div_win_width + 'px; background: #666666; margin: 5px auto; text-align: left">' +
				'<div style="width: ' + open_div_win_width + 'px; height: ' + open_div_win_height + 'px; padding: 1px; background: #FFFFFF; border: 1px solid #7597B8; position: relative; left: -6px; top: -3px">' +
				'<div onmouseover="openDivWindowdrag(this)" style="cursor: move; position: relative; left: 0px; top: 0px; width: ' + open_div_win_width + 'px; height: 30px; margin-bottom: -30px;background-color:#5a9a18"></div>' +
				'<a href="#" onclick="openDivWindow(\'close\');return false;"><img style="position: absolute; right: 20px; top: 15px" src="/admin/hlplugin/images/close.gif" title="close" border="0" /></a>' +
				'<div id="open_div_win_mask" style="margin-top: 30px; position: absolute; width: 100%; height: 100%; display: none"></div><iframe id="open_div_win_frame" name="open_div_win_frame" style="width:' + open_div_win_width + 'px;height:100%;margin-top:30px;" allowTransparency="true" frameborder="0"></iframe></div></div>';
		}
		$$('open_div_win_layer').style.display = '';
		$$('open_div_win_layer').style.top = ((clientHeight - open_div_win_height) / 2 + scrollTop) + 'px';
		if (typeof(link_href) == 'object') {
			open_div_win_frame.location = link_href.href;
		} else {
			open_div_win_frame.location = link_href;
		}
	} else if(action == 'close') {
		for(i = 0;i < objs.length; i ++) {
			if(objs[i].attributes['oldvisibility']) {
				objs[i].style.visibility = objs[i].attributes['oldvisibility'].nodeValue;
				objs[i].removeAttribute('oldvisibility');
			}
		}
		$$('open_div_win_layer').style.display = 'none';
		$$('open_div_win_layer').innerHTML = '';
	}
}

var open_div_win_drag_start = new Array();
function openDivWindowdrag(obj) {
	obj.onmousedown = function(e) {
		if(is_ie) {
			document.body.onselectstart = function() {
				return false;
			}
		}
		open_div_win_drag_start = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		open_div_win_drag_start[2] = parseInt($$('open_div_win_layer').style.left);
		open_div_win_drag_start[3] = parseInt($$('open_div_win_layer').style.top);
		$$('open_div_win_mask').style.display = '';
		doane(e);
	}
	_attachEvent(document.body, 'mousemove', function(e) {
		if(open_div_win_drag_start[0]) {
			var pmwindragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
			with($$('open_div_win_layer')) {
				style.left = (open_div_win_drag_start[2] + pmwindragnow[0] - open_div_win_drag_start[0]) + 'px';
				style.top = (open_div_win_drag_start[3] + pmwindragnow[1] - open_div_win_drag_start[1]) + 'px';
			}
			doane(e);
		}
	});
	obj.onmouseup = function(e) {
		if(is_ie) {
			document.body.onselectstart = function() {
				return true;
			}
		}
		open_div_win_drag_start = [];
		$$('open_div_win_mask').style.display = 'none';
		doane(e);
	}
	obj.onmouseover = null;
}

