/**********************************************************
 * image gallery in zeroboard5
 * created by zero (zero@nzeo.com, http://www.nzeo.com)
 *********************************************************/

var z_gallery_opacity = new Array();
var z_gallery_highlight_speed = new Array();
var z_gallery_highlight_opacity = new Array();

var _gallery_srl_list = new Array();
var _gallery_images = new Array();
var _gallery_idx = new Array();
var _obj_thumbnail = new Array();

function svc_print_binary_btn(upload_btn, delete_btn, insert_btn, width, height) {
    var ext_pattern = /\.(gif|jpg|png|jpeg)$/i

    if(ext_pattern.test(upload_btn)) upload_btn= "<img src=\""+upload_btn+"\" border='0' />";
    if(ext_pattern.test(delete_btn)) delete_btn= "<img src=\""+delete_btn+"\" border='0' />";
    if(ext_pattern.test(insert_btn)) insert_btn= "<img src=\""+insert_btn+"\" border='0' />";
    width=typeof(width)=='undefined'?80:width;
    height=typeof(height)=='undefined'?20:height;

    var margin_bottom = 0;
    if(xIE4Up) margin_bottom = -22;

    var html = ""+
        "<div id='zone_upload_btn' style='float:left;margin:0px 0px "+margin_bottom+"px 0px;width:"+width+"px;height:"+height+"px;'>"+
        upload_btn+
            "<div id='zone_file_upload_inner' style='position:relative;top:-22px;left:-15px;width:"+width+"px;height:"+height+"px;overflow:hidden;filter:alpha(opacity=0);-moz-opacity:0;'>"+
                "<input type='file' name='_upload_file' id='_upload_file' onchange=\"svc_file_upload_binary(this)\" size='1' style='width:1px;height:40px;'>"+
            "</div>"+
        "</div>"+
        "<div style='float:left;width:"+width+"px;height:"+height+"px;'>"+
        "<a href='#' onClick='svc_remove_file();return false;'>"+delete_btn+"</a>"+
        "</div>"+
        "<div style='float:left'>"+
        "<a href='#' onClick='svc_insert_file();return false;'>"+insert_btn+"</a>"+
        "</div>";

    document.write(html);

    xAddEventListener(window,'load',function _change_form_target() {svc_upload_change_form_target('_upload_file');} );
}

function svc_file_upload_binary(field_obj) {
    if(uploading_binary_file) {
        alert(alert_msg['file_uploading']);
        return;
    }

    var fo_obj = field_obj.parentNode;
    while(fo_obj.nodeName != 'FORM') { fo_obj = fo_obj.parentNode; }

    var seq_obj = xGetElementById('_seq');
    if(!seq_obj) {
        seq_obj = xCreateElement('INPUT');
        seq_obj.name = seq_obj.id = '_seq';
        seq_obj.value = '';
        seq_obj.type = 'hidden';
        fo_obj.appendChild(seq_obj);
    } 

    var seq = seq_obj.value;
    if(!seq || typeof(seq)=='undefined') {
        var response_tags = new Array('error','message','seq');
        var params = new Array();
        request_xml('member','get_new_seq',params,svc_get_new_seq_proc,response_tags, field_obj);
        return;
    }

    var method_obj = xGetElementById('_methodname');
    if(!method_obj) {
        method_obj = xCreateElement('INPUT');
        method_obj.name = method_obj.id = '_methodname';
        method_obj.value = 'module';
        method_obj.type = 'hidden';
        fo_obj.appendChild(method_obj);
    } 
    uploading_binary_file = true;
	
	xGetElementById('uploading').style.display='';
	
    fo_obj.submit();
}


function svc_file_upload_finish(str, value) {
    var opt = new Option(str, value, false, false);
    var fo_obj = xGetElementById('fo_insert');
    var file_list_obj = fo_obj.upload_file;
	
    file_list_obj.options[file_list_obj.options.length] = opt;
    uploading_binary_file = false;
	
	//썸네일
	var params = new Array();
	var response_tags = new Array('error','message','filename');

	params['sid']=sid;
	params['filename']=value;
	params['article_srl']=article_srl;
	params['module_srl']=module_srl;

	var args = params;
	
	//alert("리사이즈 시작" + sid + ", " + value + ", " + article_srl + ",");
	request_xml('module_manager', 'resize', params, resize_proc , response_tags , args);
	var check_svc_write = false;
	
}

