/*
SMF Garage (http://www.smfgarage.com)
Version 0.6.0b
garage_functions.js
*/

/**
* Taken from phpBB.com permissions.js
* Set display of page element
* s[-1,0,1] = hide,toggle display,show
*/
function dE(n, s, type)
{
    if (!type)
    {
        type = 'block';
    }

    var e = document.getElementById(n);
    if (!s)
    {
        s = (e.style.display == '') ? -1 : 1;
    }
    e.style.display = (s == 1) ? type : 'none';
}

function change_select(id, type)
{
    if(id.length == 1) id = '00'+id;
    else if(id.length == 2) id = '0'+id;
// Get Current and New Tab Objects
    var old_div = document.getElementById(type + active_model_id);
    var new_div = document.getElementById(type + id);

    // Toggle Models Panes
    dE(type + active_model_id, -1);
    dE(type + id, 1);

    // Set new active tab
    active_model_id = id;
}

function change_tab(id)
{

    // Get Current and New Tab Objects
    var old_tab = document.getElementById('tab' + active_id);
    var new_tab = document.getElementById('tab' + id);

    // No need to change anything if we are clicking the same tab
    /*if (id == active_id || new_tab == old_tab)
    {
        return;
    }*/

    // Set class for new and current tabs
    old_tab.className = 'mirrortab_back';
    new_tab.className = 'mirrortab_active_back';

    // Move the TD nodes surrounding the active tab
    moveTabTD(new_tab);

    // Toggle Options Panes
    dE('options' + active_id, -1);
    dE('options' + id, 1);

    // Set new active tab
    active_id = id;
}

function moveTabTD(new_tab) {

    // Get Parent Node Object (The Table Row)
    tab_tr = document.getElementById('tab_row');

    // Remove nodes before and after the active tab from the DOM
    tab_left = document.getElementById('tab_active_left');
    tab_right = document.getElementById('tab_active_right');
    tab_left_stored = tab_tr.removeChild(tab_left);
    tab_right_stored = tab_tr.removeChild(tab_right);

    // Clone Nodes Before and After the active tab
    // Not needed
    /*tab_left = document.getElementById('tab_active_left');
    tab_right = document.getElementById('tab_active_right');
    tab_left_clone = tab_left.CloneNode(true);
    tab_right_clone = tab_right.CloneNode(true);*/

    // Insert previously removed nodes before and after the new active tab
    tab_tr.insertBefore(tab_left_stored, new_tab);
    temp_obj = new_tab.nextSibling;
    // Find the next TD node so we can insert the right active tab spacer
    while(temp_obj.nodeName != 'TD') {
        temp_obj = temp_obj.nextSibling;
    }
    tab_tr.insertBefore(tab_right_stored, temp_obj);
}

// Toolman Functions
function setHandle(item) {
    item.toolManDragGroup.setHandle(findHandle(item))
}

function findHandle(item) {
    var children = item.getElementsByTagName("div")
    for (var i = 0; i < children.length; i++) {
        var child = children[i]

        if (child.getAttribute("class") == null) continue

        if (child.getAttribute("class").indexOf("handle") >= 0)
            return child
    }
    return item
}

function garage_join(name, isDoubleClick) {
    //alert("Joining "+name);
    var view = document.getElementById(name + "View")
    view.editor = document.getElementById(name + "Edit")

    var showEditor = function(event) {
        event = fixEvent(event)

        var view = this
        var editor = view.editor

        if (!editor) return true

        if (editor.currentView != null) {
            editor.blur()
        }
        editor.currentView = view

        var topLeft = coordinates.topLeftOffset(view)
        topLeft.reposition(editor)
        if (editor.nodeName == 'TEXTAREA') {
            editor.style['width'] = view.offsetWidth + "px"
            editor.style['height'] = view.offsetHeight + "px"
        }
        editor.value = view.innerHTML
        editor.style['visibility'] = 'visible'
        view.style['visibility'] = 'hidden'
        editor.focus()
        return false
    }

    if (isDoubleClick) {
        view.ondblclick = showEditor
    } else {
        view.onclick = showEditor
    }

    view.editor.onblur = function(event) {
        event = fixEvent(event)

        var editor = event.target
        var view = editor.currentView

        if (!editor.abandonChanges) {
            //alert("Saving Changes");
            // Update View
            view.innerHTML = editor.value
            // Run AJAX to update database record
            doit(name, editor.value);
        } else {
            //alert("Discard Changes");
            var e = document.getElementById('updateStatus');
            var string = "Your changes were discarded.";
            e.innerHTML = string;
        }
        editor.abandonChanges = false
        editor.style['visibility'] = 'hidden'
        editor.value = '' // fixes firefox 1.0 bug
        view.style['visibility'] = 'visible'
        editor.currentView = null

        return true
    }

    view.editor.onkeydown = function(event) {
        event = fixEvent(event)

        var editor = event.target
        if (event.keyCode == TAB) {
            editor.blur()
            return false
        }
    }

    view.editor.onkeyup = function(event) {
        event = fixEvent(event)

        var editor = event.target
        if (event.keyCode == ESCAPE) {
            editor.abandonChanges = true
            editor.blur()
            return false
        } else if (event.keyCode == TAB) {
            return false
        } else {
            return true
        }
    }

    // TODO: this method is duplicated elsewhere
    function fixEvent(event) {
        if (!event) event = window.event
        if (event.target) {
            if (event.target.nodeType == 3) event.target = event.target.parentNode
        } else if (event.srcElement) {
            event.target = event.srcElement
        }

        return event
    }
}

