// PLANET RUGBY - Main site functions


/* dropdown main navigation */
prNavInit = function(){

	$("div.pr-nav-t2 ul li").hover(function(){
	
	$(this).find("div").show();

	},function(){$(this).find("div").hide();});

}


/* team dropdown */
prDropdown = function(){
$('div.pr-select').hover(function(){
			$(this).click(function(){
				$(this).find('ul:first').show();}
				
				)},
			
			function(){
					$(this).find('ul:first').hide()
			}
		);

}


/* tab box function */
tabs=function(p){
		
			$(p+" .anchors li:first").addClass('tabs-selected');
			$(p+" .anchors li").click(function(){
				$(this).parents('.anchors').find('li').removeClass('tabs-selected');
				$(this).addClass('tabs-selected');
				var tablink=$(this).find('a').attr('href');
				tablink=tablink.split('#')[1];
				$(this).parents('.pr-tabs').find("div[@id^='"+tablink.slice(0,3)+"']").hide();
				$(this).parents('.pr-tabs').find("#"+tablink).show();
				return false;
			})
		
}

/* video tab box function */
tabsVideo=function(q){
		
			$(q+" .tabs-nav li:first").addClass('tabs-selected');
			$(q+" .tabs-nav li").click(function(){
				$(this).parents('.tabs-nav').find('li').removeClass('tabs-selected');
				$(this).addClass('tabs-selected');
				var tablink=$(this).find('a').attr('href');
				tablink=tablink.split('#')[1];
				$(this).parents('.pr-clip-tabs').find("div[@id^='"+tablink.slice(0,3)+"']").hide();
				$(this).parents('.pr-clip-tabs').find("#"+tablink).show();
				return false;
			})
		
}
	

/* stylesheet switcher */
prSwitch = function(){
	
		$('.pr-nav-switch a').click(function(){
		var region = $(this).attr('title');								 
		$('body#home').removeClass('north').removeClass('south').removeClass('default');
		$('body#home').addClass(region);	
		setCookie('region',region,365);
											 
		})
		
}	


