function getfeatured() {
  jQuery.ajax({
    url: document.base_url+'ajax/getfeatured',
    type: "POST",
    data: {exclude:document.exclude},
    dataType:"json",
    success: function(data) {
      document.featured = data;
      document.fi = 0;
      document.fl = data.length;
      document.scrollertimer = setTimeout("scrollfeatured()", 7000);
    }
  });
}
function scrollfeatured() {
  var g = document.featured[document.fi];
  var h = '<div class="g">';
  for (var i=0; i < g.length; i++) {
    h+= '<a id="f-'+g[i].u+'" href="'+document.base_url+g[i].n+'/_" class="person">';
    h+= '<img src="';
    if (2 == g[i].c) {
      h+= document.base_url + 'photos/users/'+g[i].n.toLowerCase()+'/'+g[i].p+'_75x75.jpg';
    } else {
      h+= document.base_url + 'images/profile_default_75x75_'+(0 == g[i].c ? 'female' : 'male')+'.jpg';
    }
    h+= '" alt="'+g[i].n+'" width="75" height="75" />'
    h+= '<div class="desc"><strong>'+g[i].n+'</strong><br />'+g[i].j+' from '+g[i].l+'</div></a>';
  }
  h += '</div>';
  jQuery(h).css('top', '444px').appendTo("#featured");
  jQuery(".g:first").animate({top: '-=111px'}, 500, function(){ jQuery(this).remove(); });
  jQuery(".g:gt(0)").animate({top: '-=111px'}, 500);
  document.scrollertimer = setTimeout("scrollfeatured()", 7000);
  document.fi++;
  if(document.fi >= document.fl) document.fi = 0;
}
 
(function ($) {
	$.event.special.cacheload = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}
				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}
				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));
