// scripts.js
// Justin Evans
// TribeAgency


// Thumbnail Gallery Motion functions "moveRight" and "moveLeft" take number of thumbnails present

// Initialize variables

var startLeft = 0;
var imgWidth = 80; // This will need updating if thumbnail size changes!
var divWidth = 900;

//This function slides a div to the right

function moveRight() {

	// Set variables
	divWidth = 900;
	imgWidth = 80;
   	startLeft = $('Scroller').offsetLeft;
	
	// Debug variable values
	
	// alert("Image Width: " + imgWidth + " | divWidth: " + divWidth + " | startLeft: " + startLeft);
   	
   	// Move if possible
   	
   	if ((startLeft>-((divWidth / 2) - imgWidth))||startLeft==1) {
   		
   		// If startLeft has been offset by 1, decrement by 1
   		
   		if(startLeft%2!=0){
   			startLeft-=1;
   		}
   		
   		// Subtract one image's width from startLeft to move div right
   	
   		startLeft=startLeft-imgWidth;
   		
   		// Scroll right
   		
   		new Rico.Effect.Position('Scroller', startLeft, null, 200, 11);
   	}
}

//This function slides a div to the left

function moveLeft() {
	
	// Set variables
	
	divWidth = 900;
	imgWidth = 80;
   	startLeft = $('Scroller').offsetLeft;
	
	// Debug variable values
	
	// alert("Image Width: " + imgWidth + " | divWidth: " + $('Scroller').style.width + " | startLeft: " + startLeft);
	
	// Move if possible
	
	if (startLeft<0) {

   		// If startLeft has been offset by 1, increment by 1

   		if(startLeft%2!=0){
   			startLeft+=1;
   		}
	
		// Add one image's width to startLeft to move div left
		
		startLeft=startLeft+imgWidth;

		// Set startLeft to 1 if it is 0 to circumvent bug in Rico
		
		if (startLeft==0) {
			startLeft=1;
		}
		
		// Scroll left
		
		new Rico.Effect.Position('Scroller', startLeft, null, 200, 11);
	}
}

// CSS columns - maintain proper height

function setColumnHeight(){
	if($('contentleft') && $('contentright')){
		if($('contentright').offsetHeight > $('contentleft').offsetHeight){
			$('contentleft').style.height = $('contentright').offsetHeight + "px";
		} else {
			$('contentright').style.height = $('contentleft').offsetHeight + "px";
		}
	} else if($('productleft') && $('productright')){
		if($('productright').offsetHeight > $('productleft').offsetHeight){
			$('productleft').style.height = $('productright').offsetHeight + "px";
		} else {
			$('productright').style.height = $('productleft').offsetHeight + "px";
		}
	}

}

// SWF Object

function activateFlash(strSwf, strTitle){
    if($('theme')){
	    var fo = new SWFObject("/flash/" + strSwf, strTitle, "720", "240", "6", "#ffffff");
	    fo.addParam("wmode", "opaque");
	    fo.write("theme");
	}
}

// Product functions

function swapProduct(imgName, thumbId){
    if($('detail') && $('thumb'+thumbId)){
    
        // Set detail area to specified image
    
        $('detail').innerHTML = '<img src="/images/products/' + imgName + '" alt="" />';
        
        // Remove all "current" classes from thumbnails
        
        var i = 0;
        for(i=1;i<4;i++){
            if($('thumb'+i)){
                $('thumb'+i).className="";
            }
        }
        
        // Set specified thumb to "current"
        
        $('thumb'+thumbId).className="current";
    }
}


function swapHeader() {
    var ran_number=Math.floor(Math.random()*4)+1;
    $('header').className="header"+ran_number;
}



//Executes multiple functions on body load

function initialize() {
    activateFlash('babyplanet_v1.swf','BabyPlanet');
    setColumnHeight();
    swapHeader(); 
}