// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function hl_menu(p_id, c_id){
  var p = $(p_id);
  if(p){
    p.childElements().each(function(e){
      var cls = e.className.split("");
      var index = cls.indexOf("selected");
      if(index > -1){
        cls.splice(index, 1);
      }
      if(e.id == c_id){
        cls.push("selected");
        
      }
      e.className = cls.join(" ");
    });
  }
}


function highlight_teacher(id){
    $$(".teacher_menu_item").each(function(e){
      e.className = "teacher_menu_item"
    });
    var el = $(id);
    if(el){
        el.className = "teacher_menu_item selected";
    }
}

function highlight_item(id){
    $$(".course_menu").each(function(e){
      e.className = "course_menu"
    });
    var el = $(id);
    if(el){
        el.className = "course_menu selected";
    }
}



var dragger=null;
var ie=/MSIE/.test(navigator.userAgent) && !window.opera;
function setValue(id,value){
    if($(id)){$(id).value = value;}
}

function toggleMyTree(name){
    Element.toggle(name+'_'+'collapsed');
    Element.toggle(name+'_'+'expanded');
    Element.toggle(name+'_'+'children');
    return false;
}

function submitCheckBox(css,action,method_name,confirm_msg,request_method){
    var str = "";
    $$("input[class="+css+"]").each(function(value, index) {
        if(value.checked){
            str+=value.value+":"
        }            
    });
    str =str.slice(0,-1);
    if(str.strip()!=""){
        if(confirm_msg=="" || (confirm_msg !="" && confirm(confirm_msg))){
            if(request_method=="ajax")
            {
                new Ajax.Request(action, {
                    method: method_name,
                    parameters: {ids: str}
                });    
            }else{
                window.open(action+"?ids="+str);
            }
        }
    }else{
        alert("You should at least choose one check box.");
    }    
}

function check_select(str){
    var es = document.getElementsByTagName('input');
    var count = 0 ;
    for(i= 0; i< es.length; i++){
        //alert(es[i].id.substr(1, str.length));
        if(es[i].id.substr(0, str.length) == str && es[i].checked == true){
            count = count + 1;
        }
    }
    if(count == 0){
        alert("Please select at least 1 student");
        return false;
    }
}

function applicationSetValue(id, value){
    $(id).value=value;
    return true;
}

/*function setInnerHTML(name ,value){
   $(name).innerHTML=value;
   return true;
}
 */
function setUpperLeft (fobj, left, top) {
    // Whenever you need to set the upper-left coordinate, use this function.
    // My XPath logic assumes that the "top" attribute follows
    // the "left" attribute.
    fobj.style.left = left;
    fobj.style.top = top;
}

function refreshScore(partitions) {
    noteDiv = $("notes");
    partitions.each(function(item) {
        var id = item[0];
        var obj = $(id);
        if (obj == null) {
            obj = document.createElement("div");
            coord = id.substr(4).split("_");
            obj.id = id;
            setUpperLeft(obj, (coord[0] * 128) + "px", (coord[1] * 128) + "px");
            obj.className = "partition";
            noteDiv.appendChild(obj);
        }
        obj.innerHTML = unescape(item[1]);
    });
    rebuildPartitionArea();
    rebuildNoteInfomations();
}

function simulateMouseDown(obj, x, y) {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("mousedown", true, true, window,
    0, 0, 0, x, y, false, false, false, false, 0, null);
    return obj.dispatchEvent(evt);
}

function returnDispatchedMouseEvent(obj, x, y, eventtype, is_right){
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent(eventtype, true, true, window,
    0, 0, 0, x, y, false, false, false, false, is_right ? 2 : 0, null);
    obj.dispatchEvent(evt);
    return evt;
}

function processSaveWithNewName(service_for_assignment, forced) {
    var score_name = prompt('Please enter a name for your score:');
    if (score_name.strip()!="") {
        setValue("new_score_name", score_name);
        if (service_for_assignment != undefined) {
            setValue("service_for_assignment", service_for_assignment);
        }
        if (forced) {
            setValue("forced", 1);
        }
        $("save_as_button").click();
    }else{alert("Name can't be blank.");}
}

