$(document).ready(function()
{
	// Click to close the error/succes notifications
	$('div.error, div.success, div.info').live('click', function() {
		$(this).hide('blind');
	})
	
	// Setup youtube videos to show on prettyPhoto
	$('a[href^=http\://www.youtube.com?v=]').attr('rel', 'lb');
	
	// Setup prettyPhoto
	$('a[rel^=lb]').prettyPhoto({
		'theme' : 'dark_square'
	});
	
	// Setup markitup
	$('#markitup').markItUp(mySettings);
	
	// Confirm for any remove submits
	$('#remove').click(function(){
		if(confirm('Are you sure you want to remove this?'))
			return true;
			
		return false;
	});
	
	// Show what sections of pages are being edited
	$('a.edit, div.edit a').hover(function(){
		$('#' + $(this).attr('rel')).css('background', 'url(/resources/img/layout/edit.png)');
	}, function(){
		$('#' + $(this).attr('rel')).css('background', '');
	}).click(function(){
		$('#' + $(this).attr('rel')).css('background', '');
	});
	
	// Setup sorting of list items
	$('.edit_order').toggle(function(){
		// Begin editing
		$(this).text('Save Order');
		
		rel = $(this).attr('rel');
		$('#' + rel).sortable('enable');
		
		// Notificiation
		$('#content').prepend("<div class='info'>Click and drag any item in the list to change its position. Click Save Order when you are done.</div>");
		
		// Unbind click events
		handler = $('#' + rel + ' li a[rel^=lb]').data('events').click[0].handler;
		$('#' + rel + ' li a[rel^=lb]').unbind('click');
	}, function(){
		// Done editing
		$(this).text('Edit Order');
		
		rel = $(this).attr('rel');
		$('#' + rel).sortable('disable');
		
		// Figure out which url we need to call
		switch(rel)
		{
			case 'machine_images':
				url = 'edit_photo/order';
				break;
			case 'machine_videos':
				url = 'edit_video/order';
				break;
			case 'machine_cats':
				url = 'edit_cat/order';
				break;
			default:
				url = 'edit/order';
				break
		}
		
		// Send ajax call
		$.ajax({
			'url'	: url,
			'type'	: 'post',
			'data'	: 'order=' + $('#' + rel).sortable('toArray').join(',') + ',',
		});
		
		// Remove notification
		$('div.info').hide('blind');
		
		// Rebind click event
		if(handler)
			$('#' + rel + ' li a[rel^=lb]').click(handler);
		
		return false;
	})
	
	$('.sortable').sortable({
		'disabled' : true,
		'containment' : 'parent',
	});
	
	// Switch bettween inuqiry forms
	$('.contact_form a').click(function(){
		$('#contact_form_distributor').toggle();
		$('#contact_form_customer').toggle();
		
		return false;
	});
	
	if(location.hash == '#distributor')
		$('#contact_form_customer a').click();
	
	
});

function onYouTubePlayerReady(yid)
{
	document.getElementById("auto_play").setVolume(0);
	document.getElementById("auto_play").playVideo();
}
