$(document).ready(function(){
	
	var nationalityid = getCookieValue ('nationality');
	var nationalitytext = $('#nationality :selected').text();
	var countryid = $('#countryid').text();
	var countryname = $('#countryname').text();
	var countrytitle = $('#countrytitle').text();
	var cityname = $('#cityname').text();
	var cityid = $('#cityid').text(); //alert('nationalityid: '+nationalityid + 'countryid: '+ countryid + 'cityid:'+ cityid);
	
	showEmbassies(countryid,cityid,nationalityid,nationalitytext);
	

	$('.briefingintro').click(function() {
        if ($(this).find("div.briefingintrobox").is(":hidden"))
        {
        	$(this).find("div.briefingintrobox").slideDown("slow");
        	$(this).find("a.readmore").text("Collapse");
        } else {
        	$(this).find("div.briefingintrobox").slideUp("slow");
        	$(this).find("a.readmore").text("Read more...");
        }
   });

/* Subject sliding */
   $('.subjectbar').click(function() {
	   var parent = $(this).parent();

	   if (parent.find("div.subjectbox").is(":hidden"))
       {
       	parent.find("div.subjectbox").slideDown("slow");
       	parent.find("a.statebutton").text("Close");
       } else {
       	parent.find("div.subjectbox").slideUp("slow");
       	parent.find("a.statebutton").text(" ");
       }
  });
   
   $('.subsubjectbar').click(function() {
	   var parent = $(this).parent();
	   if (parent.find("div.subsubjectbox").is(":hidden"))
       {
		   parent.find("div.subsubjectbox").slideDown("slow");
		   parent.find("a.substatebutton").text("-");
       } else {
    	   parent.find("div.subsubjectbox").slideUp("slow");
    	   parent.find("a.substatebutton").text("+");
       
       }
  });

   
   // When the nationality selection list is clicked:
  $('#nationality').change(function() {
	  var nationalityid= $('#nationality :selected').val(); //alert(nationalityid);
	  var nationalitytext = $('#nationality :selected').text();
	  deleteCookie ('nationality');
	  writePersistentCookie('nationality', nationalityid, 'years', '10');
	  showEmbassies(countryid,cityid,nationalityid,nationalitytext);
   });
 
   $("input[name='commentsAgree']").click(
   		function()
   		{
   		if ($("input[name='commentsAgree']").is(":checked"))
   		$("div#commentSubmitButton").slideDown("slow");
   		else
   			$("div#commentSubmitButton").slideUp("slow");
   		}
   		);

   function tooltip(){
	    xOffset = 10;
	    yOffset = 20;
	    $("a.tooltip").hover(function(e){
	        this.t = this.title;
	        this.title = "";
	        $("body").append("<p id='tooltip'>"+ this.t +"</p>");
	        $("#tooltip")
	            .css("top",(e.pageY - xOffset) + "px")
	            .css("left",(e.pageX + yOffset) + "px")
	            .fadeIn("fast");
	    },
        function(){
            this.title = this.t;
            $("#tooltip").remove();
        });
	    
	    $("a.tooltip").mousemove(function(e){
            $("#tooltip")
                .css("top",(e.pageY - xOffset) + "px")
                .css("left",(e.pageX + yOffset) + "px");
        });
  }  // end function
 
   function showEmbassies(countryid,cityid,nationalityid,nationalitytext) {
	   
	   if (nationalityid == ""){
			embassyText = "If you are going on a business trip to " + cityname +", or if you are working in " + countrytitle +" it is a good idea to have the address of your embassy or consulate in " + countrytitle +" ready.<br /><br />" +
				          "<strong>We need to know your nationality first!</strong><br /><br />" +
						  "To show the address and phonenumber of your embassy and/or consulates in " + countrytitle +", we need to know your nationality.<br /><br />" +
					      "Please select your nationality in the list on the right.<br/ >";
			$('#embassiesText').html(embassyText);
		} else {
		     $.ajax({
		          type: "POST",
		          url: "/application/40_ajax_php/ajax_get_embassies.php",
		          dataType: "html",
		          cache: false,
		          data: "countryid=" + countryid + "&cityid=" + cityid + "&nationalityid=" + nationalityid + "&nationalitytext=" + nationalitytext,
		          success: function(result) {$('#embassiesText').html("If you are going on a business trip to " + cityname +", or if you are working in " + countrytitle +" it is a good idea to have the address of your embassy or consulate in " + countrytitle +" ready.<br /><br />" +result); }
		      });
		} 
	//end if-else
	 } 
    // end function
    
 
    
    
    
});



	



