/*global window, document, confirm, setTimeout, $, lang */

/******************************************************************************************/
// close other audio players
function ap_stopAll(player_id)
{
	$('.audio').each(function(){
		if($(this).attr('id') != player_id)
		{
			$(this).find('object')[0].SetVariable("closePlayer", 1);
		}
		else 
		{
			$(this).find('object')[0].SetVariable("closePlayer", 0);
		}
	}); 
}

/******************************************************************************************/
// redirect from accessible page to Flash page
function redirect_to_flash(reload)
{
	var special = [];

	// ending
	special['/unfinished_story/view_ending/\\d+$'] = '/../../browse_endings';
	special['/histoire_incomplete/voir_denouement/\\d+$'] = '/../../explorer_denouements';
	
	// gallery
	special['/art/gallery/sculpture$'] = '/../../../art';
	special['/art/gallery/paintings$'] = '/../../../art';
	special['/art/gallery/jewelry$'] = '/../../../art';

	// gallery item
	special['/art/gallery/sculpture/\\d+$'] = '/../../../../art';
	special['/art/gallery/paintings/\\d+$'] = '/../../../../art';
	special['/art/gallery/jewelry/\\d+$'] = '/../../../../art';
	
	// photo bio
	special['/who/photo_biography/beginnings$'] = '/../../../who';
	special['/who/photo_biography/young_man$'] = '/../../../who';
	special['/who/photo_biography/middle_years$'] = '/../../../who';
	special['/who/photo_biography/elder$'] = '/../../../who';

	// photo bio item
	special['/who/photo_biography/beginnings/\\d+$'] = '/../../../../who';
	special['/who/photo_biography/young_man/\\d+$'] = '/../../../../who';
	special['/who/photo_biography/middle_years/\\d+$'] = '/../../../../who';
	special['/who/photo_biography/elder/\\d+$'] = '/../../../../who';
	
	// perspective item
	special['/who/perspectives/\\d+$'] = '/..';
	special['/qui/perspectives/\\d+$'] = '/..';
	
	// in his own words item
	special['/who/in_his_own_words/\\d+$'] = '/..';
	special['/qui/en_ses_propres_mots/\\d+$'] = '/..';
	
	var current_url = '' + window.location;
	var target_url = '';
	for(var regexp in special)
	{
		if(current_url.match(regexp))
		{
			target_url = current_url + special[regexp];
			break;
		}
	}
	
	// if no regexp has been matched, check if there isn't a special back link in the html
	if (target_url == '')
	{
		target_url = $('a.flash_toggle').attr('href');
	}
	
	if (target_url)
	{
		window.location = target_url;
	}
	else
	{
		if(reload)
		{
			window.location.reload();			
		}
	}
}

