Cufon.replace('.sears,h1,.section-right h3,.recent-activity h2,.footer li, .footer p,.share-details a,.emailbox strong', {fontFamily: 'Sears Tower',hover:true});
$(document).ready(function(){
	 if($('.detailsbox').length)
	 	$('.detailsbox').hide()
	//top menu drop-down
	
	//calculating top dorp down idv width
	$('li.has').each(function(){
		var dropdown_w=0;
		$(this).find('li').each(function(){
			dropdown_w=20+dropdown_w+$(this).find('a').width();
		})
		$(this).find('.subnav-in').css({'width':dropdown_w+'px'});
		dropdown_w=dropdown_w+20;
		$(this).find('.subnav').css({'width':dropdown_w+'px','display':'none'});
	 })
	
	
	//top hover drop down menu
	var hovered=false;
	$('li.has').hover(
		function(){
			if($(this).hasClass('hovered')){
				$(this).find('.subnav').stop(true,true).fadeIn('fast');
				hovered=true;
			}
			else{
				$(this).addClass('hovered').find('.subnav').stop(true,true).fadeIn('fast');
				hovered=false;
			}
		},
		function(){
			if (hovered===true){
				$(this).find('.subnav').fadeOut('fast');
			}
			else
			$(this).removeClass('hovered').find('.subnav').fadeOut('fast');
	})
	
	//share popup
	$('.share').parent('li').hover(
		function(){
			$(this).closest('.btns').find('.socials-buble').stop(true,true).fadeIn('fast');
		},
		function(){
			$(this).closest('.btns').find('.socials-buble').fadeOut('fast');
	})
	
	//details show hide
	 if($('.detailsbox').length){
		
		$('.details').click(function(){
			if ($(this).hasClass('details-open')){
				$(this).html('Details');
				Cufon.refresh();
				$(this).removeClass('details-open').closest('.item').find('.detailsbox').slideUp();
			}else{
				$(this).html('Close');
				Cufon.refresh();
				$(this).addClass('details-open').closest('.item').find('.detailsbox').slideDown();
			}
			return false;
		})
    }

    $('.login-btn, .signup-btn').live('click', function(){
        $(this).closest('form').ajaxSubmit({
            success: function(json, status, xhr) {
                if(json.hasOwnProperty('error')){
                    var frag = $(json.error.fragment);
                    $('#' + frag.attr('id')).replaceWith(frag);
                }
                else{
                    window.location.href = json.redirect_uri;
                }
            },
            iframe: true,
            dataType: 'json'
        });
        return false;
    });

	$('.frm-search input').focus(function() {
		$('.frm-search').addClass('frm-search-focused')	;
	});
	$('.frm-search input').blur(function(){
		$('.frm-search').removeClass('frm-search-focused')	;
	});

	
	/*- SET HEIGHT---------------------------------------------*/
	function set_height(){
		var item_h=0;
		var item_new_h=$('.item').find('.imgbox').height()-45;
		$('.item').find('.txtbox').each(function(){
			if ($(this).height()<item_new_h)
				$(this).css({'height':item_new_h+'px'});
		
		})	
	}
	//set height for activity
	if ($('.activityhome').length){
		 set_height();
	}
	
	/*--TWITTER and EVENT------------------------------------------*/

	
	
	function initalizing(init_url,targetbox,contentbox){
		showLoading(targetbox);
		set_content(init_url,contentbox);
	}
	
	function showLoading(targetbox){
		targetbox.html('<div class="loading"><img src="/static/img/ajax-loader-twitter.gif" alt="Loading..." /></div>');
	}
	
		
	function set_content(tweet_url,contentbox){
		showLoading(contentbox);
		contentbox.load(tweet_url, function(response, status, xhr) {
		  if (status == "error") {
				contentbox.html("Sorry but there was an error: " + xhr.status + " " + xhr.statusText);
			}
		  });
		}
	
  $('ul#month-list li a').live('click', function(event) {
      // Live handler called.
      event.preventDefault();
      var url_link = $(this).attr('href');
      set_content(url_link, $('div#recent-activity'));
  });

	//References
	var twitter_ico = $(".twitter-nav").find('a');
	var twitter_content = $(".twitterslider-in");
	
	twitter_ico.click(function(){
		var url_link=$(this).attr('href');
		if ($(this).attr('id') == 'next')
		{
		  TwitterFeed.next_tweet();
	  }else{
		  TwitterFeed.previous_tweet();
	  }

		return false;
		
	});

  $('div#recent-activity a.ajax').live('click',function(event){
    event.preventDefault();
    if (!$(this).hasClass('selected')){
      $('a.selected').removeClass('selected');
    }
    $(this).addClass('selected');

    var url_link = $(this).attr('href');
    var target_div = $("div#recent-activity");
    set_content(url_link,target_div);
    return false;
  });

	/*--END TWITTER------------------------------------------*/

  set_actions_to_recent_activity();
});

