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 checkFlash (version) {
	return DetectFlashVer(version, 0, 0);
}



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 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 checkCookie() {
	$j(document).ready( function () {
		showSplash=getCookie('RSPCASplash');
		if (showSplash=="") {
			setCookie('RSPCASplash','true',365);
			
			tb_show(null, '#TB_inline?height=500&width=886&inlineId=splashContent&modal=true', false);
		}
		
		if (showSplash=="true") {
			tb_show(null, '#TB_inline?height=500&width=886&inlineId=splashContent&modal=true', false);
		}
	});
}

function showSplashCheck() {
	if (document.getElementById('splashCheck').checked) {
		setCookie('RSPCASplash','false',365);
	}
}


function homePageBanner() {
	
	var adTabs = new Array("transportation", "testing", "labelling");
	var currentAd = 0;
	
	
	function startBanner() {
		if (currentAd < adTabs.length - 1) {
			currentAd++;
		} else {
			currentAd = 0;
		}
		
		banner = $j('#adBanner');
		
		banner.fadeOut( 'slow', function () {
			
			banner.css('background-image', 'url(/assets/templates/images/homeBanner1.0_'+adTabs[currentAd]+'.jpg)');
			$j('#adBox h2').html(adTabs[currentAd].substr(0,1).toUpperCase() + adTabs[currentAd].substr(1,adTabs[currentAd].length));

			$j('#adTabs li').removeClass('select');
			$j('.' + adTabs[currentAd] + '').addClass('select');
			banner.fadeIn('slow');
		});
	}
	
	$j(document).ready( function () {					  
		setInterval(startBanner, 5000);
		
	});
}



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.');
	} else {
		$j('#' + id + ' .featureContent li.submit').fadeOut('normal', function() {
			$j('#' + id + ' .featureContent ul input').fadeOut('slow', function() {
				$j('#' + id + ' .featureContent ul').html(response);
				$j('.pollText, #' + id + ' .featureContent span, .easypoll_bar').fadeIn('slow');
			});
		});
    }
};


function commentsForm(posted, name) {
	$j(document).ready( function () {
		$j('.addComment').click( function () {
			$j('.addComment, .commentDesc').hide();
			$j('.jot-form').show();
		});
		
		$j('.jot-form').css('display', 'none');
		$j('.addComment').css('display', 'inline');
		
		if ((posted > 0) || (name != "")) {	
			$j('.addComment').trigger('click');
		}
	});
}


function factSetup() {
	var showing = 0;
	
	$j(document).ready( function () {
		
		facts = $j('.facts');
		factNumber = facts.size();
		
		facts.each( function () {
			var index = facts.index(this);
			
			if (index > 1) {
				$j(this).hide();
			}
		});
		
		prevButton = $j('.factPrev');
		nextButton = $j('.factNext');
		
		prevButton.click( function() {
			factPrev();
		});
		nextButton.click( function() {
			factNext();					   
		});
		
		prevButton.hide();
		if (factNumber <= 10)
			nextButton.hide();

		function factNext() {
			$j('.facts:visible').fadeOut('slow', function() {
				$j(this).next().next().fadeIn();
				
				//$j('#fl'+(showing+2)+', #fl'+(showing+3)).fadeIn('slow');
			});
			
			showing = showing+2;
			prevButton.fadeIn();
			
			if (showing+2 >= factNumber) {
				nextButton.fadeOut('normal', function () {
					prevButton.css('margin-right', '177px');									   
			    });
				
			}
		}
		
		function factPrev() {
			visibleFacts = $j('.facts:visible');
			
			visibleFacts.fadeOut('slow', function() {
				
				if (visibleFacts.index(this) == 0) {
					$j(this).prev().fadeIn();
					$j(this).prev().prev().fadeIn();
				}
				
				//$j('#fl'+(showing+2)+', #fl'+(showing+3)).fadeIn('slow');
			});
			
			showing = showing-2;
			nextButton.fadeIn();
			prevButton.css('margin-right', '20px');
			
			if (showing == 0) {
				prevButton.fadeOut();
			}
			
		}
	});
}

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="'+ ytLinks[index] +'" id="bannerPlayer" style="visibility: visible;"><param name="movie" value="'+ ytLinks[index] +'"></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="'+ ytLinks[0] +'" id="bannerPlayer" style="visibility: visible;"><param name="movie" value="'+ ytLinks[0] +'"></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>');
	});
}
