$.ajaxSetup({ cache : false });

var magnify = function() {

    var cur_class = $(this).attr("class");
    var cur_id = $(this).attr("id");
    if ((cur_class == "level active") || (cur_id == "zoom-down") || (cur_id == "zoom-up"))
    {
        magnification_level = 0
        magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                      "level-seven");
        found = false;
        
        if (cur_class == "level active")
            magnification_level = $(this).find("a").html();
        else if (cur_id == "zoom-down") {
            current_magnification = $("#zoom li.current").find("a").html();
            magnification_level = current_magnification;
            while (!found && (magnification_level > 0)) {
                if ($("#" + magnification_ids[(magnification_level - 1) - 1]).attr("class") == "level active")
                    found = true;
                magnification_level--;
            }
            if (!found) return false;
            
        }
        else if (cur_id == "zoom-up") {
            current_magnification = $("#zoom li.current").find("a").html();
            magnification_level = current_magnification;
            while (!found && (magnification_level < 8)) {
                if ($("#" + magnification_ids[(magnification_level) - 1]).attr("class") == "level active")
                    found = true;
                magnification_level++;
            }
            magnification_level--;
            if (!found) return false;
        }
	
        var data = { magnification_level:magnification_level };
        $.getJSON("/magnify/", data, function(json) {
            set_primary_image(json['primary_image_path'])

            set_primary_ruler(json['primary_ruler_path'], json['primary_orientation']);

            previous_magnification = $("#zoom li.current").attr("id");

//            change_magnification_class(json['prev_magnification'], "level active");
            change_magnification_class(previous_magnification, "level active");
            change_magnification_class(json['new_magnification'], "level current");

//            set_primary_notes(json['primary_notes']);
            set_expanded_image("primary", json['primary_larger']);
            
            if (json['secondary_image_path'] != '') {
                set_secondary_image(json['secondary_image_path'])
                set_secondary_ruler(json['secondary_ruler_path'], json['secondary_orientation']);
//                set_secondary_notes(json['secondary_notes']);
                set_expanded_image("secondary", json['secondary_larger']);
            }

            $("img").load(function () {
                preset();
            });
        });
    }
    
    return false;
}

var change_light = function() {
                 
    if (($(this).attr("class") != "current") && ($(this).attr("class") != "inactive"))
    {
        lighting = $(this).html();
        degree = String.fromCharCode(176);
        lighting = lighting.replace(degree, "");
        
        var data = { lighting:lighting };
        $.getJSON("/change_light/", data, function(json) {
            if (json['is_video'])
            {
                set_primary_video(json['primary_video']);
                if (json['secondary_video'])
                    set_secondary_video(json['secondary_video']);
                $("#light-toggle a.current").removeClass("current");
                $("#light-toggle li.first").find("a").addClass("current");
                $("#light-icons-wrap > div").remove();
                $("#light-icons-wrap a.current").removeClass("current");
                $("#light-icons-wrap li.first").find("a").addClass("current");

                magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                              "level-seven");
                for (i = 1; i <= 7; i++) {
                    change_magnification_class(magnification_ids[i-1], "level");
                }
                
                return false;
            }
            
            magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                          "level-seven");
            for (i = 1; i <= 7; i++) {
                change_magnification_class(magnification_ids[i-1], json['magnification' + i])    
            }
            
            set_new_light(json['new_light_id'], json['new_light_angle'], json['new_light_number']);

            set_primary_image(json['primary_image_path']);
            set_primary_ruler(json['primary_ruler_path'], json['primary_orientation']);
            
//            set_primary_notes(json['primary_notes']);
            set_expanded_image("primary", json['primary_larger']);

            if (json['secondary_image_path'] != '') {
                set_secondary_image(json['secondary_image_path']);
                set_secondary_ruler(json['secondary_ruler_path'], json['secondary_orientation']);
//                set_secondary_notes(json['secondary_notes']);
                set_expanded_image("secondary", json['secondary_larger']);
            }

            add_confirm_links(json['new_light_angle']);
        });
    }
    
    return false;
}

