/*
	globals neede to use the setInterval to make animation
*/

var marginLeft=0, marginRight=0; mover=0, leftMargin=0, dayCount=1, xPosition=0;

/*
	if days are added to the time line total Days need to be encresed buy the
	number added this will allow the scrolling to work correctly.
*/
var totalDays=13;

/*
	Is this ie if so squish the double margin bug.
	ie 7 going to be a problem?
*/
if (!document.all)
{
	var marginMax=179;	
}
else
{
	var marginMax=180/2;//ie double margin bug
}

var marginMin= 400;

/*
	I need some divs to controll the calander scrolling, I'm creating them here,
	so they won't mess up my nice clean html
*/

function createDivs ()
{
	
	var cal=document.getElementById('cal');

	var wrapper=document.getElementById('wrapper');
	 
	
	
	mask2 = document.createElement("div");
	mask2.className= 'decoration';
	mask2.id='leftBuffer';
	
	mask3 = document.createElement("div");
	mask3.className= 'decoration';
	mask3.id='rightBuffer';
	

	
	insertedElement = wrapper.insertBefore(mask2, cal);
	insertedElement = wrapper.insertBefore(mask3, cal);
	
	/*
		for ie the alpha image loader.
	*/	
	
	if(document.all)
		{
			mask2.style.backgroundImage='none';
			mask3.style.backgroundImage='none';		
			mask2.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/left-buffer.png', sizingMethod='scale')";
			mask3.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/right-buffer.png', sizingMethod='scale')";
			
		}
	

	
}


/*
	the leftwards animation on the calander.
	I incrementally decreeses the left margin. as each li steps off the edge 
	I switch to the next one. I stop this when day 21 is available for clicking
	which is where dayCount comes in it keeps track of howmany days have been concealed
	and stops the animation at the correct point.
*/
function moveLeft()
{
	
	
	test=moveBanner('left');
	
	if(dayCount<=totalDays)
		{
			myDay=new Array();
			for(var days=1; days<22; days++)
				{
					myDay[days]=document.getElementById('day'+days);
				}
			
			
			leftMargin--;
			
			if (dayCount<=0) dayCount=1;
			
			myDay[dayCount].style.marginLeft=leftMargin+"px";
			if (leftMargin<=-53) 
				{
					leftMargin=0;
					myDay[dayCount].firstChild.style.color='black'
					dayCount++;
					
				}
		

		}
		else
		{
			clearInterval(mover);
			
		}
}
				
			
/*
	right wards animation the left margin is increased until it is again 0.
	except for day 1 where the x margin may increase upto marginMax=179
	to bring it out from behind the left buffer
*/

function moveRight()
{
	test=moveBanner('right');
	
	if( leftMargin<marginMax & dayCount>0 )
		{
			myDay=new Array();
			for(var days=1; days<23; days++)
				{
					myDay[days]=document.getElementById('day'+days);
				}
			
				leftMargin++;
				if(dayCount==0) dayCount=1;
				myDay[dayCount].style.marginLeft=leftMargin+"px";
				
				if (leftMargin>=-20) myDay[dayCount].firstChild.style.color='white';
			
			if (leftMargin>=0 & dayCount>1)
				{
					
					dayCount--;
					leftMargin=-53;
				
				}
			
		}
		else
				{
					clearInterval(mover);
				}
}



function moveBanner (direction)
{
	if (document.getElementById('banner1'))
	{
	var banner=document.getElementById('banner1');

	var week1=0;
	var week2=-773;
	var week3=-1546;
	


	
	
	if(dayCount>1 && dayCount<5)
		{
			
			
		if(xPosition>week2 && direction=='left')
			{
			 		if(xPosition<week2+5) 
			 			{
			 				xPosition=xPosition-1
			 			
			 			}
			 			else
			 			{
			 				xPosition=xPosition-5
			 			}
			 		
			}
		
		if(xPosition<0 && direction=='right')
			{
				
				if(xPosition>-5) 
				{
					xPosition=xPosition+1
				}
				else
				{
					xPosition=xPosition+5
				}
			}
		}
		else if(dayCount>8 && dayCount<18)
		{
			if(xPosition>week3 && direction=='left')
				{
						 		
			 		if(xPosition<week3+5) 
						{
							xPosition=xPosition-1

						}
						else
						{
							xPosition=xPosition-5
						}
				}
					
			if(xPosition<week2 && direction=='right')
				{
					if(xPosition>week2-5) 
					{
						xPosition=xPosition+1

					}
					else
					{
						xPosition=xPosition+5
			 		}
			}
		}
		/*else if(dayCount>11 & dayCount<15)
	{

	}*/
	else
	{		 
		
	
	}
		banner.style.backgroundPosition=xPosition+'px 0px';
		return true;
	}
}

