$(document).ready(function(){
  var sub_num;
  //$('.collapse_container').hide(0);
     
  jQuery('#slideshow').fadeSlideShow({
        width: 670,
        height: 300,
        speed: 3000, 
        interval: 4000,
        PlayPauseElement: false,
        NextElement: false,
        PrevElement: false,
        ListElement: false
    });


$('.top_nav_item').hoverIntent(
    function(){
      sub_num = $(this).attr('id').replace(/.+_.+_/,'');
      submenuUp('#sub_' + sub_num);
      $('#sub_' + sub_num).animate({"top": "165px"}, "fast");
    },
    function(){}
  );
  
  $('#nav_container').hoverIntent(
    function(){},
    function(){
      submenuUp('#sub_last');
    }
  );
  
  $('.submenu a').hoverIntent(
    function(){
      $('#submenu_hover_' + sub_num).animate({"top": $(this).position().top + "px"}, "fast");
      $(this).animate({"margin-left": "-=6px"}, "fast");
    },
    function(){
      $(this).animate({"margin-left": "+=6px"}, "fast");
    }
  );
  
  $('.arrow_button').click(function() {
    $(this).next('div').toggle('fast');
    if ($(this).hasClass('closed')) {
      $(this).removeClass('closed').addClass('open');
    } else {
      $(this).removeClass('open').addClass('closed');
    }
  });
  
  
  
  function submenuUp(currentItem) {
    $('.submenu').not(currentItem).animate({"top": "-100px"}, "fast");
    $('.submenu_hover').not(currentItem).css('top', '-60px');
    $('.submenu a').css('margin-left', '10px');
  }
  
  $('#right_search h3 ul.closed').hide('slow');
  
  $('#right_search h3.#exp_menu').bind().click(function(){
    if ($(this).next('ul').is(':visible')) {
      //$(this).next('ul').hide('fast');
      //$(this).removeClass('open');
    window.location = 'find-a-proi-partner?expertise_id=0';
    } else {
      $(this).next('ul').show('fast');
      $(this).addClass('open');
      
    }
  });
  $('#right_search h3.#region_menu').bind().click(function(){
    if ($(this).next('ul').is(':visible')) {
      //$(this).next('ul').hide('fast');
      //$(this).removeClass('open');
    window.location = 'find-a-proi-partner?region_id=0';
    } else {
      $(this).next('ul').show('fast');
      $(this).addClass('open');
      
    }
  });
  


  //Modal Map Window
  $('a#map_link').fancybox({
    margin: 0,
    padding: 0
  });
  
  var image;
  $('.map_hover').hover(function() {
    var newbg = $(this).attr('id') + ".jpg";
	$("#modal_map").css("background", "url(images/" + newbg + ")");
  }, function() {
	$("#modal_map").css("background", "url(images/map.jpg)");
  });


$(".map_hover").click(function() {
		var bigimgclass = $(this).attr("id");		
		var lastunderscore = bigimgclass.lastIndexOf("_");
		var region = bigimgclass.substring(0, lastunderscore);
		var bigimg = region + "_large.jpg";
		var aj_reg = region.substring(4);
		
		$("#large_map").css("background", "url(images/" + bigimg + ")");
		$("#large_map").fadeIn(500, function() {
				$(".map_hover").each(function() {
					$(this).removeClass("map_hover png_bg");
					$(this).addClass("idle");			
			 	});
				
				//displays text from map_load.php
			
			$.ajax({
			type: "POST",
            url: "map_load.php",
            data: "reg=" + aj_reg,
            datatype: "html",
            success: function(html) {
				$("#large_map #partners").append(html);
            }
    			});

				///
			});
});
  
function map_close() {
		$("#large_map").fadeOut(500, function() {
				$(".idle").each(function() {
						$(this).removeClass("idle");
						$(this).addClass("map_hover png_bg");	
						$("#large_map #partners").empty();
					});
		});
  }
  
		$("a#back").click(map_close);
		$("a#fancybox-close").click(map_close);
		$("div#fancybox-overlay").live("click", map_close);


function preloadImages(imgarray) {
	for(var i = 0; i<imgarray.length; i++)   
    {
		$("#preloadimages").append("<img src=\"" + imgarray[i] + "\" />");
    }
//$.preloadImages("image.jpg", "anotherimage.jpg", [array]); 
}

  	var preloadimgs = new Array("images/map_AME_hover.jpg", "images/map_AP_hover.jpg", "images/map_EU_hover.jpg", "images/map_NA_hover.jpg", "images/map_SA_hover.jpg");
	preloadImages(preloadimgs);
	
	});