var change_side = function() {
    if ($(this).attr("class") != "current") {
        current_side = $("#side-toggle a.current").html();
        data = { current_side:current_side };
        $.getJSON("/change_side/", data, function(json) {
            if (json['primary_image_url']) {
                if (current_side == "Front") {
                    $("#orient-toggle a").removeClass();
                    $("#orient-toggle a").addClass("inactive");
                    $("#orient-toggle > a").html('');

                    $("#side-toggle li.first").find('a').removeClass();
                    $("#side-toggle li.last").find('a').addClass("current");

                    $("#side-toggle a:first").removeClass();
                    $("#side-toggle a:first").addClass("icon back");
                    $("#side-toggle a:first").html("Back");
                }
                else {
                    $("#orient-toggle a").removeClass();
                    $("#orient-toggle li.first").find("a").addClass("current");
                    $("#orient-toggle > a").html('Surface');
                    $("#orient-toggle > a").addClass("icon surface");

                    $("#side-toggle li.last").find('a').removeClass();
                    $("#side-toggle li.first").find('a').addClass("current");

                    $("#side-toggle a:first").removeClass();
                    $("#side-toggle a:first").addClass("icon front");
                    $("#side-toggle a:first").html("Front");
                }
                
                set_primary_image(json['primary_image_url']);
                set_primary_ruler(json['primary_ruler_url'], json['primary_orientation']);
//                set_primary_notes(json['primary_notes']);
                set_expanded_image("primary", json['primary_larger']);
    
                if (json['secondary_image_url']) {
                    set_secondary_image(json['secondary_image_url']);
                    set_secondary_ruler(json['secondary_ruler_url'], json['secondary_orientation']);
//                    set_secondary_notes(json['secondary_notes']);
                    set_expanded_image("secondary", json['secondary_larger']);
                }
    
                magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                              "level-seven");
                for (i = 1; i <= 7; i++) {
                    change_magnification_class(magnification_ids[i-1], json['magnification' + i])
                }
                
                lighting_data_index = new Array("light45uv", "light0", "light45", "light90");
                lighting_ids = new Array("first", "second", "third", "last");
                for (i = 0; i < 4; i++) {
                    current_index = lighting_data_index[i];
                    if (json[current_index] == 'inactive')
                        change_light_class(lighting_ids[i], "inactive");
                    else
                        change_light_class(lighting_ids[i], "");
                }
                set_new_light(json['new_light_id'], json['new_light_angle'], json['new_light_number']);

                //Hack for not showing the video when the process is showing the Back
                if (current_side == "Front")
                    change_light_class("first", "inactive");
            }

            add_confirm_links(json['new_light_angle']);
        });
    }   
    
    return false;
}

var change_orientation = function () {

    if ($(this).attr("class") != "current") {
        current_orientation = $("#orient-toggle a.current").html();
        data = { current_orientation:current_orientation };
        $.getJSON("/change_orientation/", data, function(json) {
            magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                          "level-seven");
            for (i = 1; i <= 7; i++) {
                change_magnification_class(magnification_ids[i-1], json['magnification' + i])
            }

            set_primary_image(json['primary_image_path']);
            set_primary_ruler(json['primary_ruler_path'], json['primary_orientation']);
//            set_primary_notes(json['primary_notes']);
            set_expanded_image("primary", json['primary_larger']);
            
            if (json['secondary_image_path'] != '') {
                set_secondary_image(json['secondary_image_path']);
                set_secondary_ruler(json['secondary_ruler_path'], json['secondary_orientation']);
//                set_secondary_notes(json['secondary_notes']);
                set_expanded_image("secondary", json['secondary_larger']);
            }
            
            $("#orient-toggle a.current").removeClass("current");
            if (current_orientation == "Surface") {
                $("#orient-toggle li.last").find("a").addClass("current");
                $("#orient-toggle > a").removeClass("icon surface");
                $("#orient-toggle > a").addClass("icon edge");
                var current_class = $("#side-toggle > a").attr("class");
                $("#side-toggle > a").removeClass(current_class);
                $("#side-toggle > a").html('');
                $("#side-toggle > a").addClass("inactive");
                var current_class = $("#side-toggle li.first").find("a").attr("class");
                $("#side-toggle li.first").find("a").removeClass(current_class);
                $("#side-toggle li.first").find("a").addClass("inactive");
                var current_class = $("#side-toggle li.last").find("a").attr("class");
                $("#side-toggle li.last").find("a").removeClass(current_class);
                $("#side-toggle li.last").find("a").addClass("inactive");
            }
            else {
                $("#orient-toggle li.first").find("a").addClass("current");
                $("#orient-toggle > a").removeClass("icon edge");
                $("#orient-toggle > a").addClass("icon surface");

                $("#side-toggle > a").removeClass("inactive");
                $("#side-toggle li.first").find("a").removeClass("inactive");
                $("#side-toggle li.last").find("a").removeClass("inactive");
                if (json['primary_side'] == "front") {
                    $("#side-toggle > a").addClass("icon front");
                    $("#side-toggle li.first").find("a").addClass("current");
                }
                else {
                    $("#side-toggle > a").addClass("icon back");
                    $("#side-toggle li.last").find("a").addClass("current");
                }
            }

            lighting_data_index = new Array("light45uv", "light0", "light45", "light90");
            lighting_ids = new Array("first", "second", "third", "last");
            for (i = 0; i < 4; i++) {
                current_index = lighting_data_index[i];
                if (json[current_index] == 'inactive')
                    change_light_class(lighting_ids[i], "inactive");
                else
                    change_light_class(lighting_ids[i], "");
            }
            set_new_light(json['new_light_id'], json['new_light_angle'], json['new_light_number']);

            add_confirm_links(json['new_light_angle']);
        });
    }             

    return false;
}

