// Meglio usare SlideUp/SlideDown di jQuery piuttosto che il Dhtml.
// Ho dovuto specificare tutti gli eventi CLICK

function animatedcollapse(divId, animatetime, persistexpand, initstate){
    if(initstate=="hidden"){$("#"+divId).hide();}
    else if(initstate=="contract"){$("#"+divId).css("visibility", "hidden");}
    else if(initstate=="block") {$("#"+divId).show();}
}

// var arrayDiv = new Array("#catVideo", "#catGallery", "#catContenuto", "#catWriting", "#catBiography", "#catDesc", "#catTeam")
var arrayDiv = new Array("#catContenuto", "#catWriting", "#catBiography", "#catDesc", "#catTeam")
var tutteChiuse = true;
function closeAll(div){
	if($(div).css("display")=="block"){
		$(div).slideUp("slow");
		tutteChiuse = true;
	}
	else if($(div).css("display")=="none"){
		if(tutteChiuse){$(div).slideDown("slow"); tutteChiuse = false;}
		else{
		        for(i=0; i<arrayDiv.length; i++){
		     		if($(arrayDiv[i]).css("display")=="block"){
					$($(arrayDiv[i])).slideUp("slow", function(){
						$(div).slideDown("slow");
						tutteChiuse = false;
					})
				}
			}

		}
	}
	return false;
}

$(document).ready(function(){
   if($("#Gmap").length){var t=setTimeout("initializeMap()",2000);};
   $("#catContenuto, #catWriting, #catBiography, #catDesc, #catTeam, #dida").css("display", "none").css("visibility", "visible");
   $("#catBiography").slideDown("slow")
   $("#catContenuto").slideDown("slow")
   if($("#catContenuto").length || $("#catBiography").length){tutteChiuse = false;}
   
   $("#elencovideo li").click(function(){
        videoLink = $(this).attr("rel");
	if(videoLink.indexOf("?v=")>0){
		var splitted = videoLink.split("?v=");
		videoLink = " http://www.youtube.com/v/"+splitted[1]+"?fs=1&hl=it_IT"
	}
	if(isiPhone()){
		$('#videospace').html("<a href=\""+$(this).attr("rel")+"\"><img width=\"415\" height=\"238\" src=\""+$(this).find("img").attr("src")+"\" /></a>");
	}
	else{
		$('#videospace').flash({
			swf: videoLink,
			height: 480,
			width:640,
			wmode: "transparent",
			allowFullScreen: true,
			allowscriptaccess: "always"
		});
	}
    })
    $("#video").click(function(){
        $('#elencovideo li:eq(0)').trigger('click');
    	$("#catVideo").css("visibility", "visible").hide();
    	$("#catVideo").slideDown();
    	return false;
    })
    $("#catVideo #chiudi").click(function(){
        $("#videospace").html("");
        $("#catVideo").slideUp();
        return false;
    })

    $("#gallery, #suggestion > a").click(function(){
    	$("#catGallery").css("visibility", "visible").hide();
    	$("#catGallery").slideDown();
    	return false;
    })
    $("#writing a:first").click(function(){
    	closeAll("#catWriting");
    })
    $("#biography a:first").click(function(){
    	closeAll("#catBiography");
    })
    $("#contenuto a:first").click(function(){
    	closeAll("#catContenuto");
    })
    $("#description a:first").click(function(){
    	closeAll("#catDesc");
    })
    $("#team a:first").click(function(){
    	closeAll("#catTeam");
    })
    $("#projectData a:first").click(function(){
      $("#cat").slideToggle(); 
      return false;
    })
    $("#didaPulsante a:first").click(function(){
      $("#dida").slideToggle(); 
      return false;
    })
})



function initializeMap(){
var marker
  var myLatlng = new google.maps.LatLng(45.450092,9.172522);
  var myOptions = {
    zoom: 17,
    center: new google.maps.LatLng(45.450994,9.172522),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("Gmap"), myOptions);
        marker = new google.maps.Marker({
        position: myLatlng, 
        map: map, 
        icon: "http://google-maps-icons.googlecode.com/files/embassy.png",
        title:"MARIO BELLINI ARCHITECTS S.r.l."
      });
      attachBallon(marker)
  function attachBallon(marker){
      var infowindow = new google.maps.InfoWindow({
        content:"<div style=\"font-size:11px; color:#000;\"><strong>MARIO BELLINI ARCHITECTS S.r.l.</strong><br />Piazza Arcole, 4 20143 MILANO <br /> ITALY<br /><br />Tel +39 02 5815 191<br />Fax +39 02 5811 3466 <br /><a style=\"color:#000; font-size:11px; text-decoration:underline;\" href=\"mailto:info@mariobellini.com\">info@mariobellini.com</a><br /><br /><a style=\"color:#000;font-size:11px;\" href=\"http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=Piazza+Arcole,+4+20143+MILANO+%E2%80%93+ITALY&sll=41.442726,12.392578&sspn=24.687472,67.631836&ie=UTF8&hq=&hnear=Piazza+Arcole,+4,+20143+Milano,+Lombardia&z=16\" target=\"_blank\" >&raquo; Get Directions</a></div>"
      });
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
      });
      infowindow.open(map,marker);
  }
}

function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
        (navigator.platform.indexOf("iPad") != -1)
        || (navigator.userAgent.toLowerCase().indexOf("android") != -1)
    );
}
