if (top.location!= self.location) {top.location = self.location.href}

(function()
{
var path = window.location.pathname;
if(path.search(/\./i)==-1 && path!='/')
	{
	//alert(path.search(/\./i));
	window.location = 'http://trinityfm.org/#!'+window.location.pathname
	//alert(move);
	}
})();

hotPlug = new Object;
$(document).ready(function(){setTimeout(function()
{

/*$(window).resize(function(){
	navHeight = $('#navwrap').height();
	windowHeight = $(window).height();

	$('#bodywrap').height(windowHeight - navHeight);

}).resize();*/

$.ajax({
	url:'/js/jquery.history.js',
	success:pageLoader
	});
$.ajax({
	url:'/js/twitterFeed.js',
	success:function(data)
		{
		if(hotPlug['twitterFeed']!=true)
			{
			eval(data);
			hotPlug['twitterFeed'] = true;
			}
		}
	});

$('#player').data('iframe',$('#player').html())
		.click(function()
			{
			$(this).html($(this).data('iframe'));
			//alert("We're sorry, this isn't quite set up yet... Please try again later");
			$(this).unbind('click');
			return false;
			})
		.html($('<a href="#">Tune in</a>'))

		
},100)});


function runToggle()
	{
	$('.toggle').each(function()
		{
		if($(this).attr('toggleRan')!=1)
			{
			var rights = $(this).children('li').children('.right').hide();
			
			var right0 = rights.eq(0);
					
			var toggleTarget = $('<div>'+right0.html()+'</div>').addClass('right')
				.css({marginTop:20})

			$(this).after(toggleTarget)
			.css({width:$(this).children('li:first-child').children('h2').width(),float:'left'})
			.children().children('h2').hover(function(){$(this).css('cursor','pointer')},function(){$(this).css('cursor','auto')})
				.click(function()
				{
				toggleTarget.html($(this).next().html());
				});
			$(this).attr('toggleRan',1);
			}	
		})
	}
	

function pageLoader(historyData)
	{
	if(hotPlug['history']!==true)
		{
		eval(historyData);
		hotPlug['history'] = true;
		}

	$('#nav li > a').not('#player a')
		.each(function(index,element)
			{
			elHref = $(element).attr('href');
			elHref = '#!' + elHref;
			$(element).attr('href', elHref);
			});
	
	$.history.pages = {
		main:$('#contentMain'),
		current:'home'
		};

	$.history.pages['home'] = $('<div></div>')
		.css({
			width:$.history.pages['main'].parent().width()
			})
		.append($.history.pages['main'].clone().removeAttr('id'));
	$.history.pages.proto = $.history.pages['home'].clone();
	$.history.pages.proto.children(':first-child').empty()
	$.history.pages['swap'] = $.history.pages['home'].clone().hide();
	$.history.pages['main'].empty().removeAttr('class')
		.append($.history.pages['home'])
		.append($.history.pages['swap']);
	
	$.history.pages['home'].data('title',document.title);
	
	$.history.init(function(hash)
		{
		var B = this;
		hash = hash.replace('!/', '');
		
		if(hash == "")
			{
			hash = 'home';
			}
								
		if(typeof B.pages[hash] === 'undefined')
			{
			B.pages[hash] = $.history.pages.proto.clone().hide()
			B.pages.main.append(B.pages[hash]);
			
			thisUrl = '/index.php?page='+hash+'&ajax=1';
			//alert('ajax call');
			$.ajax({
				url:thisUrl,
				success:function(data)
					{
					data = $.parseJSON(data);
					B.pages.main.children().hide();
					B.pages[hash].children(':first-child').html(data.body);
					
					B.pages.swap.show().contentSwap(B.pages[hash].html(),function()
						{
						//alert('ajax swap callback');
						B.pages.main.children().hide();
						B.pages[hash].show();
						//alert(B.pages[hash].parent().html());
						});
					
					B.pages[hash].data('title',data.title);
					document.title = data.title;
					B.pages.current = hash;
					
					runToggle(B.pages[hash]);
					}
				});
			}
		else
			{
			if(hash!=B.pages.current)
				{
				//alert('preloaded call');
				$('#contentMain').children().hide();
				
				B.pages.swap.show().contentSwap(B.pages[hash].html(),function()
					{
					//alert('preloaded swap callback');
					B.pages.main.children().hide();
					B.pages[hash].show();
					});

				document.title = B.pages[hash].data('title');
				B.pages.current = hash;
				}
			}
		B.pages.current = hash;
		}, { unescape: ",/" });
	}

(function($){
$.fn.contentSwap = function(data, callback)
	{
	var B = $(this);
	contentSwapVars = {running:0};
	var contentSwapOld = B.data('contentSwapVars');
	$.extend(contentSwapVars,contentSwapOld);
	if(contentSwapVars['running']==1)
		{
		setTimeout(function(){id1.contentSwap(data)}, 1000)
		return this;
		}
	contentSwapVars['running'] = true;
	B.data('contentSwapVars',1);
	
	function callbackFn()
		{
		if(typeof callback === 'function')
			{
			return callback();
			}
		else
			{
			return false;
			}
		}

	id1 = this;
					
	var prevCont = id1.clone().removeAttr('id');
	var nextCont = prevCont.clone();
			
	prevCont.add(nextCont).css({
		width:id1.width(),
		float:'left'
		});
	
	var contentSwapWrapInner = $('<div></div>')
		.css({
			width:10000,
			overflow:'hidden'
			})
		.append(prevCont)
		.append(nextCont);

	var contentSwapWrap = $('<div></div>')
		.css({
			width:id1.outerWidth(true),
			overflow:'hidden'
			})
		.append(contentSwapWrapInner);

	id1.hide();	
	id1.html(data);
	id1.after(contentSwapWrap);
	
	t=setTimeout(function()
		{
		nextCont.html(id1.html());
		
		maxHeight = Math.max(prevCont.height(),id1.height());
		newHeight = id1.height();
		nextCont.height(maxHeight);
		
		if(prevCont.height()==maxHeight)
			{
			startSpeed = 0;
			}
		else
			{
			startSpeed = 500;
			}
		if(prevCont.height()==newHeight)
			{
			endSpeed = 0;
			}
		else
			{
			endSpeed = 500;
			}
		
		
		
		prevCont.animate({'height':maxHeight},startSpeed,function()
			{
	
			prevCont.css({
				float:'left',
				//'background-color':'red'
				});
			nextCont.css({
				float:'left',
				//'background-color':'blue'
				})
				.show();
			prevCont.animate({'margin-left':-prevCont.outerWidth()}, 1000, function()
				{
				prevCont.remove();
	
				nextCont.css({
					float:'none',
					'margin-left':0
					});
				
				nextCont.animate({'height':nextCont.height()}, endSpeed, function()
					{
					contentSwapWrap.remove();
					id1.show();
					
					callbackFn();
					contentSwapVars['running'] = false;
					B.data('contentSwapVars',0);
					});
				});
			});
	},100);
	
	return this;
	}
})(jQuery);
