var twitteruser = Array("jk_rowling","jescave","WarwickADavis","James_Phelps","OliverPhelps","chrisrankin","Mattdavelewis","TomFelton","Jamiebower","HarryPotterFilm");
var twitterError=false;
var twitterExecCount=0;
var currentGalId=0;
var twitterFired=false;
$(document).ready(function(){
			if(!twitterFired) {
				for(var i=0; i<twitteruser.length; i++) {
					getTwitter(twitteruser[i]);
				}
				twitterFired=true;
			}
			setTimeout(setUpTweets, 5000);
			selectImage(currentGalId);
			setTimeout(nextGalImage, 10000);

  });



function getTwitter(user) {
	$("#twitter").tweet({
				username: user,
				join_text: "auto",
				avatar_size: 48,
				count: 1
			});
}



function setUpTweets() {
			 $('.tweet_list').innerfade({ animationtype: 'fade', speed: 750, timeout: 10000, type: 'random', containerheight: '200px' });
			 $('.tweet_list').show();
}

function selectImage(id) {
	currentGalId=id;
	$('#imglist > li > a > img').fadeTo("fast", 0.5);
	$('#theimg').fadeTo("fast", 0, function() {
					$('#theimg').attr("src", $('#imglist > li > a > img:eq('+id+')').attr("src"));
					$('#theimg').attr("width", $('#imglist > li > a > img:eq('+id+')').attr('title').split('|')[0]);
					$('#theimg').attr("height", $('#imglist > li > a > img:eq('+id+')').attr('title').split('|')[1]);
					$('#theimglink').attr("href", $('#imglist > li > a:eq('+id+')').attr('href'));
					$('#theimg').fadeTo("slow", 1);			
											});
	$('#imglist > li > a > img:eq('+id+')').fadeTo("fast", 1.00);
}

function nextGalImage() {
	if(currentGalId!=4) {
		currentGalId++;
	} else {
		currentGalId=0;
	}
	selectImage(currentGalId);
	setTimeout(nextGalImage, 5000);
}










var currentNewsPane = 1;

jQuery.fn.accessNews = function( settings ) {
	settings = jQuery.extend({
        headline : "Top Stories",
        speed : "normal",
		slideBy : 2
    }, settings);
    return this.each(function() {
		jQuery.fn.accessNews.run( jQuery( this ), settings );
    });
};
jQuery.fn.accessNews.run = function( $this, settings ) {
	jQuery( ".javascript_css", $this ).css( "display", "none" );
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	if ( li.length > settings.slideBy ) {
		var $next = jQuery( ".next > a", $this );
		var $back = jQuery( ".back > a", $this );
		var liWidth = jQuery( li[0] ).width();
		var animating = false;
		ul.css( "width", ( (li.length) * (liWidth) ) );
		$next.click(function() {
			if ( !animating ) {

				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( (liWidth) * settings.slideBy  );
				if ( offsetLeft + ul.width() > 0 ) {
					ul.animate({
						left: offsetLeft
					}, settings.speed, "swing", function() {
						if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
						}
						animating = false;
						currentNewsPane++;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$back.click(function() {
			if ( !animating ) {

				animating = true;
				offsetRight = parseInt( ul.css( "left" ) ) + ( (liWidth) * settings.slideBy );
				if ( offsetRight + ul.width() <= ul.width() ) {
					ul.animate({
						left: offsetRight
					}, settings.speed,"swing", function() {
						if ( parseInt( ul.css( "left" ) ) == 0 ) {
						}
						animating = false;
						currentNewsPane--;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$next.css( "display", "inline" );
		$back.css( "display", "inline" );
		$back.parent().after( [ "<span class=\"view_all\">","<a href=\"#\">view all</a></span>" ].join( "" ) );
		jQuery( ".view_all > a, .skip_to_news > a", $this ).click(function() {
			var skip_to_news = ( jQuery( this ).html() == "Skip to News" );
			if ( jQuery( this ).html() == "view all" || skip_to_news ) {
				ul.css( "width", "auto" ).css( "left", "0" );
				$next.css( "display", "none" );
				$back.css( "display", "none" );
				if ( !skip_to_news ) {
					jQuery( this ).html( "view less" );
				}
			} else {
				if ( !skip_to_news ) {
					jQuery( this ).html( "view all" );
				}
				ul.css( "width", ( (li.length) * (liWidth) ) );
				$next.css( "display", "inline" );
				$back.css( "display", "inline" );
			}
			return false;
		});
	}
};

	$(function() {
	
		$( "#example_1" ).accessNews({
	        speed : 1500,
			slideBy : 2
	    });
		setTimeout(newsSlideNext, 7500);
	});

function newsSlideNext() {
	if($( "#example_1 ul:eq(0)" ).css("width") != "auto") {
		if(currentNewsPane<=3) {
			$( "#example_1 .next > a" ).click();
		} else {
			$( "#example_1 ul:eq(0)" ).animate({left: 0}, 1500 );
			currentNewsPane=1;
		}
	}
	setTimeout(newsSlideNext, 7500);

}

function launchPodcastPlayer() {
  popUpWin = open("http://live.theremembrall.org/player.php", 'playerWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=520,height=220');
}