// JavaScript Document

Event.observe(document.body, 'load', StartUp() );	//starting function on document load

function StartUp(){
	this.Settings				= new Object();				//page settings
	this.Settings.MenuItems		= new Array();				//stores button ID-s
	
	for(i=1; i<=6; i++){
		this.Settings.MenuItems['menuitem_'+i]	= new MenuItem();
		this.Settings.MenuItems['menuitem_'+i].SetItem( $('menuitem_'+i) );
	}
	
	this.Settings.Label			= new Array();
	this.Settings.Label['menuitemlabel']	= $('menuitemlabel');
	this.Settings.Label['labeltext']		= $('labeltext');
	this.Settings.Logo			= $('logo');
	this.Settings.Content		= $('contentfull');
	
	this.Settings.NewsTimer		= 6;
	this.Settings.News			= new News();
	this.Settings.News.StartNews();
	
	this.Settings.Music			= null;
	
	LogoItem();
	
	AddMethods_SlideShow();
	this.Settings.SlideShow		= $("slideshow");
	this.Settings.SlideShow.startSlideShow();
}


function AddMethods_SlideShow(){
	Element.addMethods( 'div' , {
		checkSlideShow: function(element){
			if( element.readAttribute('rel')=='slideshow'){
				return true;
			} else {
				return false;
			}
		}
		,
		initializeSlideShow: function(element){
			if( element.checkSlideShow() ){
				if( !(element = $(element) ) ){
					alert("this is not my element: "+ element.identify());
					return;
				} else {
					if( !element.Props ){					//check if Props is defined
						element.Props		= new Object();	//creating Props object
						element.Props.Item	= $('slideshowimages');		//this is the box containing the images
						element.Props.ContSize		= element.getDimensions();	//size holding key/value pair variable ('width', 'height')
						element.Props.Position		= 0;	//this is the current position of the images
						element.Props.ImageCount	= 0;	//this is the number of images added to the slideshow
						element.Props.Stimer		= null;	//this is the object holding the timer
						element.Props.Duplicated	= false;//this shows if the first image has been duplicated
						element.Props.Hidden		= false;
						element.Props.SlideShowTimer		= 5;	//timer value
					}
					return element;
				}
			}
		}
		,
		startSlideShow: function(element){
			if( element.checkSlideShow() ){
				if( !(element = $(element) ) ){
					return;
				} else {
					//alert(element.identify());
					element.initializeSlideShow();
					element.Props.Item.cleanWhitespace();	//clearing the whitespaces from the item residing between/arround images
					element.countSlideShowImages();			//counting images part of the slideshow
					element.duplicateFirstImage();			//duplicating the first image
					element.Props.Stimer = new PeriodicalExecuter(element.moveSlideShowImages, element.Props.SlideShowTimer);		//start timed image change
					return element;
				}
			}
		}
		,
		stopSlideShow: function(element){		//stop the slideshow image swap
			if( element.checkSlideShow() ){			//check if this is the slideshow element
				if(element.Props.Stimer!=null){		//if the timer is set
					element.Props.Stimer.stop();	//stop the timer
				}
			}
		}
		,
		hideSlideShow: function(element){		//hide the slideshow image swap
			if( element.checkSlideShow() ){				//check if this is the slideshow element
				if( !element.Props.Hidden ){			//check if element is not allready hidden
					element.Props.Hidden	= true;		//set the hidden status
					element.stopSlideShow();			//stopping the slideshow
					Effect.SlideUp(element);			//animate and hide the element 
				}
			}
		}
		,
		showSlideShow: function(element){		//show the slideshow image swap
			if( element.checkSlideShow() ){				//check if this is the slideshow element
				if( element.Props.Hidden ){				//check if element is not allready hidden
					element.Props.Hidden	= false;	//set the hidden status
					ClearScreen(Settings.Content);		//clear previous content from screen
					Effect.SlideDown(element);			//animate and show the element
					element.startSlideShow();			//starting the slideshow
				}
			}
		}
		,
		moveSlideShowImages: function(element){		//animated swapping of the slideshow images
			//alert(element.innerHTML);
			element	= Settings.SlideShow;
			if( element.checkSlideShow() ){			//check if this is the slideshow element
				if(element.Props.Position <= element.Props.ContSize['width']*(-1)*element.Props.ImageCount ){		//if the last image has been exceeded
					element.Props.Item.setStyle({ 	//reset the whole slideshow to the start
						marginLeft: '0px'
					});
					element.Props.Position	= 0;	//reset the position holder variable
				}
				element.Props.Position	+= 100;	//set the next targeted position fo anim
				element.Props.Item.morph({ marginLeft: element.Props.Position+"px" }, { queue: { position:'end', scope:'slideshow' }, duration:0.2 });
				element.Props.Position	-= element.Props.ContSize['width']+100;	//set the next targeted position fo anim
				element.Props.Item.morph({ marginLeft: element.Props.Position+"px" }, { queue: { position:'end', scope:'slideshow' }, duration:1 });
			}
		}
		,
		countSlideShowImages: function(element){	//counting slideshow images
			if( element.checkSlideShow() ){			//check if this is the slideshow element
				var correction	= 0;				//corection value
				if(element.Props.Duplicated){		//if the first image has been duplicated
					correction	-= 1;				//correcting the counted number cause first image has been duplicated
				}
				element.Props.ImageCount	= element.Props.Item.childElements().length - correction;	//slide show images counted
			}
		}
		,
		duplicateFirstImage: function(element){		//duplicate the first slideshow image
			if( element.checkSlideShow() ){			//check if this is the slideshow element
				if(!this.Duplicated){				//check if image has been duplicated
					var firstelem	= element.Props.Item.firstDescendant().cloneNode(true);					//finding the first image
					element.Props.Item.insert({ bottom: firstelem });	//adding it to the end of the images cue
					element.Props.Duplicated	= true;			//setting the duplicated status
				}
			}
		}
		,
		clone: function(element) {
		    var clone = $(element).cloneNode(true);
		    $(clone).select('*[id]').concat(clone).invoke('writeAttribute', { id: null });
		    return clone;
		  }
	});
}


