var gal_ignore_cache = new Array();
var addquotebutton = ipb_var_image_url + "/p_mq_add.gif";
var removequotebutton = ipb_var_image_url + "/p_mq_remove.gif";
var unselectedbutton = ipb_var_image_url + "/topic_unselected.gif";
var selectedbutton = ipb_var_image_url + "/topic_selected.gif";
var lang_gobutton = "With selected";
var gallery_lang_prompt = "Link to Image:";
var centerdiv;
var rate_value = 0;
var ids_to_imgs = new Array();
var the_image;
var actual_image;
var text_area = new Array();
var quote_start_x = 0;
var quote_start_y = 0;
var var_image_height = 0;
var var_image_width = 0;
var meta_loaded = 0;
var image = new Array();
var _this_select_all = 0;
function init_image_editor(img_h, img_w)
    {
    var_image_height = img_h;
    var_image_width = img_w;
    actual_image = document.getElementById('actualImage');
    actual_image.style.height = var_image_height + 'px';
    actual_image.style.width = var_image_width + 'px';
    document.getElementById('imageContainer').style.height = actual_image.style.height + 10 + 'px';
    };

function gallery_meta()
    {
    var meta_main = document.getElementById('showmeta');
    var meta_drag = document.getElementById('meta-drag');
    var meta_content = document.getElementById('meta-content');

    if (!meta_loaded)
        {
        var my_width = 0;
        var my_height = 0;

        if (typeof (window.innerWidth) == 'number')
            {
            my_width = window.innerWidth;
            my_height = window.innerHeight;
            }

        else if (document.documentElement
            && (document.documentElement.clientWidth || document.documentElement.clientHeight))
            {
            my_width = document.documentElement.clientWidth;
            my_height = document.documentElement.clientHeight;
            }

        else if (document.body && (document.body.clientWidth || document.body.clientHeight))
            {
            my_width = document.body.clientWidth;
            my_height = document.body.clientHeight;
            }
        var divheight = parseInt(meta_main.style.Height);
        var divwidth = parseInt(meta_main.style.Width);
        divheight = divheight ? divheight : 400;
        divwidth = divwidth ? divwidth : 400;
        var divxy = my_getcookie('ipb-meta-div');
        var co_ords;

        if (divxy && divxy != null)
            {
            co_ords = divxy.split(',');

            if (co_ords.length)
                {
                var final_width = co_ords[0];
                var final_height = co_ords[1];

                if (co_ords[0] > my_width)
                    {
                    final_width = my_width - divwidth;
                    }

                if (co_ords[1] > my_height)
                    {
                    final_height = my_height - divheight;
                    }
                meta_main.style.left = final_width + 'px';
                meta_main.style.top = final_height + 'px';
                }
            }

        else
            {
            meta_main.style.left = my_width / 2 - (divwidth / 2) + 'px';
            meta_main.style.top = my_height / 2 - (divheight / 2) + 'px';
            }
        Drag.cookiename = 'ipb-meta-div';
        Drag.init(meta_drag, meta_main);
        meta_loaded = 1;
        }
    meta_main.style.position = 'absolute';
    meta_main.style.display = 'block';
    meta_main.style.zIndex = 99;

    if (is_ie)
        {
        var html = meta_content.innerHTML;
        html = "<iframe id='meta-shim' src='" + ipb_var_image_url
            + "/iframe.html' class='iframshim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; right:0px; display: none;'></iframe>"
            + html;
        meta_content.innerHTML = html;
        }

    if (is_ie)
        {
        var drag_html = meta_drag.innerHTML;
        var main_drag_html =
            "<iframe id='meta-shim-two' src='" + ipb_var_image_url
            + "/iframe.html' class='iframshim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; right:0px; display: none;'></iframe>"
            + drag_html;
        meta_drag.innerHTML = main_drag_html;
        }

    if (is_ie)
        {
        meta_shim = document.getElementById('meta-shim');
        meta_shim.style.width = meta_content.offsetWidth;
        meta_shim.style.height = meta_content.offsetHeight;
        meta_shim.style.zIndex = meta_content.style.zIndex - 1;
        meta_shim.style.top = meta_content.style.top;
        meta_shim.style.left = meta_content.style.left;
        meta_shim.style.display = "block";
        meta_shim_d = document.getElementById('meta-shim-two');
        meta_shim_d.style.width = meta_drag.offsetWidth;
        meta_shim_d.style.height = meta_drag.offsetHeight;
        meta_shim_d.style.zIndex = meta_drag.style.zIndex - 1;
        meta_shim_d.style.top = meta_drag.style.top;
        meta_shim_d.style.left = meta_drag.style.left;
        meta_shim_d.style.display = "block";
        }
    }
