var default_title = "Vent Title";
var default_body = "Post your vent!";
var default_media_body = "Describe your vent (optional)";
var default_link = "Paste the URL here";
var default_link_html = "Embed Code";

jQuery(document).ready(function($){
	
	$(function () {
	    var tabContainers = $('div.tabs > div');
	
	    $('div.tabs ul.tab_navigation a').click(function () {
	        tabContainers.hide().filter(this.hash).show();
	
	        $('div.tabs ul.tab_navigation li').removeClass('current');
	        $(this).parent('li').addClass('current');
	
	        return false;
	    }).filter(':first').click();
	});
	
	var vent_title = $('#message_vent_box #title');
	var vent_body = $('#message_vent_box #body');
	var vent_category = $('#message_vent_box #category');
	
	var vent_video_title = $('#video_vent_box #video_title');
	var vent_video_body = $('#video_vent_box #video_body');
	var vent_video_category = $('#video_vent_box #video_category');
	var vent_video_link = $('#video_vent_box #link_video');
	var vent_video_link_html = $('#video_vent_box #link_video_html');
	
	var vent_photo_title = $('#photo_vent_box #photo_title');
	var vent_photo_body = $('#photo_vent_box #photo_body');
	var vent_photo_category = $('#photo_vent_box #photo_category');
	var vent_photo_link = $('#photo_vent_box #link_photo');
	var vent_photo_link_html = $('#photo_vent_box #link_photo_html');
	
	var vent_link_title = $('#link_vent_box #link_title');
	var vent_link_body = $('#link_vent_box #link_body');
	var vent_link_category = $('#link_vent_box #link_category');
	var vent_link_link = $('#link_vent_box #link_link');
	var vent_link_link_html = $('#link_vent_box #link_link_html');
	
	vent_title.focus(function() {
		if(vent_title.val() == default_title)
			vent_title.attr('value', '');
	});
	vent_title.blur(function() {
		if(vent_title.val() == '')
			vent_title.attr('value', default_title);
	});
	
	vent_video_title.focus(function() {
		if(vent_video_title.val() == default_title)
			vent_video_title.attr('value', '');
	});
	vent_video_title.blur(function() {
		if(vent_video_title.val() == '')
			vent_video_title.attr('value', default_title);
	});
	
	vent_photo_title.focus(function() {
		if(vent_photo_title.val() == default_title)
			vent_photo_title.attr('value', '');
	});
	vent_photo_title.blur(function() {
		if(vent_photo_title.val() == '')
			vent_photo_title.attr('value', default_title);
	});
	
	vent_link_title.focus(function() {
		if(vent_link_title.val() == default_title)
			vent_link_title.attr('value', '');
	});
	vent_link_title.blur(function() {
		if(vent_link_title.val() == '')
			vent_link_title.attr('value', default_title);
	});
	
	vent_body.focus(function() {
		if(vent_body.val() == default_body)
			vent_body.attr('value', '');
	});
	vent_body.blur(function() {
		if(vent_body.val() == '')
			vent_body.attr('value', default_body);
	});
	
	vent_video_body.focus(function() {
		if(vent_video_body.val() == default_media_body)
			vent_video_body.attr('value', '');
	});
	vent_video_body.blur(function() {
		if(vent_video_body.val() == '')
			vent_video_body.attr('value', default_media_body);
	});
	
	vent_photo_body.focus(function() {
		if(vent_photo_body.val() == default_media_body)
			vent_photo_body.attr('value', '');
	});
	vent_photo_body.blur(function() {
		if(vent_photo_body.val() == '')
			vent_photo_body.attr('value', default_media_body);
	});
	
	vent_link_body.focus(function() {
		if(vent_link_body.val() == default_media_body)
			vent_link_body.attr('value', '');
	});
	vent_link_body.blur(function() {
		if(vent_link_body.val() == '')
			vent_link_body.attr('value', default_media_body);
	});
	
	vent_video_link.focus(function() {
		if(vent_video_link.val() == default_link)
			vent_video_link.attr('value', '');
	});
	vent_video_link.blur(function() {
		if(vent_video_link.val() == '')
			vent_video_link.attr('value', default_link);
	});
	
	vent_photo_link.focus(function() {
		if(vent_photo_link.val() == default_link)
			vent_photo_link.attr('value', '');
	});
	vent_photo_link.blur(function() {
		if(vent_photo_link.val() == '')
			vent_photo_link.attr('value', default_link);
	});
	
	vent_link_link.focus(function() {
		if(vent_link_link.val() == default_link)
			vent_link_link.attr('value', '');
	});
	vent_link_link.blur(function() {
		if(vent_link_link.val() == '')
			vent_link_link.attr('value', default_link);
	});
	
	
	$('#message_vent_box').submit(function() {
		
		if(vent_title.val() == default_title || vent_title.val() == '')
		{	
			vent_title.attr('value', '');
			vent_title.focus();
			return false;
		}
		
		if( is_message() )
		{
		  if (vent_body.val() == default_body || vent_body.val() == '')
  		{	
  			vent_body.attr('value', '');
  			vent_body.focus();
  			return false;
  		}
  		if (vent_body.val().length > 5000)
  		{
  		  alert('Vent body cannot be greater than 5000 characters, yours is ' + vent_body.val().length);
  		  vent_body.focus();
  			return false;
  		}
  	}
		
		if(vent_category.val() == 0 || vent_category.val() == '')
		{	
			alert('Please choose a Category from the list below.');
			return false;
		}
		
		return true;
  });

  $('#video_vent_box').submit(function() {
		
		if(vent_video_title.val() == default_title || vent_video_title.val() == '')
		{	
			vent_video_title.attr('value', '');
			vent_video_title.focus();
			return false;
		}
		
		if(is_video() && vent_video_body.val() == default_media_body)
		{	
			vent_video_body.attr('value', '');
		}
		
		if(is_video() && (vent_video_link.val() == default_link || vent_video_link.val() == ''))
		{	
			alert('link');
			vent_video_link.attr('value', '');
			vent_video_link.focus();
			return false;
		}
		
		if(is_video() && (vent_video_link_html.val() == default_link_html || vent_video_link_html.val() == ''))
		{	
			alert('Please paste a valid URL');
			vent_video_link_html.attr('value', '');
			vent_video_link.focus();
			return false;
		}
		
		if(vent_video_category.val() == 0 || vent_video_category.val() == '')
		{	
			alert('Please choose a Category from the list below.');
			return false;
		}
		
		return true;
  });
  
  $('#photo_vent_box').submit(function() {
		
		if(vent_photo_title.val() == default_title || vent_photo_title.val() == '')
		{	
			vent_photo_title.attr('value', '');
			vent_photo_title.focus();
			return false;
		}
		
		if(is_photo() && vent_photo_body.val() == default_media_body)
		{	
			vent_photo_body.attr('value', '');
		}
		
		if(is_photo() && (vent_photo_link.val() == default_link || vent_photo_link.val() == ''))
		{	
			vent_photo_link.attr('value', '');
			vent_photo_link.focus();
			return false;
		}
		
		if(is_photo() && (vent_photo_link_html.val() == default_link_html || vent_photo_link_html.val() == ''))
		{	
			alert('Please paste a valid URL');
			vent_photo_link_html.attr('value', '');
			vent_photo_link.focus();
			return false;
		}
		
		if(vent_photo_category.val() == 0 || vent_photo_category.val() == '')
		{	
			alert('Please choose a Category from the list below.');
			return false;
		}
		
		return true;
  });
  
  $('#link_vent_box').submit(function() {

  	if(vent_link_title.val() == default_title || vent_link_title.val() == '')
  	{	
  		vent_link_title.attr('value', '');
  		vent_link_title.focus();
  		return false;
  	}

  	if(is_link() && vent_link_body.val() == default_media_body)
  	{	
  		vent_link_body.attr('value', '');
  	}

  	if(is_link() && (vent_link_link.val() == default_link || vent_link_link.val() == ''))
  	{	
  		vent_link_link.attr('value', '');
  		vent_link_link.focus();
  		return false;
  	}

  	if(is_link() && (vent_link_link_html.val() == default_link_html || vent_link_link_html.val() == ''))
  	{	
  		alert('Please paste a valid URL');
  		vent_link_link_html.attr('value', '');
  		vent_link_link.focus();
  		return false;
  	}

  	if(vent_link_category.val() == 0 || vent_link_category.val() == '')
  	{	
  		alert('Please choose a Category from the list below.');
  		return false;
  	}

  	return true;
  });

	$('.category_select').click(function() {
		$('.category_list').toggle('fast');
		return false
		});
	
	$(function() {
		$('.slider').slider({
			value: 1,
			min: 1,
			max: 4,
			step: 1,
			slide: function  (event, ui) {
				vent = this.attributes['vent_id'].value;
				$("#amount_" + vent).val(eval("slider_blurb_" + ui.value + "[0]"));
				flash_slider_description(vent, eval("slider_blurb_" + ui.value + "[1]"));
			},
			stop: function  (event, ui) {
				user = this.attributes['user_id'].value;
				vent = this.attributes['vent_id'].value;
				$("#flash_rate_" + vent).fadeOut('fast');
				if(user > 0)
				{
					$.ajax({
						type: "GET",
						url: "/vent/rate",
						data: "id=" + vent + "&rating=" + ui.value,
						success: function(resp) {
						  if(resp == "login_error")
						    flash_slider_message(vent, slider_error_login);
						  else
						    flash_slider_message(vent, "Your rating was saved.");
						},
						error: function() {
						  flash_slider_message(vent, "There was an error, try again later.");
						}
				  })
				}
				else
					flash_slider_message(vent, slider_error_login);
			}
		});
		$(".amount").val('Vote on this vent.');
	});
	
  init_ratings();

});