function LogoItem(){
	Settings.Logo.setStyle({
	  cursor: 'pointer'
	});	
	Settings.Logo.itemClick	= function(event){	//button click event handling
		Settings.SlideShow.showSlideShow();
	}
	Event.observe(Settings.Logo, 'click',  Settings.Logo.itemClick );
}


function MenuItem(){									//MenuItem class definition
	this.Item		= null;								//caching the button element
	//this.Content	= null;								//the address of the linked content	
	
	this.SetItem	= function(elem){							//set up the button item
		this.Item	= elem										//seting the buttn holder element
		this.GetContentAddress();
		Event.observe(this.Item, 'click', this.ItemClick );		//observing click events on menuitem
		Event.observe(this.Item, 'mouseover', this.ItemOver );	//observing mouseover events on document
	}
	
	this.ItemClick	= function(event){				//button click event handling
		var elem	= event.element();
		Settings.SlideShow.hideSlideShow();
		switch( elem.identify() ){					//switch based on id
			case "whatever":						//about us icon
				//container.update
				break;				
			default:
				//container.update
				LoadScreen( Settings.Content, elem.readAttribute("linkedcontent"));
				
				break;
		}
	}
	
	this.ItemOver	= function(event){				//button mouse over event handling
		var elem		= event.element();			//retrieving the button that generated click event
		var elemid		= elem.identify();			//geting the button's element id
		var elemtitle	= elem.readAttribute('title');			//getting the button title
		var elempos		= elem.positionedOffset();				//getting the position of the button
		Settings.MenuItems[elemid].ChangeLabel(elemtitle);		//change the text of the menu label, to the button title
		Settings.MenuItems[elemid].MoveLabel(elempos[0]-200);	//move the label under the button
	}
	
	this.ChangeLabel	= function(labeltext){						//changing the label text
		Element.update( Settings.Label['labeltext'],  labeltext);	//updateing the contents of the label
	}
	
	this.MoveLabel	= function(posx){					//moving the label
		Effect.Queues.get('label').invoke('cancel');	//canceling previous animations related to the label
		Settings.Label['menuitemlabel'].morph({ marginLeft: posx+"px" }, { queue: { position:'end', scope:'label' }, duration:0.5 });								//starting animated movement to the targeted place
	}
	
	this.GetContentAddress	= function(){
		this.Item.writeAttribute("linkedcontent", this.Item.readAttribute("href"));
		//this.Item.writeAttribute("href", "#");
		this.Item.writeAttribute("href", "javascript:void(0)");
	}
}

