// JavaScript Document

function rollOver(rollOverImage) {
		newSrc = rollOverImage.src.replace(new RegExp(".gif\\b"), "_f2.gif");
		rollOverImage.src = newSrc;
}

function rollOut(rollOutImage) {
		newSrc = rollOutImage.src.replace(new RegExp("_f2.gif\\b"), ".gif");
		rollOutImage.src = newSrc;
}

/* multiple onload event handler */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function openWindow(url,name,params)
{
	newwindow=window.open(url,name,params);
	if (window.focus) {newwindow.focus()}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HOME PAGE IMAGE TRANSITION //////////////////////////////////////////////////////////////////////////////////////////////////////


// GLOBAL VARIABLES

// Set path to home_matrixes directory
imagePath = '/assets/brand_36/images/home_matrixes/';

// Time before a transition begins
periodTime = 5000;

// How quickly the transition happens
transTime = 75;


// DO NOT MODIFY THESE GLOBAL VARIABLES ////////////////////////////////////////////////////////////////////////////////////////////
timer_x = 0;
imgTarget = null;
titleTarget = null;

/* homepage imageswap handler */
function homeTransInit() {
	
	// Check to see if browser can handle dom method, and that there is at least one h1 on the page
	if (document.getElementsByTagName && document.getElementsByTagName('h1').length > 0) {
		
		// Set global titleTarget
		var titles = document.getElementsByTagName('h1');
		
		for (var i=0; i<titles.length; i++) {
			if (titles[i].className == 'imageSwap') titleTarget = titles[i];	
		}
		
		// Check to see if titleTarget is no longer null, and that the h1 contains an image with an id of 'montage'
		if (titleTarget != null && titleTarget.getElementsByTagName('img')[0].id == 'montage') {
			
			// Set global imgTarget
			imgTarget = document.getElementById('montage');
			
			// Set the inital class of the imgTarget
			imgTarget.className = 's0';
			
			// When imgTarget loads in the DOM trigger the rest of the script
			imgTarget.onLoad = setNewBackgroundImage();
			
		}
	
	}
	
}

function setNewBackgroundImage() {
	
	// Generate a random number between 1 and 5
	var randNum = Math.floor((Math.random()*4)+1);
	
	// Create the path to a random image from the home_matrixes directory
	var path = imagePath+'mag_cover_matrix_'+randNum+'.jpg';
	
	if (path != imgTarget.getAttribute('src')) {
	
		// Set the background image of the titleTarget to the images we'll be transitioning to
		titleTarget.style.backgroundImage = 'url("'+path+'")';
		
		setTimeout("performTrans()", periodTime);
	
	} else {
	
		setNewBackgroundImage();
	
	}
	
}

function performTrans() {
	
	// If the timer is less than 10, either it hasn't started or hasn't completed the transition
	if (timer_x <= 10) {
		
		imgTarget.className = 's'+timer_x;
		timer_x++;
		setTimeout("performTrans()", transTime);
		
	} else {
		
		// reset timer back to zero
		timer_x = 0;
		
		// set the source of the image to the background image that we just transitioned to
		imgTarget.setAttribute('src', titleTarget.style.backgroundImage.split('(')[1].split(')')[0]);
		
		// Set it's class to it has 100% opacity
		imgTarget.className = 's0';
		
		// Set a new background image to transition to and start over
		setNewBackgroundImage();
		
	}
	
}






