var timer;
var image_duration = 4000;
var image_id = 'cyclic_image';
var div_id = 'image_div';

var getElementsByClassName = function(class_name, child_tags, ele) {
	var tags = child_tags == undefined ? '*' : child_tags;
	var retnode = new Array();
	var myclass = new RegExp('\\b' + class_name + '\\b');
	var eles = ele.getElementsByTagName(tags);
	for (var i = 0; i < eles.length; i++) {
		var classes = eles[i].className;
		if (myclass.test(classes)) {
			retnode.push(eles[i]);
		}
	}
	return retnode;
};

function getStyle(obj, styleProp) {
	if (obj.currentStyle) {
		var y = obj.currentStyle[styleProp];
	}
	else if (window.getComputedStyle) {
		var y = document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
	}
	return y;
}

function basename(path, suffix) {
    // http://kevin.vanzonneveld.net
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}

function fileInfo(dirpath, filename, extension) {
	this.dirpath = dirpath;
	this.filename = filename;
	this.extension = extension;
	this.fullpath = dirpath + "/" + filename + extension;
}

function cache_image() {
	var imageObject = document.getElementById(image_id);
	var src = imageObject.src;
	var path = (imageObject.src).substring(0, (imageObject.src).lastIndexOf("/") + 1);
	var filename = 
		(imageObject.src).substring(
			(imageObject.src).lastIndexOf("/") + 1, 
			(imageObject.src).lastIndexOf(".")
		);
	var filenumber = parseInt(filename);
	filenumber = (filenumber >= 3) ? 1 : (filenumber + 1);
	var image = new Image();

	var target_src = path + filenumber.toString() + '.jpg';
	image.onLoad = startFade(target_src);
	image.src = target_src;
}

function startFade(target_src) {
	if(timer) { clearTimeout(timer); }
	timer = setTimeout("fadeOut('" + target_src + "');", image_duration);
}

function fadeOut(target_src) {
	var fadeOutDuration = 1000;
	if(navigator.userAgent.indexOf("MSIE") > -1) {
		var fade2 = new Spry.Effect.DoFade(div_id, {duration: fadeOutDuration, from: 100, to: 0});
	}
	var fade = new Spry.Effect.DoFade(image_id, {duration: fadeOutDuration, from: 100, to: 0, finish:
		function() {
			if(timer) { clearTimeout(timer); }
			var imageObject = document.getElementById(image_id);
			imageObject.src = target_src;
			timer = setTimeout("fadeIn('" + target_src + "');", fadeOutDuration);
		}});
}

function fadeIn(target_src) {
	var fadeInDuration = 1000;
	if(navigator.userAgent.indexOf("MSIE") > -1) {
		var fade2 = new Spry.Effect.DoFade(div_id, {duration: fadeInDuration, from: 0, to: 100});
	}
	var fade = new Spry.Effect.DoFade(image_id, {duration: fadeInDuration, from: 0, to: 100, finish:
		function() {
			if(timer) { clearTimeout(timer); }
			timer = setTimeout('cache_image();', fadeInDuration);
		}});
}

function preLoader() {
	var imgArray = new Array();
	var tabs = getElementsByClassName("AccordionPanelTab", "DIV", document.getElementById("Accordion1"));
	for (var i = 0; i < tabs.length; i++) {
		//var imageURL = tab.currentStyle.backgroundImage;
		var stylename = navigator.userAgent.indexOf("MSIE") > -1 ? 'backgroundImage' : 'background-image'; 
		var image_url = str_replace(
				new Array('url', '(', ')', ';'),
				'',
				getStyle(tabs[i], stylename)
		);
		var fbn = basename(image_url);
		var file_info = new fileInfo(
			image_url.substr(0, image_url.lastIndexOf('/')),
			fbn.substr(0, fbn.lastIndexOf('.')),
			fbn.substr(fbn.lastIndexOf('.'), fbn.length)
		);
																				
		var img = new Image();
		img.src = str_replace(file_info.filename, file_info.filename +'_hover', file_info.fullpath);
				
		img = new Image();
		img.src = str_replace(file_info.filename, file_info.filename +'_focus', file_info.fullpath);
	}
}

cache_image();
// preLoader();
