Event.observe(window, 'load', function() {
  try{
    var t;
    Event.observe($('share_button'),'mouseover',function(e){showBlock(e,t,'share_button');});
    Event.observe($('share_button'),'mouseout',function(){t = setTimeout(hideBlock,1500);});
    $('share_button').descendants().each(function (nested) { 
      nested.observe('mouseover', function (e) {
        showBlock(e,t,'share_button');
      });
    });
  }
  catch(e){}
	var onVoteButtonClick = function(e) {
		var options = {challenge: Recaptcha.get_challenge(), response: Recaptcha.get_response()};
		
		var id = $("profile_id").value;
		var url = 'ajax/vote/' + id;
    var el = $('vote_button');
		
	  new Ajax.Request(url, {
	    method: 'post',
	    parameters: options,
	    onSuccess: function(response){
	      var json = response.headerJSON;
				
				if (json.success == true) {
					$('msg_error').hide();
					el.hide();
					
					if ($('profile_vote_for_me_text')) { $('profile_vote_for_me_text').show(); }
          if ($('profile_fan_text')) { $('profile_fan_text').show(); }
          if ($('windows7_wall_button')) { $('windows7_wall_button').show(); }
					Recaptcha.destroy();
				}
				else {
					Recaptcha.reload();
					$('msg_error').show();
				}
	    }
	  });
		
		e.stop();
	}
	
	var onCaptchaeButtonClick = function(e) {
		Recaptcha.create("6LdpJQgAAAAAAHEN1O5EGP4egiHrGorS0xiW8wX5", $('captcha'), {
        theme: 'clean',
        tabindex: 0,
				lang: 'nl',
        callback: Recaptcha.focus_response_field
    });
    
		$('captcha_button').hide();
		$('vote_button').show();
		if ($('profile_vote_for_me_text')) { $('profile_vote_for_me_text').hide(); }
    if ($('profile_fan_text')) { $('profile_fan_text').hide(); }
		
    e.stop();
	}
	
	var onJoinButtonClick = function(e) {
		window.location = 'join';
		
		e.stop();
	}
	
	var facebookShareCallback = function(id, ex) {
    if (id !== 'null') {
      $('share_button').hide()
      $('msg_share_success').show()
    }
  }
	
	var onShareButtonClick = function(e) {
		var type = $("profile_type").value;
		
		if (type == 'TWITTER') {
			var url = 'ajax/share/';
			var el = $('share_button');
			
			new Ajax.Request(url, {
				method: 'get',
				onSuccess: function(response){
					var json = response.headerJSON;
					
					if (json.success == true) {
						el.hide();
						$('msg_share_success').show()
					}
				}
			});
	   }
		 else {
		 	var href = location.href;
		 	var attachment = {'name':'Windows7','href':href,'description':'Vote for me'};
      FB.Connect.streamPublish('Please vote for me!', attachment, null, null, 'Convince your friends to vote for you', facebookShareCallback);
		 }
    e.stop();
	}
	
	if ($("vote_button")) {
		Event.observe('vote_button', 'click', onVoteButtonClick);
	}
	
	if ($("captcha_button")) {
		Event.observe('captcha_button', 'click', onCaptchaeButtonClick);
	}
	
	if ($("share_button")) {
		Event.observe('share_button', 'click', onShareButtonClick);
	}
});

function showBlock(e,t,id){
  if(e.element()==$(id)){
     clearTimeout(t);
  }
  
  $(id).descendants().each(function(el){
    if(el==e.element()){
      clearTimeout(t);
    }
  });
  
  var popHeight = $('popup').getHeight();
  $('popup').setStyle({
      marginTop: '-'+popHeight+'px'
  });
  $('popup').show();
}

function hideBlock(){
  $('popup').hide();
}

