// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var notice_timer;
var show_new_pickin_tips = false;

document.observe('dom:loaded', function() {

	
  $$(".hideonload").invoke("hide");
  $$(".showonload").invoke("show");


	if ($('flash') != null) {
  	flashNotice(5000)
  }
	

  //Setup observer for 
  if ($("tweet_body")) new Form.Element.Observer('tweet_body', 0.2, tweetCharCounter) 

	if ($$("#suggested_tweets span").size() > 0) {
		flashTweet();		
  }

  if ($$('a.scroll_me')) {
    $$('a.scroll_me').invoke('observe','click', function(event) {
	    Event.stop(event);
	    var elementLink = Event.element(event).readAttribute('href');
	    var scrollHere = elementLink.substr(1, elementLink.length);
	    Effect.ScrollTo(scrollHere);
	    return false;
    });
  }
    var hint = new HintText();

  if (show_new_pickin_tips) {
       $('options_footer').addTip('An empty list is a boring list! Add an item or two to get things going.  It will encourage others to contribute.<br/> <a href="javascript:undefined;" class="close">Got it!</a>', 'Add an Item to get started', { style: 'additem' });
       $('twidget').addTip('Don&#146;t keep this a secret! Tweet this message to invite your followers to contribute. <br/> <a href="javascript:undefined;" class="close">Got it!</a>', 'Invite your Friends', { style: 'tweet' });
  }
		
})

function log(message) {
	if (typeof(console) != "undefined") {
		console.log(message);
	}
}

function flashNotice(time) {
	clearTimeout(notice_timer);
  Effect.SlideDown("notice_panel", { duration: 1.0 });
  notice_timer=setTimeout("(function() {Effect.SlideUp('notice_panel', { duration: 1.0 });})()", time);
}

function flashTweet() {
    if (!$('twidget_body').visible()) {
      Effect.SlideDown('twidget_body', { duration: .3 });
    }
	  $('tweet_body').value = $$("#suggested_tweets span").first().innerHTML		
	  new Effect.Highlight('tweet_body', { startcolor: '#fffc7d', endcolor: '#fff8ce', duration:3 });
}

function hideTweetNotice(time) {
  clearTimeout(notice_timer);
	$('notice_panel').fade();
}


function sendFeedback(field, option, value) {
  log('form = ' + 'feedback_form_' + option);
	var feedback_form = $('feedback_form_' + option);
	feedback_form.reset();
	feedback_form.down('.' + field + '_field').value = value;
	$('feedback_form_' + option).onsubmit();	
}

	
function filter_profile(type) {
  $('empty_list').hide();
	$$("div.profile").invoke('hide');
	$$("div.details_tab").invoke("removeClassName", "caret");

  switch (type) {
    case "all": 
		      $$("div.profile").invoke('show');
				  $("all_tab").addClassName("caret");
				break;
    case "like": 
		      $$("div.like").invoke('show');
		      $("like_tab").addClassName("caret");
		      break;
    case "dislike": 
		      $$("div.dislike").invoke('show');
					$("dislike_tab").addClassName("caret");
					break;
    case "use": 
					$$("div.use").invoke('show');
					$("use_tab").addClassName("caret");
				  break;
    case "advice": 
		      $$("div.advice").invoke('show');
					$("advice_tab").addClassName("caret");
					break;     
  }
	
	var visibleProfiles = $$('div.profile').findAll(function(el) { return el.visible(); });
	if (visibleProfiles.size() == 0) { $('empty_list').show();}
	
}
		
function tweetCharCounter(field, text){

  if (field.value.length > 0) {
  	$("tweet_box").addClassName("tweet_highlight");
  }
  else {
    $("tweet_box").removeClassName("tweet_highlight");		
	}
  
	if (field.value.length > 140) {
		$("tweet_char_count").addClassName("negative_number");
		$("tweet_submit").disabled = true;
	}
	else {
		$("tweet_char_count").removeClassName("negative_number");
		$("tweet_submit").disabled = false;
	} 
  $("tweet_char_count").innerHTML = 140 - field.value.length;

}

function showTwitterLogin(content){
  facebox.loading();
  facebox.reveal($("fb_twitter").innerHTML, null);
  new Effect.Appear(facebox.facebox, {duration: .3});
	
}


function switch_list (option) {

  var elem = $(option);	
  elem.fade(2);
		
	setTimeout (this.switch_list_show.bind(this,elem), 1500);
	
}

function switch_list_show (elem) {

	     if (elem.descendantOf('options_body')) {
        $('options_body_published').appendChild(elem);
        elem.select('a.js_move')[0].innerHTML = "Drop from List"
      }
      else {
        first_item = $('options_body').firstDescendant();
        $('options_body').insertBefore(elem, first_item);
        elem.select('a.js_move')[0].innerHTML = "Add to List"
      }
      Event.fire("options", "items:changed")
      elem.show();
      new Effect.Highlight(elem, { startcolor: '#ffff00', endcolor: '#ffffff', duration:3 });

}

function arrange_list() {
	renumber_items();
	if ($('options_body').empty()) {
		$('suggested_header').hide();
	}
	else {
		$('suggested_header').show();
	}
	
  if ($('options_body_published').empty()) {
		
    $('published_header').show();
  }
  else {
    $('published_header').hide();
  }
}

function renumber_items() {
	$$("#options_body_published .rank_widget").each(function(item, index) {  
  item.update(index+1);
  }); 
  $$("#options_body .rank_widget").each(function(item, index) {  
  item.update(index+1);
  }); 
}


function expandNote(id)
{
   $(id+"_note_long").show();
   $(id+"_note_short").hide();
}

function contractNote(id)
{
   $(id+"_note_short").show();
   $(id+"_note_long").hide();
}

function updatePointsWidgets(id, points)
{
  $$('span[rel^=' + id + ']').invoke('update', points);
}