function set_secondary_process(process_id) {
	var data = { process_id:process_id };
    $.getJSON("/set_secondary_process/", data, function(json) {
		if ((json['primary_image_url'] != '') && (json['secondary_image_url'] != '')) {

            if ($("#secondary-notes").attr("class") != "notes") { window.location = "/compareprocesses/"; }

            change_class("primary", "compare half");
            change_class("secondary", "compare");
            change_class("secondary-notes", "notes");
            $("#secondary-selection > h2").show();

           if ($("#secondary-group-select a.groups-toggle").html() != "Select a New Process")
           {
//                $("#secondary-group-select a.groups-toggle").html("Select a New Process");
                $("#secondary-group-select a.groups-toggle").removeClass("add-comparison-btn");
                //$("#secondary-group-select").prepend('<em>from the group:</em> <strong> </strong>');
            }

            set_primary_image(json['primary_image_url']);
            set_secondary_image(json['secondary_image_url']);

            set_primary_ruler(json['primary_ruler_url'], json['primary_orientation']);
            set_secondary_ruler(json['secondary_ruler_url'], json['secondary_orientation']);

//            set_primary_notes(json['primary_notes']);
//            set_secondary_notes(json['secondary_notes']);

            set_expanded_image("primary", json['primary_larger']);
            set_expanded_image("secondary", json['secondary_larger']);
            
            magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                          "level-seven");
            for (i = 1; i <= 7; i++) {
                change_magnification_class(magnification_ids[i-1], json['magnification' + i])
            }
            
            lighting_data_index = new Array("light45uv", "light0", "light45", "light90");
            lighting_ids = new Array("first", "second", "third", "last");
            for (i = 0; i < 4; i++) {
                current_index = lighting_data_index[i];
                if (json[current_index] == 'inactive')
                    change_light_class(lighting_ids[i], "inactive");
                                else
                                    change_light_class(lighting_ids[i], "");
            }
            set_new_light(json['new_light_id'], json['new_light_angle'], json['new_light_number']);

            set_new_hints(json);

            add_confirm_links(json['new_light_angle']);

            var div_id = "secondary-selection";
//            var select_id = "secondary-select";
//            $("#" + div_id + " .selection-wrap > input").remove();
//            $("#" + div_id + " .selection-wrap > div:first").remove();
//            $("#" + div_id + " .selection-wrap > select").remove();

//            var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
//                '<option value="">(Select a Process)</option>';

//            var i = 0;
//            while (json['process_id_' + i] != null) {
//                tag = tag + '<option value="' + json['process_id_' + i] + '"';
//                if (json['process_id_' + i] == json['process_id'])
//                    tag = tag + " selected";
//                tag = tag + '>' + json['process_name_' + i] + '</option>';
//                i++;
//            }

//            tag = tag + '</select>';
//            $("#" + div_id + " p:first").before(tag);
//            $("#" + select_id).selectbox({debug: true});

            $("#" + div_id + " strong:last").html(json['group_name']);            

            $("img").load(function () {
                preset();
            });

        }
    });
}

