﻿	
	function initSlideShowMain(){
	
		pics.sort(function(){return 0.5 - Math.random()});
		var imgs = "";
		var showLock = false;
		var prevBp = false;
		for(var i=0; i<50; i++){
			imgs += '<div style="position:relative;float:left;width:75px;height:75px;overflow:hidden;cursor:pointer" onclick="window.location=\'galeria.php?s='+pics[i].split('#')[1]+'\'">';
			imgs += '	<img src="'+pics[i].split('#')[0]+'_s.jpg" style="opacity:.3;filter:alpha(opacity=30)"/>';
			imgs += '	<img src="'+pics[i].split('#')[0]+'_m.jpg" style="position:absolute;top:0;left:0;visibility:hidden;" class="bp"/>';
			imgs += '</div>';
		}
		var slideShow = document.getElementById('slideshow');
		slideShow.innerHTML += imgs + '<div style="clear:both;"></div>';
		var bp = []; 
		var bps = document.getElementsByTagName('img');
		var getBps = window.setInterval( function(){
			var cnt = 0;
			for(var i=0;i<bps.length;i++){
				if(bps[i].className == 'bp' && !bps[i].chk && (bps[i].offsetWidth >= 240 || bps[i].offsetHeight >= 180 )){
					bps[i].chk = true;
					bps[i].oWidth = bps[i].offsetWidth;
					bps[i].oHeight = bps[i].offsetHeight;
					if(bps[i].oWidth > bps[i].oHeight){
						bps[i].sHeight = 75;
						var rat = bps[i].oHeight / 75; 
						bps[i].sWidth = bps[i].oWidth / rat;
					}
					if(bps[i].oWidth < bps[i].oHeight){
						bps[i].sWidth = 75;
						var rat = bps[i].oWidth / 75; 
						bps[i].sHeight = bps[i].oHeight / rat;
					}
					if(bps[i].oWidth == bps[i].oHeight){
						bps[i].sWidth = 75;
						bps[i].sHeight = 75;
					}
					bps[i].style.width = bps[i].sWidth + 'px';
					bps[i].style.height = bps[i].sHeight + 'px';
					bps[i].bg = bps[i].parentNode.getElementsByTagName('span')[0];
					bps[i].prnt = bps[i].parentNode;	
					bps[i].prnt.chld = bps[i];
					if(bps[i].oHeight > 150 && bps[i].oWidth > 150)
						bp.push(bps[i]);
					
					if(bp.length == 50){
						window.clearInterval(getBps);
					}
				}	
			}
		}, 300);
		
		var show = function(trg){
			
			if(showLock)
				return false;
				
			showLock = true;	
			
			var full = document.createElement('div');
			var maxWidth = 750;
			var maxHeight = 375;
			var step = 1;
			var timeBuff = 0;
			var timeDiff = 0;
			if(!trg){
				showLock = false;
				return false;
			}
		
			full.innerHTML = '<img src="'+trg.src+'" style="position:relative;height:100%;width:100%;z-index:1000"/><div id="shdw"><div id="shdw_a"><div id="shdw_b"></div><div id="shdw_c"></div></div></div>';
			slideShow.appendChild(full);
			full.shdw = $id('shdw');
			with(full.style){
				position = 'absolute';
				width = trg.sWidth + 'px';
				height = trg.sHeight + 'px';
				top = trg.prnt.offsetTop + 'px';
				left = trg.prnt.offsetLeft + 'px';
				opacity = 1;			
			}
			with($id('shdw').style){
				position = 'absolute';
				left = '20px';
				top = '20px';
			}	
			with($id('shdw_a').style){
				position = 'absolute';
				bottom = '0px';
				right = '0px';
				width = '100%';
				height = '100%';
				background = 'url(properties/graphics/shdw.png) no-repeat bottom right';
			}		
			with($id('shdw_b').style){
				position = 'absolute';
				top = '-20px';
				right = '0px';
				width = '20px';
				height = '20px';
				background = 'url(properties/graphics/shdw.png) no-repeat -20px 0';
			}		
			with($id('shdw_c').style){
				position = 'absolute';
				bottom = '0px';
				left = '-20px';
				width = '20px';
				height = '20px';
				background = 'url(properties/graphics/shdw.png) no-repeat 0 0';
			}
				
			var animate = window.setInterval(function(){
				
				var curTime = new Date().getTime();
				
				if(timeBuff != 0){
					timeDiff = curTime - timeBuff;
					if(timeDiff <= 1) step = 1;
					else if(timeDiff <= 7) step = 2;
					else if(timeDiff <= 14) step = 3;
					else if(timeDiff <= 21) step = 4;
					else if(timeDiff <= 28) step = 5;
					else if(timeDiff >  28) step = 6;
				}
				timeBuff = curTime;

				var nextWidth = full.offsetWidth + step;
	
				var wStep = step;
				if(trg.oWidth != trg.oHeight){
					var nextHeight = trg.oHeight / (trg.oWidth / nextWidth);
					var hStep = nextHeight - full.offsetHeight;
				}
				else{
					hStep = wStep;
					nextHeight = nextWidth;
				}
				
				if(	nextWidth  < maxWidth && nextWidth  < trg.oWidth &&
					nextHeight < maxHeight && nextHeight < trg.oHeight ){
				
					full.style.width = nextWidth + 'px';
					full.style.height = nextHeight + 'px';
					full.shdw.style.width = full.offsetWidth + 'px';
					full.shdw.style.height = full.offsetHeight + 'px';					
					// left
					var lShift = (full.offsetWidth - 75) / 2;
					if(full.offsetLeft > 0)
						full.style.left = trg.prnt.offsetLeft - lShift + 'px';
					// top
					var tShift = (full.offsetHeight - 75) / 2;
					if(trg.prnt.offsetTop > 0)
						full.style.top = trg.prnt.offsetTop - tShift + 'px';
					// right
					var fRight = full.offsetLeft + full.offsetWidth;
					if(fRight + wStep - lShift > maxWidth)
						full.style.left = full.offsetLeft - (fRight - maxWidth) + 'px';
					// bottom
					var fBottom = full.offsetTop + full.offsetHeight;
					if(fBottom + hStep - tShift > maxHeight)
						full.style.top = full.offsetTop - (fBottom - maxHeight) + 'px';		
				}
				else{
					window.clearInterval(animate);
					var wait = window.setTimeout(function(){
						var op = 100;
						var fadeOut = window.setInterval(function(){								
							if(op > 0){
								op -= 5;
								full.style.opacity = op / 100;
								full.style.filter = 'alpha(opacity='+op+')';
							}
							else{
								slideShow.removeChild(full);
								showLock = false;
								window.clearTimeout(wait)
								window.clearInterval(fadeOut);
							}								
						}, 13);	
					}, 2000);
				}
			}, 1);
		}
		
		var introPic = $id('introPic');
		window.setTimeout(function(){
			var op = 100;
			var fadeOut = window.setInterval(function(){								
				if(op > 0){
					op -= 5;
					introPic.style.opacity = op / 100;
					introPic.style.filter = 'alpha(opacity='+op+')';
				}
				else{
					introPic.style.display = 'none';
					window.setInterval(function(){
						if(!showLock){
							var i = Math.floor(Math.random() * bp.length);
							show(bp[i]);
						}
					}, 20);
					window.clearInterval(fadeOut);
				}								
			}, 13);	
		}, 5000);
	}
	
	
	function initSlideShowSide(){
			
		var slideShow = document.getElementById('slideshow');
		var endPoint = 1;
		pics.sort(function(){return 0.5 - Math.random()});
		
		var addPic = function(i){
			var imgs = "";
			imgs += '<div id="sideShow'+i+'" style="position:relative;cursor:pointer;width:150px;overflow:hidden;background:url('+pics[i].split('#')[0]+'_m.jpg) no-repeat center top" onclick="window.location=\'galeria.php?s='+pics[i].split('#')[1]+'\'">';
			imgs += '<img src="'+pics[i].split('#')[0]+'_m.jpg" style="visibility:hidden;margin-bottom:20px;"/>';	
			imgs += '</div>';	
			slideShow.innerHTML += imgs; 
			return $id('sideShow'+i);
		}
		
		var nextPic = function(){
		
			var lastPic = $tn('div', slideShow)[0];
			var ops = lastPic.offsetHeight / 100;
			var opv = 100;
			endPoint++;
			if(endPoint >= pics.length) endPoint = 0;
			var hide = window.setInterval(function(){
				if(lastPic.offsetHeight > 4){
					opv -= ops;
					with(lastPic.style){
						height = lastPic.offsetHeight - 4 + 'px'; 
						opacity = opv / 100; 
						filter = 'alpha(opacity=' + opv + ')';
					}
				}
				else{
					slideShow.removeChild(lastPic);
					var next = addPic(endPoint);
					var opv_s = 0;
					with(next.style){
						opacity = 0;
						filter = 'alpha(opacity=0)';
					}
					var show = window.setInterval(function(){
						if(opv_s < 100){
							opv_s += 2;
							with(next.style){
								opacity = opv_s / 100;
								filter = 'alpha(opacity=' + opv_s + ')';
							}
						}
						else{
							window.clearInterval(show);
						}
					}, 13);
					window.clearInterval(hide);
				}
			}, 13);
		}
		for(var i=0; i<=endPoint; i++){
			addPic(i);		
		}
		var showReel = window.setInterval( function(){ nextPic() }, 5000);
	}
	
	
	function $id(trg){
		return document.getElementById(trg);
	}

	
	function $tn(name_,parent_){
		if(typeof(parent_)=='undefined') parent_=document;
		return parent_.getElementsByTagName(name_);
	}
	
