window.addEvent( 'domready', function() {
	
    /* marking that jQuery works */
    $$( 'body' ).addClass( "mooTools" );
	
    labelToInput( 'f_l_login' );
    labelToInput( 'f_l_pass' );
    labelToInput( 'f_email' );
    labelToInput( 'f_s_query' );

    $$('#facebook').addEvent('click', function() {
        facebookAnimation();
    });
	
    //menuClick ('#navigation > #submenu > ul > li > a');
	
    if( $$( '#mainContent > div.feedback' ).length ) {
		
        var feedback = $$( '#mainContent > div.feedback' )[0];
        var headers = feedback.getChildren( 'h3' );
        var contents = feedback.getChildren( 'div.feedback-in' );
        var feedbackNav = new Element( 'ul' ).set( 'class', 'feedbackNav' );
        feedbackNav.inject( feedback, 'top' );
		
        headers.each( function( el, index ) {
            el.addClass( 'h' );
            new Element( 'li' ).set( {
                'text': el.get( 'text' ),
                'index': index
            } ).addEvent( 'click', function() {
			
                contents.addClass( 'h' );
                contents[ index ].removeClass( 'h' );
				
                feedbackNav.getChildren( 'li' ).removeClass( 'active' );
                $( this ).addClass( 'active' );
			
            }).inject( feedbackNav );
        });
		
        feedbackNav.getChildren( 'li' )[1].fireEvent( 'click' );
		
    }
	
    if ( $( 'mainBanner' ) ) {
        new slideShow ( $( 'mainBanner' ), bannerImagesSubpage, bannerLinksSubpage );
    }
	
    pics=$$('.pic ul.thumbs a');
    if ( pics.length ) {
		
        pics.addEvent( 'click', function() {
            var a=$('photo');
            a.href=$(this).href;
            a.getElement('img').src=$(this).name;
            return false;
        });
		
    }
	
});





var slideShow = function ( wrapper, images, links ) {
	
    /* list of images urls */
    var imagesArray = images;
    var linksArray = links;
    /* images wrapper */
    var container = wrapper;
	
    var lastImageIndex = images.length - 1;
    if (Browser.Engine.name == 'trident')
        lastImageIndex = images.length - 2;
    var imagesList = new Element( 'ul', {
        'id': 'slideshow-images'
    } );
    var imagesControl = new Element( 'ul', {
        'id': 'slideshow-controls'
    } );
    var currentItem;
    var timer = 0;
	
    var initiate = function() {
		
        preloadedImages = new Asset.images( imagesArray, {
            onComplete: function() {
				
                prepareContainer();
                activateItem( 0 );
                window.setInterval( animate, 1000 );
				
            }
        });
		
    };
	
    var prepareContainer = function() {
		
        /* hiding default content */
        container.getChildren().setStyle( 'display', 'none' );
		
        /* append images and controls */
        imagesList.inject( container );
        imagesControl.inject( container );
		
        for ( var i = 0; i <= lastImageIndex; i++ ) {
			
            /* inject images */
            var imagesListItem = new Element( 'li', {
                'class': 'disactive'
            } ).inject( imagesList );
            var imagesListLink = new Element( 'a', {
                'href': linksArray[ i ]
            } ).inject( imagesListItem );
            preloadedImages[ i ].inject( imagesListLink );
			
            /* create controls */
            new Element( 'li', {
                'class': 'disactive',
                'index': i,
                'text': i+1,
                'events': {
                    'click': function() {
                        activateItem( $( this ).getProperty( 'index' ) );
                    }
                }
            } ).inject( imagesControl );
			
        }
		
        new Element( 'li', {
            'class': 'prev',
            'events': {
                'click': function() {
                    activatePreviousItem();
                }
            }
        } ).inject( imagesControl, 'top' );
		
        new Element( 'li', {
            'class': 'next',
            'events': {
                'click': function() {
                    activateNextItem();
                }
            }
        } ).inject( imagesControl );
		
    };
			
    var activateItem = function( index ) {
		
        if( index != currentItem ) {
			
            /* hide active elements */
            imagesControl.getChildren( '.active' ).removeClass( 'active' ).addClass( 'disactive' );
            imagesList.getChildren( '.active' ).removeClass( 'active' ).addClass( 'disactive' );
			
            /* show activated elements */
            imagesControl.getChildren()[ parseInt(index)+1 ].removeClass( 'disactive' ).addClass( 'active' );
            imagesList.getChildren()[ index ].removeClass( 'disactive' ).addClass( 'active' );
			
            timer = 0;
            currentItem = index;
        }
		
    };
	
    var activateNextItem = function() {
		
        if ( currentItem < lastImageIndex ) {
            activateItem( parseInt( currentItem ) + 1 );
        }
        else {
            activateItem( 0 );
        }
		
    };
	
    var activatePreviousItem = function() {
		
        if ( currentItem > 0 ) {
            activateItem( parseInt( currentItem ) - 1 );
        }
        else {
            activateItem( lastImageIndex );
        }
		
    };
	
    var animate = function() {
		
        timer += 1;
		
        if ( timer > 6 ) {
			
            timer = 0;
            activateNextItem();
			
        }
		
    };
	
    initiate();
	
};

function labelToInput ( id )
{
    /* input */
    i = $( id );
	
    if( i ) {
        /* setting default value */
        i.setProperty( 'originalValue', i.getPrevious( 'label[for='+id+']' ).get( 'text' ).toLowerCase() );
        i.set( 'value', i.getProperty( 'originalValue' ) );
		
        /* click on input events */
        i.addEvent( 'focus', function() {
            if ( $( this ).get( 'value' ).toLowerCase() == $( this ).getProperty( 'originalValue' ) ) {
                $( this ).set( 'value', '' );
            }
        });
        i.addEvent( 'blur', function() {
            if ( $( this ).get( 'value' ) == '' ) {
                $( this ).set( 'value', $( this ).getProperty( 'originalValue' ) );
            }
        });
    }
}

function textToInput ( id, text )
{
    /* input */
    i = $( id );
	
    if ( i ) {
        /* setting default value */
        i.setProperty( 'originalValue', text );
        i.set( 'value', i.getProperty( 'originalValue' ) );
		
        /* click on input events */
        i.addEvent( 'focus', function() {
            if ( $( this ).get( 'value' ).toLowerCase() == $( this ).getProperty( 'originalValue' ) ) {
                $( this ).set( 'value', '' );
            }
        });
        i.addEvent( 'blur', function() {
            if ( $( this ).get( 'value' ) == '' ) {
                $( this ).set( 'value', $( this ).getProperty( 'originalValue' ) );
            }
        });
    }
}

function menuClick ( id )
{

    i = $$( id );
	
    i.addEvent( "click", function() {
	
        /* list item and its class */
        p = $( this ).getParent();
        c = p.get( "class" );
		
        /* class manipulation */
        if ( p.hasClass( "collapsed" ) ) {
            p.removeClass( "collapsed" );
            p.addClass( "expanded" );
			
            return false;
        }
        else if ( p.hasClass( "expanded" ) ) {
            p.removeClass( "expanded" );
            p.addClass( "collapsed" );
					
            return false;
        }
        else {
            p.addClass( "expanded" );
			
            return false;
        }
		
    });
}

var facebook = 0;

function facebookAnimation() {
    var myEffect = new Fx.Morph('facebook', {
        duration: 'short',
        transition: Fx.Transitions.Sine.easeOut
        });

    if (facebook == 0) {
    myEffect.start({
        'right': [-364, -149]
    });
    facebook = 1;
    } else {
        myEffect.start({
        'right': [-149, -364]
    });
    facebook = 0;
    }
    
}
