//High Impact Ad
var highAdLayout = function(){
$('body').addClass('highAdLayout'); 
};

//Select a section


$(function(){
	$("#sectionSel").change(function(){
		var url = $(this).val();
		if (url !== '') {
			location.href = url;
		}
	});


//Search Lists

	var defTxt = "SEARCH LISTS";
	var srchStrg = $("#query2").val();
	$("#query2").bind("click", function(){
		if (srchStrg === defTxt) {$("#query2").val("");}
	});
	$("#query2").bind("mouseleave", function(){
		if ($("#query2").val() === "") {$("#query2").val(defTxt);}
	});


//Advertiser Tab 
$('#sponserTab').live('click',function(){  
	 var $this = $(this);  
	 $this.parent().find('.sel').removeClass('sel');  
	 $('#storyContent').hide().after('<div id="sponserAdOverlayContent"><iframe src="/time/specials/sponsors/timeAd.html" height="100%" width="100%" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" /></div>');  
	 $this.click(function(){  
	 return false;  
	 });  
	return false;  
});

/* Category selection */

var flatUrl = $("meta[name='flatUrl']").attr('content'),
		$catWrap = $('#catWrap'),
		$specialsAllTopTen = $('#specialsAllTopTen'),
		$topTenCat = $('#topTenCat');

	$topTenCat.find('li').bind('click', function(){
			$topTenCat.find('li.select').removeClass('select');
	  	var $this = $(this).addClass('select'),
			pos = $topTenCat.find('li').index($this)+1;
			$catWrap.load(flatUrl+pos+'.txt');
			});

var trigger = (location.hash && $topTenCat.find('a[href="'+location.hash+'"]').parent().length) ? $topTenCat.find('a[href="'+location.hash+'"]').parent() : $topTenCat.find('li:eq(0)');

trigger.click();

if($('.twitter-connect').length > 0){

	  twttr.anywhere(function (T) {

  if($.cookie('twttr-sign-in') !== 'true'){
    twttr.anywhere.signOut();
  }

    var $twitterConnect = $('.twitter-connect'),
        $lastClicked,
        lastUser,
        lastAction;

    function renderFollow(u,$el){
      $el.addClass('notFollowing followAction').html('<span class="twitter-status-icon"></span>Follow @<a href="http://www.twitter.com/'+u+'" data-user="'+u+'">'+u+'</a> on Twitter');
    }        
    function renderUnFollow(u,$el){        
      $el.removeClass('notFollowing followAction').addClass('following unfollowAction').html('<span class="twitter-status-icon"></span>You\'re following @<a href="http://www.twitter.com/'+u+'" data-user="'+u+'">'+u+'</a>');
    }

    function followAction(u,$this){
      T.currentUser.isFollowing(u,function(res){
        if(res){
          renderUnFollow(u,$this);
          $this.removeClass('loading'); 
        }else{
          T.User.find(u).follow(function(r){
      	   renderUnFollow(u,$this);
           $this.removeClass('loading notFollowing followAction').addClass('following unfollowAction');
          }); 
        }
      });
    }
    
    function unFollowAction(u,$this){
      T.User.find(u).unfollow(function(r){
        renderFollow(u,$this);
        $this.removeClass('loading following unfollowAction').addClass('notFollowing followAction');	 	
      });   
    }            

    T.bind('authComplete', function (e, user) {
      $.cookie('twttr-sign-in','true');
            
      $twitterConnect.each(function(){
        var $this = $(this),
            u = $this.attr('data-user'),
            lastUser = $lastClicked.attr('data-user'),
            lastAction = $lastClicked.hasClass('followAction') ? 'follow' : 'unFollow';
    		user.isFollowing(u,function(r){
    		   
           if(u === lastUser){
              if(lastAction === 'follow'){
                followAction(u,$this);
              } else{
                unFollowAction(u,$this);
              }
                          
           } else{
            if(r){
             renderUnFollow(u,$this); 
            }else{
              renderFollow(u,$this);
            }
           } 
    		  });    
      });
    });

        
		$('.followAction').live('click',function(e){
			e.preventDefault();
      var $this = $(this),
          u = $(this).attr('data-user');          	
      $this.addClass('loading');          	
      if(!T.isConnected()){
        $lastClicked = $this;
        T.signIn();
        
      }else{
        followAction(u,$this);    
      }
    
		});
		
    $('.unfollowAction').live('click',function(e){		
    	e.preventDefault();
    	var $this = $(this),
    			u = $(this).attr('data-user');
    	$this.addClass('loading');
    	
    	if(!T.isConnected()){
    		$lastClicked = $this;
    		T.signIn();
    
    	}else{
        unFollowAction(u,$this);       
    	}
		
    });   	   
       
    if(T.isConnected()){
      
      var currentUser = T.currentUser;
      
      $twitterConnect.each(function(){
      var $this = $(this),
          u = $(this).attr('data-user');	
            currentUser.isFollowing(u,function(r){
          if(r){
            renderUnFollow(u,$this);
           }else{
            renderFollow(u,$this);
           }
        });    
      
      });
    }else{
      $twitterConnect.each(function(){
      var $this = $(this),
          u = $(this).attr('data-user');		    
        renderFollow(u,$this);
      });
    }
});
	
	
}


});	
