// Nimi: Image Slideshow
// Versioon: 0.5
// kribas Hannes
// 13-12-2005
// Vajab Timer klassi: http://www.codingforums.com/archive/index.php?t-10531.html

/* N2ide 1 - slaidsh6u tyyp 1:
<script type="text/javascript" src="timer.js"></script>
<script type="text/javascript" src="random_img.js"></script>
pilt1 = new ImageSlideshow("cool", "td");
pilt1.set_timeout(1000);
pilt1.set_height(100);
pilt1.set_width(200);
pilt1.add_image("1.jpg");
pilt1.add_image("2.jpg");
pilt1.init();

 * N2ide 2 - slaidis6hu tyyp 2:
pilt2 = new ImageSlideshow("cool", "div");
pilt2.set_timeout(1000);
pilt2.set_height(100);
pilt2.set_width(200);
pilt2.add_image("1.jpg");
pilt2.add_image("2.jpg");
 pilt2.init();
*/

// TODO
// css_property_to_javascript_reference() l6puni.


// div_id - unikaalne div, mille sisse pilt l2heb
// images - piltide aadresside massiiv
// strSlideshowType - pildid
function ImageSlideshow(div_id, strSlideshowType)
{
	this.div_id = div_id;
	this.arrImages = new Array();
	this.intTimeoutDefault=1000;
	this.counter=0;
	this.strSlideshowType=strSlideshowType;
	this.intDivHeight=false;
	this.intDivWidth=false;
	this.arrStyles=new Array();
	this.arrErrors=new Array();
	
	this.arrErrors[1] = "Viga! '"+this.strSlideshowType+"' on vigane slaidishõu tüüp. Hetkel on olemas td, div ja img."
	this.arrErrors[2] = "Viga! Lisa konteineri kommentaari lahtrisse piltide vaheldumise aeg millisekundites. Kasutan vaikimisi 1000."
	
	this.timer = new Timer(this);

	// meetodid
	this.init = init;
	this.set_timeout = set_timeout;
	this.randomizearray = randomizearray;
	this.hell_or_heaven = hell_or_heaven; // brauserikontroll
	this.displaysplashes_as_img_notIE = displaysplashes_as_img_notIE;
	this.displaysplashes_as_img = displaysplashes_as_img;
	this.displaysplashes_as_div_background = displaysplashes_as_div_background;
	this.displaysplashes_as_td_background = displaysplashes_as_td_background;
	this.choose_slideshow_type = choose_slideshow_type;
	this.set_height = set_height;
	this.set_width = set_width;
	this.add_css = add_css;
	this.arrStyles_to_str = arrStyles_to_str;
	this.apply_css_to_object = apply_css_to_object;
	this.css_property_to_javascript_reference = css_property_to_javascript_reference;
	this.add_image = add_image;
} // ImageSlideshow

/**
 * Kui k6ik muutujad on paigas, pannakse init() abil script t88le.
 */
function init ()
{
	this.randomizearray(this.arrImages);
	this.choose_slideshow_type(this.strSlideshowType);
}

/**
 * Lisab pildid this.arrImages massiivi.
 */
function add_image(strSrc)
{
	if (strSrc.length>0)
	{
		var objImage = new Image();
		objImage.src = strSrc;
		this.arrImages[this.arrImages.length] = objImage;
		return true;
	}
	else
		return false;
}

/**
 * Siin valitakse slaidsh6u tyyp.
 *
 * Hetkel on v6imalik valida kahe slaidsh6u vahel. Yks t88tab div taustana, teine l2bi img tag'i.
 * Div taustana on siis hea kasutada, kui pildi suurused erinevad, kuid objekti suurus ei tohi
 * muutda. St kui pilt on suurem, kui n2htav osa, siis yleliigne osa kaob lihtsalt div'i alla.
 * L2bi pildi objekti n2eb ie's huvitavat fade effekti.
 */