jQuery(document).ready(function($) {
  document.exclude = [];
  jQuery(".person").each(function(){
    document.exclude.push(this.id.substr(2));
  });
  getfeatured();
  $("#featured").hover(
    function(){
      clearTimeout(document.scrollertimer);
      document.scrollertimer = null;
    },
    function(){
      if(!document.scrollertimer) document.scrollertimer = setTimeout("scrollfeatured()", 7000);
    }
  );
  // declaring which elements belong to which menu
  $('#show-benefit-1').data('elements', '#benefit-1, #tooltip1, #now');
  $('#show-benefit-2').data('elements', '#benefit-2');
  $('#show-benefit-3').data('elements', '#benefit-3, #benefit-3-mascot');
  $('#show-benefit-4').data('elements', '#benefit-4');
  $('#show-benefit-5').data('elements', '#benefit-5');

  $("#benefit-2").append('<img class="poster" alt="" />');
  $("#benefit-3").append('<img class="poster" alt="" />');
  $("#benefit-4").append('<img class="poster" alt="" />');
  $("#benefit-5").append('<img class="poster" alt="" />');
  $("#benefit-2 img.poster").attr('src', document.base_url+'images/benefit-2.jpg').bind('cacheload', function(ev) { 
    $(this).parent().find(".loading").remove();
    $('#show-benefit-2').data('elements', '#benefit-2, #tooltip2');
  });
  $("#benefit-3 img.poster").attr('src', document.base_url+'images/benefit-3.jpg').bind('cacheload', function(ev) {
    $(this).parent().find(".loading").remove(); 
    $('#show-benefit-3').data('elements', '#benefit-3, #tooltip3, #benefit-3-mascot');
  });
  $("#benefit-4 img.poster").attr('src', document.base_url+'images/benefit-4.jpg').bind('cacheload', function(ev) {
    $(this).parent().find(".loading").remove(); 
    $('#show-benefit-4').data('elements', '#benefit-4, #tooltip4');
  });
  $("#benefit-5 img.poster").attr('src', document.base_url+'images/benefit-5.jpg').bind('cacheload', function(ev) {
    $(this).parent().find(".loading").remove() 
    $('#show-benefit-5').data('elements', '#benefit-5, #tooltip5');
  });
  $('#benefit-1 a').bind({
    mouseenter:function(){
      $(this).find('.overlay').show();
    },
    mouseleave:function(){
      $(this).find('.overlay').hide();
    }
  });
  $("#btn_connect_browse").live('click', function() {
	  $('#tooltip6').hide();
  });
  if (jQuery.support.opacity) {
    $('#why').bind({
      mouseleave:function(){ // moving outside from a menu
        if ($("#why li.active").removeClass('active').data('elements') != '#benefit-1, #tooltip1, #now') { // moving outside, not from menu 1
    	    $('#now').fadeIn('fast');
          $('#benefit-1').fadeIn("fast");
        } else { // moving from menu 1 to outside
          $('#now').stop(true).css({opacity:''});
          $('#benefit-1').stop(true).css({opacity:''});
    	    $('#tooltip1').fadeOut('fast');        
        }
      }
    });
    $("#why li").bind({
      mouseenter:function() {
        if ($('#now, #benefit-1').is(":visible")) { // from outside
          if($(this).data('elements') == '#benefit-1, #tooltip1, #now') { // to the first menu
            $('#now').stop(true).css({opacity:''});
            $('#benefit-1').stop(true).css({opacity:''});
      	    $('#tooltip1').fadeIn('fast');        
          } else {
            $('#now').fadeOut('fast');        
            $('#benefit-1').fadeOut('fast');        
          } 
        };
        if ($("#why li.active")) { 
          $($("#why li.active").removeClass('active').data('elements')).fadeOut("fast"); // remove the active class
        }
        $($(this).addClass('active').data('elements')).fadeIn("fast"); // activate the current menu
      },
      mouseleave:function() {
        $($(this).data('elements')).fadeOut("fast"); // deactivate the current menu, but DON'T remove the class here :)
      }
    });
    $('#btn_connect_browse').bind({
      mouseenter:function(){
  	 $('#tooltip6').fadeIn('fast');
      },
      mouseleave:function(){
  	 $('#tooltip6').fadeOut('fast');
      }
    });
    $('#btn_connect').bind({
      mouseenter:function(){
  	    $('#tooltip-signup').fadeIn('fast');
      },
      mouseleave:function(){
  	    $('#tooltip-signup').fadeOut('fast');
      }
    });
  } else {
    $('#why').bind({
      mouseleave:function(){ // moving outside from a menu
        if ($("#why li.active").removeClass('active').data('elements') != '#benefit-1, #tooltip1, #now') { // moving outside, not from menu 1
    	    $('#now').show();
          $('#benefit-1').show();
        } else { // moving from menu 1 to outside
    	    $('#tooltip1').hide();
    	    $('#now').show();
          $('#benefit-1').show();
        }
      }
    });
    $("#why li").bind({
      mouseenter:function() {
        if ($('#now, #benefit-1').is(":visible")) { // from outside
          if($(this).data('elements') == '#benefit-1, #tooltip1, #now') { // to the first menu
      	    $('#tooltip1').show();
          } else { // to other menu
            $('#now').hide();
            $('#benefit-1').hide();
          } 
        };
        if ($("#why li.active")) { 
          $($("#why li.active").removeClass('active').data('elements')).hide(); // remove the active class
        }
        $($(this).addClass('active').data('elements')).show(); // activate the current menu
      },
      mouseleave:function() {
        $($(this).data('elements')).hide(); // deactivate the current menu, but DON'T remove the class here :)
      }
    });
    $('#btn_connect_browse').bind({
      mouseenter:function(){
        $('#tooltip6').show();
      },
      mouseleave:function(){
        $('#tooltip6').hide();
      }
    });
    $('#btn_connect').bind({
      mouseenter:function(){
  	    $('#tooltip-signup').show();
      },
      mouseleave:function(){
  	    $('#tooltip-signup').hide();
      }
    });
  }
  $("#btn_connect").live('click', function() {
	$('#tooltip-signup').hide();
    FB.login(function(response){
      if("connected" == response.status) {
        $.ajax({
          url: document.base_url+'fb/check',
          type: "POST",
          dataType:"json",
          success: function(data) {
            if('login' == data.state) {
              if ('0' == data.user_type) {
                location.href = document.base_url + 'signup/free';
              } else {
                location.href = document.base_url + 'account';
              }
            } else if('exist' == data.state) {
              fb.loadAnchor(document.base_url+'fb/merge', 'theme:custom width:470 height:360 scrolling:no outerBorder=0 innerBorder=1 showClose:false modal:true padding:15 innerBorder:0 disableScroll:false shadowType=halo overlayOpacity:0.001');
            } else if('added' == data.state) {
              location.href = document.base_url + 'signup/free';
            }
          }
        });  
      }
    },
    {perms:'email'});
  });
  $("#btn_connect_browse").live('click', function() {
    FB.login(function(response){
      if("connected" == response.status) {
        $.ajax({
          url: document.base_url+'fb/check',
          type: "POST",
          dataType:"json",
          success: function(data) {
            if('login' == data.state) {
              location.href = document.base_url + 'find/?js=1#fb';
            } else if('exist' == data.state) {
              fb.loadAnchor(document.base_url+'fb/merge', 'theme:custom width:470 height:360 scrolling:no outerBorder=0 innerBorder=1 showClose:false modal:true padding:15 innerBorder:0 disableScroll:false shadowType=halo overlayOpacity:0.001');
            } else if('added' == data.state) {
              location.href = document.base_url + 'find/?js=1#fb';
            }
          }
        });  
      }
    },
    {perms:'email'});
  });
});