/* video-flickbox function */
videoFlickBox=function(el,auto){
	
		$(el+" h2").append("<div><a href=# class=flickf></a> <a href='#' class=flicker4> <a href='#' class=flicker3> </a><a href='#' class=flicker2> </a><a href='#' class=flicker1> </a><a href=# class=flickb></a></div>");
		
		$(el+" h2 a.flicker1").addClass('high')
		$(el+" h2 a.flickf").click(function(){
				var a=this;
				$(a).hide();
				$(el+" h2").prepend('<span class=flickf></span>');
				
				clearTimeout(msflbt1);clearTimeout(msflbt2);clearTimeout(msflbt3);
				
				var u = parseInt($(el+" div.pr-flick-list").find('ul').css('margin-left').replace('px',''));
				
				if(u!=-864){$(el+" div.pr-flick-list").find('ul').animate({marginLeft:u-288},800,function(){$(el+' span.flickf').remove();$(a).show()})}
				$(el+" h2 a[@class^='flicker']").removeClass('high');
				
				if(u==0){$('a.flicker2').addClass('high')}
				if(u==-288){$('a.flicker3').addClass('high')}
				if(u==-576){$('a.flicker4').addClass('high')}
				if(u==-864){$('a.flicker4').addClass('high');$(el+' span.flickf').remove();$(a).show()}
				
		return false;
		
		})
		
		$(el+" h2 a.flickb").click(function(){
				var a=this;
				$(a).hide();
				$(el+" h2").append('<span class=flickb></span>');
			
				clearTimeout(msflbt1);clearTimeout(msflbt2);clearTimeout(msflbt3)
				var u = parseInt($(el+" div.pr-flick-list").find('ul').css('margin-left').replace('px',''));
				if(u!=0){$(el+" div.pr-flick-list").find('ul').animate({marginLeft:u+288},800,function(){$(el+' span.flickb').remove();$(a).show()})}
				$(el+" h2 a[@class^='flicker']").removeClass('high');
				
				if(u==0){$('a.flicker1').addClass('high');$(el+' span.flickb').remove();$(a).show()}
				if(u==-288){$('a.flicker1').addClass('high')}
				if(u==-576){$('a.flicker2').addClass('high')}
				if(u==-864){$('a.flicker3').addClass('high')}	
		return false;
		})
		
		$(el+" h2 a[@class^='flicker']").click(function(){
			$(el+" h2 a[@class^='flicker']").removeClass('high');
			if($(this).attr('class')=='flicker1'){clearTimeout(msflbt1);clearTimeout(msflbt2);clearTimeout(msflbt3);clearTimeout(msflbt4);clearTimeout(msflbt5);$(el+" div.pr-flick-list").find('ul').animate({marginLeft:0},800)};
			
			if($(this).attr('class')=='flicker2'){clearTimeout(msflbt1);clearTimeout(msflbt2);clearTimeout(msflbt3);clearTimeout(msflbt4);clearTimeout(msflbt5);$(el+" div.pr-flick-list").find('ul').animate({marginLeft:-288},800)};
			
			if($(this).attr('class')=='flicker3'){clearTimeout(msflbt1);clearTimeout(msflbt2);clearTimeout(msflbt3);clearTimeout(msflbt4);clearTimeout(msflbt5);$(el+" div.pr-flick-list").find('ul').animate({marginLeft:-576},800)};
			
			if($(this).attr('class')=='flicker4'){clearTimeout(msflbt1);clearTimeout(msflbt2);clearTimeout(msflbt3);clearTimeout(msflbt4);clearTimeout(msflbt5);$(el+" div.pr-flick-list").find('ul').animate({marginLeft:-864},800)};
			
			$(this).addClass('high');
		return false;
		})
	}
	
	videoFlickBoxAuto=function(){
		window.msflbt1 = setTimeout("$('#pr-flickbox-1 div.pr-flick-list').find('ul').animate({marginLeft:-288},800);$('#pr-flickbox-1 h2 a[@class^=flicker]').removeClass('high');$('#pr-flickbox-1 .flicker2').addClass('high')",10000);
		window.msflbt2 = setTimeout("$('#pr-flickbox-1 div.pr-flick-list').find('ul').animate({marginLeft:-576},800);$('#pr-flickbox-1 h2 a[@class^=flicker]').removeClass('high');$('#pr-flickbox-1 .flicker3').addClass('high');",21000);
		window.msflbt3 = setTimeout("$('#pr-flickbox-1 div.pr-flick-list').find('ul').animate({marginLeft:-864},800);$('#pr-flickbox-1 h2 a[@class^=flicker]').removeClass('high');$('#pr-flickbox-1 .flicker4').addClass('high');",32000);
		
		var x = $('#pr-flickbox-1 .pr-flick-list ul li:lt(2)').clone();
		$('#pr-flickbox-1 .pr-flick-list ul').append(x);
	
		
		window.msflbt4 = setTimeout("$('#pr-flickbox-1 div.pr-flick-list').find('ul').animate({marginLeft:-1152},800)",43000)
		window.msflbt5 = setTimeout("$('#pr-flickbox-1 div.pr-flick-list').find('ul').css('margin-left',0);$('#pr-flickbox-1 h2 a[@class^=flicker]').removeClass('high');$('#pr-flickbox-1 .flicker1').addClass('high');$('#pr-flickbox-1 .pr-flick-list ul').find('li:gt(7)').remove();videoFlickBoxAuto();",44000);
		

	}
	
	configVideo= function(vidXML) {
		var newHTML = "";
			$.ajax({
			    url: vidXML,
			    dataType: 'xml',
			    timeout: 1000,
			    success: function(xml){
				
					
					$(xml).find("video").each(function(i) {
						if(i<8){
							newHTML += "<li><a href='" + $(this).attr("src") + "'><img src='" + $(this).attr("bigThumb") + "'><p>" + $(this).attr("title") + "</p></a></li>";
						}
					});
					$("#pr-flickbox-1 .pr-flick-list ul").html(newHTML);
					videoFlickBox('#pr-flickbox-1','yes');
					videoFlickBoxAuto();
			    }
		});
	}
	

/* photo-galleries function */
configGallery=function(el) {				
	$(el+ ' ul.pr-photo-gallery li a').mouseover(function(){
		$(el+ ' ul.pr-photo-gallery li a').removeClass('highlight');
		$(this).addClass('highlight');		
		var im = $(this).find('img').attr('src');
		im = im.replace('68x60','247');
		$('#bigThumb img').attr('src',im);

		var h4 = $(this).find('h4').html();
		var p = $(this).find('p').html();
		$(el).find('span').html("<h4>"+h4+"</h4><p>"+p+"</p>");
	})
}

