
	//IE6 background image flicker
	try { document.execCommand( "BackgroundImageCache", false, true); } catch(e) {};

	$(function(){
		Site.init();
	});
	
	var Site = {
		init: function(){
			/* Get defauilt for back link on videoplayer*/
			
			var video = Site.embedvideo('../AttachmentsByTitle/features/$FILE/measure_up.mp4','../AttachmentsByTitle/features/$FILE/default.jpg');
			$('#features .selected').html(video);
			Site.defaultFeature = video;
			Site.defaultTitle = $('h3.featurebar1').html();
			Site.curClicked = false;
			Site.polaroidTimer = false;
			
			/* Attach click functionality to Homepage Features */
			$('#features a').click(function(){
				Site.feature(this);
				return false;
			});
			/* Resize the #content bar on subpages to be minheight of the navbar */
			$('#content').not('.homepage').each(function(){
				var $c = $(this);
				var $nav = $('#navigation');
				if ($nav.height() > $c.height()) {
					$c.css('height',$nav.height()+20+'px'); //add 20 for paddings
				}
			});
			
			$('a.polaroid').mouseover(function(){
				clearTimeout(Site.polaroidTimer);
				if ($('#polaroid_clone').length) {
					return;
				}
				
				var $p = $(this);
				$p.css('position','relative');
				
				var $copy = $('<div/>').attr('id','polaroid_clone').css({
					zIndex: 50,
					position: 'absolute',
					top: 0,
					left: 0,
					width: $p.width()+'px',
					height: $p.height()+'px',
					backgroundPosition: '259px 0px',
					backgroundImage: $p.css('background-image')
				});
				
				$(this).prepend($copy);
				
				$copy.hide().fadeIn('fast');
				
			}).mouseout(function(){
				clearTimeout(Site.polaroidTimer);
				Site.polaroidTimer = setTimeout(function(){
					$('#polaroid_clone').fadeOut('fast',function(){
						$(this).remove();
					});
				},100);
			});
			
			
		},
		feature: function(item) {
			
			var backlink = $('<a></a>').attr({href: '#'}).addClass('vidback').html('<span>Watch advertisement</span>').click(function(){
				$('#features .selected').html(Site.defaultFeature);
				$('h3.featurebar1').html(Site.defaultTitle);
				Site.curClicked = false;
				return false;
			});
			
			//get the id of the item clicked. either b1, b2 or b3
			var id = $(item).parents('li:first').attr('class');
			id = id.split(' ');
			id = id[0];
			if (Site.curClicked == id) {
				return;
			}
			Site.curClicked = id;
			
			
			var title = $(item).attr('title');
			$('h3.featurebar1').html(title).prepend(backlink);
			var url = $(item).attr('href');
			var img = '../AttachmentsByTitle/features/$FILE/' + id + '.png';
			
			var video = Site.embedvideo(url,img);
			
			/* Show new content */
			$('#features .selected').fadeOut('fast',function(){
				$(this).html('').html(video).show(); /* Needs to be a show, not a fade in, IE has 'issues' */
			});
			
			/* Put class 'on' on the clicked item, remove form others. */
			$('.on',$(item).parents('ul:first')).removeClass('on');
			$(item).parents('li:first').addClass('on');
			
		},
		embedvideo:function(url,img) {
			/* Dont use HTML5 for IE < 9, fallback to Flash. Embed for Flash 10 minimum */
			if ($.browser.msie && parseInt($.browser.version) < 9) {
				var video = '<object name="player" id="player" width="426" height="248" type="application/x-shockwave-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" data="../AttachmentsByTitle/features/$FILE/player.swf?file='+url+'&amp;controlbar=none&amp;image='+img+'"><param name="movie" value="../AttachmentsByTitle/features/$FILE/player.swf?file='+url+'&amp;controlbar=none&amp;image='+img+'" /></object>';
			} else {
				/* Lets use some HTML5, enables video in iphone etc */
				/* OGV is required for Firefox and HTML5 */
				var ogv = url.replace(/\.[^.]*$/i,'.ogv');
				var video = '<video poster="'+img+'" width="426" height="248" controls><source src="'+ogv+'" type="video/ogg"/><source src="'+url+'" type="video/mp4"/><style type="text/css" media="screen"></video>';
			}
			
			return video;
		}
	};
	
	

	

