var activeplayer = null;
var activemedia = null;

$(document).ready(function(){
	
	// input boxes
	// search
	$('#query').focus(function(){
		if (this.value == 'Search') {
			this.value = '';
		}
	});
	$('#query').blur(function(){
		if (this.value == '') {
			this.value = 'Search';
		}	
	});
	
	// email signup
	$('#email').focus(function(){
		if (this.value == 'Your email address') {
			this.value = '';
		}
	});	
	
	$('#email').blur(function(){
		if (this.value == '') {
			this.value = 'Your email address';
		}
	});	
	//search results
	
	
	// print
	var print = $('div.description div.rc');
	if (print.length > 0) {
		if (print.hasClass('printlink')) {
			print.prepend('<ul><li><a href="javascript:window.print();">Print this page</a></li></ul>');
		}
	}
	// tables
	var tables = $('div.table tr')
	var sResults = $('div.searchResults div.item');
	var exlhead = null;
	if (tables.length > 0) {
		exlhead = tables.slice(1,tables.length);
		exlhead = exlhead.add(sResults);
	} else if (sResults.length > 0) {
		exlhead = sResults;
	}
	if (exlhead != null) {
		if (exlhead.length > 0) {
			exlhead.hover(function(){
				$(this).addClass('over');
			},function(){
				$(this).removeClass('over');
			});
			exlhead.click(function(me){
				me.preventDefault();
				window.document.location = $(this).find('a').attr('href');
				return false;
			});
		}
	}	
	
	// works page short / long	
	var workd = $('div.workcontent');
	if (workd.length > 0) {
		var contentps = workd.find('p').not('.small, .synopsis');
		var related = $('div.rc div.item');
		// look at the details table
		// check the hight of the first four entries... if that's higher then that's the short target
		// add the other entries to related
		var details = $('table.detailstable tr');
		var cnt = 1;
		var detailsh = 0;
		var detailsth = 0;
		details.each(function(){
			if (cnt < 4) {
				detailsh += $(this).height();
			} else {
				//console.log($(this));
				var ob =$(this);
				related = related.add(ob);
			}
			detailsth += $(this).height();
			cnt++;
		});
		detailsh += 10;
		detailsth += 10;
		
		$('div.workcontent .rc h3').each(function(){
			detailsth += $(this).height()+5+10;
		});
		$('div.workcontent .rc p').each(function(){
			detailsth += $(this).height()+10;
		});
		$('div.workcontent .rc ul').each(function(){
			detailsth += $(this).height();
		});
		$('div.workcontent .rc div.item').each(function(){
			detailsth += 20;
		});
		var slength = workd.find('p.small').height() + workd.find('p.synopsis').height();
		var contentl = slength;
		contentps.each(function(){
			contentl += $(this).height();
		});
		
		if (detailsh > slength) {
			var shorth = detailsh;		
		} else {
			var shorth = slength;
		}

		if (detailsth > contentl) {
			var longh = detailsth;		
		} else {
			var longh = contentl;
		}
		// make short
		workd.css('height',shorth+'px');
		workd.css('overflow','hidden');
		contentps.hide();
		//console.log(related);
		related.hide();

		// make bar
		workd.after('<div class="details" id="workbar"><p><a href="#">Hide full details, documents and links</a></p></div>')
		var bar = $('div#workbar a');
		bar.html('Show full details, documents and links');
		bar.click(function(me){
			me.preventDefault();
			if ($(this).hasClass('active')) {
				$(this).removeClass('active');
				$(this).html('Show full details, documents and links');
				workd.css('overflow','hidden');
				contentps.hide();
				related.hide()
				workd.animate({height:shorth+'px'},200);		
			} else {
				$(this).addClass('active');
				$(this).html('Hide full details, documents and links');
				workd.animate({height:longh+'px'},200,function(){
					workd.css('overflow','visible');
					contentps.show();
					related.show();
				});
				
			}
			return false;
		});
	
	}
	
	
	// replace headings
	// but only if we're not in opera
	if (!$.browser.opera) {
		var h1 = $('h1');
		if (h1.html() != '') {
			h1.sifr({build:436,version:3,save:true,forceSingleLine:true,width:955,fontWeight:'normal',fontSize:'40',font:'/layout/swf/helvetica',delayRun:false});
		}
		
		// replace home page text
		var hp = $('.intro .lc. p');
		if (hp.html() != '') {
			hp.sifr({build:436,version:3,save:true,width:hp.width(),height:hp.height()+15,fontWeight:'normal',fontSize:26,leading:'10px',font:'/layout/swf/helvetica',delayRun:false});
		}
	}
	homeVideo();
	
	mediaPlayer();
	
});


