﻿/* ====================================================================
This copyright notice must be untouched at all times.
Copyright (c) 2009 Stu Nicholls - stunicholls.com - all rights reserved.
==================================================================== */

onload = function() {

/* default variable settings */
timer = 0;
current = 1;
button = 1;
//removeindex = 0;
ismouseover = false;
swfname = $('#p'+ current).attr('obj_url');
imgname = $('#p'+ current).attr('obj_alt_url');

/* delay in miliiseconds */
showTime=9000;
animatespeed = 700;

/* automatic variable settings */
pictures = $('#buttonList li').length; 
width = 880

/* initial set up */
$('#b'+button + ' img').attr('src', 'utility/autoslide/image/button_ani.gif');
$('#p1').animate({"left": "0px"}, animatespeed, "easeInCubic");

/* start the timer */
timer = setTimeout ("autoPlay()", showTime);

$("#buttonList li").click(function() {
 button=current;
 
 clickButton = $(this).attr('id');
 current = parseInt(clickButton.slice(1)); 
 if (current > button) {animateLeft(current,button)}
 if (current < button) {animateRight(current,button)}
});

function animateRight(current,button) {
 $('#p'+current).css("left",-width+"px");
 $('#p'+current).animate({"left": "0px"}, animatespeed, "easeInCubic");
 $('#p'+button).animate({"left": width+"px"}, animatespeed, "easeInCubic"); 
 setbutton()
}//end animateRight function

/* initiate the first time creation for flash */
createFlash(swfname, current, imgname);

$("#wrap").mouseover(function() {		
		ismouseover = true; 
    clearTimeout(timer);       
    $('#b'+current+' img').attr('src', 'utility/autoslide/image/button_red.gif');  
    });
    
$("#wrap").mouseout(function() {    
    ismouseover = false;
    $('#b'+current+' img').attr('src', 'utility/autoslide/image/button_ani.gif');
    timer = setTimeout ("autoPlay()", showTime);     
    });
       
$("#buttonList li").mouseover(function() {
	$('#'+ this.id + ' img').attr('src', 'utility/autoslide/image/button_red.gif'); //manually cast it into string	
		});
		
$("#buttonList li").mouseout(function() {
	if(this.id != 'b'+current){ $('#'+ this.id + ' img').attr('src', 'utility/autoslide/image/button_orig.gif');  }
		});

}//end onload function

function autoPlay () {
 
 //removeindex++;
 button = current;
 current++;

 
 if (current == (pictures+1) ) {current = 1} 
  animateLeft(current,button);
  timer = setTimeout ("autoPlay()", showTime);  
 }//end autoplay function
 
function animateLeft(current,button) {
   $('#p'+current).css("left",width +"px");
   $('#p'+current).animate({"left": "0px"}, animatespeed, "easeInCubic");
   $('#p'+button).animate({"left": -width+"px"}, animatespeed, "easeInCubic");
   setbutton()
}//end animateLeft function

function setbutton () {

	/* Need to re-declare here due to looping */
	swfname = $('#p'+ current).attr('obj_url');
	imgname = $('#p'+ current).attr('obj_alt_url');
  createFlash(swfname, current,	imgname);  
  
  $('#b'+button + ' img').attr('src', 'utility/autoslide/image/button_orig.gif'); 
  
 if(ismouseover){
  $('#b'+current+ ' img').attr('src', 'utility/autoslide/image/button_red.gif'); }
 else {	
  $('#b'+current+ ' img').attr('src', 'utility/autoslide/image/button_ani.gif'); }
}//end setbutton function

/* ================= created by WP - JAMES to display swfobject v2.2 ===================== */      
      
/* SWFObject v2.2 Create Flash */
function createFlash(swfname, current, imgname) {
			
			if (swfobject.hasFlashPlayerVersion("6")) {
			
			  /* remove the whole swfs div before creating any swfobject			  
			  var i = 0;
			  for (i=0;i<=pictures;i++) {			  
			  removeFlash(i);
			  }			*/  
				  
			  //check if attribute obj_type 
			  var check = $('#p'+ current).attr('obj_type');
			  
        if (check == 'flash'){ //got flash obj_type detected. then create the swfobject  		  
          
          // check if div for SWF hasn't been removed, if this is the case, create id for flash
		      var flashcreated = document.getElementById("hasFlash" + current);
				  if (!flashcreated) {
  				  
					  var flashe = document.createElement("div");
					  flashe.setAttribute("id", "hasFlash" + current);
					  document.getElementById("p" + current).appendChild(flashe);
  					
				  }//end of if-condition !flashcreated
  				
				  // create SWF
				  var att = { data: swfname, width:"880", height:"290" };
				  var par = { menu:"false", wmode:"transparent" };
				  var id = "hasFlash" + current; 
				  
				  swfobject.createSWF(att, par, id);	
				  				  				
				}//end of if-condition check == flash				
							
			}//end of if-condition hasFlashPlayerVersion
			else {
				//else: flash is not add on in the browser itself
				
				var imgcreated = document.getElementById("hasImage" + current);
					if (!imgcreated){
						
						var imge = document.createElement("img");
						imge.id = "hasImage" + current; 
						imge.src = imgname;
						
						document.getElementById("p" + current).appendChild(imge);
										
					}//end of if-condition imgcreated
			}//end of else-condition
						
		}//end of function createFlash

/* SWFObject v2.2 Remove Flash */		
function removeFlash(rindex) {
			swfobject.removeSWF("hasFlash" + rindex);
}//end of function removeFlash

