// poll pies
function generate_pie(pie_div, id)
{
	results = id.split('_');
	total = parseInt(results[1]) + parseInt(results[2]) + parseInt(results[3]) + parseInt(results[4]) + parseInt(results[5]);
	votes_str = '' + total + ',' + results[1] + ',' + results[2] + ',' + results[3] + ',' + results[4] + ',' + results[5];
	
	pie_div.flash({
		swf: swf_pollpie,
		params:
		{
			loop: 'false',
			quality: 'high',
			scale: 'noborder',
			wmode: 'transparent'
		},
		flashvars:
		{
			votes: votes_str 
		},
		width: 133,
		height: 133
	});	
}

$(document).ready(function() {
    $('#header_search .text').defaultWhenEmpty({
        str: search_str
    });

    $('#poll_input_element input').defaultWhenEmpty({
        str: poll_invite_str
    });

	// header search submit button: hover effect
	$("form a.search_button").hover(
		function()
		{
			$(this).children('img').attr('src', base_url + 'images/layout/search_button_hover.gif');
		}, 
		function()
		{
			$(this).children('img').attr('src', base_url + 'images/layout/search_button.gif');
		}
	);
	
	$(".pie").each(function(){
		id = $(this).attr('id');
		generate_pie($(this), id);
	});

	
	// video
	$(".media .video").each(function(){
		video_id = $(this).children('span').attr('class');

		config = "{";
		config += "'videoFile': '" + mediapath + '?id=' + video_id + "'" + ",";
		config += "'controlsOverVideo': 'ease'" + ",";
		config += "'controlBarBackgroundColor': '0xcccccc'" + ",";
		config += "'loop': false,";
		config += "'initialScale': 'fit'" + ",";
		config += "'showFullScreenButton': false,";
		config += "'showMenu': false";
		config += "}";
		
		$(this).flash(
			{
				swf: flowplayer,
				flashvars:
				{
					config: config 
				},
				width: 535,
				height: 300,
				wmode: 'transparent'
			}
		);
	});
	
	// video on home page
	$(".inline_video").click(function(){
		video_id = $(this).children('span').attr('class');

		config = "{";
		config += "'videoFile': '" + mediapath + '?id=' + video_id + "'" + ",";
		config += "'controlsOverVideo': 'ease'" + ",";
		config += "'loop': false,";
		config += "'controlBarBackgroundColor': '0xcccccc'" + ",";
		config += "'initialScale': 'fit'" + ",";
		config += "'showFullScreenButton': false,";
		config += "'showMenu': false";
		config += "}";
		
		$(this).flash(
			{
				swf: flowplayer,
				flashvars:
				{
					config: config 
				},
				width: 295,
				height: 214
			}
		);
		
		return false;
	});

	// video in comments
	$(".inline_video_comments").each(function(){
		video_id = $(this).children('span').attr('class');
	
		config = "{";
		config += "'videoFile': '" + mediapath + '?id=' + video_id + "'" + ",";
		config += "'controlsOverVideo': 'ease'" + ",";
		config += "'loop': false,";
		config += "'autoPlay': false,";
		config += "'controlBarBackgroundColor': '0xcccccc'" + ",";
		config += "'initialScale': 'fit'" + ",";
		config += "'showFullScreenButton': false,";
		config += "'showMenu': false";
		config += "}";
		
		$(this).flash(
			{
				swf: flowplayer,
				flashvars:
				{
					config: config 
				},
				width: 295,
				height: 214
			}
		);
		
		return false;
	});
	
	// contest video
	$(".contest_video").click(function(){
		video_url = $(this).attr('href');

		config = "{";
		config += "'videoFile': '" + video_url + "'" + ",";
		config += "'controlsOverVideo': 'ease'" + ",";
		config += "'loop': false,";
		config += "'controlBarBackgroundColor': '0xcccccc'" + ",";
		config += "'initialScale': 'fit'" + ",";
		config += "'showFullScreenButton': false,";
		config += "'showMenu': false";
		config += "}";
		
		$(this).flash(
			{
				swf: flowplayer,
				flashvars:
				{
					config: config 
				},
				width: 370,
				height: 264
			}
		);
		
		return false;
	});
	
});