function homeVideo() {
	var div = $('#hpflashvideo');
	if (div.length > 0) {
		var img = div.find('img');
		var info = div.parent().find('span.hd').html().split(',');
		var today = new Date();
		var mill = today.getTime();
		//var video = new SWFObject("mainh.swf?"+mill, 'mainh', w, h, '9', '#000000');	
	
		var video = new SWFObject("/layout/flash/mainh.swf?"+mill, 'main', info[1], info[2], '9', '#000000');
		video.addParam("allowFullScreen","true");
		video.addParam("scale", "noScale");
		video.addParam("wmode", "transparent");
		video.addParam("salign", "tl");
		video.addVariable("videourl", info[0]);
		video.addVariable("w", info[1]);
		video.addVariable("h", info[2]);
		video.addVariable("img", img.attr('src'));
		video.write("hpflashvideo");
	}
	
}

function mediaPlayer(){
	var uls = $('div.player ul.contextMenu');
	if (uls.length > 0) {
		uls.hide();
		uls.parent().prepend('<ul class="contextMenu" id="contextMenu"></ul>');
		var contextm = uls.parent().find('#contextMenu');
		var images = null;
		var videos = null;
		var audio = null;
		
		// get all the elements
		uls.each(function(){
			if ($(this).hasClass('video')) {
				var ul = $(this);
				contextm.append('<li id="player_videonav"><a href="#">Video</a></li>');
				
				videos = {ul:ul,elem:[],nav:$('#player_videonav'),type:'video'};
				videos.nav.find('a').click(function(){
					activate(videos);
					return false;
				});
				ul.find('li').each(function(){
					videos.elem.push($(this));
					$(this).find('a').click(function(){
						
						show($(this).parent(),videos);
						return false;
					});
				});
				ul.prev().remove();
			}
			if ($(this).hasClass('images')) {
				var ul = $(this);
				contextm.append('<li id="player_imgnav"><a href="#">Images</a></li>');
				images = {ul:ul,elem:[],nav:$('#player_imgnav'),type:'image'};
				images.nav.find('a').click(function(){
					activate(images);
					return false;
				});
				// we have to include the main image in the list as well.... but only if we have one
				var mimg = $('div.player .lc img');
				if (mimg.length > 0) {
					ul.prepend('<li><a rel="'+mimg.height()+','+mimg.width()+'" href="'+mimg.attr('src')+'">1</a><span class="caption">'+$('div.player .rc p.caption').html()+'</span></li>');
				}
				var cnt = 1;
				ul.find('li').each(function(){
					$(this).find('a').html(cnt);
					images.elem.push($(this));
					$(this).find('a').click(function(me){
						me.preventDefault();
						show($(this).parent(),images);
						return false;
					});
					cnt++;
				});
				ul.prev().remove();
			}
			if ($(this).hasClass('audio')) {
				var ul = $(this);
				contextm.append('<li id="player_audionav"><a href="#">Audio</a></li>');
				audio = {ul:ul,elem:[],nav:$('#player_audionav'),type:'audio'};
				audio.nav.find('a').click(function(){
					activate(audio);
					return false;
				});
				ul.find('li').each(function(){
					audio.elem.push($(this));
					$(this).find('a').click(function(){
						show($(this).parent(),audio);
						return false;
					});
				});
				ul.prev().remove();			
			}
		});
		if (videos != null) {
			activate(videos);
		} else {
			activate(images);
		}

	
	}
}

function activate(which) {
	//console.log(activeplayer);
	if (activeplayer != null) {
		activeplayer.ul.hide();
		activeplayer.nav.removeClass('on');
	}
	activeplayer = which;
	activeplayer.ul.show();
	activeplayer.nav.addClass('on');
	show(activeplayer.elem[0],activeplayer);
	
	// hide previous active one
	// show list
	// display first item

}