function set_primary_process(process_id) {
    var data = { process_id:process_id };
    $.getJSON("/set_primary_process/", data, function(json) {
        if (json['primary_image_url']) {

            set_primary_image(json['primary_image_url']);
            set_primary_ruler(json['primary_ruler_url'], json['primary_orientation']);
//            set_primary_notes(json['primary_notes']);
            set_expanded_image("primary", json['primary_larger']);

            if (json['secondary_image_url']) {
                change_class("primary", "compare half");
                change_class("secondary", "compare");
                change_class("secondary-notes", "notes");
                set_secondary_image(json['secondary_image_url']);
                set_secondary_ruler(json['secondary_ruler_url'], json['secondary_orientation']);
//                set_secondary_notes(json['secondary_notes']);
                set_expanded_image("secondary", json['secondary_larger']);
            }
            else {
                change_class("primary", "compare half");
                change_class("secondary", "inactive");
                change_class("secondary-notes", "notes inactive");
                set_orientation(json['primary_view'], json['primary_side']);
                if (json['primary_side'] == "front" && json['primary_view'] == "surface") {
                    $("#orient-toggle a").removeClass();
                    $("#orient-toggle li.first").find("a").addClass("current");
                    $("#orient-toggle > a").html('Surface');
                    $("#orient-toggle > a").addClass("icon surface");

                    $("#side-toggle li.last").find('a').removeClass();
                    $("#side-toggle li.first").find('a').addClass("current");

                    $("#side-toggle a:first").removeClass();
                    $("#side-toggle a:first").addClass("icon front");
                    $("#side-toggle a:first").html("Front");                    
                }
            }
            
            magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                          "level-seven");
            for (i = 1; i <= 7; i++) {
                change_magnification_class(magnification_ids[i-1], json['magnification' + i])
            }
            
            lighting_data_index = new Array("light45uv", "light0", "light45", "light90");
            lighting_ids = new Array("first", "second", "third", "last");
            for (i = 0; i < 4; i++) {
                current_index = lighting_data_index[i];
                if (json[current_index] == 'inactive')
                    change_light_class(lighting_ids[i], "inactive");
                else
                    change_light_class(lighting_ids[i], "");
            }
            set_new_light(json['new_light_id'], json['new_light_angle'], json['new_light_number']);
            set_new_hints(json);

            add_confirm_links(json['new_light_angle']);

            var div_id = "primary-selection";
//            var select_id = "primary-select";
//            $("#" + div_id + " .selection-wrap > input").remove();
//            $("#" + div_id + " .selection-wrap > div:first").remove();
//            $("#" + div_id + " .selection-wrap > select").remove();

//            var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
//                '<option value="">(Select a Process)</option>';

//            var i = 0;
//            while (json['process_id_' + i] != null) {
//                tag = tag + '<option value="' + json['process_id_' + i] + '"';
//                if (json['process_id_' + i] == json['process_id'])
//                    tag = tag + " selected";
//                tag = tag + '>' + json['process_name_' + i] + '</option>';
//                i++;
//            }

//            tag = tag + '</select>';
//            $("#" + div_id + " p:first").before(tag);
//            $("#" + select_id).selectbox({debug: true});

            $("#" + div_id + " strong").html(json['group_name']);

            $("img").load(function () {
                preset();
            });
        }
    });
}

