// Load the scripts
$(function() 
{
	// Event Delegation
	$('.tags').focus(function()
	{
		el = $(this);
		if(el.attr('bound')) // Check if the element is already bound
			return;

		craption.tags_auto_complete(el);
	});
});

var craption = new function()
{
	o = this; // Name space internally
	o.tags = [];
	o.modal = null;

	o.set_tags = function(a)
	{
		o.tags = a;
	}
	// Scroller settings
	o.scroll_set =
	{
		inc: 147, // how far to move the scroll window each direction
		panel_current: 0,
		panel_total: 0,
		btn_left: null, 
		btn_right: null,
		container: null
		
		
	}

	/*
		 The main init for the public craptions
		 Move the scroller to the correct position
	*/
	o.main_init = function(panel_current, panel_total)
	{
		o.scroll_set.panel_current = panel_current * 1; // Which Panel to start on
		o.scroll_set.panel_total = panel_total * 1; // How many panels
	
		// Store the elements and, if it's at the start, hide the start button
		$(function() 
		{
			o.scroll_set.btn_left = $(".PastCraptionsGroup .Nav4 .arrowLeft");
			o.scroll_set.btn_right = $(".PastCraptionsGroup .Nav4 .arrowRight");
			o.scroll_set.container = $('#ItemsListContainer');
			
			if(o.scroll_set.panel_current == 0)
				o.scroll_set.btn_left.css("visibility", "hidden");

			if(o.scroll_set.panel_current == o.scroll_set.panel_total - 1)
				o.scroll_set.btn_right.css("visibility", "hidden");

			// If we need to scroll to another panel
//			if(o.scroll_set.panel_current > 0)
//				o.scroll_set.container.animate({"left": "-=" + (o.scroll_set.panel_current * o.scroll_set.inc) + "px"}, "slow");

		});
	}

	// Scroll the crap	
	o.scroll_right = function()
	{
		o.scroll_set.container.animate({"left": "-=" +  o.scroll_set.inc + "px"}, "normal");
		o.scroll_set.btn_left.css("visibility", "visible");
		if(++o.scroll_set.panel_current == o.scroll_set.panel_total - 1)
			o.scroll_set.btn_right.css("visibility", "hidden");
	}

	o.scroll_left = function()
	{
		o.scroll_set.container.animate({"left": "+=" +  o.scroll_set.inc + "px"}, "normal");
		o.scroll_set.btn_right.css("visibility", "visible");
		if(--o.scroll_set.panel_current == 0)
			o.scroll_set.btn_left.css("visibility", "hidden");
	}

	o.show_modal = function(text, offset)
	{
		// Create the modal if it doesn't exist
		if(!o.modal)
		{
			o.modal = $('<div></div>');
			var css = 
			{
				display: 'none',
				position: 'absolute',
				zIndex: 1001,
				left: '40%',
				background: '#eee',
				border: '#00f solid 1px',
				color: '#00f',
				fontWeight: 'bold',
				padding: '20px'
			}
			o.modal.css(css);
			o.modal.appendTo('body');
		}
		var window_center = $(window).scrollTop() + ($(window).height() / 2);
		var max_width = $('body').width();

		var css = 
		{
			top: offset.top,
			left: offset.left
		}
		o.modal.css(css); // Position it at the top
		o.modal.html(text);
		o.modal.slideDown('fast', function()
		{
			o.modal.fadeOut(2000);
		});
		
		
	}
	
	// Updates the craption tags
	o.submit_tags = function(e_form)
	{
		var options =
		{
			success: function(data)
			{
				o.submit_tags_done(data, e_form);
			},
			dataType: 'json'
		};
		
		$(e_form).ajaxSubmit(options);
		return false;
	}
	o.submit_tags_done = function(data, e_form)
	{
		o.show_modal('The tags have been updated', $(e_form).offset());
		o.set_tags(data);
		$('.tags').removeAttr('bound'); // unbind all tags 
	}
	
	o.tags_auto_complete = function(el)
	{
		var options =
		{
			minChars: 0,
			width: 310,
			max: 30,
			matchContains: true,
			formatItem: function(item) {return item.name; }
		};
		el.attr('bound', 'true'); // Bind it
		el.autocomplete(o.tags, options).result( o.ac_result_done );
		el.click();
	}

	// Save the tag id in the hidden column
	o.ac_result_done = function(event, item)
	{
		$('#'+event.target.id+'_id').val(item.tag_id);
	}
	
	o.rate = function(el)
	{
		var id = el.id.replace('craption_', '');
		if(!user.id)
		{
			var html = ''+
				'<div style="text-align: center;">'+
					'Please login (or register) to vote for this craption.<br />'+
					'<br />'+
					'Would you like to do so now?<br/>'+
					'<br />'+
					'<div style="width: 250px; margin: 0 auto;">'+
						'<a href="/users/login/?return='+ window.location.pathname+(window.location.search?window.location.search:'')+'" class="button_2" style="width: 50px;"><span>Yes</span></a>'+
						'&nbsp;&nbsp;&nbsp;'+
						'<a onclick="popup.close();" class="button_1" style="width: 50px;"><span>No</span></a>'+
					'</div>'+
				'</div>'+
			'';
			popup.inline(html, 'Login to Continue');
			return;
		}
		var url = '/craptions/a_up.php';
		var data = 
		{
			id: id
		};
		$.get(url, data, function(data) { o.rate_done(data, el); });
	}	
	
	o.rate_done = function(data, el)
	{
		el = $(el);
		el.unbind();
		el.attr('onclick', '');
		el.attr('class', 'Voted');
		el.html(data);
	}
	
	o.show_craption_image = function()
	{
		$('#craption_image_helper').show();
		$.scrollTo(
			"#craption_image_helper",
			{
				duration: 800,
				easing: 'easeOutBounce',
				offset: 0
			}
		);

	}
}
