

jQuery(function($){
	// AJAX enable in-line gallery module
	$('.moduleType_Images:has(.type-inline)').each(function(index,section){ // all inline sections
		$('.gallerylist li a',section).click(function(){
			thumbpath = $('img',this).attr('src');
			imgID = thumbpath.substring(thumbpath.lastIndexOf("_")+1);
			imgID = imgID.substring(0,imgID.indexOf('.'));
			//console.log(imgID);
			$('.image-inline',section).attr('src','/UserFiles/Image/images_full_'+imgID+'.jpg')
			$('.image-inline',section).attr('alt',$('img',this).attr('alt'))
			$('.gallerylist li.selected',section).removeClass('selected');
			$(this).parent().addClass('selected');
			
			$('.image-description',section).load($(this).attr('href')+' #'+$(section).attr('id')+' .image-description p' );
			if((typeof pageTracker)!="undefined")
			{
				// don't neglect analytics!
				pageTracker._trackPageview($(this).attr('href'));
			}
			return false;
		});
	});
	
	
	//homepage quotes
	$('.quoteItem .logo').click(SelectQuoteItem);
	$('.quoteItem .logo').click(function(){clearInterval(QuotesInterval);});
	function SelectQuoteItem()
	{
		$('.quoteItem .quote').fadeOut();
		$('.quoteItem').removeClass("selected");
		$('.quote',$(this).parent()).fadeIn();
		$(this).parent().addClass("selected");
		return false;
	}
	var QuoteItemIndex = 1;
	var QuotesInterval = setInterval(function(){
		SelectQuoteItem.call($('.quoteItem .logo')[QuoteItemIndex]);
		QuoteItemIndex=QuoteItemIndex==3?0:QuoteItemIndex+1;
	},5000);
	
	
	
	$('#sectionId_213').each(function(){
		function AboutBalloon(text, latLng, map) {
			this.text_ = text;
			this.latLng_ = latLng;
			this.map_ = map;
			this.div_ = null;
			this.setMap(map);
		}
		AboutBalloon.prototype = new google.maps.OverlayView();
		AboutBalloon.prototype.draw = function() {
			var overlayProjection = this.getProjection();
			var pos = overlayProjection.fromLatLngToDivPixel(this.latLng_);

			var div = this.div_;
			div.style.left = (pos.x-13) + 'px';
			div.style.top = (pos.y - div.offsetHeight) + 'px';
		};
		AboutBalloon.prototype.onAdd = function() {

			// Create the DIV and set some basic attributes.
			var div = document.createElement('DIV');
			jdiv = $(div);
			jdiv.addClass("AboutBalloon");
			jdiv.html(this.text_);
			overlay = this;
			$(".msg",div).load("/lastfm/default.aspx",function(){
				overlay.draw();
				map.panBy((div.offsetWidth/2)-13, 0-(div.offsetHeight/2));
			});
			
			this.div_ = div;
			var panes = this.getPanes();
			panes.floatPane.appendChild(div);
		}
		
		aryCoords = $('div[title]',this).attr("title").split(",");
		htmlText = $('div[title]',this).html();
		var latlng = new google.maps.LatLng(aryCoords[0], aryCoords[1]);
		var myOptions = {
			zoom: 14,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.SATELLITE,
			backgroundColor: "#D61410",
			mapTypeControl: false,
			navigationControl: false,
			streetViewControl: false,
			scaleControl: false
		};
		var map = new google.maps.Map(this, myOptions);
		overlay = new AboutBalloon(htmlText, latlng, map);
		
		/*
		izoom = 2;
		map.setZoom(izoom);
		zoomr = setInterval(function(){
			map.setZoom(izoom);
			map.panTo(latlng);
			map.panBy((overlay.div_.offsetWidth/2), 0-(overlay.div_.offsetHeight/2));
			izoom+=2;
			if(izoom>14)
				clearInterval(zoomr);
		},900);
		*/
	});
	
	// load tweets on phil/thom pages
	$('.TwitterMessage').hide();
	function LoadDirectorTweets(userid)
	{
		$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?user_id='+userid+'&count=5&callback=?',
		function(data){
			for(x=0;x<data.length;x++){
				if(data[x].in_reply_to_user_id_str==null && data[x].text.substring(0,3) != 'RT ')
				{
					
					$('.TwitterBody').html(data[x].text
						.replace(/</g,'&lt;')
						.replace(/(http:\/\/[^\s]+)/g,'<a href="$1">$1</a>')
						.replace(/(\W)@(\w+)/ig,'$1<a href="http://twitter.com/$2">@$2</a>')
					);
					// stripping out the timezone fixes the parsing errors in IE, but does mean this will only work in uk timezone
					$('.TweetedAt').text('').text($.timeago(new Date(data[x].created_at.replace(' +0000',''))));
					$('.TwitterMessage').fadeIn();
					x = data.length;
				}
			}
		});
	}
	if($('body').hasClass('currentpage-about_thom-shannon'))
	{
		LoadDirectorTweets('761089');
	}
	else if($('body').hasClass('currentpage-about_phil-blything'))
	{
		LoadDirectorTweets('777715');
	}
	
	// embed tate game on homepage
	$('#sectionId_4').each(function(){
		$('#sectionId_4 .featuresListItem:eq(3)').prepend('<div id="tateflash"></div>');
		swfobject.embedSWF("/templates/default/tate.swf", "tateflash", "980", "320","9.0.0",
			null, null, {wmode:"transparent"}, null, null);
		if (swfobject.hasFlashPlayerVersion("9.0.0")) {
			$('#sectionId_4 .featuresListItem:eq(3) img').remove();
		}
	});
	
	setTimeout(function(){
		$('.page-portfolio .featuresList').css("width","598px");
	},200);
	
});

$(function() {

	//contact form post messages
	if ($("#sectionId_19 #p_ctl13_lblThanks").length > 0)
	{
		$("#sectionId_19 h3.sectionTitle").hide();
		
		var num = $("#sectionId_216 div.snip").length;
		var rnd = Math.random();
		var divider, divider2;
		
		for (var i = 0; i < num; i++)
		{
			divider = (i == 0) ? 0 : (i/num);
			//divider2 = (i == num) ? num : ;
			
			if (rnd > divider && rnd <= ((i+1)/num))
			{
				$("#sectionId_216 div#snip"+(i+1)).show();
			}
		}
	}

});