var hint_change = function() {
    //var value = $(this).find("option:selected").attr("value");
    var value = $(this).attr("href");
 
    if (value != 'null') {
        var data = { explorer_data_id:value };
        $.getJSON("/hint_change/", data, function(json) {

            if (json['primary_image_path']) {
                set_primary_image(json['primary_image_path'])
    
                set_primary_ruler(json['primary_ruler_path'], json['primary_orientation']);
//                set_primary_notes(json['primary_notes']);
                set_expanded_image("primary", json['primary_larger']);
    
                magnification_ids = new Array("level-one", "level-two", "level-three", "level-four", "level-five", "level-six",
                                              "level-seven");
                for (i = 1; i <= 7; i++) {
                    change_magnification_class(magnification_ids[i-1], json['magnification' + i])
                }

                lighting_data_index = new Array("light45uv", "light0", "light45", "light90");
                lighting_ids = new Array("first", "second", "third", "last");

                for (i = 0; i < 4; i++) {
                    current_index = lighting_data_index[i];
                    if (json[current_index] == 'inactive')
                        change_light_class(lighting_ids[i], "inactive");
                    else
                        change_light_class(lighting_ids[i], "");
                }                
    
                set_new_light(json['new_light_id'], json['new_light_angle'], json['new_light_number']);
                set_orientation(json['primary_view'], json['primary_side']);
            }
            if (json['secondary_image_path']) {
                set_secondary_image(json['secondary_image_path']);
                set_secondary_ruler(json['secondary_ruler_path'], json['secondary_orientation']);
//                set_secondary_notes(json['secondary_notes']);
                set_expanded_image("secondary", json['secondary_larger']);
            }

        });          
    }
    
    return false;
}

var reload = function() {
    var curr_id = $(this).attr("id");
    var data = { id:curr_id };

    $.getJSON("/comparison_update/", data, function(json) {
        window.location = json['page'];
    });
    
    return false;
}

var set_confirm = function() {
    $.getJSON("/set_confirm/", null, function(json) {
        if (json['primary_image_url']) {
            set_primary_image(json['primary_image_url']);
            set_primary_ruler(json['primary_ruler_url'], json['primary_orientation']);
//            set_primary_notes(json['primary_notes']);
            set_expanded_image("primary", json['primary_larger']);
        }

        if (json['secondary_image_url']) {
            set_secondary_image(json['secondary_image_url']);
            set_secondary_ruler(json['secondary_ruler_url'], json['secondary_orientation']);
//            set_secondary_notes(json['secondary_notes']);
            set_expanded_image("secondary", json['secondary_larger']);
        }

        $("#light-toggle a.current").removeClass("current");
        $("#light-icons-wrap > div").remove();
        $("#light-icons-wrap").prepend('<div class="light-source light3"><img src="/media/themes/ga/images/' +
                                       '45-light-source.png"></div>');
    });
    
    return false;
}

var enlarge_both = function() {
	winWidth = 400;
	winheight = 400;
	
	if (screen){ // weeds out older browsers who do not understand screen.width/screen.height
		winWidth = screen.width;
		winHeight = screen.height;
	}
	winWidth = winWidth/2 - 20;
	
	winRight = winWidth + 40;
	
    $.getJSON("/enlarge_both/", null, function(json) {
        window.open(json['secondary_image'], 'expand_right', 'resizable=yes,left='+winRight+',width='+winWidth+',height='+winHeight).focus();
        window.open(json['primary_image'], 'expand_left', 'resizable=yes,left=20,width='+winWidth+',height='+winHeight).focus();
    });
              
    return false;
}

