$(document).ready(function(){
	//$('#mainContent').lionbars();

	// Handle clicks
	$('#original_slider a').live('click', function(){
		createPlayer(URL + 'uploads/' + $(this).metadata().src + '', 'flv/' + $(this).metadata().src + '.jpg', $(this).metadata().title, $(this).metadata().description, true);
	});

	$('#image_slider .slider a').live('click', function(){
		$('h2.title').html($(this).metadata().title);
		$('p.description').html($(this).metadata().description);
		$('#main_image').attr('src', URL + 'i/c/400x294/' + $(this).metadata().src + '');
	});

	$('.cat').click(function(e){
		$('ul').removeClass('selected');
		$(this).parent().find('ul').addClass('selected');
		$('.submenu ul').not('.selected').hide();
		$(this).parent().find('ul').slideToggle();
		if ($(this).parent().find('ul').children().length)
		{
			e.preventDefault();
		}
	});

	$('.submenu ul').not('.selected').hide();

	if ($('#mainContent')[0])
	{
		 $('#sidebar').css('cssText', 'height: '+($('#mainContent')[0].clientHeight-15)+'px !important;');
	}

	$('#calendarSlider .controls .prev').click(function(e){
		c_cur--; if (c_cur<=0) c_cur=0;
		$('.nivoslider a').each(function(index){ 
			if (index!=c_cur) $(this).fadeOut();
			else $(this).fadeIn();
		});
		$('.next').fadeIn();
		if (c_cur==0) $(this).fadeOut();
		e.preventDefault();
	});

	$('#calendarSlider .controls .next').click(function(e){
		c_cur++; if (c_cur>=7) c_cur=7;
		$('.nivoslider a').each(function(index){ 
			if (index!=c_cur) $(this).fadeOut();
			else $(this).fadeIn();
		});
		$('.prev').fadeIn();
		if (c_cur==6) $(this).fadeOut();
		e.preventDefault();
	});

	if ($('.positions_wrap').length > 0) {
		$('.positions_wrap .text').hide();

		$('.positions_wrap .titles').bind('click', function(){
			if (!$(this).next().hasClass('opened')) {
				$('.positions_wrap .opened').slideUp().removeClass('opened');
				$(this).next('.text').slideDown().addClass('opened');
			}
			else if ($(this).next().hasClass('opened')) {
				$('.positions_wrap .opened').slideUp().removeClass('opened');
			}
		});
	}
});

var c_cur = 0;

function createPlayer(theFile, theImage, title, description, autostart, width, height, first)
{
	var width = width || '400';
	var height = height || '294';

	if (!first) $('h2.title').html(title);
	if (!first) $('p.description').html(description);

	jwplayer('player_wrap').setup({
    'id': 'video_wrap',
    'width': width,
    'height': height,
    'file': theFile,
	'autostart': autostart,
    'image': theImage,
	'skin':URL + 'flash/glow/glow.xml',
    'modes': [
        {type: 'flash', src: URL + 'flash/jwplayer.swf?v=2'},
		{type: 'html5'},
        {type: 'download'}
    ],
	events: {
		onComplete: function() {
			var metadata = $('a[class=\'{src:\''+theFile.replace(URL+'uploads/', '')+'\'}\']').parent().next().find('a').metadata();
			createPlayer(URL+'uploads/'+metadata.src, '', true);
		}
	}
  });
}
