window.addEvent('domready', function() {
        startPresentation();
	}); 


var preloadedFlag = false;
var imageListStatic = new Array("img/arrowL.png","img/arrowL_over.png","img/arrowR.png","img/arrowR_over.png","img/arrowA_over.png","img/arrowA_over.png");
for(i=1;i<=12;i++)
    imageListStatic.push("img/wave/dot_" + i + ".png");    

var i=0;
var im;
var nrOb=0;
var currentMyImage;
var numerPres=1;

var imageList = new Array();
var elementList = new Array();
var imageSrcList = new Array();
var iloscIOb = 0;
var points = new Array();
var imgWidth = 0;
var imgHeight = 0 ;

var slideShowInterval;

var is3dNow=-1;
//-----
function postLoadAction()
{
	
}
function startPresentation()
{
	$('smallCover').set('tween',{duration: 1200});
    var fx = new Fx.Morph('smallCover', {duration:1200, wait:false});
    fx.start({'background-color': ["#000","#fff"]});
    $('smallCover').fade(0);
	
	
	if(getURLParam("idPres")!="")
	{
		numerPres = getURLParam("idPres") ;		
	}
	preloadedFlag = Get_Cookie("imagesLoaded_" + numerPres);
	
	$('popup').fade('hide');
	
	hideArrows('hide');
	getPresentationData();		
	
	
	
	$('cover').addEvent('click',function(){
                                        hidePopup();    
                                    });
    $('popup').addEvent('click',function(){
                                        hidePopup();    
                                    });
	
	var allInGallery = $$("#imageContent *");
	allInGallery.each(function(el){
		el.addEvent('mouseover',function(){										
										clearInterval(slideShowInterval);
                                        //showArrows();    
                                    });
		el.addEvent('mouseout',function(){
                                        //hideArrows();
										clearInterval(slideShowInterval);
										slideShowInterval = setInterval("autoLoadNext()",5000);
                                    });
	});
	
 
	var arrows = $$(".arrows img");
	arrows.each(function(el)
				{
					el.addEvent('click',function()
									{
										clearInterval(slideShowInterval);
										showImage(el.id);
										
									});
					el.addEvent('mouseover',function()
									{
										el.src="img/arrow"+ el.id +"_over.png";
									});
					el.addEvent('mouseout',function()
									{
										el.src="img/arrow"+ el.id +".png";
									});
				});
	
}
function getPresentationData()
{
	var url = "ajax/getGalleryData.php";
    
	var myRequest = new Request({
							method: 'get',
							url: url,
							onSuccess: dataRevievedCompleted });
	myRequest.send("idPres="+numerPres);
							
}
function dataRevievedCompleted(response,responseXML)
{
    var x,y,e;
    var mImg;
	var xmlData = responseXML;
	
	if(xmlData.getElementsByTagName("ERROR") == undefined)
	{
		alert(xmlData.getElementsByTagName("ERROR")[0].childNodes[0].nodeValue);
		return;
	}	
	iloscIOb = xmlData.getElementsByTagName("GALLERY")[0].getAttribute("count");
	imgWidth = xmlData.getElementsByTagName("GALLERY")[0].getAttribute("width");
	imgHeight = xmlData.getElementsByTagName("GALLERY")[0].getAttribute("height");
	//--images and their points
	for(var i=0;i<iloscIOb;i++)
	{
        var myE = xmlData.getElementsByTagName("IMAGE")[i];
        mImg = new MyImage(myE.getAttribute('id'),
                            myE.getAttribute('filename'));
                            
		mImg.setNeighbors(  myE.getAttribute('left'),
                            myE.getAttribute('right'),
                            myE.getAttribute('up'),
                            myE.getAttribute('down'),
                            myE.getAttribute('anaglyph'),
							myE.getAttribute('notanaglyph'));
                            
		for(var j=0;j<myE.childNodes.length;j++)
		{	
			x = myE.childNodes[j].getAttribute('x') * imgWidth;
			y = myE.childNodes[j].getAttribute('y') * imgHeight;
			e = myE.childNodes[j].getAttribute('element');
            mImg.addPoint(new Punkt(x,y,e));
		}		
		imageList.push(mImg);
		imageSrcList.push(mImg.imageUrl);
	}
	currentMyImage = imageList[0];
	//----
	//--elements
	var myEls = xmlData.getElementsByTagName("ELEMENT");
	for(var i=0;i<myEls.length;i++)
	{
		var tmpEl = new MyElement(myEls[i].getAttribute('id'),
								  myEls[i].getAttribute('name'),
								  "images/elements/" + myEls[i].getAttribute('image'),
								  myEls[i].childNodes[0].nodeValue);
		
		elementList.push(tmpEl);
		imageSrcList.push(tmpEl.imageUrl);		
	}
	
	//----
	$('punktyDiv').style.width = imgWidth;
	$('punktyDiv').style.height = imgHeight;
	
	if(preloadedFlag!="true")
	{
		var il = new ImageLoader(imageSrcList,//.concat(imageListStatic),
							function(){
									  //$('header').innerHTML += this.src + " | ";
										var progressPos = (this.iterator+1)/(this.images.length)*100;
										$('myLoading').style.width = progressPos + "%";
									},
									function(){allImageLoaded();});
	
		il.startLoading();
		
		var il2 = new ImageLoader(imageListStatic,function(){},function(){});
		il2.startLoadingS();
	}
	else
	{
		allImageLoaded();
	}
}
function doStep(s,l)
{
	var progressPos = s/l*100;
	$('myLoading').style.width = progressPos + "%";	
}
function allImageLoaded()
{
	$('loadingImage').fade('hide');//style.display="none";
    $('myLoadingOuter').fade('hide');//.style.display="none";
	$('cover').fade('hide');//style.display="none";	
	$('popup').fade('hide');//style.display="none";
	$('smallPopup').fade('hide');//style.display="none";
	
    $('image').style.height = imgHeight + "px";
    $('image').style.width = imgWidth + "px";
    $('imageSecond').style.height = imgHeight + "px";
    $('imageSecond').style.width = imgWidth + "px";
    
    var myEffect = new Fx.Morph('imageContent',{ onComplete : function(){
    
                                    $('image').fade('hide');
                                    $('image').innerHTML = "<img src=\"" + currentMyImage.imageUrl + "\" border=0 height=" + imgHeight + " width="+imgWidth +">";
                                    $('image').fade(1);
                                    setPoints();
                                    showArrows();
                                    preloadedFlag = true;
                                    Set_Cookie("imagesLoaded_" + numerPres,"true");
									if(slideShowInterval  == undefined)
										slideShowInterval = setInterval("autoLoadNext()",5000);
									
									postLoadAction();
                            }}); 
    myEffect.start({height: imgHeight,
                    width: imgWidth});
    $('imgWrapper').style.height = imgHeight;
    $('imgWrapper').style.width = imgWidth;
    $('cover').style.height = imgHeight;
    $('cover').style.width = imgWidth;
}
function autoLoadNext()
{
	showImage('R');
}