/* live match slidebox function */
prLiveMatches = function(p){
	
	$(p+ ' .pr-live-matches-nav a').click(function(){
		$(p+ ' .pr-live-matches-nav a').css('visibility','hidden')
		var toscroll = parseInt(this.rel);
		var currentleft = parseInt($(p+ ' .pr-live-matches ul').css('margin-left'));
		var howmanylis = $(p+ ' .pr-live-matches ul li').length;
		if(toscroll==1){
			var li = $(p+ ' .pr-live-matches ul li:last').clone();
			$(p+ ' .pr-live-matches ul').prepend(li).css('margin-left',-265)
			$(p+ ' .pr-live-matches ul').animate({marginLeft:0},function(){
			$(p+ ' .pr-live-matches ul li:last').remove();																		 
			$(p+ ' .pr-live-matches-nav a').css('visibility','visible')														 
																		 
			})
			
		}
		
		if(toscroll==-1){
			var li = $(p+ ' .pr-live-matches ul li:first').clone();
			$(p+ ' .pr-live-matches ul').append(li).css('margin-left',0);			
			$(p+' .pr-live-matches ul').animate({marginLeft:-265},function(){
			$(p+' .pr-live-matches ul li:first').remove();
			$(p+' .pr-live-matches ul').css('margin-left',0);
			$(p+' .pr-live-matches-nav a').css('visibility','visible')
				
			})
			
		}
		
		return false;										
											
	})	
		
}




/* article bookmark ul */
htmlStuff = function(){
$('.pr-art-bookmark').prepend("<a class='' href='#'>Save to</a>");
		$(".pr-art-bookmark").hover(
			function(){
				$(this).find("ul").show().css('z-index','4');
				$(this).find('a').addClass('pr-bookmarks')},
				
			function(){
				$(this).find("ul").hide();
				$(this).find('a').removeClass('pr-bookmarks')
			});
};


/* accordian function */
prAccordian = function(a){

	if($('#'+a).find('h2.pr-acc-open')[0]){
		$('#'+a).find('h2.pr-acc-open').addClass('pr-acc-on');
		$('#'+a).find('h2.pr-acc-open').next('div.pr-acc-con').show();
	}
	else{
		$('#'+a).find('h2:first').addClass('pr-acc-on');
		$('#'+a).find('h2:first').next('div.pr-acc-con').show();
	}
	
	$('#'+a).find('h2').click(function(){
		$('#'+a).find('h2').removeClass('pr-acc-on');
		$(this).addClass('pr-acc-on');
		if($(this).next('div.pr-acc-con').css('display')=="block"){return false};
		var b = $(this).next('div.pr-acc-con');
		$('div.pr-acc-con:visible').slideUp(600,function(){b.slideDown(600)});
	
	})
	
 
}


/* thickbox function */
	articleImages=function(p){
	
		var w = $(p).eq(0).width();
		var h = $(p).eq(0).height();
		
		var m = "/"+w+"x"+h+"/";
		$(p).each(function(){
			if($(this).attr('src').match(m)){	
				var s=$(this).attr('src').split(w+'x'+h);
				$(this).wrap("<a href='"+s[0]+"800x600"+s[1]+"' class='thickbox' title=''></a>")
			}
			
		});
		
	}

/* profile gallery function */

profileGallery = function(p){
            
	$(p).find('.pr-profiles-prev a').css('visibility','hidden');

	var lis = Math.ceil(($(p+ ' .pr-profiles-r1 li').length)/2);

	var widthoflis = Math.ceil(lis/9);
	lis = lis*69;
	$(p+ ' .pr-profiles-r1').css('width',lis);

	$(p).find('.pr-profiles-prev a,.pr-profiles-next a').click(function(){

		//l is the amount we want to go back or forth
		var l = parseInt($(this).attr('rel'));

		// current margin left position
		var c = parseInt($(p+ ' .pr-profiles-r1').css('margin-left'));

		//lenth of the lis box
		var length = $(p).find('.pr-profiles-prev a').attr('rel');

		//maxmargin is max to go right
		var maxmargin = (((widthoflis-1)*length)*-1);
		var pos = c+l;

		if(l=="-621"){$(p).find('.pr-profiles-prev a').show();}
		if(pos>0){pos=0}
		if(maxmargin==c && l!=length){pos=maxmargin};

		$(p+ ' .pr-profiles-r1').animate({marginLeft:pos})

		if(c==(maxmargin-l)){
			    $(p).find('.pr-profiles-prev a').css('visibility','visible');
			    $(p).find('.pr-profiles-next a').css('visibility','hidden');   
		}
		else if(c==(0-l)){
			    $(p).find('.pr-profiles-next a').css('visibility','visible');
			    $(p).find('.pr-profiles-prev a').css('visibility','hidden');
		}
		else{ 
			    $(p).find('.pr-profiles-next a').css('visibility','visible');
			    $(p).find('.pr-profiles-prev a').css('visibility','visible');
		}

		return false;
	})
	
	}	
	