function save_tags()
    {
    var url = ipb_base_url;
    do_request_function = function()
        {
        if (!xmlobj.readystate_ready_and_ok())
            {
            xmlobj.show_loading('Saving Tags...');
            return;
            }
        xmlobj.hide_loading();
        var html = xmlobj.xmlhandler.responseText;
        };
    xmlobj = new ajax_request();
    xmlobj.onreadystatechange(do_request_function);
    xmlobj.process(url);
    document.getElementById('tag_edit_box').style.display = 'none';
    };
function create_quote_box()
    {
    quote_div = document.getElementById('quote_div');
    quote_div.style.display = 'block';
    image['top'] = _get_obj_toppos(actual_image);
    image['left'] = _get_obj_leftpos(actual_image);
    quote_div.style.top = image['top'] + 5 + 'px';
    quote_div.style.left = image['left'] + 5 + 'px';
    image['right'] = image['left'] + parseInt(var_image_width);
    image['bottom'] = image['top'] + parseInt(var_image_height);
    image['right_barrier'] = image['right'] - parseInt(quote_div.style.width);
    image['bottom_barrier'] = image['bottom'] - parseInt(quote_div.style.height);
    Drag.init(quote_div, quote_div, image['left'], image['right_barrier'], image['top'], image['bottom_barrier']);
    quote_div.onmouseup = pop_text_area;
    document.getElementById('quote_url').href = 'javascript:alert( "You have to save the current quote box first." );';
    document.getElementById('quote_text').innerHTML = '<s>Create Quote Box</s>';
    };
function pop_tag_box()
    {
    document.getElementById('tag_edit_box').style.display = 'block';
    };
function pop_text_area(event)
    {
    text_area['box'] = document.getElementById('quote_textarea');
    text_area['container'] = document.getElementById('textarea_div');
    text_area['container'].style.top = parseInt(_get_obj_toppos(quote_div)) + parseInt(quote_div.style.height) + 'px';
    text_area['container'].style.left = parseInt(quote_div.style.left) - 5 + 'px';
    text_area['container'].style.display = 'block';
    text_area['box'].style.display = 'block';
    };
function end_quote_box()
    {
    var url = ipb_base_url;
    do_request_function = function()
        {
        if (!xmlobj.readystate_ready_and_ok())
            {
            xmlobj.show_loading('Saving Quote Box...');
            return;
            }
        xmlobj.hide_loading();
        var html = xmlobj.xmlhandler.responseText;
        };
    xmlobj = new ajax_request();
    xmlobj.onreadystatechange(do_request_function);
    xmlobj.process(url);
    text_area['container'].style.display = 'none';
    quote_div.style.display = 'none';
    document.getElementById('quote_url').href = 'javascript:create_quote_box();';
    document.getElementById('quote_text').innerHTML = 'Create Quote Box';
    };
function track_quote_box(event)
    {
    change_cursor('move');
    };
function upload_pane_init()
    {
    var pane_main = document.getElementById('gallery_upload');
    var pane_drag = document.getElementById('upload-drag');
    Drag.init(pane_drag, pane_main);
    };
function gallery_upload_pane()
    {
    upload_pane_init();
    centerdiv = new center_div();
    centerdiv.divname = 'gallery_upload';
    centerdiv.move_div();
    };
function gallery_link_to_post(pid, img)
    {
    temp = prompt(gallery_lang_prompt, ipb_var_base_url + "autocom=gallery&req=si&img=" + img + "&findpid=" + pid);
    return false;
    };
function delete_img(theURL)
    {
    if (confirm(lang_suredelete))
        {
        window.location.href = theURL;
        }

    else
        {
        alert(ipb_lang_js_del_2);
        }
    };
