/*
	digist_lightbox.js
	Script by Venister(venister@empal.com)
*/
function initDigistLightbox() { dlbo = new digist_lightbox(); }
function digist_lightbox() {
	this.bLytebox = true;
	this.resizedOnly = false;
	this.slideInterval = 2000;
	this.bDisplaySave = false;
}

digist_lightbox.prototype.drag = function( id ) {
	var mx = 0, my = 0, el = xGetElementById(id);
	if (el) {
		el.xDragEnabled = true;
		xAddEventListener(el, 'mousedown', dragStart, false);
	}

	function dragStart(e)
	{
		if (el.xDragEnabled) {
			var ev = new xEvent(e);
			xPreventDefault(e);
			mx = ev.pageX;
			my = ev.pageY;
			xAddEventListener(document, 'mousemove', drag, false);
			xAddEventListener(document, 'mouseup', dragEnd, false);
		}
	}
	function drag(e)
	{
		var ev, dx, dy;
		xPreventDefault(e);
		ev = new xEvent(e);
		dx = ev.pageX - mx;
		dy = ev.pageY - my;
		mx = ev.pageX;
		my = ev.pageY;
		
		xMoveTo(el, xLeft(el) + dx, xTop(el) + dy);
	}
	function dragEnd(e)
	{
		var ev = new xEvent(e);
		xPreventDefault(e);
		xRemoveEventListener(document, 'mouseup', dragEnd, false);
		xRemoveEventListener(document, 'mousemove', drag, false);
	}
}

/**
 * @common.js의 resizeImageContents을 응용한 코드이다. 다만 이것을 가로채서 lytebox를 로딩한다. resizeImageContents와 원리가 동일하기 때문에 버그도 똑같다. -_-
 **/
 
 function digist_lightbox_hook() {
	var lytebox_rel = "lytebox[xeImage]";
	if( !dlbo.bLytebox ) lytebox_rel = "lyteshow[xeImage]";
 
    var objs = xGetElementsByTagName("IMG");
		
    //for (var i in objs) {
	for (var i=0; i<objs.length; i++) {
        var obj = objs[i];
		var bApply = false;
		var bBanner = false;

        if (!obj.parentNode) continue;
		
        if (/\/modules\//i.test(obj.src)) continue;
        if (/\/layouts\//i.test(obj.src)) continue;
        if (/\/widgets\//i.test(obj.src)) continue;
        if (/\/classes\//i.test(obj.src)) continue;
        if (/\/common\/tpl\//i.test(obj.src)) continue;
        if (/\/member_extra_info\//i.test(obj.src)) continue;
		
		if(/slide_gallery_/i.test(obj.id) > 0) continue;	// Slide Gallery

        var parent = obj.parentNode;
        while (parent) {
            if(parent.className && parent.className.search(/document_|comment_/i) != -1) break;
			if(parent.nodeName == 'A') bBanner = true;
            parent = parent.parentNode;
        }
        if (!parent || parent.className.search(/document_|comment_/i) < 0) continue;
		
        if (parent.parentNode) {
            xWidth(parent, xWidth(parent.parentNode));
        }
        parent.style.width = "100%";
        parent.style.overflow = "hidden";
        var parent_width = xWidth(parent);
        if (parent.parentNode && xWidth(parent.parentNode) < parent_width) {
            parent_width = xWidth(parent.parentNode);
        }
        var obj_width = xWidth(obj);
        var orig_img = new Image;
        orig_img.src = obj.src;
		
        if (parent_width < 1 || obj_width < 1 || (dlbo.resizedOnly && (parent_width-20 >= orig_img.width))) continue;

        obj.style.cursor = "pointer";
        obj.source_width = orig_img.width;
        obj.source_height = orig_img.height;
		
        if (obj_width >= parent_width) {
            var new_w = parent_width - 20;
            var new_h = Math.round(xHeight(obj) * new_w / obj_width);
		
            xWidth(obj, new_w);
            xHeight(obj, new_h);
			
			if(dlbo.resizedOnly) bApply = true;
        }
		
		if(!bBanner && (!dlbo.resizedOnly || bApply)){
			var lytebox_title = String(obj.getAttribute('title'));
			if(lytebox_title == "null" || obj.src.match(lytebox_title)) lytebox_title = " ";
			if(dlbo.bDisplaySave) lytebox_title = '<a href="'+obj.src+'">'+'<img src="'+request_uri+'addons/digist_lightbox/lytebox/images/save_grey.png" border="0">'+'</a> ' + lytebox_title;

			var lightbox_link = xCreateElement('a');
			lightbox_link.setAttribute('href', obj.src);
			lightbox_link.setAttribute('rel', lytebox_rel);
			lightbox_link.setAttribute('title', lytebox_title);

			obj.parentNode.insertBefore(lightbox_link, obj);
			lightbox_link.appendChild(obj);
		}
    }
	initLytebox();
	myLytebox.slideInterval = dlbo.slideInterval;
	dlbo.drag('lbMain');
}

// common.js에서 로딩해놓은거 내리기 -_-
xRemoveEventListener(window, "load", resizeImageContents);
// 내꺼 올리기 -_-a
xAddEventListener(window, "load", digist_lightbox_hook);
