//THIS FILE CONTAINS MOST OF THE JS FUNCTIONS


//the constants
var highlight_color='#AACCFF';
var text_color='#000000';
var current_page_color='#114499';
var current_text_color='#FFFFFF';
var background_color="#5588FF";

//performs the highlighting for the menu and submenu.
function highlight_menu(){

//mouseover script
   $(".main_menu, .submenu").css({'background-color': background_color, 'color': text_color}).mouseover(function(){$(this).css({'background-color': highlight_color, 'color': text_color});})
   .mouseout(function(){$(this).css({'background-color': background_color, 'color': text_color})});



//current page script
for (i=0; i<menu_keys.length; i++){
    //alert('menu_key'+i+' = '+menu_keys[i]+' and menu_values'+i+' = '+menu_values[i]);
    var pathy = location.pathname.split('/');
    var path1;
    if (basepath.length>1){
        if (pathy[2].length>0){
        path1=pathy[0]+'/'+pathy[1]+'/'+pathy[2]+'/';
        }
        else {path1=pathy[0]+'/'+pathy[1]+'/';}
    }
    else {
        if (pathy[1].length>0){path1=pathy[1]+'/';}
        else {
            path1=pathy[1];}
        //alert (path1+' = '+basepath+menu_values[i]);
    }
    if (path1==basepath+menu_values[i]){
        //highlights the menu item of the page the user is on (and just clicked).
        $('#'+menu_keys[i]).css({'background-color': current_page_color, 'color': current_text_color}).unbind("mouseover").unbind("mouseout");
        //adds an additional class to the submenu items class, the name of the additional class is the highlighted menu item.
        $(".submenu").addClass(menu_keys[i]);
    }
}//end of for loop


}//end of function

//form ajax control; submits form data and outputs response to target element
function ajax_controls(key, value){
    //this function controls the ajax functions
        $('.form_submit.'+key).click(function(){
            //$('html, body').animate({scrollTop:200}, 'fast');
            $('#'+key).ajaxSubmit({target: '#'+value});
        });

}

//triggers when user clicks delete icon; sends info to server to delete and provides img_cat and image_id; server response appended to element.
function delete_img(image_id, img_cat){
    $("#img_del_"+img_cat+"_"+image_id).click(function(){
    $.get("", {action:'delete', img_cat1:img_cat, image_id1:image_id}, function(data){$(".td_"+img_cat+"_"+image_id).empty().append(data);});
    });
}

//triggers when user clicks get_mean_tdata button in moderator panel
function get_mean_tdata(test){
    $("#get_mean_"+test).click(function(){
    $.get("", {action:'get_mean', test_type: test}, function(data){$("#get_mean_"+test+"_response").empty().append(data);});
    });
}
/*
function vi_img_comp (img_cat1, image_id1, image_id2){
    $("#img_comp1 > img").click(function(){
        $.get("", {action:'vi_img_comp', img_cat: img_cat1, image_id1: image_id1, image_id2: image_id2}, function(data){$(".img_comp").empty().append(data);});
    });
    $("#img_comp2 > img").click(function(){
        $.get("", {action:'vi_img_comp', img_cat: img_cat1, image_id1: image_id2, image_id2: image_id1}, function(data){$(".img_comp").empty().append(data);});
    });
}
*/

//called when someone clicks username; shows user's profile
function show_user_profile(username1){
    $("."+username1).click(function(){
    $(".content > div").hide();
        highlight_menu();
        $("#UserProfile").css("background-color", current_page_color).unbind("mouseover").unbind("mouseout");
        $(".UserProfile").show();
        $.get("", {username:username1}, function(data){$(".UserProfile").empty().append(data)});
        scroll(0,0);
    });
}

//change username color on mouseover
function highlight_user_link (){
    $(".user_link").mouseover(function(){$(this).css("color", "blue")})
    .mouseout(function(){$(this).css("color", "black")});
}

//controls window position for popup windows
function popup_window (){
      $('a.popup').click(function(){
      window.open(this.href, "mywindow", "location=1,status=1,scrollbars=1, width=300,height=300");
      return false;
      });
}

function highlight_ssmenu(highlighted_submenu_item){
           //get position
       var num_sm = $(".submenu").length;//get number of submenu items
       var listItem = document.getElementById(highlighted_submenu_item);
       var rpos = $(".submenu").index(listItem);
       var pos = rpos/(num_sm)*100;
       if (pos > 70){pos = 70;}
       //define css
            $("#subsubmenu_"+highlighted_submenu_item).css({'margin-left': pos+"%",
                                "color":"black",
                                "font-size":"8pt",
                                "text-align":"center",
                                "font-family":"arial,sans-serif",
                                "font-weight":"bold",
                                "cursor":" pointer",
                                "width":" 30%"                                                                                      });
             $(".subsubmenu."+highlighted_submenu_item).css("background-color", background_color).css("color", text_color).mouseover(function(){$(this).css("background-color", highlight_color);})
            .mouseout(function(){$(this).css("background-color", background_color)});
    }

//displays the currently selected submenu section and hides the rest.
function menu_state(s1, s2){
    //show relevant smenu
        $(".content > div").hide();
        highlight_menu();
        $("#"+s1+".submenu").css({"background-color": current_page_color, "color": current_text_color}).unbind("mouseover").unbind("mouseout");
        $("."+s1).show();
   //show relevant ssmenu
   if (s2 === undefined){s2=0;}
   if (s2.length > 2){
       //change colors & content
        $("."+s1+" > div").not("#subsubmenu_"+s1).hide();
        highlight_ssmenu(s1);
        $("#"+s2+'.'+s1).css("background-color", current_page_color).css("color", current_text_color).unbind("mouseover").unbind("mouseout");
        $('.'+s2+', .'+s2+' > div').show();
}
}//end of function

//handles est vi ajax
function est_vi_comp (est_iteration1, img_cat1, image_id11, image_id21){
    $.post("index.php", {form_name: 'est_form', set: 3, est_iteration: est_iteration1, img_cat: img_cat1, image_id_w: image_id11, image_id_l: image_id21}, function(data){$("#est_form_response").empty().append(data);});

}

//updates vi img data
function update_vi_img_data(){
    $("#update_vi_img_data").click(function (){
    $.post('index.php', {moderate: 'update_vi_img_data'}, function(data){$("#update_vi_img_data").empty().append(data);});
    });}

//gets vi stats
function get_img_stats(){
    $("#get_img_stats").click(function (){
    $.post('index.php', {moderate: 'get_img_stats'}, function(data){$("#get_img_stats").empty().append(data);});
    });}
