var eventQueue = new Object;
eventQueue['run'] = null;

function addLoader( func )
{

    if ( eventQueue['run'] && eventQueue['run'] != null )
        {
            var oldRun = eventQueue['run'];
            eventQueue['run'] = function(e) { oldRun(e); func(e) }
        }
    else
        {
            eventQueue['run'] = function(e) { func(e) }
        }

}


function addEvent(oTarget, sType, fpDest) {

  if(oTarget) {
    sType = 'on'+sType;
    var oOldEvent = oTarget[sType];
    if (typeof oOldEvent != "function") {
	oTarget[sType] = fpDest;
    } else {
	oTarget[sType] = function(e) {
	    oOldEvent(e);
	    fpDest(e);
	}
    }
    
    EventCache.add(oTarget, sType, fpDest);

  } 
}

/* Event Cache Code copyright 2005 Mark Wubben

Provides a way for automagically removing events from nodes and thus preventing memory leakage.
See <http://novemberborn.net/javascript/event-cache> for more information.
	
This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

if(Array.prototype.push == null){
    Array.prototype.push = function(){
	for(var i = 0; i < arguments.length; i++){
	    this[this.length] = arguments[i];
	};
	return this.length;
    };
};

var EventCache = function(){
    var listEvents = [];
	
    return {
	listEvents : listEvents,
	
	    add : function(node, sEventName, fHandler, bCapture){
	    listEvents.push(arguments);
	},
	
	    flush : function(){
		var i, item;
		for(i = listEvents.length - 1; i >= 0; i = i - 1){
		    item = listEvents[i];
				
		    if(item[0].removeEventListener){
			item[0].removeEventListener(item[1], item[2], item[3]);
		    };
		    
		    /* From this point on we need the event names to be prefixed with 'on" */
		    if(item[1].substring(0, 2) != "on"){
			item[1] = "on" + item[1];
		    };
		    
		    if(item[0].detachEvent){
			item[0].detachEvent(item[1], item[2]);
		    };
		    
		    item[0][item[1]] = null;

		};
		eventHolder.run = null;
	    }
    };
}();

function get_event_target( e )
{
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    return targ;

}

function setup_subsectors()
{
    if ( ! ( document.getElementById('jobTypeMulti') && subsubsectors && document.getElementById('coreskill') ) ) { return }

    addEvent( document.getElementById('coreskill'), 'change', set_subsectors );
    
    document.getElementById('coreskill').onchange();
}

function set_subsectors()
{
    if ( this.selectedIndex == 0 ) { hide_multi(); return }
    if ( ! subsubsectors[this.options[this.selectedIndex].value] ||  subsubsectors[this.options[this.selectedIndex].value].length == 0 ) { hide_multi(); return }

    var sub_sector_list = subsubsectors[this.options[this.selectedIndex].value];
    var target = document.getElementById('jobTypeMulti');

    target.innerHTML = '';

    var selected = null;

    if ( current_specialisms && current_specialisms.length > 0 )
	{
	    selected = new Object;
	    for ( var i = 0; i < current_specialisms.length; i++ )
		{
		    selected[current_specialisms[i]] = true;
		}
	    
	    current_specialisms = null;
	}
    
    
    for ( var j = 0; j < sub_sector_list.length; j++ )
	{
	    var cell2 = document.createElement('P');
	    target.appendChild( cell2 );
	    
	    var div = document.createElement( 'DIV');
	    div.className = 'checkWrap';
	    cell2.appendChild(div);
	    
	    var check = document.createElement( 'INPUT' );
	    check.type = 'checkbox';
	    div.appendChild(check);
	    check.value = sub_sector_list[j];
	    check.name = 'job_spec';
	    check.className = 'togglee';
	    check.id = 'ck'+j;
	    
	    
	    if ( selected && selected[sub_sector_list[j]] ) check.checked = true;
	    
	    var wrap = document.createElement( 'LABEL' );
	    cell2.appendChild( wrap );
	    wrap.setAttribute('for','ck'+j);
	    
	    if (! wrap.addEventListener) addEvent( wrap, 'click', toggle );

	    wrap.appendChild( document.createTextNode( ' '+sub_sector_list[j] ) );
	}
    show_multi();
}

function toggle( e)
{
    get_input_child(this).click();
}

function get_input_child(t)
{

    if ( t.nodeName == 'INPUT' ) { return t }

    if ( t.getAttribute('for') ) { return document.getElementById( t.getAttribute('for') ) }

    for ( var j= 0; j< t.childNodes.length; j++)
	{
	    if ( t.childNodes[j].nodeName == 'INPUT' ) { return t.childNodes[j] }
	}


    return null;
}


function hide_multi()
{
    document.getElementById('jobTypeMulti').style.display = 'none';
    document.getElementById('jobTypeHead').style.display = 'none';
    document.getElementById('jobTypeMulti').innerHTML = '';
}

function show_multi()
{
    document.getElementById('jobTypeMulti').style.display = 'block';
    document.getElementById('jobTypeHead').style.display = 'block';
}

function popper( e )
{
    var targ = get_event_target( e );

    if ( targ.nodeName == 'IMG' ) { targ = targ.parentNode }

    if ( ! targ.href ) { return true }

    var width=300;
    var height=300;
    var scroll='yes';
    if ( targ.rel )
        {
            var size = targ.rel.match(/^(pop.+) (.+$)/);
            if ( size )
                {
                    var tmp = size[2].split(/x/);
                    width = tmp[0];
                    height = tmp[1];
                    if ( size[1] == 'popad' ) { scroll = 'no' }
                }

        }

    var left = 800 - width;
    var top = 600 - height;

    window.open(targ.href,'help','width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top+',scrollbars='+scroll);

    return false;
}


// Give a function and a tag ( with an optional class) 
// applys the function to all tags of this type (with class matching)
function apply_function_to_tag_class( func, tag, class_name )
{
    var list = document.getElementsByTagName( tag );
    if ( class_name == null ) { class_name = '' }

    for ( var i = 0; i < list.length; i++ )
	{
	    if ( list[i].className.indexOf( class_name ) != -1 )
		{
		    func( list[i] );
		}
	}
}


addLoader( setup_subsectors );
addLoader( function() { apply_function_to_tag_class( function(t) { addEvent( t, 'click', popper ) }, 'A', 'popper' ) } );