function previewVent(t, vent_type) {
  preview({'link':$(t).val()}, vent_type);
}

function preview(value, vent_type) {
  $.ajax({data:value, success:function(request) { load(request, vent_type) }, type:'get', url:'/vents/' + vent_type + '/preview'});
}

function load(request, vent_type) {
  $('#link_' + vent_type + '_html').attr('value', request);
	$('p.preview_' + vent_type).html(request);
}

var slider_blurb_1 = new Array("Sucks","Just wasted 5 sec of my life viewing this post!");
var slider_blurb_2 = new Array("Boring","MEH, not that interesting");
var slider_blurb_3 = new Array("Interesting","This is a pretty cool post");
var slider_blurb_4 = new Array("Wow!","This is a great post");

var slider_error_login = "Please <a href='/user_session/new'>Login</a> or <a href='/users/new'>Sign up</a> to vote on this vents.";

var arrRatings = new Array();
var arrVents = new Array();
var _vent;
var _flashTimer;

function flash_slider_message(vent,message)
{
  $("#flash_rate_" + vent).html(message);
	$("#flash_rate_" + vent).fadeIn();
	set_timeout(vent);
}
function flash_slider_description(vent,message)
{
  $("#flash_desc_" + vent).html(message);
	$("#flash_desc_" + vent).fadeIn();
	set_timeout(vent);
}