function gallery_toggle_pid(id)
    {
    saved = new Array();
    clean = new Array();
    add = 1;
    tmp = document.modform.selectedgcids.value;
    saved = tmp.split(",");

    for (i = 0; i < saved.length; i++)
        {
        if (saved[i] != "")
            {
            if (saved[i] == id)
                {
                add = 0;
                }

            else
                {
                clean[clean.length] = saved[i];
                }
            }
        }

    if (add)
        {
        clean[clean.length] = id;
        eval("document.pid" + id + ".src=selectedbutton");
        }

    else
        {
        eval(" document.pid" + id + ".src=unselectedbutton");
        }
    newvalue = clean.join(',');
    my_setcookie('modgcids', newvalue, 0);
    document.modform.selectedgcids.value = newvalue;
    newcount = stacksize(clean);
    document.modform.gobutton.value = lang_gobutton + '(' + newcount + ')';
    return false;
    };
function multiquote_add(id)
    {
    saved = new Array();
    clean = new Array();
    add = 1;

    if (tmp = my_getcookie('gal_pids'))
        {
        saved = tmp.split(",");
        }

    for (i = 0; i < saved.length; i++)
        {
        if (saved[i] != "")
            {
            if (saved[i] == id)
                {
                add = 0;
                }

            else
                {
                clean[clean.length] = saved[i];
                }
            }
        }

    if (add)
        {
        clean[clean.length] = id;
        eval("document.mad_" + id + ".src=removequotebutton");
        }

    else
        {
        eval(" document.mad_" + id + ".src=addquotebutton");
        }
    my_setcookie('gal_pids', clean.join(','), 0);
    return false;
    };
function gallery_toggle_img(id)
    {
    saved = new Array();
    clean = new Array();
    add = 1;
    tmp = document.modform.selectedimgids.value;
    saved = tmp.split(",");

    for (i = 0; i < saved.length; i++)
        {
        if (saved[i] != "")
            {
            if (saved[i] == id)
                {
                add = 0;
                }

            else
                {
                clean[clean.length] = saved[i];
                }
            }
        }

    if (add)
        {
        clean[clean.length] = id;
        eval("document.img" + id + ".src=selectedbutton");
        }

    else
        {
        eval(" document.img" + id + ".src=unselectedbutton");
        }
    newvalue = clean.join(',');
    my_setcookie('modimgids', newvalue, 0);
    document.modform.selectedimgids.value = newvalue;
    newcount = stacksize(clean);
    document.modform.gobutton.value = ipsclass.html_entity_decode(lang_gobutton) + ' (' + newcount + ')';
    return false;
    };

function gallery_select_all()
    {
    clean = new Array();
    saved = new Array();
    var topics_this_page = new Array();
    tmp = document.modform.selectedimgids.value;

    if (tmp != "")
        {
        saved = tmp.split(",");
        }

    if (_this_select_all == 0)
        {
        var the_topics = document.getElementsByTagName('input');

        for (var i = 0; i <= the_topics.length; i++)
            {
            var e = the_topics[i];

            if (e && (e.type == 'hidden') && (!e.disabled))
                {
                var s = e.id;
                var a = s.replace(/^img_(.+?)$/, "$1");

                if (a)
                    {
                    try
                        {
                        document.getElementById('img' + a).src = selectedbutton;
                        clean[clean.length] = a;
                        topics_this_page[a] = 1;
                        }
                    catch (err)
                        {
                        }
                    }
                }
            }
        document.getElementById('imgs-all').src = selectedbutton;
        _this_select_all = 1;
        }

    else
        {
        var the_topics = document.getElementsByTagName('input');

        for (var i = 0; i <= the_topics.length; i++)
            {
            var e = the_topics[i];

            if (e && (e.type == 'hidden') && (!e.disabled))
                {
                var s = e.id;
                var a = s.replace(/^img_(.+?)$/, "$1");

                if (a)
                    {
                    try
                        {
                        document.getElementById('img' + a).src = unselectedbutton;
                        topics_this_page[a] = 1;
                        }
                    catch (err)
                        {
                        }
                    }
                }
            }
        document.getElementById('imgs-all').src = unselectedbutton;
        _this_select_all = 0;
        }

    for (i = 0; i < saved.length; i++)
        {
        if (saved[i] != "" && topics_this_page[saved[i]] != 1)
            {
            clean[clean.length] = saved[i];
            }
        }
    newvalue = clean.join(',');
    my_setcookie('modimgids', newvalue, 0);
    document.modform.selectedimgids.value = newvalue;
    newcount = stacksize(clean);
    document.modform.gobutton.value = ipsclass.html_entity_decode(lang_gobutton) + ' (' + newcount + ')';
    return false;
    }
