var $j = jQuery.noConflict();

function setExternalLinks() {
  var el_list = document.getElementsByTagName('A');
  for (i=0; i<el_list.length; i++) {
    if (el_list[i].getAttribute('rel') == 'external') {
      el_list[i].setAttribute('target', '_blank');
    }
  }
}


function checkImageWidth () {
	var images = document.getElementById("contentMain").getElementsByTagName("IMG");
	
	for (var i=0; i<images.length; i++) {
		if (images[0].clientWidth > 480) {
			images[0].className+=" noImageMargin";
		}
	}
}

function checkFlash (version) {
	return DetectFlashVer(version, 0, 0);
}


function detectBrowser() {
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	if ((browser=="Microsoft Internet Explorer") && (b_version.search("MSIE 6.0"))) {		
		return true;
	}
}


//Sfhover functionality
sfHover = function() {
		var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}

}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function checkCookie() {
	showSplash=getCookie('RSPCAPASplash');	
	if (showSplash==""||showSplash==null) {
		setCookie('RSPCAPASplash','true',365);
		tb_show(null, '#TB_inline?height=450&width=740&inlineId=splashContent&modal=true', false);
	}
	
	if (showSplash=="true") {
		tb_show(null, '#TB_inline?height=450&width=740&inlineId=splashContent&modal=true', false);
	}
}