function show(which,obj) {
	if (activemedia != null) {
		activemedia.removeClass('on');
	}
	activemedia = which;
	activemedia.addClass('on');
	if (obj.type == 'image') {
		var link = activemedia.find('a');
		// work out width and height
		var dim = link.attr('rel').split(',');
		var oh = dim[0]; var w = dim[1];
		h = oh/w * 475;
		$('div.player .lc').html('<img src="'+link.attr('href')+'" width="475" height="'+h+'" />');
		$('div.player .rc p.caption').html(activemedia.find('span.caption').html());
	}
	if (obj.type == 'video') {
		swfobject.switchOffAutoHideShow();
		var link = activemedia.find('a');
		var today = new Date();
		var mill = today.getTime();
		var dim = link.attr('rel').split(',');
		var w = dim[0]; var oh = dim[1];
		h = Math.round(oh/w * 475);
		var flv = link.attr('href');
		$('div.player .lc').html('<div id="player_video">&nbsp;</div>');
		$('div.player .rc p.caption').html(activemedia.find('span.caption').html());
		
		/*var video = new SWFObject("/layout/flash/main.swf?"+mill, 'main', 475, h, '9', '#000000');
		video.addParam("allowFullScreen","true");
		video.addParam("scale", "noScale");
		video.addParam("salign", "tl");
		video.addVariable("videourl", flv);
		video.addVariable("w", 475);
		video.addVariable("h", h);
		// need to add an image preview to the video box for podcasting and for the video output here
		video.addVariable("img", dim[2]);
		var videos = video.write("player_video");
		if (videos == false) {
			$('div#player_video').html('We could not detect the flash plugin. <Please download and install the flash plugin.');
		}*/
		
		if ($.browser.opera) {
		
			var flashvars = {
				videourl: flv,
				w: "475",
				h: h,
				img: dim[2]
			};
			var params = {
				allowFullScreen: "true",
				scale: "noScale",
				salign: "tl",
				bgcolor: "#525252"
			};
			
			swfobject.embedSWF("/layout/flash/main.swf?"+mill, "player_video", "475", h, "9.0.124","/layout/flash/expressInstall.swf", flashvars, params);

		
		} else {
			var qth = h+16;
			
			var video_html = '<object id="swfContent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+475+'" height="'+h+'"><param name="movie" value="/layout/flash/main.swf?'+mill+'" /><param name="allowFullScreen" value="true" /><param name="bgcolor" value="#525252" /><param name="scale" value="noScale" /><param name="salign" value="tl" /><param name="flashvars" value="videourl='+flv+'&w='+475+'&h='+h+'&img='+dim[2]+'" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="/layout/flash/main.swf?'+mill+'" width="'+475+'" height="'+h+'"><param name="allowFullScreen" value="true" /><param name="bgcolor" value="#525252" /><param name="scale" value="noScale" /><param name="salign" value="tl" /><param name="flashvars" value="videourl='+flv+'&w='+475+'&h='+h+'&img='+dim[2]+'" /><object type="video/mp4" data="'+flv+'" width="'+475+'" height="'+qth+'"><!-- QuickTime, iPhone --><param name="controller" value="true" /><param name="src" value="'+dim[2]+'" /><param name="href" value="'+flv+'" /><param name="target" value="myself" /><!--<![endif]--><img src="'+dim[2]+'" alt="Video content" /><!-- alternative content --><!--[if !IE]>--></object></object><!--<![endif]--></object>';
			$('div#player_video').html(video_html);
			swfobject.registerObject("swfContent", "9.0.124","/layout/flash/expressInstall.swf");
		}
		

		
	
	}
	if (obj.type == 'audio') {
		var link = activemedia.find('a');
		var today = new Date();
		var mill = today.getTime();
		var time = link.attr('rel');
		var mp3 = link.attr('href');
		$('div.player .lc').html('<div id="player_audio"></div>');
		$('div.player .rc p.caption').html(activemedia.find('span.caption').html());

		/*var video = new SWFObject("/layout/flash/audio.swf?"+mill, 'audio', '475', '353', '9', '#DBE1DD');
		video.addParam("allowFullScreen","true");
		video.addParam("scale", "EXACT_FIT");
		video.addParam("salign", "tl");
		video.addVariable("audioTrack", mp3);
		video.addVariable("audioDuration", time);
		video.write("player_audio");
		
		var qth = h+16;*/
		
		var audio_html = '<object id="swfContent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+475+'" height="'+353+'"><param name="movie" value="/layout/flash/audio.swf?'+mill+'" /><param name="allowFullScreen" value="true" /><param name="scale" value="EXACT_FIT" /><param name="salign" value="tl" /><param name="flashvars" value="audioTrack='+mp3+'&audioDuration='+time+'" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="/layout/flash/audio.swf?'+mill+'" width="'+475+'" height="'+h+'"><param name="allowFullScreen" value="true" /><param name="scale" value="EXACT_FIT" /><param name="salign" value="tl" /><param name="flashvars" value="audioTrack='+mp3+'&audioDuration='+time+'" /><object type="audio/mp3" data="'+mp3+'" width="'+475+'" height="'+16+'"><!-- QuickTime, iPhone --><param name="controller" value="true" /><param name="href" value="'+mp3+'" /><param name="target" value="myself" /><!--<![endif]--><p>Audio content cannot be displayed.</p><!-- alternative content --><!--[if !IE]>--></object></object><!--<![endif]--></object>';
		$('div#player_audio').html(audio_html);
		swfobject.registerObject("swfContent", "9.0.124");
	
	}
	//console.log(which);
	//console.log(obj.type);
}


