window.addEvent( 'domready', function() {
	
	/* marking that jQuery works */
	$$( 'body' ).addClass( "mooTools" );
	
	labelToInput( 'f_s_query' );
	labelToInput( 'f_n_name' );
	labelToInput( 'f_n_mail' );
	
	sidebar();
	
	if ( $$( 'div.home' ).length ) {
		homePage();
	}
	
});

function sidebar () {
	
	boxes = $$( '#sidebar div.box-collapsable' );
	
	boxes.each( function( el ) {
						 
		var boxHeader = $( el ).getChildren( 'h3' ).dispose();
		
		boxContent = new Element( 'div', { class: 'box' } );		
		boxContent.wraps( $( el ) );
		
		$( el ).removeClass( 'box' ).addClass( 'box-inner h' );
		
		boxHeader.inject( $( el ), 'before' );
		
		boxHeader.addEvent( 'click', function() {
											  
			$( this ).getNext( '.box-inner' ).toggleClass( 'h' );
											  
		});
						 
	});
	
}

function homePage() {
	
	homePageCat = $$( 'div.home > .category' );
	
	homePageCat.addEvent( 'click', function() {
	
		window.location = $( this ).getElement( 'h2' ).getChildren( 'a' )[0].get( 'href' );
	
	});
	
	homePageCat.addEvent( 'mouseover', function() {
	
		this.addClass( 'catHover' );
	
	});
	
	homePageCat.addEvent( 'mouseout', function() {
	
		this.removeClass( 'catHover' );
	
	});
	
}

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' ) );
			}		
		});
	}
}

var curPhoto=0;

function display_loading () {Loading();}

function hide_loading() {
    destroyLoading();
}

function zaznacz_wszystkie(obiekt) {
	ile=obiekt.length;
	i=0;
	while (i < ile) {
		obiekt.options[i].selected=1;
		i=i+1;
	}

}
function odznacz_wszystkie(obiekt) {
	ile=obiekt.length;
	i=0;
	while (i < ile) {
		obiekt.options[i].selected=0;
		i=i+1;
	}
}

function showNext(){
	var images=$$('.itemDetails .pic a');
	var ilosc=images.length;
	if(ilosc <= (curPhoto+1)) return false;
	for(i=0; i < ilosc; i++){
		if(i==(curPhoto+1))	images[i].setStyle('display', 'block');
		else images[i].setStyle('display', 'none');
	}
	curPhoto++;
	refreshButtons(ilosc);
}

function showPrev(){
	var images=$$('.itemDetails .pic a');
	var ilosc=images.length;
	if(curPhoto==0) return false;
	for(i=0; i < ilosc; i++){
		if(i==(curPhoto - 1))	images[i].setStyle('display', 'block');
		else images[i].setStyle('display', 'none');
	}
	curPhoto--;
	refreshButtons(ilosc);	
}

function refreshButtons(ilosc){
	if(curPhoto==(ilosc - 1)) $('next-photo').setProperty('src', odnosnik+'system/application/views/img/ikony/arrow_right_off.gif');
	else $('next-photo').setProperty('src', odnosnik+'system/application/views/img/ikony/arrow_right.gif');
	
	if(curPhoto==0) $('prev-photo').setProperty('src', odnosnik+'system/application/views/img/ikony/arrow_left_off.gif');
	else $('prev-photo').setProperty('src', odnosnik+'system/application/views/img/ikony/arrow_left.gif');	
}