// labels
function selectLabel(li, label_id){
	if( ! li.hasClass('selected')){ // don't add twice the same label
		// copy to selected list
		li_copy = li.clone(true);
		li_copy_new_text = li_copy.find('a').text().replace('|', '');
		li_copy.find('a').text(li_copy_new_text);
		li_copy.hide();
		$('.label_selected').append(li_copy);
		li_copy.show('fast');
		
		// apply selected class
		li.addClass('selected');
		
		// check associated checkbox
		$('.label_mbox[value="' + label_id + '"]').attr('checked', 'checked');
	}
}

function removeLabel(li){
	// remove from selected list
	li.hide('fast', function(){
		li.remove();		
	});

	// remove selected class
	$('.label_list .' + li_class).removeClass('selected');
		
	// uncheck associated checkbox
	$('.label_mbox[value="' + label_id + '"]').attr('checked', '');
}

$(document).ready(function() {
	$('ul.label_list li:not(:last-child) > a').append(' |');
	
	$('form .label_list li a, .label_selected li a').click(function(){
		li = $(this).parent();
		li_class = li.attr('class');
		label_id = li_class.split('_').pop();

		if(li.parent('ul').hasClass('label_list')) {			
			selectLabel(li, label_id);
		}
		else {
			removeLabel(li, label_id);
		}
		return false;
	});

	$('.label_mbox:checked').each(function(){
		label_id = $(this).attr('value');
		li = $('.label_' + label_id);
	
		if(li.parent('ul').hasClass('label_list'))
		{			
			selectLabel(li, label_id);
		}
	});
	
});


// inline voting
$(document).ready(function() {
	// inline update of results
	function updatePoll(data, status, form){
		poll_div = $('#' + data.id);
		
		// update pie
		new_pie_id = "poll_" + data['2'] + "_" + data['1'] + "_" + data['0'] + "_" + data['-1'] + "_" + data['-2'];
		generate_pie(poll_div.find('.pie').attr('id', new_pie_id), new_pie_id);
		
		// update totals
		poll_div.find('.answer1 b').text(data['2']);
		poll_div.find('.answer2 b').text(data['1']);
		poll_div.find('.answer3 b').text(data['0']);
		poll_div.find('.answer4 b').text(data['-1']);
		poll_div.find('.answer5 b').text(data['-2']);
		
		// update vote message
		poll_div.find('.vote').html(data.vote);
	}
	
	// display vote form
	$('.vote_button').click(function(){
		poll_id = $(this).parents('.poll').attr('id');
		
		poll_vote_form = $('.poll_vote_form').clone();
		poll_vote_form.find('input[name=pollId]').attr('value', poll_id);
		
		vote_div = $(this).parent('.vote');
		vote_div.append(poll_vote_form);

		poll_vote_form.find('form').ajaxForm({dataType:'json', success:updatePoll});
		poll_vote_form.show();
		
		// cancel
		poll_vote_form.find('.cancel').click(function(){
			$(this).parents('.poll_vote_form').remove();
			return false;
		});			

		return false;
	});
});


// search
$(document).ready(function() {
	$('#header_search form a').click(function(){
		$(this).parents('form').submit();		
		return false;
	});
});


// like
$(document).ready(function() {
	$('.like').click(function(){
		if($(this).hasClass('liked'))
		{
			alert(already_liked_str);
		}
		else
		{
			$(this).addClass('liked');
			$(this).siblings('.like_result').load($(this).attr('href'));
		}
		return false;
	});
});

//add resource form
$(document).ready(function() {
	$('.related .share_link a').click(function(){
		$(this).parent('p').next('form').toggle('fast');
		return false;
	});
});

// olympics: dynamic tabs
$(document).ready(function() {
	$('.olympic .tabs li a').click(function(){
		order = $(this).attr('class');

		// update contents
		$('.olympic .list ul').addClass('list_not_selected');
		$('.olympic .list ul.' + order).removeClass('list_not_selected');

		// update tabs
		$('.olympic .tabs li').removeClass('selected');
		$(this).parent('li').addClass('selected');
		
		return false;
	});
});

//war of 1812: dynamic tabs
$(document).ready(function() {
	$('.war_of_1812 .tabs li a').click(function(){
		order = $(this).attr('class');

		// update contents
		$('.war_of_1812 .list ul').addClass('list_not_selected');
		$('.war_of_1812 .list ul.' + order).removeClass('list_not_selected');

		// update tabs
		$('.war_of_1812 .tabs li').removeClass('selected');
		$(this).parent('li').addClass('selected');
		
		return false;
	});
});