function set_timeout(vent)
{
  _vent = vent;
  clearTimeout(_flashTimer);
	_flashTimer = setTimeout("flash_confirm_fadeout(_vent)", 3000);
}

function flash_confirm_fadeout(vent)
{
 $("#flash_rate_" + vent).fadeOut(); 
 $("#flash_desc_" + vent).fadeOut();
}

function set_rating(vent,rating)
{
	arrVents[arrRatings.length] = vent;
	arrRatings[arrRatings.length] = rating;
}

function init_ratings()
{
	for(x=0;x<arrVents.length;x++)
	{
		if(arrRatings[x] > 0)
		{
			$("#amount_" + arrVents[x]).val(eval("slider_blurb_" + arrRatings[x] + "[0]"));
			$("#slider_" + arrVents[x]).slider('option', 'value', arrRatings[x]);
		}
	}
}

function select_category(id,name)
{
	if( is_message() )
	{
		$('#message_vent_box #category').attr('value', id);
		$('#message_vent_box .category_select span a').html(name);
	}
	else if( is_video() )
	{
		$('#video_vent_box #video_category').attr('value', id);
		$('#video_vent_box .category_select span a').html(name);
	}
	else if( is_photo() )
	{
		$('#photo_vent_box #photo_category').attr('value', id);
		$('#photo_vent_box .category_select span a').html(name);
	}
	else if( is_link() )
	{
		$('#link_vent_box #link_category').attr('value', id);
		$('#link_vent_box .category_select span a').html(name);
	}
	$('.category_list').hide('fast');
}

function is_video()
{
	return ( $('#video_vent').css('display') != 'none' );
}

function is_photo()
{
	return ( $('#photo_vent').css('display') != 'none' );
}

function is_link()
{
	return ( $('#link_vent').css('display') != 'none' );
}

function is_message()
{
	return ( $('#message_vent').css('display') != 'none' );
}
