var shRecordsArr = new Array();
var currSlide = -1;

var isOpera = self.opera
var isIE = document.all && !isOpera;

if(isIE || isOpera) {	
	window.attachEvent("onload", initS);
} else {	
	window.addEventListener("load", initS, true);
}


function addSlideShowBlock() {
	//var d = new Date();
	var uniqId = (new Date()).getMilliseconds();
	
	while(/*uniqId < 100 ||*/ document.getElementById('shMainTbl'+uniqId)) {
		uniqId = (new Date()).getMilliseconds();
	}
	
	var divContainerHtml = '<div id="slideShowContainer"></div>';
	
	var tblContainerHtml = '</pre><table class="MainTbl w100" cellpadding=0 cellspacing=0 border=0 id="MainTbl">'+
							'<col width="400"><col width="548">'+	
								'<tr>'+
									'<td class="vT"><div id="BigLImgBlock" class="BigLImgBlock"><div class="BigLImg" id="BigLImg"></div><div id="BigLImgLoader" class="BigLImgLoader"></div></div></td>'+
									'<td>'+
										
										'<table cellpadding=0 cellspacing=0 border=0 class="h100 w100">'+
											'<tr>'+
												'<td class="TopTxtField vT" id="TopTxtField"></td>'+
											'</tr>'+
											'<tr>'+
												'<td class="BotTxtField vT" id="BotTxtField"></td>'+
											'</tr>'+
										'</table>'+
									
									'</td>'+
								'</tr>'+
							'</table>';
	
	var blockHtml = '</pre><table class="shMainTbl w100" cellpadding=0 cellspacing=0 border=0 id="shMainTbl'+uniqId+'">'+
					'<col width="400"><col width="548">'+	
						'<tr>'+
							'<td class="shBigImg vT" id="shBigImg'+uniqId+'"></td>'+
							'<td>'+
								
								'<table cellpadding=0 cellspacing=0 border=0 class="h100 w100">'+
									'<tr>'+
										'<td class="shTopTxtField vT" id="shTopTxtField'+uniqId+'"></td>'+
									'</tr>'+
									'<tr>'+
										'<td class="shBotTxtField vT" id="shBotTxtField'+uniqId+'"></td>'+
									'</tr>'+
								'</table>'+
							
							'</td>'+
						'</tr>'+
					'</table>'+
					'<script language="javascript">'+
						'createSlideShow('+uniqId+')'+
					'</script>';
					
	if (!document.getElementById('MainTbl')) {
		//document.write(divContainerHtml + blockHtml);
		//document.getElementById('slideShowContainer').innerHTML = tblContainerHtml;
		document.write(tblContainerHtml + blockHtml);
	} else {
		document.write(blockHtml);		
	}
	document.close();
}


function createSlideShow(pCrId) {
	if (!parent.document.getElementById('templateframe')) {
		
		var leftImage = document.getElementById('shBigImg'+pCrId).innerHTML;
		var topText = document.getElementById('shTopTxtField'+pCrId).innerHTML;
		var botText = document.getElementById('shBotTxtField'+pCrId).innerHTML;
		
		document.getElementById('MainTbl').style.background = "none";		

		shRecordsArr[shRecordsArr.length] = new slideShowRecord(pCrId, leftImage, topText, botText);		

		//document.getElementById('shMainTbl'+pCrId).style.display = "none";
		var shMainTbl = document.getElementById('shMainTbl'+pCrId);
		shMainTbl.parentNode.removeChild(shMainTbl);
	}	
}

function testArray() {
	var str = '';
	for(i=0; i<shRecordsArr.length; i++) {
		str += shRecordsArr[i].uId;
		str += shRecordsArr[i].imgHtml;
		str += shRecordsArr[i].topTxt;
		str += shRecordsArr[i].botTxt;
		alert("Result " + i + ": " + str);
		
		
		str = '';
	}	
		
}


/*function getInnerImgSrc(pTdId) {
	var tdChildNodes = document.getElementById('shBigImg'+pTdId).childNodes;
	var result;
	for(i=0; i<tdChildNodes.length; i++)
		if(tdChildNodes[i].nodeType == 1 && tdChildNodes[i].tagName == "IMG")
			return tdChildNodes[i].src;		
	
	//return (default = 'img_0/1px.gif');
}*/


function doNothing() {
	return false;	
}

function slideShowRecord(pRecId, pImgHtml, pTopTxt, pBotTxt) {
	this.uId = pRecId?pRecId:'';
	this.imgHtml = pImgHtml?pImgHtml:'';
	this.topTxt = pTopTxt?pTopTxt:'';
	this.botTxt = pBotTxt?pBotTxt:'';
	this.imgSrc = /src="([^"]*)"/.exec(this.imgHtml)[1] || null;
	this.loadAttemptCnt = 0;
}


function initS() {
	if (!parent.document.getElementById('templateframe') && document.getElementById('MainTbl')) {
		
		document.getElementById('EBPT').style.overflow = "hidden";
	
		if(isIE) {
			document.getElementById('BigLImg').style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=0.0,overlap=0.0)";
			document.getElementById('BigLImg').filters[0].Apply();				
		}
		
		changeSlide();
	} else {

	}	
}

function getNextSlideIndex(pCurrent) {
	var next;
	pCurrent = (pCurrent) ? pCurrent : currSlide;
	
	if((pCurrent + 1) >= shRecordsArr.length ) {
		next = 0;		
	} else {
		next = pCurrent + 1;
	}
	return next;
}

function preload(index) {
	var html = shRecordsArr[index].imgHtml + shRecordsArr[index].topTxt + shRecordsArr[index].botTxt;
	
	if(typeof shRecordsArr[index].loaded == 'undefined') {
		shRecordsArr[index].loaded = false;
	
		var loader = new ImageSetLoader(
			html,
			function() {
				shRecordsArr[index].loaded = true;
			}
		);
		loader.load();
		
		return true;
	}
	
	return false;
}

function showLoadingIndicator() {
	document.getElementById('BigLImgLoader').style.display = 'block';
}

function hideLoadingIndicator() {
	document.getElementById('BigLImgLoader').style.display = 'none';
}

function changeSlide() {
	if(isIE) {
		document.getElementById('BigLImg').filters[0].Apply();
		//document.getElementById('BigLImg').filters[0].Play();
	}
	
	var next = getNextSlideIndex();
	
	if(document.getElementById('MainTbl')) {
		
		if(!shRecordsArr[next].loaded) {
			++shRecordsArr[next].loadAttemptCnt;
			if(typeof shRecordsArr[next].loaded == 'undefined') {				
				preload(next);
			}
			
			showLoadingIndicator();
			
			// If we can't load images in 10*500=5 seconds, we just skip next slide!
			if(shRecordsArr[next].loadAttemptCnt > 10) {				
				currSlide = next;
			}
			
			setTimeout("changeSlide()", 500);
			return false;
		}
		hideLoadingIndicator();
	
		document.getElementById('BigLImg').innerHTML = shRecordsArr[next].imgHtml;
		document.getElementById('TopTxtField').innerHTML = shRecordsArr[next].topTxt;
		document.getElementById('BotTxtField').innerHTML = shRecordsArr[next].botTxt;	
		if(isIE)
			document.getElementById('BigLImg').filters[0].Play();
		
		currSlide = next;
		preload(getNextSlideIndex(currSlide));		
		setTimeout("changeSlide()", 6000);
	}
}