/* footer feature image function */
$(document).ready(function(){ 
            lastBlock = $("#feature-one")[0];
            maxWidth = 350;
            minWidth = 80;
            $("ul.pr-footer-features li span").hover(
                        function(){
                                    $(lastBlock).animate({width: minWidth}, {queue:false, duration:400});
                                    $(this).animate({width: maxWidth}, {queue:false, duration:400});
                                    lastBlock = this;
            },function(){}
            );
});


/* Lions Venue Image Hover */
function hoverImage(p){
	var n = $(p+' .pr-hover-gallery-thumbs li').length;
	var h = $(p+' .pr-hover-gallery li').height();
	$(p).find('.pr-hover-gallery-thumbs li a').each(function (i) {  
		$(this).hover( 
		function(){
			$(p).find('.pr-hover-gallery-thumbs .on').removeClass("on");
		   	$(p+' .pr-hover-gallery ul').css({'top': -(i*h)});
	   },
	   function(){
			$(this).parent().addClass("on");
	   });
	}); 
	   
};



/* article multiple image gallery */
liveGallery=function(p){
	
	//get some info about photos
	var im = $(p.container+' span img');
	var l = im.length;
	
	
	//if there is more than one photo begin
	if(l>1){
	
	//set up auto timer
	if (!p.timer){window.timer = 5000}
	else{window.timer = p.timer}
	
	//clear current animation timeout
	if(window.t){clearTimeout(t)};
	
		//set up some css
		var width = im.width();
		var height = im.height();
		if($(p.container+ ' p')[0]){height=height+40}
		$(p.container).css({width:width+"px",height:height+"px"});
		

	//set up counter
	if(!window.counter){window.counter=0};
	if(p.auto==true || p.direction=='f'){
		window.counter++;
		if(window.counter>l){window.counter=1}
	
	}
	
	if(p.direction=='b'){
		window.counter--;
		if(window.counter<1){window.counter=l}
	
	}
	var paneloffset = 0;
	if($(p.container+ ' p')[0]){paneloffset=20}
	//append html and add count
	if(!$(p.container+' #livegall-count')[0]){$(p.container).append('<div id="livegall-count" style="position:absolute;display:none;top:100%;margin-top:'+((-45)-paneloffset)+'px;width:41px;height:170px;background:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.png) no-repeat -249px 0px;_background-image:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.gif);padding:3px 0px 5px 0px;text-align:center;left:50%;margin-left:-5px;font-size:10px;font-family:arial,sans-serif;color:white;z-index:3"><em>1</em>&nbsp;of '+l+'</div><div class="live-gall-nav" style="cursor:pointer;display:none;width:20px;height:36px;z-index:3;position:absolute;left:20px!important;top:120px;background:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.png) no-repeat -352px 0px;_background-image:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.gif)"><a href="#" onclick="liveGallery({direction:\'b\',timer:window.timer,container:\''+p.container+'\'});return false;" style="width:20px;height:36px;display:block"> </a></div><div class="live-gall-nav" style="cursor:pointer;display:none;background:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.png) no-repeat -373px 0px;_background-image:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.gif);width:20px;height:36px;z-index:3;position:absolute;right:10px;top:120px"><a href="#" onclick="liveGallery({direction:\'f\',timer:window.timer,container:\''+p.container+'\'});return false;" style="width:15px;height:31px;display:block"> </a></div><div id=live-play style="display:none;cursor:pointer;position:absolute;background:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.png) no-repeat -325px 0px;_background-image:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.gif);width:22px;height:20px;left:50%;margin-left:-31px;z-index:3;top:100%;margin-top:'+((-45)-paneloffset)+'px;"> </div><div class="live-gall-tool" style="display:none;background:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.png) no-repeat -124px 0px;_background-image:url(http://clients.demodesign2.localnet/planetrugby/build/images/site/pr-live-gallery-images.gif);width:120px;margin-left:-80px;height:36px;left:50%;z-index:3;position:absolute;top:100%;margin-top:'+((-85)-paneloffset)+'px;"> </div>')
	
	$(p.container).hover(function(){
		clearTimeout(window.livephotooverlay)
		$('#livegall-count,.live-gall-nav,#live-play').fadeIn();
	
	
	},function(){
		
		window.livephotooverlay = setTimeout("$('#livegall-count,.live-gall-nav,#live-play').fadeOut()",1000);
	
	
	})
	
	// add actions to play/pause button
	$(p.container+' #live-play').click(function(){
		
		if($(this).hasClass('on')==false){
			$(p.container+' span div').stop();
			$(this).css({backgroundPosition:"-298px 0px"})
			$(this).addClass('on');
			$(p.container+' .live-gall-tool').css({backgroundPosition:"0px 0px"})
			
		
		}
		
		else{
		
			liveGallery({auto:true,timer:window.timer,container:p.container})
			$(this).css({backgroundPosition:"-325px 0px"})
			$(this).removeClass('on');
		
			window.counter--;
			$(p.container+' .live-gall-tool').css({backgroundPosition:"-124px 0px"})
		}
	
	
	return false;
	});
	
	$(p.container+' #live-play').hover(function(){
		$(p.container+' .live-gall-tool').fadeIn();
	
	
	},function(){
		$(p.container+' .live-gall-tool').fadeOut();
	
	})
	
	}
		

		if(p.auto){
		
		var c = $(p.container+' span div:eq('+(l-1)+')');
			
			c.animate({marginTop:0},timer,function(){
				$(p.container+' .live-gall-nav a').hide();
				$(this).fadeOut(1000,function(){
					$(p.container+' span').prepend(c);
					c.show();
					liveGallery({auto:true,timer:window.timer,container:p.container})
					$(p.container+' .live-gall-nav a').show();
					
					// do counter
					$(p.container+' #livegall-count b').html(l);
					$(p.container+' #livegall-count em').html(window.counter);
				});
			
			
			})
		}
		
		if(p.direction=='f'){
			
			$(p.container+' .live-gall-nav a').hide();
			$(p.container+' span div').stop();
			$(p.container+' #live-play').css({backgroundPosition:"-298px 0px"})
			$(p.container+' #live-play').addClass('on');
			var c = $(p.container+' span div:eq('+(l-1)+')');
			c.fadeOut(1000,function(){
				$(p.container+' span').prepend(c);
				c.show();
				$(p.container+' .live-gall-nav a').show();
				$(p.container+' .live-gall-tool').css({backgroundPosition:"0px 0px"});
				
				// do counter
				$(p.container+' #livegall-count b').html(l);
				$(p.container+' #livegall-count em').html(window.counter);

			});
			
			
		
		}
		
		if(p.direction=='b'){
			$(p.container+' span div').stop();
			$(p.container+' #live-play').css({backgroundPosition:"-298px 0px"})
			$(p.container+' #live-play').addClass('on');
			var c = $(p.container+' span div:eq('+(0)+')');
			$(p.container+' span div:eq('+(l-2)+')').after(c);
			
			$(p.container+' span div:eq('+(l-1)+')').fadeOut(1000,function(){
			
				$(p.container+' span div:eq('+(l-3)+')').after($(this));
				$(this).show();
				$(p.container+' .live-gall-tool').css({backgroundPosition:"0px 0px"})
				
				// do counter
				$(p.container+' #livegall-count b').html(l);
				$(p.container+' #livegall-count em').html(window.counter);
			
			})
			
		}

	
		return false;
	}
}