$(document).ready(function() {
    $("#zoom > li").click(magnify);
    $("#light-toggle  a").click(change_light);
    $("#side-toggle a").click(change_side);
    $("#orient-toggle a").click(change_orientation);
    //$("#hint-select").change(hint_change);
    $("#primary-notes a").click(hint_change);
    $("#secondary-notes a").click(hint_change);
    $("#remove-comparison").click(reload);
    $("#switch-comparison").click(reload);
    $("#s-identification").click(reload);
    $("#s-guided-tour").click(reload);
    $("#s-technology").click(reload);
    $("#confirm-light").click(set_confirm);
    $("#confirm-light2").click(set_confirm);
    $("#enlarge-both").click(enlarge_both);
 
   setGroups();

    //####################################################//

    $(".browse li a").click(function() {
		
        var process_id = $(this).attr("href");

        var _parent = $(this).parents().get(5);
        var parent_id = $(_parent).attr("id");
        
        if (parent_id == "secondary-group-select-box")
            set_secondary_process(process_id);
        else
            set_primary_process(process_id);

        closeWindow();

        return false;
    });

	$("a.groups-toggle").click(function() {
		$("div.browse").show();
        if(openStatus==0){
			openWindow($(this));
		}else {
			closeWindow();
		}
		return false;
	});
    
	$("div.groups p > a").click(function() {
		closeWindow($(this));
		return false;
	});

	//Browsing drop-downs
	$("div.cats ul").hide();
	$("div.cats h5").click(function(event) {
		$(this).next("ul").toggle();
		
		if($(this).children("span").hasClass("collapse")){
			$(this).children("span").removeClass();
		}else {
			$(this).children("span").addClass("collapse");
		}
	});
	
	/*$("div.groups ul > li > a").click(function() {
        var group_id = $(this).attr("href");
        var _parent = $(this).parents().get(2);
        var parent_id = $(_parent).attr("id");

        var div_id = "secondary-selection";
        var select_id = "secondary-select";
        if (parent_id == "primary-group-select-box") {
            var div_id = "primary-selection";
            var select_id = "primary-select";
        }

        var data = { group_id:group_id };
        $.getJSON("/change_select_group/", data, function(json) {
            $("#" + div_id + " .selection-wrap > input").remove();
            $("#" + div_id + " .selection-wrap > div:first").remove();
            $("#" + div_id + " .selection-wrap > select").remove();

            var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
                '<option value="">(Select a Process)</option>';

            var i = 0;
            while (json['process_id_' + i] != null) {
                tag = tag + '<option value="' + json['process_id_' + i]  + '">' +
                    json['process_name_' + i] + '</option>';
                i++;
            }

            tag = tag + '</select>';
            $("#" + div_id + " p:first").before(tag);
            $("#" + select_id).selectbox({debug: true});

            $("#" + div_id + " a.groups-toggle").html(json['group_name']);
        });
         
		closeWindow();
		
		if(parent_id == "secondary-group-select-box"){
			$("#secondary-selection > h2").show();
			$('#secondary-selection #secondary-select_input').show();
            if ($('#secondary-group-select em').html() != "from the group:")
                $('#secondary-group-select').prepend("<em>from the group:</em>");
		}
		return false;
    }); */
});

function set_orientation(current_orientation, primary_side) {
    $("#orient-toggle a.current").removeClass("current");
    if (current_orientation == "cross") {
        $("#orient-toggle li.last").find("a").addClass("current");
        $("#orient-toggle > a").removeClass("icon surface");
        $("#orient-toggle > a").addClass("icon edge");
        var current_class = $("#side-toggle > a").attr("class");
        $("#side-toggle > a").removeClass(current_class);
        $("#side-toggle > a").html('');
        $("#side-toggle > a").addClass("inactive");
        var current_class = $("#side-toggle li.first").find("a").attr("class");
        $("#side-toggle li.first").find("a").removeClass(current_class);
        $("#side-toggle li.first").find("a").addClass("inactive");
        var current_class = $("#side-toggle li.last").find("a").attr("class");
        $("#side-toggle li.last").find("a").removeClass(current_class);
        $("#side-toggle li.last").find("a").addClass("inactive");
    }
    else {
        $("#orient-toggle li.first").find("a").addClass("current");
        $("#orient-toggle > a").removeClass("icon edge");
        $("#orient-toggle > a").addClass("icon surface");

        $("#side-toggle > a").removeClass("inactive");
        $("#side-toggle li.first").find("a").removeClass("inactive");
        $("#side-toggle li.last").find("a").removeClass("inactive");
        if (primary_side == "front") {
            $("#side-toggle > a").addClass("icon front");
            $("#side-toggle li.first").find("a").addClass("current");
        }
        else {
            $("#side-toggle > a").addClass("icon back");
            $("#side-toggle li.last").find("a").addClass("current");
         }
    }
}

function call_preset() {
    $("img").load(function () {
        preset();
    });
}

function change_class(element_id, new_class) {
    var current_class = $("#" + element_id).attr("class");
    if (current_class != new_class) {
        $("#" + element_id).removeClass(current_class);
        $("#" + element_id).addClass(new_class);
    }
}

