/**
 * @author Tan
 */
var element = null;
var url = '<img src="http://www.duilawyerinarizona.com/wp-content/themes/Gagic/images/image';
var topImage = "invisible";
var x = 1;
var y = 0;
var speed = 3500;

/*Variables for Video playlist*/

var menu = null;
var increment = 0;
var control = 0;/*Used to limit the length of the scrolling animation*/
var currentMargin = 0;

function fadein() {
	y += .1;
	z = y*100;
	element.style.opacity = y;
	element.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=' + z + ')';
	if (element.style.opacity < 1) {
		setTimeout(fadein,40);
	} else {
		setTimeout(swap,speed);
	}
}

function fadeout() {
	y -= .1;
	z = y*100;
	element.style.opacity = y;
	element.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=' + z + ')';
	if (element.style.opacity > 0) {
		setTimeout(fadeout,40);
	} else {
		setTimeout(swap,speed);
	}
}

function swap() {
	if (x < 3) {
		x++;
	} else {
		x = 1;
	}
	
	if (topImage == "invisible") {
		element.innerHTML = url + x + '.jpg" alt="" />';
		topImage = "visible";
		fadein();
	} else {
		topImage = "invisible";
		document.getElementById("first").innerHTML = url + x + '.jpg" alt="" />';
		fadeout();
	}
}

function init() {
	element = document.getElementById("second");
	element.style.opacity = 0;
	element.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=0)';
	setTimeout(swap,speed);
}

/*Functions for the video playlist*/

function scroll() {
	/*Restricts scrolling beyond the maximum range*/
	if ((currentMargin + increment) > 0 || (currentMargin + increment) < -1728) {
		return
	}
	control += increment;
	currentMargin += increment;
	menu.style.marginLeft = currentMargin + 'px';
	
	if (Math.abs(control) == 216) {
		control = 0;/*Resets the animation length limit*/
		return
	} else {
		setTimeout(scroll, 1);
	}
}

function begin(number) {
	/*Does not allow scrolling if the animation is already in play*/
	if (currentMargin % 216 === 0) {
		increment = number;
	} else {
		return
	}
	scroll();
}

function swap_vid(q) {
	document.getElementById('video_box').style.marginLeft = (q * (-288)) + 'px';
}

function init2() {
	menu = document.getElementById('video_menu');
	menu.style.marginLeft = 0 + 'px';
}