function loadStory (basketId, storyId) {
	var targetUrl = 'http://www.planetrugby.com/story/0,25883,thisisnotanoid,00.html';

	if (targetUrl.length > 0) {
		if (storyId == 0) {
			targetUrl = targetUrl.replace("thisisnotanoid", basketId);
		} else {
			targetUrl = targetUrl.replace("thisisnotanoid", basketId + "_" + storyId );
		}
		window.location.href = targetUrl;
	}
}

function loadVideoStory (basketId,storyId) {
	var targetUrl = "";	
	targetUrl = 'http://www.planetrugby.com/video/0,25862,thisisnotanoid,00.html';

	//this will replace thisisnotanOID with the id passed in
	if (targetUrl.length > 0) {
		targetUrl = targetUrl.replace("thisisnotanoid", basketId + "_" + storyId );
		window.location.href = targetUrl;
	}
}

//call functions
tabs("#pr-tabs");
tabsVideo("#pr-clip-tabs");
prNavInit();
prDropdown();
prSwitch();
prLiveMatches('.pr-wrap');
liveGallery({auto:true,timer:5000,container:'.pr-live-gallery'});
htmlStuff();
prAccordian('pr-accordian-1');
articleImages('.pr-art-subcol img');
articleImages('#article-gallery img');
profileGallery('.pr-box-profiles');
hoverImage('.pr-box-venue');