


// Javascript from Moodle modules
<?php 
/**
 * gets the appropriate javascript to use, depending on the displaytype
 *
 * @return string
 */
function dd_get_javascript($displaytype, $gapcount, $mediaborderwidth, $imageborderwidth, $backgroundname) {
    $jstext = "
    function set_all_form_values()
    {
        var length = containers.length;
        for (var i = 0; i < length; i++) {
            if (containers[i].mainchild.positioned) {
                set_form_values(containers[i].mainchild, false);
                set_form_values(containers[i].mainchild.hotspot, true); 
            } 
        }
    }
    
    function setValueById(id, val) {
        var obj = document.getElementById(id);
        obj.value = val;
    }
    
    //dd.elements.$backgroundname.setZ(0);
    
    // create the list of containers, so we don't need to search through all the elements every time
    //var containers = new Array($gapcount);
    var containers = new Array();
    
    var j = 0;
    for (var i = 0; i < dd.elements.length; i++)
    {   
        if (dd.elements[i].mainchild != null) {
            containers[j] = dd.elements[i];
            if (containers[j].mainchild.hotspot != null) {
                containers[j].mainchild.hotspot.hide();
            }
            containers[j].borderwidth = containers[j].mainchild.is_media ? $mediaborderwidth : $imageborderwidth;
            containers[j].resizeTo(containers[j].mainchild.w  + 2*containers[j].borderwidth, containers[j].mainchild.h  + 2*containers[j].borderwidth);
            containers[j].moveTo(containers[j].mainchild.x - containers[j].borderwidth, containers[j].mainchild.y - containers[j].borderwidth);
            containers[j].addChild(containers[j].mainchild);
            containers[j].mainchild.setZ(1);
            containers[j].mainchild.show();
            containers[j].setZ(2);
            j++;
        }
    }
    function isDefined(variable)
    { 
        return typeof(window[variable]) == 'undefined' ? false : true;
    }    
    //bk: to resolve overlib ie problem (causes dragged image to drop prematurely):
    //tip from: http://groups.yahoo.com/group/overlibmws/message/541
    if ((isDefined('OLie4') && OLie4) && (!isDefined('OLopr') || !OLopr)) document.onmousemove=null;
    ";

    
    if ($displaytype == 'edit') {
        $jstext .= "
            var glbl_image_visible = true;
            var glbl_hotspot_visible = false;
            
            
            function my_DropFunc()
            {
                hotspots_on_top();
                if (dd.obj.mainchild != null && !dd.obj.mainchild.positioned && (dd.obj.x != dd.obj.defx || dd.obj.y != dd.obj.defy)) {
                        dd.obj.mainchild.positioned = 1;
                }
            }
            
            function my_ResizeFunc()
            {
                if (dd.obj.mainchild != null) {
                    dd.obj.mainchild.resizeTo(dd.obj.w - 2*dd.obj.borderwidth, dd.obj.h - 2*dd.obj.borderwidth);
                    if (dd.obj.mainchild.is_media) {
                       resize_media(dd.obj.mainchild, 2*dd.obj.borderwidth);
                    }
                }
            }
            
            function resize_media(obj, borderwidth) {
                    var newx = obj.w;
                    var newy = obj.h;
                    var rewidth = new RegExp('(width *= *\")[0-9]+(\")', 'gi');
                    var reheight = new RegExp('(height *= *\")[0-9]+(\")', 'gi');
                    var replacewidth = '$1' + (newx - borderwidth) + '$2';
                    var replaceheight = '$1' + (newy - borderwidth) + '$2';
                    var newtag = obj.originalcontents.replace(rewidth, replacewidth);
                    newtag = newtag.replace(reheight, replaceheight)
                    obj.write(newtag);
                    obj.resizeTo(newx, newy);
                    obj.originalcontents = newtag;
            }
            
            function set_form_values(obj, ishotspot) {
                if (ishotspot) {
                    var form_x = 'hotspot' + obj.gapkey + '_x';
                    var form_y = 'hotspot' + obj.gapkey + '_y';
                    var form_width = 'hotspot' + obj.gapkey + '_width';
                    var form_height = 'hotspot' + obj.gapkey + '_height';
                } else {
                    var form_x = 'gapimage' + obj.gapkey + '_x';
                    var form_y = 'gapimage' + obj.gapkey + '_y';
                    var form_width = 'gapimage' + obj.gapkey + '_width';
                    var form_height = 'gapimage' + obj.gapkey + '_height';
                    var form_positioned = 'gapimage' + obj.gapkey + '_positioned';
                }

                eval('document.forms[0].' + form_x + '.value = ' + (obj.x - dd.elements.".$backgroundname.".x));
                eval('document.forms[0].' + form_y + '.value = ' + (obj.y - dd.elements.".$backgroundname.".y));
                eval('document.forms[0].' + form_width + '.value = ' + obj.w);
                eval('document.forms[0].' + form_height + '.value = ' + obj.h);
                if (!ishotspot) {
                    eval('document.forms[0].' + form_positioned + '.value = ' + obj.positioned);
                }
            }

            // snaps the hotspots to their associated images
            function snapto_hotspots(frm)
            {
                var length = containers.length;
                for (var i = 0; i < length; i++) {
                    hs = containers[i].mainchild.hotspot;
                    var newx = hs.w > containers[i].mainchild.w ? containers[i].mainchild.x - Math.floor((hs.w - containers[i].mainchild.w)/2) : containers[i].mainchild.x + Math.ceil((containers[i].mainchild.w - hs.w)/2);
                    var newy = hs.h > containers[i].mainchild.h ? containers[i].mainchild.y - Math.floor((hs.h - containers[i].mainchild.h)/2) : containers[i].mainchild.y + Math.ceil((containers[i].mainchild.h -hs.h)/2);
                    hs.moveTo(newx, newy);
                    hs.maximizeZ();
                }
                if (!glbl_image_visible) {
                    toggle_image_display(frm);
                }
                if (!glbl_hotspot_visible) {
                    toggle_hotspot_display(frm);
                }
            }
            
            // snaps the images to their associated hotspots 
            function snapto_images(frm)
            {
                var length = containers.length;
                for (var i = 0; i < length; i++) {
                    hs = containers[i].mainchild.hotspot;
                    var newx = hs.w < containers[i].mainchild.w ? hs.x + Math.floor((hs.w - containers[i].mainchild.w)/2) : hs.x - Math.floor((containers[i].mainchild.w -hs.w)/2);
                    var newy = hs.h < containers[i].mainchild.h ? hs.y + Math.floor((hs.h - containers[i].mainchild.h)/2) : hs.y - Math.floor((containers[i].mainchild.h -hs.h)/2);
                    containers[i].moveTo(newx, newy);
                    //containers[i].mainchild.hotspot.maximizeZ();
                }
                if (!glbl_image_visible) {
                    toggle_image_display(frm);
                }
                if (!glbl_hotspot_visible) {
                    toggle_hotspot_display(frm);
                } 
            }
            
            function toggle_image_display(frm)
            {  
                var length = containers.length;
                for (var i = 0; i < length; i++) {
                    if (glbl_image_visible) {
                        containers[i].hide();
                    } else {
                        containers[i].show();
                    }
                }
                // toggle buttons and the toggle value
                glbl_image_visible = !glbl_image_visible;
                if (glbl_image_visible) { 
                    frm.toggleimages.value = '" . get_string('hideimages', 'qtype_dragdrop') . "';
                    hotspots_on_top();
                } else {
                    frm.toggleimages.value = '" . get_string('showimages', 'qtype_dragdrop') . "';
                }
            }
            
            function toggle_hotspot_display(frm)
            {
                // toggle
                glbl_hotspot_visible = !glbl_hotspot_visible;
                if (glbl_hotspot_visible) { 
                    hotspots_on_top();
                    frm.togglehotspots.value = '" . get_string('hidehotspots', 'qtype_dragdrop') . "';
                } else {
                    hide_hotspots();
                    frm.togglehotspots.value = '" . get_string('showhotspots', 'qtype_dragdrop') . "';
                }
            }
            
            function show_hotspot(container, show) {
                if (show) {
                    container.mainchild.hotspot.show();
                    container.mainchild.hotspot.maximizeZ();
                } else {
                    container.mainchild.hotspot.hide();
                }
            }
            
            function hide_hotspots() {
                for (var i = containers.length - 1; i >= 0; i--)  {
                    containers[i].mainchild.hotspot.hide();
                }
            }
            
            function hotspots_on_top() {
                if (glbl_hotspot_visible) {
                    for (var i = containers.length - 1; i >= 0; i--)  {
                        show_hotspot(containers[i], true);
                    }
                }
            }
        ";
    } else if ($displaytype == 'display'){
        $jstext .= "
            function my_DropFunc()
            {
                if (dd.obj.mainchild != null) {
                    var bg = dd.elements[dd.obj.background];
                    var fieldvalue = (dd.obj.mainchild.x - bg.x) + ',' + (dd.obj.mainchild.y - bg.y) + ',' + dd.obj.mainchild.w + ',' + dd.obj.mainchild.h;
                    //eval(dd.obj.formfieldname + '.value = \"' + fieldvalue + '\"');
                    setValueById(dd.obj.formId, fieldvalue);
                }
            }
        ";
    }

    return $jstext;       
}
?>

