var twitts;
root = "./";

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

function voting($target) {
	var message = { text: 'Thank you for voting. Your vote has been counted.', kind: 'ok-message'};
	var id = $target.parents('.twavel-wrapper').attr('id').replace('t','');
        var thumb = $target.parent().attr('class') == 'votePos'? 'up': 'down';
        var url = root + 'thumbs-' + thumb + '/' + id;
	$.post(url, {}, function(data){});
	$('ul.voting', $target.parents("div.twavel"))
		.fadeOut(400, function(){
			addRemoveMsg(message);
			$(this).remove();
		});
	return false;
}

function addRemoveMsg(message){
	var $notificationArea = $('#notificationArea'),
		$message = $('div.message', $notificationArea),
		timeOut = message.kind == 'error-message' ? 10000 : 3000,
		alerttimer = window.setTimeout(function () {
			$notificationArea.trigger('click');
		}, timeOut);

	if ($message.length == 0) {
		$('<div class="message ' + message.kind +  '" />')
			.addClass(message.kind)
			.html(message.text)
			.hide()
			.appendTo($notificationArea)
			.fadeIn()
	} else {
		$notificationArea.click();
		$('<div class="message ' + message.kind +  '" />')
			.html(message.text)
			.hide()
			.appendTo($notificationArea)
			.fadeIn()
	}

	$notificationArea.click(function () {
		window.clearTimeout(alerttimer);
		$notificationArea.hide().height('0px');
		$('div.message', $(this)).remove();
	});

	$notificationArea.animate({height: $message.height() || '42px'}, 200);

}

$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});

$(function(){

	if (TWAVEL.message['text'] != "") {
		addRemoveMsg(TWAVEL.message);
	}

	 // add class 'js' to body element for progressive enhacement if JS is enabled
	$('body').addClass('js').css('visibility','visible');

	$('div.twavel li.votePos a, div.twavel li.voteNeg a').live("click", function(e){
		voting($(e.target));
		return false;
	});

	var retweet;

	$('div.twavel ul.user-actions li.retweet-link a').live("click", function(e){

		var id = this.href.replace(/.*?id=(\d+)$/, '$1'),
			$retweeting = $(e.target).parents('div.twavel').siblings('div.retweeting').toggle()

 		$.getJSON("/tweetedit", {id: id}, function(r){
			retweet = r;
			var $retweeting = $(e.target).parents('div.twavel').siblings('div.retweeting'),
			$input = $('input.retweet-text', $retweeting);

			$input.val(retweet.post);
 		});

		return false;
	});

	$('div.retweeting form a.retweet-edit').live("click", function(e){
		var $retweeting = $(e.target).parents('div.retweeting'),
			$input = $('input.retweet-text', $retweeting);

		$input.attr('readonly','').focus().removeClass('read-only');

		return false
	});

	$('div.retweeting form a.retweet-cancel').live("click", function(e){
		$(e.target).parents('div.retweeting').hide();

		return false
	});

	$('#twavelsPanel a.deal').live("click", function(e){
                var id = $(e.target).parents('.twavel-wrapper').attr('id').replace('t','');
                var url = root + 'clickthrough/' + id;
                $.post(url, {}, function(data){});
		return true
	});

	$.fn.nyroModal.settings.minHeight = null;

  // register and log in popups on /add-your-deal
  $('#registerLink a,#logInLink a').nyroModal({
    bgColor: '#b4dee4'
  });
  // open popup if there is an error
  $('.register-errors #registerLink a').click();
  $('.login-errors #logInLink a').click();

  // 'Add a deal' form
  $('#dealType').change(function(){
    var $selected = $('option:selected', this),
      $otherType = $('#dealTypeOther-formItem');
    if ($selected.val() == '') {
      $otherType.show();
    } else {
      $otherType.hide();
    }
  });
  $('#zoneWorld').live('click', function() {
      $("fieldset#dealZone input[id^='zone']").attr('checked', this.checked);
  });


  // Register form
  $('#twitterProfile').bind('blur', function(e){
    var twprofile = e.target.value;
    if (twprofile != '') {
        $.post('/register/log/' + twprofile);
    }
    return 1;
  });
  $('#registerAssistance').hide();
  $('#buttonAssistance').live('click', function() {
      $('#registerAssistance').toggle();
      $('#assistanceText').focus();
  });


  // fixes IE7 z-index issues
  if ($.browser.msie && $.browser.version == 7) {
    var zIndexNumber = 1000;
    $('div').each(function() {
      $(this).css('zIndex', zIndexNumber);
      zIndexNumber -= 10;
    });
  }


});
