
/**
 *
 *		CODE CREATED BY KOW a.k.a. Tibor Szász
 *		2:16 2008.01.07.
 *
 *		All rights reservesd
 *      
 *      Minden jog fenntartva!
 *
 *
 *		More projects @ http://skyweb.hu
 *
 */

var duoSite = new Class({

	initialize: function( container ){
	
		this.gal = $( 'gallery' );
		this.galLink = $('gallery_link');
		this.contentLayer = $( 'content_layer' );

		this.gId = 0;
		this.complete = {};
		this.imgDir = 'gallery/';
		this.imgArr = $ES( 'img' );
		this.imagesWidth = 0;		// Holds all the images width in one (int) var.
		this.imagesToLoad = 5;		// How many images to load???
		this.yellowDelay = 1000; // Delay between yellow effects

		this.res = '';

		this.isNavigated = false;	// yellow line: open / close state stuff

		this.currentImage = 0;

		this.scroll = new Fx.Scroll( this.gal, {
			wait: false,
			duration: 800,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Quad.easeInOut
		});

		/**
		 *  	Some events
		 */
		 
		// Open yellow line
		this.galLink.addEvent( 'click', this.showYellowLine.bindAsEventListener(this) );

		// Add events to top navigation links
		$('top_navigation').getElements( 'a' ).each( function( el ){ 

				el.addEvent( 'click', this._loadLayer.bindAsEventListener(this) );
				
		}, this );
		
		// Make logo clickable
		$('logo').addEvent( 'click', function(){ document.location = 'index.html' } );

		// Balra scroll
		$('left_arrow').addEvent('click', this.scrollLeft.bindAsEventListener(this) );

		// Jobbra scroll
		$('right_arrow').addEvent('click', this.scrollRight.bindAsEventListener(this) );

		/**
		 *		Prepare gallery and site
		 */

		this._getImages(0);

		this.highlightImage(0);

        //var www = ( this._countImagesWidth() ) ? this._countImagesWidth() : 0;

		//$( 'images_wrapper' ).setStyle( 'width', www + ( this.imgArr.length * 3 ) ); 
		// 1 == image padding

		this.gal.style.width = Window.getWidth() - 64 + 'px';
		
	},

	scrollLeft: function(){

			if( this.currentImage > 0 ){
				this.currentImage--;
				this.scroll.toElement( this.imgArr[ this.currentImage ] );
			}
			this.highlightImage();
	},

	scrollRight: function(){

			if(  this.currentImage < this.imgArr.length - 1 ){
				this.currentImage++;
				this.scroll.toElement( this.imgArr[ this.currentImage ] );
			}
			this.highlightImage();
			this.preloadOneImage();
	},

    preloadOneImage: function(){
    
            // imgArr aktuális hosszától függően hozzátölteni az assetArray következő elemét, ha van
            if( assetArray[ this.imgArr.length ] ) var nextImgToLoad = assetArray[ this.imgArr.length ]
            else return false;
            
            var element = new Asset.image( nextImgToLoad );
            
            this.imgArr.push( element )
            
            $('images_wrapper').appendChild( element );
    },

	/**
	 *		currentElement kivételével az összes képet átltszóvá teszi 0,5-re
	 */

	highlightImage: function(){
	
			for( i = 0; i < this.imgArr.length; i++ ){
			
					if( i != this.currentImage ) this.imgArr[ i ].setOpacity( 0.5 );
					else this.imgArr[ i ].setOpacity( 1 );
			}	
	},

	showYellowLine: function(){

			new Fx.Style( $('yellow_line'), 'height', {duration: 700, transition: Fx.Transitions.bounceOut } ).start( 0, 26 );
	
	},

	hideYellowLine: function(){

			new Fx.Style( $('yellow_line'), 'height', {duration: 700, transition: Fx.Transitions.bounceOut } ).start( 26, 0 );
	
	},
	
	/**
	 *		Utility functions
	 */

	_countImagesWidth: function(){

			for( i = 0; i < this.imgArr.length; i++ ){
					this.imagesWidth += parseInt( this.imgArr[ i ].getCoordinates().width );
			}
			
			return this.imagesWidth;
	},

	_loadLayer: function( event ){

			event = new Event(event).stop();

			var lid = event.target.parentNode.id
			var linkText = event.target.innerHTML;

			if( lid == 'gallery_link' ){

					this._unloadContentLayer();

					if( this.isNavigated == true ) this.hideYellowLine();

					//if( this.isNavigated == true ) setTimeout( this.showYellowLine, this.yellowDelay );
					
					this._setTitle( '' );

					// Itt kell meghívni a galéria kezelő metódusokat

					this._loadCategories( 0 );

			} else {

					$( 'content_layer' ).setHTML( '' );

					this._setContentLayer();

					if( this.isNavigated == true ) this.hideYellowLine();

					delay = ( this.isNavigated == true ) ? this.yellowDelay : 0;

					( function(){ 

							// Kill the gallery menu
							$( 'yellow_line' ).setHTML( '' );

							this.showYellowLine();

							this._setTitle( linkText );

							this._loadAjaxContent( lid );

					} ).delay( delay, this );
			}
			
			/**
			 *		Top menu activation
			 */

			$('top_navigation').getElements( 'a' ).each( function( el ){ 

						el.parentNode.className = '';
			} );

			event.target.parentNode.className = 'active';
			
			this.isNavigated = true;
	},

	_setContentLayer: function(){

			this.contentLayer.style.width = Window.getWidth() - 64 + 'px';
			new Fx.Style( this.contentLayer, 'opacity', {duration: 1000} ).start( 1, 0.75 );
	},

	_unloadContentLayer: function(){

			new Fx.Style( this.contentLayer, 'opacity', {duration: 1000} ).start( 0.75, 0 );
	},

	_loadAjaxContent: function( id, extra ){

			var extra = ( extra == 'undefined' ) ? '' : '?' + extra;

			url = 'content/' + id + '.php' + extra;

			new XHR( { method: 'get', onSuccess: function( rsp ){ 

					$('content_layer').setHTML( rsp );

			} }).send( url );
		
	},

	_setTitle: function( str ){

			$('yellow_title').setHTML( str );
	},
	
	
	/**
	 *		Image loading AND gallery handling methods
	 *
	 *		_loadCategories ( id alapján, 0 == top level )
	 *		_loadImages ( id alapján, 0 == default képek, amúgy pedig a kategória képei )
	 *
	 */

	_loadCategories: function( id ){

			url = 'catgegories.php?id=' + id;

			if( this.isNavigated == true ) this.hideYellowLine();
			
			delay = ( this.isNavigated == true ) ? this.yellowDelay : 0;
			
			( function(){ 

					this.showYellowLine();

					new XHR( { method: 'get', onSuccess: function( rsp ){ 

							$('yellow_line').setHTML( rsp );

					} }).send( url );

			} ).delay( delay, this );
					
	},
	
	_getImages: function( id ){

			$('yellow_line').getElements( 'a' ).each( function( el ){ 

						el.parentNode.className = '';
			});
			
			$('yellow_line').getElements( 'a' ).each( function( el ){ 

						if( el.rel == id ) el.parentNode.className = 'active';

			});

			this.gId = ( id ) ? id : -1;

			url = 'images.php?id=' + this.gId;
			
			this.res = new XHR( { method: 'get', onSuccess: this._loadImages.bindAsEventListener( this ) }).send( url );
	
	}, 
	
	_loadImages: function( x ){
 
            this.currentImage = 0;
			
			this.scroll.scrollTo(0,0);

			if( this.gId > 0 ){ 
			
					//this._setTitle( '<img src="images/loader.gif" align="absmiddle">' );

			}

			if( x == 'zero' ){
			
					alert( 'A kategória nem tartalmaz képeket' );
					
					this._setTitle( '' );
					
			} else {

					assetArray = x.split('|');;

                    $('images_wrapper').setHTML( '' );

					this.currentImage = 0;
										
                    this.imgArr = [];

                    this.imagesToLoad = ( assetArray.length > this.imagesToLoad ) ? this.imagesToLoad : assetArray.length;

                    for( i=0; i < this.imagesToLoad; i++ ){

                            var element = new Asset.image( assetArray[ i ], { onload: this.highlightImage.bindAsEventListener(this) } );
                            
                            this.imgArr.push( element )
                            
                            $('images_wrapper').appendChild( element );
                    }
			}
	},

	_doAppend: function(){
	
			this._setTitle( '' );
	
	},

	/**
	 *		Functions to paginate the time selector
	 */

	getMonth: function( str ){

			this._loadAjaxContent( 'lefoglalt', 'date=' + str );

	},
	
	toggle: function( id ){
                
            $( id ).style.display = ( $( id ).style.display == 'none' )	? 'block' : 'none';
	},
	
	sendIT: function(){
            
            var missing = false;
            
            $ES( '.req' ).each(
            
                function( el ){
                
                    if( !el.value ) missing = true;

                }            
            );
            
            if( missing ){
            
                    alert( 'Minden csillaggal jelölt mező kitöltése kötelező' )
            
                    return false;
            
            }

            q = new XHR({method: 'post', onSuccess: function( rsp ){
            
                        if ( rsp == 1 ) {
                        
                            alert( 'Köszönjük a megkeresését, az ajánlatkérés elküldését követően hamarosan válaszolunk.' );
                            $( "meg" ).reset();
                        
                        } else {
                        
                            alert( 'Hiba, kérem próbálja később' );
                        
                        }
            }});
            
            q.send( 'mail.php', $( "meg" ).toQueryString() );

 	},
	
	select: function(){

            $( "eskuvo" ).style.display = ( $( "rend" ).value == 'Esküvő' )	? 'block' : 'none';
            $( "egyeb" ).style.display = ( $( "rend" ).value == 'Egyéb' )	? 'block' : 'none';
	}

});

var duo = {};

window.addEvent('domready', function(){
    duo = new duoSite( 'gallery' );  
});