function resize_proc( ret_obj , response_tags){
	xGetElementById('uploading').style.display='none';
	//alert(ret_obj['message']);

}


function z_add_gallery(srl, thumbnail_url, source_file_url) {
    var obj = new Array();

    var image = new Image();
    image.src = source_file_url;

    var t_image = new Image();
    t_image.src = thumbnail_url.replace("&amp;","&");

    obj['thumbnail_url'] = thumbnail_url;
    obj['source_url'] = source_file_url;
    obj['thumbnail'] = t_image;
    obj['gallery'] = image;

    if(typeof(_gallery_images[srl])=='undefined') {
        _gallery_images[srl] = new Array();
        _gallery_idx[srl] = 0;
        _obj_thumbnail[srl] = new Array();
    }

    _gallery_images[srl][_gallery_images[srl].length] = obj;
}

function z_start_gallery() {

    for(var i = 0; i < _gallery_srl_list.length; i++) {
        var srl = _gallery_srl_list[i];

        if(!_gallery_images[srl].length) continue;

        var zone_gallery = xGetElementById('zone_gallery_'+srl);
        var zone_thumbnail = xGetElementById('zone_thumbnail_'+srl);

        xInnerHtml(zone_gallery,'');

        var obj = _gallery_images[srl][0];

        var obj_gallery = new Image();
        obj_gallery.src = obj['gallery'].src;
        obj_gallery.id = 'main_gallery_'+srl;
        obj_gallery.srl = srl;
        obj_gallery.style.cursor = 'pointer';
        obj_gallery.style.opacity = 1;
        obj_gallery.style.filter = "alpha(opacity=100)";
        obj_gallery.start_opacity = 0;

        zone_gallery.appendChild(obj_gallery);

        for(var idx = 0; idx < _gallery_images[srl].length; idx++) {
            var s_obj = _gallery_images[srl][idx];

            if(s_obj) {
                _obj_thumbnail[srl][idx] = new Image();
                _obj_thumbnail[srl][idx].src = s_obj['thumbnail'].src;
                _obj_thumbnail[srl][idx].start_opacity = 0;
                _obj_thumbnail[srl][idx].id = 'thumbnail_'+srl+'_'+idx;
                _obj_thumbnail[srl][idx].srl = srl;
                _obj_thumbnail[srl][idx].idx = idx;
                _obj_thumbnail[srl][idx].style.cursor = 'pointer';
                if(idx == _gallery_idx[srl]) {
                    _obj_thumbnail[srl][idx].style.opacity = 1;
                    _obj_thumbnail[srl][idx].style.filter = "alpha(opacity=100)";
                } else {
                    _obj_thumbnail[srl][idx].style.opacity = 0.3;
                    _obj_thumbnail[srl][idx].style.filter = "alpha(opacity=30)";
                }
                _obj_thumbnail[srl][idx].style.margin = '0px 5px 5px 5px';
                zone_thumbnail.appendChild(_obj_thumbnail[srl][idx]);

            }
        }
    }

    if(xIE4Up) {
        xAddEventListener(document,'mousewheel',z_gallery_check_wheel);
    } 
    xAddEventListener(document,'mouseup',z_change_gallery);
    xAddEventListener(document,'mouseover',z_gallery_do_focus);
    xAddEventListener(document,'mouseout',z_gallery_do_focusout);

    _gallery_srl_list = new Array();
}

function z_show_gallery(srl, idx) {
    if(!_gallery_images[srl].length) return;
    if(_gallery_idx[srl] == idx) return;

    _gallery_idx[srl] = idx;

    var obj = _gallery_images[srl][_gallery_idx[srl]];
    var obj_gallery = xGetElementById('main_gallery_'+srl);
    if(obj['gallery'].src == obj_gallery.src) return;

    obj_gallery.parentNode.removeChild(obj_gallery);

    obj_gallery = new Image();
    obj_gallery.src = obj['gallery'].src;
    obj_gallery.id = 'main_gallery_'+srl;
    obj_gallery.srl = srl;
    obj_gallery.style.cursor = 'pointer';

    obj_gallery.start_opacity = 0;
    obj_gallery.style.width = obj['gallery'].width;
    obj_gallery.style.height = obj['gallery'].height;
    obj_gallery.style.opacity = 0;
    obj_gallery.style.filter = "alpha(opacity=0)";
    obj_gallery.start_opacity = 1;

    var zone_gallery = xGetElementById('zone_gallery_'+srl);
    zone_gallery.appendChild(obj_gallery);

    setTimeout(function _start() {z_gallery_opacity_up(obj_gallery,z_gallery_opacity[srl],z_gallery_highlight_opacity[srl]);}, z_gallery_highlight_speed[srl]);

    for(var idx = 0; idx < _gallery_images[srl].length; idx ++) {
        var s_obj = xGetElementById('thumbnail_'+srl+'_'+idx);
        if(!s_obj) continue;

        if(idx == _gallery_idx[srl]) {
            s_obj.style.opacity = 1;
            s_obj.style.filter = "alpha(opacity=100)";
        } else {
            s_obj.style.opacity = z_gallery_opacity[srl];
            s_obj.style.filter = "alpha(opacity="+(z_gallery_opacity[srl]*100)+")";
        }
    }
}