/*
	
	Center the current week; we fined out which week we are in and then use the move right 
	move left functions to center it
	
*/

function centerDay() {

	var bodyTag= document.getElementsByTagName('body')[0].className;
	var currentWeek=bodyTag.match(/b-week[0-9]+/);
    
    if(currentWeek)
    	{
			currentWeek=currentWeek.toString();
			currentWeek=currentWeek.match(/[0-9]+/);
		}
	else
		{
			currentWeek=1
		}

	var weekStart= new Array(0,0,4,12,19);//the start day of weeks 0, 1, 2, 3, 4
	
	if(currentWeek==1)  {
	
		moveRight();
	
	}
	else 
	{
		
			
		if (dayCount<=weekStart[currentWeek])
			{
				moveLeft();
			}
		else
			{
				clearInterval(mover);
			}			
	}
}

/*

	the setInterval function used so we can animate things

*/
function animation()
	{
		
		
		var leftBuffer=document.getElementById('leftBuffer');
		var rightBuffer=document.getElementById('rightBuffer');
		
		mover=setInterval(centerDay, 10);
		
		//if I mouse over the leftBuffer move the icons leftward
		leftBuffer.onmouseover= function ()
			{		
				if(mover) clearInterval(mover);
				mover=setInterval(moveRight, 10);	
			}
		
		//stop moving when I mouse out
		leftBuffer.onmouseout= function()
			{
				clearInterval(mover);
			}
		
		//if I mouse over the rightBuffer move the icons rightward
		rightBuffer.onmouseover= function ()
			{	
				clearInterval(mover);
				mover=setInterval(moveLeft, 10);		
			}
		
		//stop moving when I mouse out
		rightBuffer.onmouseout= function()
			{
				clearInterval(mover);
			}
		
	}


/*
	display the text associated with the link
*/
function showStory() {

  	var links = document.getElementsByTagName("a");
	var stories=document.getElementsByTagName("div");


  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("info")) {
      links[i].onclick = function() {
      
      /*
      	hide any displayed paragraphs first
      */
      	for (var i=0; i < stories.length; i++) {	
	  			if (stories[i].className.match("article")) stories[i].style.display='none'
		}
      
        var traget=this.href;
        var idValue=traget.match(/#.*/);
        idValue=idValue.toString();
       	idValue=idValue.replace('#','');
       	if (document.getElementById(idValue))
       		{
       		var myElement= document.getElementById(idValue);                
        		myElement.style.display='block';
        	}
        	else
        	{
        		var myElement= document.getElementById('article99');                
        		myElement.style.display='block';
        	}
        return true;
      }
      }
    }
  }

function showStoryFlash(idValue)
{
	var stories=document.getElementsByTagName("div");
	
	// Hide any previously displayed stories 
	for (var i=0; i < stories.length; i++) {	
			  			if (stories[i].className.match("article")) stories[i].style.display='none'
		}

 	if (document.getElementById(idValue))
 		{
			var target=document.getElementById(idValue);
			target.style.display='block';
		}
	else
	     {
	        var myElement= document.getElementById('article99');                
	        myElement.style.display='block';
        }
		
}