function set_actions_to_recent_activity() { 
    $('.days td').mouseenter(function(){
        if($(this).find('.tooltip').length){
            $(this).find('.tooltip').show();
        }
    });
    $('.days td').mouseleave(function(){
        if($(this).find('.tooltip').length){
            $(this).find('.tooltip').hide();
        }
    });
}

$('a.colorbox_media').colorbox({transition:'fade', speed:500, width:650, height:560});
$('a.colorbox_photo').each(function(){
    var href = $(this).attr('href');
    $(this).colorbox({transition:'fade', speed:500, width:650, height:560, photo:true, html:function() {
            var response = $.ajax(href, { async: false });
            return response.responseText;
        }
    });
});

$('form.comment_form').submit(function(event){
  event.preventDefault();
  event.stopPropagation();

  var form = $(this);
  var url = form.attr('action');
  
  var comment = $('form.comment_form #id_comment').val();
  $('form.comment_form #id_comment').val(comment);

  if( comment=='' || comment=='Write a comment ...' )
    return false;
  
  var submit_value = $('form.comment_form #id_comment_submit').val();
  
  $.ajax({ 
      type: 'post',
      url: url, 
      data: form.serialize(),
      beforeSend: function() {
          $('form.comment_form #id_comment_submit').val('loading...');
      },
      success: function(data, textStatus, XMLHttpRequest){
          var li = $(data);
          li.css('display', 'list-item');

          var comment_list = $('.comment_list');
          var visible_lis = null;

          if(comment_list.find('li:first.filler').length == 1){
              comment_list.html(li);
              li.fadeIn();
          }
          else{
              visible_lis = comment_list.find('li[style*="display: list-item"]');
              if(visible_lis.length >= 10){
                  $(visible_lis[9]).fadeOut();
              }
              li.prependTo('.comment_list').fadeIn();
          }

          $('.comment_form #id_comment').val('Write a comment ...');
          $('.comment_form #id_comment').addClass('comment_holding');

          var submit = $('.comment_form #id_comment_submit');
          submit.attr('disabled', 'disabled');
          submit.removeClass('enabled');
          submit.val(submit_value);
      }
  });
});

$('form.comment_form textarea').live('keyup mouseout', function(){
    var has_data = $(this).val().length > 0;
    var submit = $(this).closest('form').find('input[type=submit]');
    if(!has_data){
        submit.attr('disabled', 'disabled');
    }
    else{
        submit.removeAttr('disabled');
    }
    submit.toggleClass('enabled', has_data);
});

function set_event_attendance(event_id) {
    $.ajax({
        url: '/events/'+event_id+'/set-attending/',
        data: {},
        success: function(data) {
            if(eval(data.success)) {
                alert("Thank you, your attendance has been noted.");
                $("#are_you_attending_q").hide();
                $("#submit_attendance_a").text("I am attending this event");
            }
        },
        dataType: 'json'
    });
}		

$(function(){
	if ($('html').css('direction') == 'rtl'){
		$('h1').wrapInner('<bdo dir=ltr></bdo>');
		if ($('.recent-activity h2').length)
			$('.recent-activity h2').wrapInner('<bdo dir=ltr></bdo>');
		if ($('.section-right h3').length)
			$('.section-right h3').wrapInner('<bdo dir=ltr></bdo>');		   
		if ($('.share-details a').length)
			$('.share-details a').wrapInner('<bdo dir=ltr></bdo>');		   
		if ($('.emailbox strong').length)
			$('.emailbox strong').wrapInner('<bdo dir=ltr></bdo>');		   
		
		
		$('a.sears').wrapInner('<bdo dir=ltr></bdo>');
		Cufon.replace('bdo');
		$('bdo').each(function()    {
		var word = $(this).text();
		var splittext = word.split("");
		var reversedtext = splittext.reverse();
		var newtext = reversedtext.join("");
		$(this).text(newtext);
		});
	}

});				   
	