$(document).ready(function() {

/******************************************************************************************/
// set #contents min-height

$('#contents').css('min-height', $(window).height() + 'px');


/******************************************************************************************/
// home: hover on box

$('#contents.home .section').hover(
	function() {
		$(this).css({
			'background-color': '#252b2e',
			'cursor': 'pointer'
		});
		
		var link = $(this).find('a');
		link.data('color', link.css('color'));
		link.css('color','#c0c0c0');
	},
	function() {
		$(this).css({
			'background-color': '#22272a'
		});
		
		var link = $(this).find('a');
        link.css('color', link.data('color'));
	}
);

/******************************************************************************************/
// home: click on box

$('#contents.home .section').click(function() {
	var link = $(this).find('a');
	window.location = link.attr('href');
});


/******************************************************************************************/
// audio transcript

$('.audio_transcript_contents').hide();

$('.audio_transcript .transcript_toggle').each(function() {
	$(this).wrapInner('<a href="#"></a>');
});

$('.audio_transcript a').click(function() {
	$(this).parent().next('div').toggle('slow');
	$(this).toggleClass('open');
	return false;
});


/******************************************************************************************/
// admin: confirm deletion

$('a.delete').click(function() {
	var str = "Delete this ending? [Deleting is permanent and cannot be undone]";
	return confirm(str);
});


/******************************************************************************************/
// on ending submission

$('.upload form').submit(function() {
	// disable submit button
	$(this).find('input[type=submit]').attr('disabled', 'disabled');

	// wait before displaying uploading message (to avoid jumpy behaviour)
	var form = $(this);	
	setTimeout(function() {
		$('.upload .nav div span:eq(1)').removeClass('selected');
		$('.upload .nav div span:eq(2)').addClass('selected');

		$('.upload .upload_message').addClass('show');

		form.hide();
	}, 1000);
});


/******************************************************************************************/
// get browser window size then get corresponding resized image from server

$('.type_video .fs_flash').each(function() {
	var width = $(this).width();
	var height = $(this).height();

	var max_width = $(window).width() - (12 * 2);
	var max_height = $(window).height() - 110 - 57;
	
	var ratio_width = width / max_width;
	var ratio_height = height / max_height;
	
	var new_width = 0;
	var new_height = 0;
	
	if (ratio_width > ratio_height)
	{
		new_width = max_width;
		new_height = height / ratio_width;
	}
	else
	{
		new_height = max_height;
		new_width = width / ratio_height;
	}
		
	$(this).width(new_width);
	$(this).height(new_height);
	$('.type_video .container').width(new_width + 2);
});


/******************************************************************************************/
// Flash

if($.flashPlayerVersion[0] >= 10)
{
	// splash page: to html version links
	$('#splash a.low_bw_en').click(function(){
		$.cookies.set('html', 'true');
		window.location = 'en';
	});
	
	$('#splash a.low_bw_fr').click(function(){
		$.cookies.set('html', 'true');
		window.location = 'fr';
		return false;
	});

	// splash page: to Flash version link
	$('#splash a.flash').click(function(){
		$.cookies.del('html');
	});

	var flash_activated = ($.cookies.get('html') == 'undefined' || $.cookies.get('html') == '' || $.cookies.get('html') === null);

	// footer: toggle Flash link
	var message_flash_on = 'Enhanced version';
	var message_flash_off = 'Accessible version';

	if(lang == 'fr')
	{
		message_flash_on = 'Version animée';
		message_flash_off = 'Version accessible';
	}
	
	if(flash_activated)
	{
		$('#footer a.flash').empty().append(message_flash_off).addClass('html');
		$('.accessible_only').remove();
	}
	else
	{
		$('#footer a.flash').empty().append(message_flash_on).removeClass('html');
	}
	
	$('#footer a.flash').attr('href', '');
	
	$('#footer a.flash').click(function(){
		if($(this).hasClass('html'))
		{
			$.cookies.set('html', 'true');
		}
		else
		{
			$.cookies.del('html');
		}
		
		// redirect to right Flash page if necessary or just reload
		redirect_to_flash(true);
		
		return false;
	});
	
	$('.activate_flash_view_video').each(function(){
		var is_audio = $(this).hasClass('view_audio');
		
		var message = '';
		if (lang == 'en')
		{   
			if(is_audio)
			{
				message = '<a href="" class="activate_flash">Hear audio</a><br />';				
			}
			else
			{
				message = '<a href="" class="activate_flash">View video</a><br />';				
			}
			message += '<em>Note: This will take you to the "Enhanced version" of the site, which employs Flash Player.</em>';
		}
		else
		{
			if(is_audio)
			{
				message = '<a href="" class="activate_flash">Écouter</a><br />';				
			}
			else
			{
				message = '<a href="" class="activate_flash">Voir la vidéo</a><br />';
			}
			message += '<em>Note : vous passerez à la "Version animée" du site utilisant Flash Player.</em>';
		}
		
		$(this).html(message);
		$('.activate_flash_view_video').click(function(){
			$.cookies.del('html');			
			redirect_to_flash(true);
		});		
	});
	
	// insert Flash components
	if(flash_activated)
	{
		// redirect to right Flash page if necessary
		redirect_to_flash(false);

		$('.art_gallery_flash').each(function(){
			$(this).empty();
			
			// take right column's full width
			$(this).css('width','750px');
			$(this).css('margin-left','-20px');

			$(this).flash(
				{
					swf: '../flash/photoGallery.swf',
					flashvars:
					{
						xml_file: '../' +  lang + '/xml/art_gallery',
						css_file: '../css/gallery.css',
						section_type: 'art',
						lang: lang
					},
					width: 750,
					height: 473
				}
			);

		});

		$('.fs_flash').each(function(){

			// get xml file path
			var xml_file = $(this).children('.xml_file').attr('href');
			
			$(this).flash(
				{
					swf: '../../../flash/listings.swf',
					flashvars:
					{
						xml_file: xml_file,
						player_skin: '../../../flash/SkinUnderPlayStopSeekCaptionVol.swf',
						section_type: 'endings',
						full_screen: 'true',
						lang: lang
					},
					width: $(this).width(),
					height: $(this).height()
				}
			);

		});


		$('.animation_flash').each(function(){
			$(this).empty();
			$(this).flash(
				{
					swf: '../flash/listings.swf',
					flashvars:
					{
						xml_file: '../' +  lang + '/xml/animation',
						player_skin: '../flash/SkinUnderPlayStopSeekCaptionVol.swf',
						section_type: 'animation',
						lang: lang
					},
					width: 730,
					height: 475
				}
			);

		});

		$('.story_ending_list').each(function(){
			var ending_id = "";
			var uri = decodeURI(window.location);    
			if (uri.indexOf("#") >= 0)
			{
				ending_id = uri.substring(uri.indexOf("#") + 1);
				ending_id = ending_id.replace('n', '');   
			}
			
			$(this).empty();
			$(this).flash(
				{
					swf: '../../flash/listings.swf',
					flashvars:
					{
						xml_file: '../../ ' +  lang + '/xml/ending',
						player_skin: '../../flash/SkinUnderPlayStopSeekCaptionVol.swf',
						section_type: 'endings',
						lang: lang,
						id: ending_id
					},
					width: 730,
					height: 473
				}
			);

		});

		$('.who_photo_bio_flash').each(function(){
			$(this).empty();
			
			// take right column's full width
			if($.hasFlashPlayer)
			{	
				$(this).css('width','750px');
				$(this).css('margin-left','-20px');
			}

			$(this).flash(
				{
					swf: '../flash/photoGallery.swf',
					flashvars:
					{
						xml_file: '../' +  lang + '/xml/who_bio',
						css_file: '../css/gallery.css',
						section_type: 'who',
						lang: lang
					},
					width: 750,
					height: 473
				}
			);

		});

		$('.who_perspectives_flash').each(function(){
			var video_id = "";
			var uri = decodeURI(window.location);    
			if (uri.indexOf("#") >= 0)
			{
				video_id = uri.substring(uri.indexOf("#") + 1);
				video_id = video_id.replace('n', '');   
			}

			$(this).flash(
				{
					swf: '../../flash/listings.swf',
					flashvars:
					{
						xml_file: '../../' +  lang + '/xml/who_perspectives',
						player_skin: '../../flash/SkinUnderPlayStopSeekCaptionVol.swf',
						section_type: 'perspectives',
						lang: lang,
						id: video_id
					},
					width: 730,
					height: 473
				}
			);

		});

		$('.who_in_his_own_words_flash').each(function(){
			var video_id = "";
			var uri = decodeURI(window.location);    
			if (uri.indexOf("#") >= 0)
			{
				video_id = uri.substring(uri.indexOf("#") + 1);
				video_id = video_id.replace('n', '');   
			}

			$(this).flash(
				{
					swf: '../../flash/listings.swf',
					flashvars:
					{
						xml_file: '../../' +  lang + '/xml/who_in_his_own_words',
						player_skin: '../../flash/SkinUnderPlayStopSeekCaptionVol.swf',
						section_type: 'inhisownwords',
						lang: lang,
						id: video_id
					},
					width: 730,
					height: 473
				}
			);

		});

		$('.audio').each(function(){
			var audio_id = $(this).attr('id');

			$(this).append('<div class="audio_flash"></div>');

			$(this).find('.audio_flash').flash(
				{
					swf: '../../../flash/audioplayer.swf',
					flashvars:
					{
						playerID: "'" + audio_id + "'",
						soundFile: '../../../files/lesson_plan/audio/' + audio_id,
						quality: 'high',
						menu: false,
						lefticon: '0xFFFFFF',
						righticon: '0xFFFFFF',
						leftbg: '0x357CCE',
						rightbg: '0x32BD63',
						rightbghover: '0x2C9D54',
						wmode: 'transparent'
					},
					height: 30
				}
			);

		});

		$('.fs_flash_audio').each(function(){

			var xml_file = $(this).children('.xml_file').attr('href');

			$(this).flash(
				{
					swf: '../../../flash/audioplayer_admin.swf',
					flashvars:
					{
						xml_file: xml_file,
						player_skin: '../../../flash/SkinUnderPlayStopSeekMuteVol.swf',
						section_type: 'audio'
					},
					width: 490,
					height: 115
				}
			);

		});
		
		$('.small_raven_flash').each(function(){
			$(this).flash(
				{
					swf: '../../../flash/raven3.swf',
					width: 50,
					height: 40
				}
			);
		});
		
		$('.inline_video_container .video_title').wrapInner('<a href="" class="inline_video"></a>');
		
		$('a.inline_video').click(function(){
			var base_url = '../../';
			var file_name = 'PH02.flv';
			
			var inline_popup = $('<div class="inline_popup"><div class="close_container"><a class="close" href="#">CLOSE</a></div><div class="contents"></div></div>');
			$('.text_contents').append(inline_popup);

			var top = $(this).offset().top - 365;
			
			$('.inline_popup').css({
				top:	top
			});
			
			$('.inline_popup .contents').flash({
				swf: base_url + 'flash/listings.swf',
				flashvars:
				{
					xml_file: base_url + '' +  lang + '/xml/art_guided_journey_video/' + file_name,
					player_skin: base_url + 'flash/SkinUnderPlayStopSeekCaptionVol.swf',
					section_type: 'endings',
					full_screen: 'true',
					lang: lang
				},
				width: 670,
				height: 475,
				wmode: 'opaque'
			});
			 

			$('.inline_popup a.close').click(function(){
				$('.inline_popup').hide('slow', function(){					
					$(this).remove();
				});
				return false;
			});

			$('.inline_popup').show('slow');
			
			return false;
		});
	}
	
	$('.audio_player object').each(function(){
		var id = $(this).attr('id');
		var base_url = $(this).find('param[name=base_url]').attr('value');
		var file_path = $(this).find('param[name=file_path]').attr('value');

		$(this).after('<div class="audio_flash"></div>');
		
		$(this).next('.audio_flash').flash({
				swf: base_url + 'flash/audioplayer.swf',
				flashvars:
				{
					playerID: "'" + id + "'",
					soundFile: file_path,
					quality: 'high',
					menu: false,
					lefticon: '0xFFFFFF',
					righticon: '0xFFFFFF',
					leftbg: '0x357CCE',
					rightbg: '0x32BD63',
					rightbghover: '0x2C9D54',
					wmode: 'transparent'
				},
				height: 30
		});
		
		$(this).remove();
	});
	
	
}
/******************************************************************************************/
// set #nav_container height based on contents height

$('#nav_container').css('min-height', $('#contents').height() + 'px');


/******************************************************************************************/
// Safari 3 fix: prevent from crashing when going from / to /en for the fisrt time
$('#splash').append('<h6>&nbsp;</h6>');

});