function choose_slideshow_type(strSlideshowType)
{
	if (strSlideshowType=="img")
	{
		var strHtml="";
	
		strHtml = '<div id="'+this.div_id+'"><img src="'+this.arrImages[0]+'" alt="" id="splashimage_'+this.div_id+' style="'+this.arrStyles_to_str()+'""></div>';
		document.write(strHtml);
		this.hell_or_heaven();
		return 0;
	}

	if (strSlideshowType=="div")
	{
		var strHtml=""
		
		strHtml = '<div id="'+this.div_id+'"></div>';
		document.write(strHtml);
		this.timer.setTimeout("displaysplashes_as_div_background", 0, this.counter, this.arrImages, this.div_id);
		return 0;
	}
	if (strSlideshowType=="td")
	{
		var strHtml="";
		
		strHtml = '<td id="'+this.div_id+'"></div>';
		document.write(strHtml);
		if (this.arrImages.length > 0)
			this.timer.setTimeout("displaysplashes_as_td_background", 0, this.counter, this.arrImages, this.div_id, this.arrStyles);
		return 0;
	}
	
	alert (this.arrErrors[1]);
}

/**
 * Div'i v6i pildi k6rgus seadistamine.
 */
function set_height (intDivHeight)
{
	if (this.intDivHeight=intDivHeight)
		return true;
	else
		return false;
}

/**
 * Div'i v6i pildi pikkuse seadistamine.
 */
function set_width (intDivWidth)
{
	if (this.intDivWidth=intDivWidth)
		return true;
	else
		return false;
}

/**
 * Seadistab piltide vaheldumise intervalli millisekundites.
 */
function set_timeout (mytimeout)
{
	if (mytimeout)
		this.intTimeoutDefault = mytimeout;
	else
		alert (this.arrErrors[2]);
}

/**
 * Kasutaja lisab siin globaalsesse muutujasse css'i.
 */
function add_css (strStyleName, strStyleValue)
{
	var arrStyle = new Array();
	arrStyle[0] = strStyleName;
	arrStyle[1] = strStyleValue;
	
	this.arrStyles[this.arrStyles.length] = arrStyle;
	return true;
}

/**
 * Kasutaja lisab siin globaalsesse muutujasse css'i.
 *
 * Kasutatakse yx kord funktsioonis choose_slideshow_type()
 */
function arrStyles_to_str ()
{
	var k = this.arrStyles.length;
	
	var strOutput="";
	
	for (i=0;i<k;i++)
	{
		strOutput = strOutput+this.arrStyles[i][0]+': '+this.arrStyles[i][1]+'; ';
	}
	return strOutput;
}

/**
 * Parse css'i.
 */
function apply_css_to_object (objObject)
{
	var k = this.arrStyles.length;
	
	if (k>0)
	{
		for (i=0;i<k;i++)
		{
			eval ( 'objObject.style.'+css_property_to_javascript_reference(this.arrStyles[i][0])+' = "'+this.arrStyles[i][1]+'";' );	
		}
	}
}

/**
 * Muudab css'i stiili nimetused javascripti omadeks ymber
 *
 * Poolik funktsioon.
 */
function css_property_to_javascript_reference (strStyleName)
{
	var strOutput = strStyleName;

	if (strStyleName=="background-attachment")
		strOutput = "backgroundAttachment";
	if (strStyleName=="background-color")
		strOutput = "backgroundColor";
	if (strStyleName=="background-image")
		strOutput = "backgroundImage";
	if (strStyleName=="background-position")
		strOutput = "backgroundPosition";
	if (strStyleName=="background-repeat")
		strOutput = "backgroundRepeat";
	if (strStyleName=="border-bottom")
		strOutput = "borderBottom";
	if (strStyleName=="vertical-align")
		strOutput = "verticalAlign";
		
	return strOutput;
}


/**
 * Keerab massiivi elemendid segamini.
 */
function randomizearray(arr)
{
	var i = arr.length;
	while(i--){
	        var j = Math.floor(Math.random() * ( i + 1 ));
	        var arr_tempi = arr[i];
	        var arr_tempj = arr[j];
	        arr[i] = arr_tempj;
	        arr[j] = arr_tempi;
	}
}

/**
 * IE kontroll.
 */
