var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", 'http://nationalaglawcenter.org/usamap/usamap.css');
document.getElementsByTagName("head")[0].appendChild(fileref);



// Add a stylesheet to the document and return a reference to it
function thisMovie(movieName){
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  }
  else {
    return document[movieName];
  }
}

if (swfobject) {
  /*
   * Script that works back and forth witht the flash movie
   */
  var states = new Array();
  states.clickstates = new Array();
  states.openmenu = null;
  
  states.pingClickStates = function(){
    thisMovie('usamap_swf').pongClickStates(states.clickstates);
  }

  states.timer = null;
  states.timeout = function (whichone) {
    clearTimeout(states.timer);
    states.close('all');
  }

  states.over = function (whichone) {
    if (whichone == states.openmenu) {
      clearTimeout(states.timer);
    }
    return false;
  }
  states.out = function (whichone) {
    if (whichone == states.openmenu) {
      clearTimeout(states.timer);
      states.timer = setTimeout("states.timeout();", 2000);
    }
    return false;
  }
  states.click = function (whichone, posx, posy) {
    clearTimeout(states.timer);
    states.close('all');
    if (whichone) {
      states.openmenu = whichone;
      states.open(whichone, Math.ceil(posx-3), Math.ceil(posy-3));
    }
    return false;
  }
  
  states.close = function (whichone) {
    if (whichone == 'all') {
      $('#states_menus .state_menu').css('display', 'none');
      return false;
    } else {
      $('#'+whichone).css('display', 'none');
    }
  }
  states.open = function (whichone, posx, posy) {
	  var element_name = "#" + whichone + " a";
		var location = window.location.toString();
	  state_link = location.replace(/[a-z0-9]*\.html/g, "") + $(element_name).attr("href");
		window.location.assign(state_link);
    //$('#states_menus #'+whichone).css('display', 'block').css('margin-left', posx+'px').css('margin-top', posy+'px');
  }
  states.init = function () {
    states.close('all');
    $('#states_menus .state_menu').each(function(i){
      states.clickstates.push(this.id);
    });
    //alert(states.clickstates[0]);
    
    $('#mapitems').css('display', 'none');
    
    $('#mapitems a').bind('click',function(e){
      return states.click( $(this).attr("href").slice(1) );
    }).bind('mouseenter',function(e){
      return states.over( $(this).attr("href").slice(1) );
    }).bind('mouseleave',function(e){
      return states.out( $(this).attr("href").slice(1) );
    });
    
    $('.state_menu').bind('mouseenter',function(e){
      return states.over( $(this).attr("id") );
    }).bind('mouseleave',function(e){
      return states.out( $(this).attr("id") );
    });
    //document.getElementById('splash_layout').style.display = 'block';
    swfobject.embedSWF('/usamap/usamap.swf', 'usamap_map', '714', '380', '8.5', 'http://nationalaglawcenter.org/usamap/expressinstall.swf', false, {
      // flash player parameters
      wmode: 'transparent'
    }, {
      // html attributes
      id: 'usamap_swf',
      name: 'usamap_swf'
    });
  }
  $(document).ready(function(){
    states.init();
  });
}