function change_magnification_class(element_id, new_class) {
    var current_class = $("#" + element_id).attr("class");
    $("#" + element_id).removeClass(current_class);
    $("#" + element_id).addClass(new_class)
}

function change_light_class(element_id, new_class) {
    var current_class = $("#light-toggle li." + element_id).find("a").attr("class");
    $("#light-toggle li." + element_id).find("a").removeClass(current_class);
    $("#light-toggle li." + element_id).find("a").addClass(new_class);
}

function set_primary_ruler(url, orientation) {
    ruler_img_tag = '<img src="' + url + '" id="primary-image-ruler" class="' + orientation + '">';
    $("#primary-image-ruler").remove();
    if (orientation == 'landscape')
        $("#primary-image").after(ruler_img_tag);
    else if (orientation == 'portrait')
        $("#primary-image").before(ruler_img_tag);
}

function set_secondary_ruler(url, orientation) {
    $("#secondary-image-ruler").attr({
        src: url
    });
    change_class("secondary-image-ruler", orientation);
}   

function set_primary_image(url) {
    if ($("#primary-image").attr('src') == null)
    {
        $("#primary .images").empty();
        tag = '<img src="' + url + '" id="primary-image" />';
        tag += '<img src="" id="primary-image-ruler" class=""  />'
        $("#primary .images").html(tag);
            
    }
    $("#primary-image").attr({
        src: url
    });
}

function set_secondary_image(url) {
    if ($("#secondary-image").attr('src') == null)
    {
        $("#secondary .images").empty();
        tag = '<img src="' + url + '" id="secondary-image" />';
        tag += '<img src="" id="secondary-image-ruler" class=""  />'
        $("#secondary .images").html(tag);
    }
    
    $("#secondary-image").attr({
        src: url
    });
}

function set_new_light(new_light_id, new_light_angle, new_light_number) {
    $("#light-toggle a.current").removeClass("current");
    $("#light-toggle li." + new_light_id).find("a").addClass("current")
    $("#light-icons-wrap > div").remove();
    $("#light-icons-wrap").prepend('<div class="light-source light' + new_light_number +
                                   '"><img src="/media/themes/ga/images/' +  new_light_angle +
                                   '-light-source.png"></div>')
    if ((new_light_angle == "45uv") && ($("#orient-toggle a.current").html() != "Edge"))
        $("#light-icons-wrap").prepend('<div class="light-source light3"><img src="/media/themes/ga/images/' +
                                       '45-light-source.png"></div>');
    //$("#light-icons-wrap > div").find("img").attr("src", "/media/themes/ga/images/" + new_light_angle + "-light-source.png");
    //$("#light-icons-wrap > div").removeClass(); 
    //$("#light-icons-wrap > div").addClass("light-source light" + new_light_number);
}

function set_primary_notes(notes) { /*
    $("#primary-notes").children("p").remove();
    $("#primary-notes").append("<p>" + notes + "</p> "); */
}

function set_secondary_notes(notes) { /*
    $("#secondary-notes").children("p").remove();
    $("#secondary-notes").append("<p>" + notes + "</p> "); */
}

function add_confirm_links(light_angle) {
    if (light_angle == "45uv") {
        current_orientation = $("#orient-toggle a.current").html();
        if (current_orientation != "Edge") {
            $("#primary-notes").append('<p><a href="" id="confirm-light">Confirm Light</p>');
            $("#secondary-notes").append('<p><a href="" id="confirm-light2">Confirm Light</p>');
            $("#confirm-light").click(set_confirm);
            $("#confirm-light2").click(set_confirm);
        }
    }
}

function set_expanded_image(id, url) {
    $("#" + id + "-larger").attr({
        href: url
    });
}