function hell_or_heaven ()
{
	var ieImages = new Array(); // ie effektide tarvis loome Image objektide massiivi
	
	if(navigator.appVersion.indexOf("Win")>=0 && parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE ")+5), 10)>=4)
	{
		for(i=0;i<this.arrImages.length;i++)
		{
			ieImages[i] = new Image();
            ieImages[i].src = this.arrImages[i];
		}
	
		this.timer.setTimeout("displaysplashes_as_img", 0, this.counter, ieImages, this.div_id);
	}
	else
	{
		this.timer.setTimeout("displaysplashes_as_img_notIE", 0, this.counter, this.arrImages, this.div_id);
	}

}

/**
 * Muudab pilte pildi objekti kaudu k6ikidele brauseritele va IE
 */
function displaysplashes_as_img_notIE(local_count, local_images, local_div_id)
{
	var img_bigsplash = document.getElementById(local_div_id).getElementsByTagName('img');

	if (local_images.length==local_count)
		local_count = 0;

	if (this.intDivHeight==false)
		height = local_images[local_count].height;
	else
		height = this.intDivHeight;

	if (this.intDivWidth==false)
		width = local_images[local_count].width;
	else
		width = this.intDivWidth;

	img_bigsplash[0].src = local_images[local_count].src;
	img_bigsplash[0].height = height;
	img_bigsplash[0].width = width;

	local_count++;

	//window.setTimeout(this.displaysplashes_notIE, this.timeout);
	this.timer.setTimeout("displaysplashes_as_img_notIE", this.intTimeoutDefault, local_count, local_images, local_div_id);
}

/**
 * Muudab pilte pildi objekti kaudu IE jaoks.
 */
function displaysplashes_as_img(local_count, local_images, local_div_id)
{
	if (local_images.length==local_count)
	{
		local_count = 0;
	}
	
	if(navigator.userAgent.indexOf("MSIE 4.0") > 1)
		eval ("splashimage_"+local_div_id+".style.filter = 'revealTrans(transition=7,duration=2)';");
	else
		eval ("splashimage_"+local_div_id+".style.filter = 'progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.00,wipestyle=0,motion=reverse)';")
		
	eval ("splashimage_"+local_div_id+".filters[0].Apply(); "+
	"splashimage_"+local_div_id+".style.display = 'block';"+
	"splashimage_"+local_div_id+".filters[0].Play();"+
	"splashimage_"+local_div_id+".src = local_images[local_count].src;");
    

	local_count++;
	this.timer.setTimeout("displaysplashes", this.intTimeoutDefault, local_count, local_images, local_div_id);	
}

/**
 * Muudab pilte div'i taustana.
 */
function displaysplashes_as_div_background (local_count, local_images, local_div_id)
{
	var objDiv = document.getElementById(local_div_id);

	if (local_images.length==local_count)
		local_count = 0;
	
	if (this.intDivHeight==false)
		height = local_images[local_count].height;
	else
		height = this.intDivHeight;

	if (this.intDivWidth==false)
		width = local_images[local_count].width;
	else
		width = this.intDivWidth;

	objDiv.style.background = "url("+local_images[local_count].src+")";
	objDiv.style.height = height+"px";
	objDiv.style.width = width+"px";

	local_count++;

	this.timer.setTimeout("displaysplashes_as_div_background", this.intTimeoutDefault, local_count, local_images, local_div_id);
}

/**
 * Muudab pilte tabeli td'i taustana.
 */
function displaysplashes_as_td_background (local_count, local_images, local_div_id, local_css)
{
	var objDiv = document.getElementById(local_div_id);

	if (local_images.length==local_count)
		local_count = 0;
	
	if (this.intDivHeight==false)
		height = local_images[local_count].height;
	else
		height = this.intDivHeight;

	if (this.intDivWidth==false)
		width = local_images[local_count].width;
	else
		width = this.intDivWidth;

	objDiv.style.background = "url("+local_images[local_count].src+")";
	objDiv.style.height = height+"px";
	objDiv.style.width = width+"px";

	this.apply_css_to_object(objDiv);

	local_count++;

	this.timer.setTimeout("displaysplashes_as_td_background", this.intTimeoutDefault, local_count, local_images, local_div_id, local_css);
}