function z_gallery_opacity_up(obj,opacity,up) {
    var srl = obj.srl;
    if(obj.start_opacity!=1) {
        if(obj.id == 'main_gallery_'+srl || obj.idx == _gallery_idx[srl]) {
            obj.style.opacity = 1;
            obj.style.filter = "alpha(opacity=100)";
        } else {
            obj.style.opacity = z_gallery_opacity[srl];
            obj.style.filter = "alpha(opacity="+(z_gallery_opacity[srl]*100)+")";
        }
        return;
    }
    if(opacity<1) {
        up = z_gallery_highlight_opacity[srl];
        opacity += up;
        if(opacity>=1) opacity = 1.0;
        if(opacity>=0.5) opacity = 1.0;
        obj.style.opacity = opacity;
        obj.style.filter = "alpha(opacity="+(opacity*100)+")";
        if(opacity<1) setTimeout(function _opacity_up() {z_gallery_opacity_up(obj, opacity,up);},z_gallery_highlight_speed[srl]);
    } else {
        obj.start_opacity = 0;
    }
}

function z_change_next_gallery(srl) {
    var idx = _gallery_idx[srl] +1;
    if(idx>=_gallery_images[srl].length) idx = 0;
    setTimeout(function show_gallery() { z_show_gallery(srl, idx); }, 50);
}

function z_change_prev_gallery(srl) {
    var idx = _gallery_idx[srl] -1;
    if(idx<0) idx = _gallery_images[srl].length-1;
    setTimeout(function show_gallery() { z_show_gallery(srl, idx); }, 50);
}

function z_change_gallery(evt) {
    var e = new xEvent(evt);
    var obj = e.target;
    var srl = obj.srl;
    var idx = obj.idx;
    if(typeof(srl)=='undefined') return;
    if(typeof(idx)=='undefined') z_change_next_gallery(srl);
    else {
        setTimeout(function show_gallery() { z_show_gallery(srl, idx); }, 50);
    }
}

function z_gallery_do_focus(evt) {
    var e = new xEvent(evt);
    var obj = e.target;
    var srl = obj.srl;
    if(typeof(srl)=='undefined') return;
    if(obj.idx == _gallery_idx[srl]) return;
    if(obj.id == 'main_gallery_'+srl) return;
    obj.start_opacity = 1;
    z_gallery_opacity_up(obj,z_gallery_opacity[srl],z_gallery_highlight_opacity[srl]);
    xPreventDefault(evt);
    xStopPropagation(evt);
}

function z_gallery_do_focusout(evt) {
    var e = new xEvent(evt);
    var obj = e.target;
    var srl = obj.srl;
    if(typeof(srl)=='undefined') return;
    if(obj.idx == _gallery_idx[srl]) return;
    if(obj.id == 'main_gallery_'+srl) return;
    obj.start_opacity = 0;
    obj.style.opacity = z_gallery_opacity[srl];
    obj.style.filter = "alpha(opacity="+(z_gallery_opacity[srl]*100)+")";
}

function z_gallery_check_wheel(evt) {
    var e = new xEvent(evt);
    var obj = e.target;
    var srl = obj.srl;
    if(typeof(srl)=='undefined') return;
    if(e.target.id != 'main_gallery_'+srl) return;
    if(evt.wheelDelta<0) z_change_next_gallery(srl);
    else z_change_prev_gallery(srl);
    xPreventDefault(evt);
    xStopPropagation(evt);
}

function zb5_board_cancel(url) {
    if(!confirm(alert_msg['msg_cancel'])) return false;
    location.href=url;
    return false;
}