function getCookie(c_name) {
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return "";
}
function setCookie(c_name, value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function showSplashCheck() {
	if (document.getElementById('splashCheck').checked) {
		setCookie('RSPCAPASplash','false',365);
	}
}


var poll_displayResults = function(response, id){
	if(response == false){
        $(id + "submit").disabled = true;
    } else if (response.indexOf('error') != -1) {
		alert('Please select an option from the poll before voting.');
		$(id + "submit").disabled = false;
	} else {
		$j('#' + id + ' li.submit, #' + id + ' ul li.radio span').fadeOut('slow', function() {
			$j('#' + id + ' ul').html(response.replace(/easypoll_bar/gi, "easypoll_bar hidden"));
			$j('.easypoll_bar').fadeIn('slow');
		});
    }
};



function setupYT(ytlinks) {
	
	$j(document).ready( function () {
		
			$j('.ytlink').click( function () {
				index = $j('.ytlink').index(this);
								
				$j('#ytPlayer').html('<object width="352" height="222" type="application/x-shockwave-flash" data="http://www.youtube.com/v/'+ ytLinks[index] +'&hl=en&fs=1" id="bannerPlayer" style="visibility: visible;"><param name="movie" value="http://www.youtube.com/v/'+ ytLinks[index] +'&hl=en&fs=1"></param><param name="flashvars" value="pathToSWF=/sites/all/themes/monitor/swf/"/><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent" /></object>');
			});
			
			//set player to first video
			$j('#ytPlayer').html('<object width="352" height="222" type="application/x-shockwave-flash" data="http://www.youtube.com/v/'+ ytLinks[0] +'&hl=en&fs=1" id="bannerPlayer" style="visibility: visible;"><param name="movie" value="http://www.youtube.com/v/'+ ytLinks[0] +'&hl=en&fs=1"></param><param name="flashvars" value="pathToSWF=/sites/all/themes/monitor/swf/"/><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent" /></object>');
			
			$j('#ytWrapper ul').jcarousel();

	});
}


function homepageCarousel() {
		
	var adTabs = new Array("circus", "dogs", "labelling", "testing");
	var currentAd = 0;
	
	function fadeBanner() {
		if (currentAd < adTabs.length - 1) {
			currentAd++;
		} else {
			currentAd = 0;
		}
		
		goToBanner(currentAd);
	}
	
	function goToBanner(bannerNum) {
		$j('.mainBgImg:visible, #banner h2:visible, #banner p:visible').fadeOut( 'slow', function () {			
			$j('.mainBgImg').eq(bannerNum).fadeIn('slow');
			$j('#banner h2').eq(bannerNum).fadeIn('slow');
			$j('#banner p').eq(bannerNum).fadeIn('slow');
			
			$j('.bannerNav a').removeClass('active').eq(bannerNum).addClass('active');
		});
	}
	
	labelling = Math.ceil(Math.random()*2);
	dog = Math.ceil(Math.random()*2);
	testing = Math.ceil(Math.random()*2);
	
	$j('#banner img').after('<img class="mainBgImg dog'+dog+'" src="/assets/templates/images/homeImg_dog_'+dog+'.png" alt="Dog Control" /><img class="mainBgImg labelling'+labelling+'" src="/assets/templates/images/homeImg_labelling_'+labelling+'.png" alt="Food labelling" /><img class="mainBgImg testing'+testing+'" src="/assets/templates/images/homeImg_testing_'+testing+'.png" alt="Animal testing" />');
	
	$j('img.mainBgImg').not('img.mainBgImg:first').hide();
	
	timeOut = setInterval(fadeBanner, 8000);
	
	$j('.bannerNav a').click(function(event) {
		if (!($(this).hasClass('active'))) {	
			
		clearTimeout(timeOut);
		currentAd = $j('.bannerNav a').index($j(this));
		goToBanner(currentAd);
		
		timeOut = setInterval(fadeBanner, 8000);
		}
		event.preventDefault();
	});

}



function setupTimeline() {
	$j(document).ready( function () {
		$j('#timelineOuterContainer').show();			  
		//makes horizontal timeline wrapper
		$j('#timelineWrap').jScrollHorizontalPane({showArrows:true, scrollbarHeight:41, scrollbarMargin: 0, arrowSize: 37});
		$j('#actinfoWrap').hide();
		//$j("#timelineAccord").accordion({header: "a.head" ,alwaysOpen: true,autoHeight:false});
		//active: "a.default", 
		//setup clouds on timeline
		clouds = new Array();
		$j('.cloud').each(function() {
			$elem = $j(this);
			tempCloud = new Cloud;
			tempCloud.width = $elem.width();
			tempCloud.height = $elem.height();
			
			tempCloud.leftSide = $elem.children('img').css('left');
			tempCloud.topSide = $elem.children('img').css('top');
			clouds.push(tempCloud);
		});
		
		//hover states for timeline clouds
		$j('#timeline .cloud').hover(
			function(){
				cloudIndex = $j('#timeline .cloud').index($j(this));
				$j(this).children('img').stop().animate({'width':clouds[cloudIndex].width, 'top':'0px', 'left':'0px'});
			},
			function() {
				cloudIndex = $j('#timeline .cloud').index($j(this));
				$j(this).children('img').stop().animate({'width':clouds[cloudIndex].width-10, 'top':clouds[cloudIndex].topSide, 'left':clouds[cloudIndex].leftSide});
			}
		);
		
		$j('#timeline .cloud a').click(function (event) {
			//ajax call to the modx javascript in here and then once finished...fade in
			getActs($j(this).html(), 0);
			event.preventDefault();
		});
		
		
		$j('#timeline .signpost a').click(function (event) {
			position = 0;
				
			switch ($j('#timeline .signpost a').index($j(this))) {
				case 0:
					position = 0;
					break;
				case 1:
					position = 2;
					break;
				case 2:
					position = 0;
					break;
				case 3:
					position = 3;
					break;
				case 4:
					position = 1;
					break;
				case 5:
					position = 2;
					break;
			}
			
			//ajax call to the modx javascript in here and then once finished...fade in
			getActs($j(this).attr('rel'), position);
			
			event.preventDefault();
		});
		

		//get back to the timeline
		$j('.actControls .buttonWrap a').click(function(event) {
		 	$j('#actinfoWrap').fadeOut('slow');
			event.preventDefault();
		});
		
		//switch views
		$j('.actControls .listView').click(function(event) {
		 	$j('.cloudView').not('a').fadeOut('slow', function() {
				$j('#timelineAccord').fadeIn('slow');										  
			});
			event.preventDefault();
		});
		$j('.actControls .cloudView').click(function(event) {
		 	$j('#timelineAccord').not('a').fadeOut('slow', function() {
				$j('.cloudView').fadeIn('slow');										  
			});
			event.preventDefault();
		});
	});
	
}

function Cloud() {
	var height = 0;
	var width = 0;
	var leftSide = '0px';
	var topSide = '0px';
}


function calibrateControls(requestedAct) {
	//cloud controls hover effects
	$j('.cloudControls a').hover(
		function() {
			cloudColour = $j(this).attr('class').split(" ");
			
			switch(cloudColour[0]) {
				case 'blue':
					labelColor = '#4084D0';
					break;
				case 'purple':
					labelColor = '#D13679';
					break;
				case 'green':
					labelColor = '#82CA02';
					break;
				case 'orange':
					labelColor = '#FFAD26';
					break;
			}
			$j('div.actLabel').css('background-color', labelColor);
							
			cloudIndex = $j('.cloudControls a').index($j(this));
			$j(this).children('img').stop().animate({'opacity': '1'});
			
			$j('.actLabel p:visible').hide();
			$j('.actLabel p').eq(cloudIndex).show();
			
			$j('div.labelArrow').css('left', (parseInt($j(this).position().left)+36)+'px');
		},
		function() {
			if (!$j(this).hasClass('selected'))
				$j(this).children('img').stop().animate({'opacity': '0'});
		}
	);
	
	//load in new content on click
	$j('.cloudControls a').click(function(event) {
		$j('.cloudControls a').removeClass('selected').children('img').css({'opacity':'0'});
		$j(this).addClass('selected').children('img').css({'opacity':'1'});
		
		clickedIndex = $j('.cloudControls a').index($j(this));
		
		$j('div.actDesc div:visible').fadeOut(function() {
			$j('div.actDesc div').eq(clickedIndex).fadeIn();
		});
		$j('div.actImage img:visible').fadeOut(function() {
			$j('div.actImage img').eq(clickedIndex).fadeIn();
		});
		event.preventDefault();
	});
		
	//cloud controls once on a particular year
	$j('.cloudControls a').eq(requestedAct).addClass('selected');
	$j('.actLabel p').eq(requestedAct).show(); //shows only first label
	$j('div.labelArrow').css('left', '36px');
	$j('.actDesc div').eq(requestedAct).show(); //shows only first description
	$j('.actImage img').eq(requestedAct).show(); //shows only first description
	$j('.cloudControls a').not('.selected').children('img').css('opacity', '0'); //makes all hover images opacity 0
	
	$j("#timelineAccord").accordion('destroy').accordion({header: "a.head" ,alwaysOpen: true,autoHeight:false});
}