/*
	is flash installed prity much grabed the scripts form http://www.quirksmode.org/js/flash.html
	and than added the activx detection from  http://blog.deconcept.com/flashobject/
	
	it was just a small part of a much lager script;
*/
function flashDetect() {
var flashinstalled = 0;
var flashversion = 0;
MSDetect = "false";
if (navigator.plugins && navigator.plugins.length)
{
	x = navigator.plugins["Shockwave Flash"];
	if (x)
	{
		flashinstalled = 2;
		if (x.description)
		{
			y = x.description;
			flashversion = y.charAt(y.indexOf('.')-1);
		}
	}
	else
		flashinstalled = 1;
	if (navigator.plugins["Shockwave Flash 2.0"])
	{
		flashinstalled = 2;
		flashversion = 2;
	}
}
	else if (navigator.mimeTypes && navigator.mimeTypes.length)
	{
		x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin)
			flashinstalled = 2;
		else
			flashinstalled = 1;
	}
	else
	{
		MSDetect = "true";
		if (window.ActiveXObject){
				   try {
				   var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
					flashinstalled = 2;
				   } catch (e) {}
				}
			else
			{
			flashinstalled = 1;
			}
		}
		
	return flashinstalled; 
}
	
	
	/*
		need to complete the insert flash object need to add the necissary attributes
		and pram tags using appendChild() and setAttribute();
	
	*/
	
function doFlash()  {

	gotFlash=flashDetect();
	var banner=document.getElementById('banner');
	var banner1=document.getElementById('banner1');
	var wrapper=document.getElementById('wrapper');

	if (gotFlash==2)
		{
			banner.style.height="420px";
			var htmlDate=getDate()
			banner.style.marginTop="7px";
			banner.style.marginLeft="4px";
			wrapper.style.display='none';
			banner1.style.display='none';
			var path='flash/bae.swf?test_date='+htmlDate;
			/*
				ie ignors param values when added by appendChild so we use innerHTML for ie instead.
				We know it is ie since it has activeXObject (need to check if this is always true)
			*/
			if (window.ActiveXObject)
			{
				banner.style.marginTop="3px";
				var htmlFragment='<object type="application/x-shockwave-flash" height="400" width="770"><param name="movie" value="'+path+'" /><param name="allowScriptAccess" value="sameDomain" /><param name="quality" value="high" /></object>';
				banner.innerHTML=htmlFragment;
			}
			else
			{
			
			
			var flashObject=document.createElement('object');
			
			
			
			flashObject.setAttribute('data', path);
			flashObject.setAttribute('type', 'application/x-shockwave-flash');
			flashObject.setAttribute('height', '400');
			flashObject.setAttribute('width', '770');
			
			
			var pram1=document.createElement('param');
			pram1.setAttribute('name', 'allowScriptAccess');
			pram1.setAttribute('value', 'sameDomain');
			
			
			var pram2=document.createElement('param');
			pram2.setAttribute('name', 'movie');
			pram2.setAttribute('value', path);
			
			
			var pram3=document.createElement('param');
			pram3.setAttribute('name', 'quality');
			pram3.setAttribute('value', 'high');
			
			flashObject.appendChild(pram1);
			flashObject.appendChild(pram2);
			flashObject.appendChild(pram3);
		
			banner.appendChild(flashObject);
			}
			
			
		}
		else
		{
			
			
			var wrapper=document.getElementById('wrapper');
			
			wrapper.style.display='block';
			banner.style.height='264px';
		}	
}

function doPopups() {
 
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
		if (links[i].className.match("slideshow")) {
		  links[i].onclick = function() {
			window.open(this.href, "", "location=0,status=1,scrollbars=0,width=320,height=350");
			return false;
		  }
		}
		  if (links[i].className.match("interview")) {
		  links[i].onclick = function() {
			window.open(this.href, "", "location=0,status=1,scrollbars=0,width=410,height=420");
			return false;
		  }
		 }
		  if (links[i].className.match("pdf")) {
		  		  links[i].onclick = function() {
		  			window.open(this.href, "", "location=1,status=1,scrollbars=1");
		  			return false;
		  }
		 }
	}
  }

function getDate()
	{
		if(document.getElementById)
			{
				myDate=document.getElementById('date').innerHTML;
				return myDate;
			}
	}

function detectFlash()
	{
		try
		{
			doFlash();
			clearInterval(interval)
		}
		catch(e)
			{
				
			}
	}
if(document.getElementsByTagName && document.getElementById)
{
	// We need to detect flash as soon as possible this does it
	var interval=setInterval(detectFlash, 10);	
	
	window.onload= function () {
		doPopups();
		createDivs ();
		animation();
		showStory();
	}
	
}