function set_new_hints(json) {
/*    $("#hint-select").empty();

    var tag = '<option value="null">What to notice about this print:</option>';
    var i = 1;
    
    if (json['secondary_process_name'])
        tag += '<optgroup label="' + json['primary_process_name'] + '">';
    while (json['primary_hint_id' + i]) {
        tag += '<option value="' + json['primary_hint_id' + i] + '">' + json['primary_hint_name' + i] + '</option>';
        i++;
    }
    if (json['secondary_process_name']) {
        tag += '</optgroup>';
        i = 1;

        tag += '<optgroup label="' + json['secondary_process_name'] + '">';
        while (json['secondary_hint_id' + i]) {
            tag += '<option value="' + json['secondary_hint_id' + i] + '">' + json['secondary_hint_name' + i] + '</option>';
            i++
        }
        tag += '</optgroup>';
    }
    $("#hint-select").html(tag); */

    //$("#primary-notes").children("ul").remove();
    //$("#secondary-notes").children("ul").remove();

    var tag = "\n<h3>What to notice:</h3>\n";
    var i = 1;
    
    if (json['primary_hint_id' + i]) {
        tag += "<ul>\n";
        while (json['primary_hint_id' + i]) {
            tag += '<li><a href="' + json['primary_hint_id' + i] + '">' + json['primary_hint_name' + i] + "</a></li>\n";
            i++;
        }
        tag += "</ul>\n";
    }

    $("#primary-notes").html(tag);

    var tag2 = "<h3>What to notice:</h3>\n";
    var i = 1;

    if (json['secondary_hint_id' + i]) {
        tag2 += "<ul>\n";
        while (json['secondary_hint_id' + i]) {
            tag2 += '<li><a href="' + json['secondary_hint_id' + i] + '">' + json['secondary_hint_name' + i] + "</a></li>\n";
            i++;
        }
        tag2 += "</ul>\n";
    }

    $("#secondary-notes").html(tag2);
    
    $("#primary-notes a").click(hint_change);
    $("#secondary-notes a").click(hint_change);
}

function set_primary_video(url)
{
    $("#primary .images").empty();

    /*    var tag = '<object CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="465" height="310" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">'
    tag += '<param name="src" value="' + url  + '">'
	tag += '<param name="autoplay" value="true">'
	tag += '<param name="loop" value="true">'
	tag += '<param name="wmode" value="transparent">'
	tag += '<param name="controller" value="true">'
    tag += '<embed src="'+ url + '" width="465" height="310" wmode="transparent" autoplay="true" loop="true" controller="true" pluginspage="http://www.apple.com/quicktime/"></embed>'
    tag += '</object>' */

    var tag = '<div id="video-holder"></div>';
    tag += '<script type="text/javascript">';
    tag += "var s1 = new SWFObject('/media/mediaplayer/player.swf','player','465','310','9');";
    tag += "s1.addParam('allowfullscreen','true');";
    tag += "s1.addParam('allowscriptaccess','always');";
    tag += "s1.addParam('wmode','transparent');";
    tag += "s1.addVariable('file', '" + url + "');";
    tag += "s1.addVariable('autostart','true');";
    tag += "s1.addVariable('repeat','always');";
    tag += "s1.addVariable('controlbar','none');";
    tag += "s1.write('video-holder');";
    tag += "</script>";
    
    $("#primary .images").html(tag);
}

function set_secondary_video(url)
{
    $("#secondary .images").empty();

    /*    var tag = '<object CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="465" height="310" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">'
    tag += '<param name="src" value="' + url  + '">'
	tag += '<param name="autoplay" value="true">'
	tag += '<param name="loop" value="true">'
	tag += '<param name="wmode" value="transparent">'
	tag += '<param name="controller" value="true">'
    tag += '<embed src="'+ url + '" width="465" height="310" wmode="transparent" autoplay="true" loop="true" controller="true" pluginspage="http://www.apple.com/quicktime/"></embed>'
    tag += '</object>' */

    var tag = '<div id="video-holder2"></div>';
    tag += '<script type="text/javascript">';
    tag += "var s2 = new SWFObject('/media/mediaplayer/player.swf','player','465','310','9');";
    tag += "s2.addParam('allowfullscreen','true');";
    tag += "s2.addParam('allowscriptaccess','always');";
    tag += "s2.addParam('wmode','transparent');";
    tag += "s2.addVariable('file', '" + url + "');";
    tag += "s2.addVariable('autostart','true');";
    tag += "s2.addVariable('repeat','always');";
    tag += "s2.addVariable('controlbar','none');";
    tag += "s2.write('video-holder2');";
    tag += "</script>";    
    
    $("#secondary .images").html(tag);
}