add_onload_event(fix_linked_image_sizes);
function fix_linked_image_sizes()
    {
    if (ipsclass.settings['do_linked_resize'] != 1)
        {
        return true;
        }

    else if (popup_type == 'new')
        {
        return true;
        }

    else if (popup_type == 'lightbox')
        {
        return true;
        }
    var images = document.getElementsByTagName('IMG');
    var _padding = 2;
    var _count = 0;
    var _img =
        '<img src="' + ipb_var_image_url + '/img-resized.png" style="vertical-align:middle" border="0" alt="" />';

    for (i = 0; i < images.length; i++)
        {
        if (images[i].className == 'galattach')
            {
            _count++;
            var _width = images[i].width;
            var _height = images[i].height;
            var _percent = 0;
            var _href = images[i].parentNode.href;

            if (_href != 'undefined' && images[i].parentNode.className == 'gal')
                {
                if (popup_type == 'popup')
                    {
                    images[i]._src = _href;
                    images[i].onclick = fix_linked_images_onclick;
                    images[i].onmouseover = fix_linked_images_mouseover;
                    images[i].title = ipb_global_lang['click_to_view'];
                    }

                else
                    {
                    var img = document.createElement('img');
                    img.id = '--ipb-img-resizer-' + _count;
                    img.title = ipb_global_lang['click_to_view'];
                    img.src = images[i].src;
                    img.className = images[i].className;
                    img.width = images[i].width;
                    img.height = images[i].height;
                    var div = document.createElement('div');
                    div.innerHTML = _img + '&nbsp;' + lang_clickme;
                    div.style.width = img.width + (_padding * 2) + 'px';
                    div.className = 'resized-linked-image';
                    div.style.paddingTop = _padding + "px";
                    div.style.paddingBottom = _padding + "px";
                    div.style.paddingLeft = _padding + "px";
                    div.style.paddingRight = _padding + "px";
                    div.style.position = 'relative';
                    div._is_div = 1;
                    div._resize_id = _count;
                    div._src = _href;
                    div.onclick = fix_linked_images_onclick;
                    div.onmouseover = fix_linked_images_mouseover;
                    div.title = ipb_global_lang['click_to_view'];
                    div.appendChild(img);
                    images[i].parentNode.href = '#';
                    images[i].parentNode.className = '';
                    images[i].parentNode.parentNode.appendChild(div, images[i].parentNode);
                    images[i].parentNode.parentNode.removeChild(images[i].parentNode);
                    }
                }
            }
        }
    };
function fix_linked_images_onclick(e)
    {
    PopUp(this._src, 'popup', screen.width, screen.height, 1, 1, 1);
    e = ipsclass.cancel_bubble_all(e);
    return false;
    };
function fix_linked_images_mouseover(e)
    {
    try
        {
        this.style.cursor = 'pointer';
        }
    catch (acold)
        {
        }
    };
function show_ignored_post(pid)
    {
    try
        {
        var post_main = document.getElementById('post-main-' + pid);
        var post_ignore = document.getElementById('post-ignore-' + pid);
        post_main.innerHTML = gal_ignore_cache[pid];
        }
    catch (e)
        {
        }
    return false;
    };
function init_ignored_post(pid)
    {
    try
        {
        var post_main = document.getElementById('post-main-' + pid);
        var post_ignore = document.getElementById('post-ignore-' + pid);
        gal_ignore_cache[pid] = post_main.innerHTML;
        post_main.innerHTML = post_ignore.innerHTML;
        }
    catch (e)
        {
        }
    };