function confirmOverwrite() {
    if (confirm('The file already exists. Overwrite?')) {
        setValue("forced", 1);
        $("save_as_button").click();
    }
}

function processLoad() {
    var score_name = prompt('Please enter the name for the score you want to load: ');
    if (score_name.strip()!="") {
        setValue("new_score_name", score_name);
        setValue("type_of_action", 'load');
        $("submit_button").click();
    }else{alert("Name can't be blank.");}
}

function setClickAndSprinkle() {
    Draggables.drags.each(function(item) {
        item.options.revert=false;
    });
}

function clearClickAndSprinkle(){
    Draggables.drags.each(function(item) {
        id = item.element.id;
        num=id.substring(1)
        item.options.revert=true;
        item.destroy();
    });
	
    var nextPal = 10;
    for (var i=1; i < 14; i++)	{
        var id = 'n' + i;
        setUpperLeft($(id), nextPal + "px", topMargin);
        new Draggable(id,{revert:true, change: function (draggable) {
                currentOffset = draggable.offset;		 	
            }
        });
        nextPal += pitchDistance*4 + palSpace;
    }
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
//function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}
//function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? 
//
//        document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
//function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? 
//
//        document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function posRight() {return posLeft()+pageWidth();}
//function posBottom() {return posTop()+pageHeight();}



function topPos() {
    return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? 
        document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}
function leftPos() {
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? 

        document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}

function move_palette() {  
    $('palette').style.left = leftPos() + "px";
    if (leftPos() > (parseInt($('droppable').style.width)-pageWidth())) {
        $('droppable').style.width = (leftPos() + pageWidth() * 2) + "px";
    }
}

/*
function daniel_rounder(v){
    code="";
    if(v == 'top'){
        code = code + "    <div class='top'> \
                                <div class='r1'></div> \
                                <div class='r2'></div> \
                                <div class='r3'></div> \
                                <div class='r4'></div> \
                            </div>";
    }else if(v=='bottom'){
        code=code + "<div class='bottom'> \
                        <div class='r4'></div> \
                       <div class='r3'></div> \
                       <div class='r2'></div> \
                       <div class='r1'></div> \
                    </div>";
    }
    return code;
}

function insert_rounder(str){
    if(str == undefined) str = "round_box";
    var dvs = document.getElementsByTagName('div');
    var re = new RegExp("(^" + str + ")(.*)");
    var top = daniel_rounder('top');
    var bottom = daniel_rounder('bottom');
    for(i=0; i < dvs.length; i++){
        h = dvs[i].innerHTML;
        res = re.exec(dvs[i].id);
        if(res != null){
            dvs[i].innerHTML= top + "<div class='content'>" + h + "</div>" + bottom;
        }
    }
}
*/
function show_filer(id, str){
    if(str != undefined){
        dvs = document.getElementsByTagName('div');
        var re = new RegExp("(^" + str + ")(.*)");
        for(i=0; i < dvs.length; i++){
            res = re.exec(dvs[i].id);
            if(res != null){
                dvs[i].style.display = "none";
            }
        }
    }
    
    d = document.getElementById(id);
    d.style.display = "block";
}

function select_style(str, obj){
    if(str != undefined){
        var as = document.getElementsByTagName('a');
        var re = new RegExp("(^" + str + ")(.*)");
        var ne = /(^&gt; )(.*)/
        for(i=0; i < as.length; i++){
            res = re.exec(as[i].id);
            if(res != null){
                as[i].className = "none";
                //alert(ne);
                //alert(as[i].innerHTML)
                nes = ne.exec(as[i].innerHTML);
                if(nes != null){
                    as[i].innerHTML = nes[2];
                }
            }
        }
    }
    obj.className = "link_selected";
    // unset the focus
    obj.blur();
    //obj.innerHTML = "> " + obj.innerHTML;
}