function News(){										//News class definition
	this.Newsbox	= $('news');						//this is the box hosting the news slideshow
	this.Items		= $('newsitems');					//this is the box containing the news
	this.Duplicated	= false;							//this shows if the first news item has been duplicated
	this.Current	= 0;								//this shows the currently shown item
	this.Stepsize	= -70;
	this.Count		= this.Items.childElements().length;
	this.Ntimer		= null;
	
	this.StartNews		= function(){
		this.DuplicateFirst();
		this.Stimer		= new PeriodicalExecuter(this.MoveNewsItems, Settings.NewsTimer);		//start timed news change
	}
	
	this.MoveNewsItems	= function(){
		Settings.News.Current++;
		if(Settings.News.Current>Settings.News.Count){
			Settings.News.ResetNews();
		}
		target	= Settings.News.Stepsize * Settings.News.Current;
		Settings.News.Items.morph({ marginTop: target+"px" }, { queue: { position:'end', scope:'news' }, duration:1 });
	}
	
	this.ResetNews		= function(){
		var y	= 0;
		this.Items.setStyle({ 									//reset the whole newsshow to the start
			'marginTop': y+'px'
		});
		this.Current	= 0;
	}
	
	this.DuplicateFirst	= function(){										//duplicating the first image
		if(!this.Duplicated){												//if the item has not been duplicated
			var firstelem	= this.Items.firstDescendant().cloneNode(true);		//duplicating the first element with descendants
			this.Items.insert({ bottom: firstelem });				//adding it to the end of the images cue
			this.Duplicated	= true;									//setting the duplicated status
		}
	}
}



function LoadScreen(target, address){												//file loader, loads external files
	var screencontent	= "<P>Loading...</P>";
	var screenelement	 = '<div id="screen_'+ Settings.Screen +'">';
	screenelement		+= '</div>';
	var elem			= target;
	elem.update(screencontent);
	var url				= address;
	var req	= new Ajax.Request(url,
  	{
    	method:'get',
		onLoading: function(){
			
		},
    	onSuccess: function(transport){
      		var response = transport.responseText || "no response text";
      		//alert( response);
      		elem.update(response);
			//return response;
			//Screens.UpdateScreen(ScreenName, response);
			//InitSounds();
		},
    	onFailure: function(){
			alert('Sikertelen letöltési kisérlet... Kérjük, próbálkozzon újra!')
			return null;
		}
  	});

}

function ClearScreen(target){												//file loader, loads external files
	var screencontent	= "";
	var elem			= target;
	elem.update(screencontent);
}

function InitSounds(){
	Settings.Music		= $$('a.music');
	if( Settings.Music.length>0){
		for(i=0; i<Settings.Music.length; i++){
			alert (Settings.Music[i].readAttribute("href"));
			Settings.Music[i].writeAttribute("linkedcontent", this.readAttribute("href"));
			Settings.Music[i].writeAttribute("href", "javascript:void(0)");
			alert (Settings.Music[i].readAttribute("href"));
		}
	}
	
	
	Settings.Music.observe('click', function(event) {
		var musicfile	= this.readAttribute("linkedcontent");
	    event.stop();
	    Sound.play(musicfile, {replace:true});
	});
}