var photostrip_cell_ids = new Array();
var cur_left = 0;
var cur_right = 0;
var cur_image = 0;
var most_left = 0;
var most_right = 0;
function slide_init()
    {
    if (cur_left > 0)
        {
        document.getElementById('slide_left_buttons').style.display = '';
        document.getElementById('slide_left_td').onmouseover = display_mouseover;
        document.getElementById('slide_left_td').onmouseout = display_mouseout;
        document.getElementById('slide_left_td').onclick = do_onclick_left;
        }

    else
        {
        document.getElementById('slide_left_buttons').style.display = 'none';
        document.getElementById('slide_left_td').style.cursor = 'default';
        document.getElementById('slide_left_td').className = 'post1';
        document.getElementById('slide_left_td').onmouseover = '';
        document.getElementById('slide_left_td').onmouseout = '';
        document.getElementById('slide_left_td').onclick = '';
        }

    if (cur_right > 0)
        {
        document.getElementById('slide_right_buttons').style.display = '';
        document.getElementById('slide_right_td').onmouseover = display_mouseover;
        document.getElementById('slide_right_td').onmouseout = display_mouseout;
        document.getElementById('slide_right_td').onclick = do_onclick_right;
        }

    else
        {
        document.getElementById('slide_right_buttons').style.display = 'none';
        document.getElementById('slide_right_td').style.cursor = 'default';
        document.getElementById('slide_right_td').className = 'post1';
        document.getElementById('slide_right_td').onmouseover = '';
        document.getElementById('slide_right_td').onmouseout = '';
        document.getElementById('slide_right_td').onclick = '';
        }
    };
function display_mouseover()
    {
    this.className = 'post2';
    this.style.cursor = 'pointer';
    };
function display_mouseout()
    {
    this.className = 'post1';
    };
function do_onclick_left(event)
    {
    slide_right(event);
    };
function do_onclick_right(event)
    {
    slide_left(event);
    };
function slide_left(event)
    {
    global_cancel_bubble(event, true);
    var url = ipb_var_base_url + 'autocom=gallery&req=quickch&op=slide_left&img=' + cur_right + '&cur_img=' + cur_image;
    do_request_function = function()
        {
        if (!xmlobj.readystate_ready_and_ok())
            {
            xmlobj.show_loading('');
            return;
            }
        xmlobj.hide_loading();
        var html = xmlobj.xmlhandler.responseText;

        if (html == 'nopermission')
            {
            alert(js_error_no_permission);
            }

        else if (html != 'nopermission')
            {
            var data = html.split('~|~');
            tablerow = document.getElementById('photostrip');
            tablerow.deleteCell(1);
            var newCell = tablerow.insertCell(4);
            newCell.width = '25%';
            newCell.style.height = data[1] + 'px';
            newCell.style.minHeight = data[1] + 'px';
            newCell.align = 'center';
            newCell.valign = 'middle';
            newCell.className = data[2];
            newCell.id = data[0];
            newCell.innerHTML = data[3];
            reset_current_ids(tablerow);
            slide_init();
            }
        };
    xmlobj = new ajax_request();
    xmlobj.onreadystatechange(do_request_function);
    xmlobj.process(url);
    return false;
    };
function slide_right(event)
    {
    global_cancel_bubble(event, true);
    var url = ipb_var_base_url + 'autocom=gallery&req=quickch&op=slide_right&img=' + cur_left + '&cur_img=' + cur_image;
    do_request_function = function()
        {
        if (!xmlobj.readystate_ready_and_ok())
            {
            xmlobj.show_loading('');
            return;
            }
        xmlobj.hide_loading();
        var html = xmlobj.xmlhandler.responseText;

        if (html == 'nopermission')
            {
            alert(js_error_no_permission);
            }

        else if (html != 'nopermission')
            {
            var data = html.split('~|~');
            tablerow = document.getElementById('photostrip');
            tablerow.deleteCell(4);
            var newCell = tablerow.insertCell(1);
            newCell.width = '25%';
            newCell.style.height = data[1] + 'px';
            newCell.style.minHeight = data[1] + 'px';
            newCell.align = 'center';
            newCell.valign = 'middle';
            newCell.className = data[2];
            newCell.id = data[0];
            newCell.innerHTML = data[3];
            reset_current_ids(tablerow);
            slide_init();
            }
        };
    xmlobj = new ajax_request();
    xmlobj.onreadystatechange(do_request_function);
    xmlobj.process(url);
    return false;
    };
function pixel_left_move()
    {
    };

function reset_current_ids(tablerow)
    {
    for (var j = 0; j < tablerow.cells.length; j++)
        {
        if (j == 1)
            {
            cur_left = tablerow.cells[j].id;
            }

        if (j == 4)
            {
            cur_right = tablerow.cells[j].id;
            }
        }
    };
