$(function(){

	$slider = $('#brand_slider');
	$video = $('#video_wrapper');

	// CSS columns for IE etc

	if(! Modernizr.csscolumns)
	{
		$('#brand_list').columnizeList({
		    columnCount: 3,
		    columnGap: 0
		});
	}

	// Equalise column heights in the blog
	// (ensures the dotted border goes all the way to the bottom if the content is short and that's about it)

	$blog_content = $('#blog_content');
	$sidebar = $('#sidebar');

	if($blog_content.length > 0 && $sidebar.height() > $blog_content.height())
	{
		$blog_content.height($sidebar.height() - 10);
	}

	// Page content should align with the bottom of its parent div, apparently
	
	$page_content = $('#page_content');
	$copy = $('.copy', $page_content);
	
	if($page_content.length > 0 && $copy.height() < $page_content.height() && !$page_content.hasClass('centered'))
	{
		var top_margin = $page_content.height() - $copy.height() - 15;
		$copy.css('marginTop', top_margin);
	}

	// Find us Google map
	if($('#map_container').length > 0)
	{
		var latlng = new google.maps.LatLng(51.479457,-0.179332);
		var myOptions = {
			zoom: 14,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map_container"), myOptions);
		
		var marker = new google.maps.Marker({
			position: latlng,
			animation: google.maps.Animation.DROP,
			title:"RDA Offices"
		});
		
		marker.setMap(map);  
	}

	// Brands slider
	$(window).load(function() {
		if($slider.length > 0)
		{
		   	$slider.nivoSlider({
				effect: 'fade',
				manualAdvance: true,
				directionNav: false,
				controlNavThumbs: true,
				controlNavThumbsSearch: '514x369',
				controlNavThumbsReplace: '123x87',
				afterChange: function(){
					toggle_video();
				},
				afterLoad: function(){
					toggle_video();
				}
		    });
		}
	});

	// Show/hide the video depending on which nivo slide is active	
	function toggle_video() {

		current_slide = $slider.data('nivo:vars').currentSlide;

		if($('img', $slider).eq(current_slide).hasClass('video_thumb'))
		{
			$video.show();
			$('.nivo-box, .nivo-slice', $slider).hide();
		}
		else
		{
			$('.nivo-box, .nivo-slice', $slider).show();
			$video.hide();
		}
	}

	// Build a <source> array to use as a playlist for projekktor. Why? Because IE seems to be too dumb to locate them on its own.
	video_sources = new Object;
	video_sources[0] = new Object;

	$('#video_wrapper source').each(function(index) {

		video_sources[0][index] = {
			src: $(this).attr('src'),
			type: $(this).attr('type')
		}
	});

	// Spangly HTML5 video for you, you and you
	if($video.length > 0)
	{
		setup_vid = function() {
			rda_vid = new projekktor('video', {
				playerFlashMP4: 'http://local.rda.tv/static/js/projekktor/jarisplayer.swf',
				width: 514,
				height: 369,
				volume: 0.6,
				playlist: video_sources
			});
		}
		setTimeout('setup_vid()', 5); /* This minute delay fixes poster positioning glitches in IE */
	}

	// In IE objects are showing through in full screen and no amount of z-index tweaking seems to help.
	// Workaround: Hide the problem elements when full screen is active.

	var toggle_fullscreen = function(data) {

		var $problems = $('footer #right_content');

		if(data === true)
		{
			$problems.hide();
		}
		else
		{
			$problems.show();
		}
	}

	if(typeof rda_vid != 'undefined')
	{
		rda_vid.addListener('fullscreen', toggle_fullscreen);
	}

});