function whenLoading(){
    var e = document.getElementById('updateStatus');
    e.innerHTML = "<p>Sending Data...</p>";
}

function whenLoaded(){
    var e = document.getElementById('updateStatus');
    e.innerHTML = "<p>Data Sent...</p>";
}

function whenInteractive(){
    var e = document.getElementById('updateStatus');
    e.innerHTML = "<p>Getting Status...</p>";
}

function whenCompleted(){
    var e = document.getElementById('updateStatus');
    if (ajax.responseStatus){
        //alert(ajax.response);
        if(ajax.response == 1) {
            var string = "Your changes were saved correctly.";
            window.location.reload()
        } else {
            var string = "Your changes were not saved.";
            /*var err = document.getElementById('errorNotes');
            err.innerHTML = ajax.response;*/
        }
        //var string = "<p>Status Code: " + ajax.responseStatus[0] + "</p><p>Status Message: " + ajax.responseStatus[1] + "</p><p>URLString Sent: " + ajax.URLString + "</p>"+ajax.response;
    } else {
        //var string = "<p>URLString Sent: " + ajax.URLString + "</p>";
        var string = "There was an error saving your changes.";
        /*var err = document.getElementById('errorNotes');
        err.innerHTML = ajax.response;*/
    }
    e.innerHTML = string;
}

function doit(id, data){
    // IF YOU CHANGE THE DEFAULT LANGUAGE FILE FOR THIS YOU NEED TO UPDATE THE LINE BELOW
    // This checks to see if the default placeholder text is still there, if so, don't run the AJAX update
    if(data != "Double-click to add description") {
        ajax.setVar("submit_button", "Update Desc");
        ajax.setVar("image_id", id.substr(5));
        ajax.setVar("desc", data);
        ajax.requestFile = "index.php?action=garage;sa=update_attach_desc";
        ajax.method = 'POST';
        ajax.element = 'updateStatus';
        ajax.onLoading = whenLoading;
        ajax.onLoaded = whenLoaded;
        ajax.onInteractive = whenInteractive;
        ajax.onCompletion = whenCompleted;
        ajax.runAJAX();
    }
}

function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}
    
function confirmDelete(delUrl, message) { 
    if (confirm(message)) {
        document.location = delUrl;
    }
}

function checkUncheckAll(theElement) 
{
    var theForm = theElement.form, z = 0;
    for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
        theForm[z].checked = theElement.checked;
      }
    }
}

function shrinkSection(object, image, object2)
{
    current_mode = document.getElementById(object).style.display;
    if(current_mode == "") {
        document.getElementById(object).style.display = "none";
        if(object2 != undefined) {
            document.getElementById(object2).style.display = "none";
        }
    document.getElementById(image).src = smf_images_url + "/upshrink2.gif";
    } else if(current_mode == "none") {
        document.getElementById(object).style.display = "";
        if(object2 != undefined) {
            document.getElementById(object2).style.display = "";
        }
        document.getElementById(image).src = smf_images_url + "/upshrink.gif";
    }
}

function setforminputvalue(formname, inputname, newvalue)
{
    eval('document.forms[\'' + formname + '\'].' + inputname + '.value = ' + newvalue);
}