//------------------------
var fadeTime = 500; //ms
//----------
var hold = false;
function showImage(dir)
{
	if(hold==true)
        return;
    hold = true;
	//clearInterval(slideShowInterval);
	
    hideArrows('hide');
	hideInfos(0);
	
	var tmpImg;
	switch(dir)
	{
		case "R": tmpImg = getMyImageById(currentMyImage.right,imageList);break;
		case "L": tmpImg = getMyImageById(currentMyImage.left,imageList);break;
		case "B": tmpImg = getMyImageById(currentMyImage.down,imageList);break;
		case "U": tmpImg = getMyImageById(currentMyImage.up,imageList);break;
		case "A": tmpImg = getMyImageById(currentMyImage.anaglyph,imageList);break;
		case "A2": tmpImg = getMyImageById(currentMyImage.notanaglyph,imageList);break;		
	}
	fadeImage(tmpImg)	  
}

var zmienna=false;
function fadeImage(MyImg)
{                 
    $('imageSecond').innerHTML = "<img src=\"" + currentMyImage.imageUrl + "\" border=0 height=" + imgHeight + " width="+imgWidth +">";   
    $('imageSecond').fade('show');
    $('image').innerHTML = "<img src=\"" + MyImg.imageUrl + "\" border=0 height=" + imgHeight + " width="+imgWidth +">";
	
	$('imageSecond').set('tween',{
						fps:30, 
						duration: 1000,
						onComplete: function()
						{
							currentMyImage = MyImg;
							setPoints();
							showInfos();
							showArrows();
							hold = false;
						}
					});
               
	$('imageSecond').fade('out');
    
}
function setPoints()
{
	rotI=0;
	$('punktyDiv').innerHTML = "";
	var tmpPoints = currentMyImage.punkty;
	for(var i=0;i<tmpPoints.length;i++)
	{
		$('punktyDiv').innerHTML += "<div id=\"wave_"+ (i+1) +"\" class=\"wave\" \
									style=\"position:absolute;left: " + (tmpPoints[i].x - 29.0) + "px; top: " + (tmpPoints[i].y-27) + "px;\" ></div>";
		
		$('punktyDiv').innerHTML += "<div id=\"info_"+ (i+1) +"\" name=\""+ getMyImageById(tmpPoints[i].element,elementList).name +"\" class=\"redDotDiv\" \
									style=\"position:absolute;left: " + tmpPoints[i].x + "px; top: " + tmpPoints[i].y + "px\" ></div>";
		
	}
	
	var pointsTmp = $$(".redDotDiv");
	pointsTmp.each(function(element,i){
                       element.addEvent('click',function(){
                                                    rozwin(this,i);
                                                });
                       element.addEvent('mouseover',function(){
													$('smallPopup').style.top = element.style.top.replace("px","")-35;
													$('smallPopup').style.left = element.style.left.replace("px","")-60;													
													
													$('smallPopup').set('tween',{duration:200});
													$('smallPopup').fade('in');
													
													element.style.background = "url('img/dot_over.png')";
													//var fx = new Fx.Morph('smallPopup',{});
													//fx.start()
													
                                                    //startMyRotate(this)
                                                });
                       element.addEvent('mouseout',function(){
													element.style.background = "url('img/dot.png')";
													$('smallPopup').set('tween',{duration:200});
                                                    $('smallPopup').fade('out');
                                                });
        });
	startMyRotate();
}
//----------------------
//------------------
var timer;
var sx;
var sy;
var sw=2;
var sh=2;
var tw;
var th;
var stepX=5;
var stepY=5;
var iterations=6;
var it=0;
var running=false;
var hold=false;
function setPopupContent(id)
{	
	var tmpEl = getMyImageById(id,elementList);
	
	$('popupTitle').innerHTML = tmpEl.name;
	$('popupImage').innerHTML = "<img class=elementImg src=\""+ tmpEl.imageUrl +"\" alt='Image'>";
	$('popupDesc').innerHTML = tmpEl.description ;
}
function rozwin(point,id)
{
	if(hold==true)
        return;
    hold=true;
	
	$('popupTitle').innerHTML = "";
	$('popupImage').innerHTML = "";
	$('popupDesc').innerHTML = "";
	
	//hideArrows();
	
	var fx = new Fx.Morph('popup',{duration:1000,
							//transition:Fx.Transitions.Bounce.easeOut,
							'onComplete':function()
							{
								setPopupContent(currentMyImage.punkty[id].element);
							}
						});
	
	$('popup').style.display='';
	fx.start({
				top: [Math.round(point.style.top.replace("px",""))+25,(imgHeight-300)/2],
				left: [Math.round(point.style.left.replace("px",""))+25,(imgWidth-450)/2],
				height: [0,300],
				width: [0,450]
			});
	
	
	$('popup').fade(1);
	$('cover').fade(0.7);
	
	
	return;
}
function hidePopup()
{	
	showArrows();
	
	$('cover').fade(0);
	$('popup').fade(0);
	
	hold=false;
	
	return;	
}
//-----------------------------------
var rotating = false;
var rotName = "";
var rotI = 0;
var rotTimer;
var rotInterval;
function startMyRotate()
{
    if(rotating==false)
    {
        rotI = 0;
        rotating=true;
        rotInterval = setInterval("myRotate()",50);
    }
}
function myRotate()
{    
	var waves = $$(".wave");
	waves.each(function(wave){
				wave.style.background = "url(img/wave/dot_" + (Math.abs(rotI-11)+1) + ".png)";							
		});
	
	rotI = (rotI+1)%23;			     
}
function stopMyRotate()
{
	if(running)
		return;
    rotating = false;
    rotName="";
    $('wave').fade('hide');
    clearInterval(rotInterval);
    rotI=0;
    
}
function hideInfos()
{
    $('punktyDiv').fade('hide');
}
function showInfos()
{    
    $('punktyDiv').fade('show');    
}
function hideArrows(arg)
{
    if(arg=="")
        arg=0;
	var arrows = $$(".arrows");
	
	arrows.each(function(el){
		el.fade(arg);
	});
}
function showArrows()
{
	if(currentMyImage.left == "")	
		$("L").fade('hide');
	else
		$("L").fade('show');
	if(currentMyImage.right == "")
		$("R").fade('hide');
	else
		$("R").fade('show');
		
	if(currentMyImage.notanaglyph == "")
		$("A2").fade('hide');
	else
		$("A2").fade('show');
		
	if(currentMyImage.anaglyph == "")
		$("A").fade('hide');
	else
		$("A").fade('show');	
	
	$$(".arrows").each(function(el){el.fade('in')})
}
function doPrint()
{
	alert("Not implemented");
    //window.open("print.php?img=" + $('image'),"_blank","");
}
//----------------------
function getURLParam(name)
{
         get_string = document.location.search;         
         return_value = '';
         
         do { //This loop is made to catch all instances of any get variable.
            name_index = get_string.indexOf(name + '=');
            
            if(name_index != -1)
              {
              get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
              
              end_of_value = get_string.indexOf('&');
              if(end_of_value != -1)                
                value = get_string.substr(0, end_of_value);                
              else                
                value = get_string;                
                
              if(return_value == '' || value == '')
                 return_value += value;
              else
                 return_value += ', ' + value;
              }
            } while(name_index != -1)
            
         //Restores all the blank spaces.
         space = return_value.indexOf('+');
         while(space != -1)
              { 
              return_value = return_value.substr(0, space) + ' ' + 
              return_value.substr(space + 1, return_value.length);
							 
              space = return_value.indexOf('+');
              }
          
         return(return_value);        
         }
