$(document).ready(function(){
	
		// set the position of the overlay
		$("#pickBtnEducation").click(function(e){
				
			$(this).hide(); //.removeClass("pickBtn").addClass("pickBtnDiss");	
			
			var position = $(this).parent().position();

			//var chooseFlagHeight = $("#chooseFlag").height();			
			
			$("#chooseFlag").css({"left" : position.left + 230,"top" : position.top - 30}).show();
			$("#chooseFlagInner").fadeIn();
		});

		// set the position of the overlay on the homepage
		$("#pickBtnHomepage").click(function(e){
				
			$(this).hide(); //.removeClass("pickBtn").addClass("pickBtnDiss");	
			
			var position = $(this).parent().position();

			//var chooseFlagHeight = $("#chooseFlag").height();			
			
			$("#chooseFlag").css({"left" : position.left + 230,"top" : position.top + 110}).show();
			$("#chooseFlagInner").fadeIn();
		});
		
		
		//on mousever set the country name and votes from the title and rel attributes
		$("#cficFlags li a").mouseover(function(e){

			var name = $(this).attr("title");
			//var votes =$(this).attr("rel");

			//ajax call
	                  $.ajax({
           	         		type: 'POST',
	                  		url: 'http://www.worldskillslondon2011.com/umbraco/webservices/wslservices.asmx/GetCountrySupport',
		                  data: '{username:"wsUser", password: "Zonepa55", country: "' + name + '"}',
         			         contentType: "application/json; charset=utf-8",
                  			dataType: "json",
		                  success: function(msg) {
         			               $(".cficCountryVotes").html(msg);

                  			},
	                  		error: AJAXError
         			});


			$(".cficCountryName").html(name);
			//$(".cficCountryVotes").html(votes);

			});
				
		// when we click on a flag
		$("#cficFlags li a").click(function(e){
			var countryUrl = $(this).attr("href");
			
			$("a#ftLink").attr({href: countryUrl});

			var name = $(this).attr("title");

			//ajax call
	                  $.ajax({
           	         		type: 'POST',
	                  		url: 'http://www.worldskillslondon2011.com/umbraco/webservices/wslservices.asmx/AddCountrySupport',
		                  data: '{username:"wsUser", password: "Zonepa55", country: "' + name + '"}',
         			         contentType: "application/json; charset=utf-8",
                  			dataType: "json",
		                  success: function(msg) {
				      $(".cficCountryVotes").html(msg);
                  			},
	                  		error: AJAXError
         			});

			var cssclass = $(this).attr("class");
			$(".cficCountryName").addClass(cssclass);

			$("#chooseFlagInner").hide();
			$("#supportThanks").show();

			});
			
		// close the overlay
		$(".cfClose a").click(function(e){
			$("a.pickBtn").show(); //.removeClass("pickBtnDiss").addClass("pickBtn");	
			$("#chooseFlag").fadeOut("fast");
			$("#supportThanks").hide();
			$(".cficCountryName").removeAttr("class").addClass("cficCountryName");
		});
	
	
});
        function AJAXError(XMLHttpRequest, textStatus, errorThrown){
            //alert(textStatus + ": " + errorThrown);
        }
