var shRecordsArr = new Array();
var currSlide = 0;

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="BigLImg vT" id="BigLImg"></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";
		/*document.getElementById('shTopTxtField'+pCrId).style.border = "none";
		document.getElementById('shBotTxtField'+pCrId).style.border = "none";
		document.getElementById('shBigImg'+pCrId).style.border = "none";*/
						
		shRecordsArr[shRecordsArr.length] = new slideShowRecord(pCrId, leftImage, topText, botText);
		
		//testArray();
		document.getElementById('shMainTbl'+pCrId).style.display = "none";		
	}	
}

function testArray() {
	var str = '';
	for(i=0; i<shRecordsArr.length; i++) {
		str += shRecordsArr[i].uId;
		str += shRecordsArr[i].imgSrc;
		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, pImgSrc, pTopTxt, pBotTxt) {
	this.uId = pRecId?pRecId:'';
	this.imgSrc = pImgSrc?pImgSrc:'';
	this.topTxt = pTopTxt?pTopTxt:'';
	this.botTxt = pBotTxt?pBotTxt:'';
}

/* 
//Она уже определена в файле motiongallery.js
function getIndex(pId) {
	for(i=0; i<gRecArray.length; i++)
		if(gRecArray[i].uId == pId)
			return i;
}
*/

function initS() {
	if (!parent.document.getElementById('templateframe')) {
		
		document.getElementById('EBPT').style.overflow = "hidden";
		
		if(document.getElementById('MainTbl')) {			
			document.getElementById('BigLImg').innerHTML = shRecordsArr[currSlide].imgSrc;
			document.getElementById('TopTxtField').innerHTML = shRecordsArr[currSlide].topTxt;
			document.getElementById('BotTxtField').innerHTML = shRecordsArr[currSlide].botTxt;
			
			//currSlide = 0;
			if(isIE) {
				document.getElementById('BigLImg').style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=1.0,overlap=1.0)";
				document.getElementById('BigLImg').filters[0].Apply();				
			}
		}
		
		//for(i=0; i<10; i++)
		//currSlide = 0;
			setTimeout("changeSlide()", 4000);
			//alert("2: "+currSlide);
	} else {
		//document.getElementById('MainTbl').style.display = "none";
	}
	
	
}


function changeSlide() {
	if(isIE) {
		document.getElementById('BigLImg').filters[0].Apply();
		document.getElementById('BigLImg').filters[0].Play();
	}
	if((currSlide + 1) >= shRecordsArr.length ) {
		currSlide = 0;		
	} else {
		++currSlide;
	}
	//alert(currSlide + ": " + shRecordsArr[currSlide].topTxt);
	if(document.getElementById('MainTbl')) {
		document.getElementById('BigLImg').innerHTML = shRecordsArr[currSlide].imgSrc;
		document.getElementById('TopTxtField').innerHTML = shRecordsArr[currSlide].topTxt;
		document.getElementById('BotTxtField').innerHTML = shRecordsArr[currSlide].botTxt;	
		if(isIE)
			document.getElementById('BigLImg').filters[0].Play();
		setTimeout("changeSlide()", 4000);
	}
}

/*
function getImg() {
	var tdChildNodes = document.getElementById('BigLImg').childNodes;
	
	for(i=0; i<tdChildNodes.length; i++)
		if(tdChildNodes[i].nodeType == 1 && tdChildNodes[i].tagName == "IMG")
			return tdChildNodes[i];
}
*/