window.addEvent( 'domready', function() {
	
	/* marking that jQuery works */
	$$( 'body' ).addClass( "mooTools" );
	
	$each( $$( ".itemsOverview .oddItem" ), function( el ) {
	
		c = new Element( 'li', {
						'class': 'clear'
						});
	
		c.inject( el, 'before' );
	
	});
	

	/* nested submenu */
	$$( "#navigation .submenu li a" ).addEvent( "click", function() {
	
		/* list item and its class */
		p = $( this ).getParent();
		c = p.get( "class" );
		
		/* class manipulation */
		if ( c == "collapsed"  ) {
			p.set( "class", "expanded" );
			return false;
		}
		else if ( c == "expanded" ) {
			p.set( "class", "collapsed" );
			return false;
		}
		
	});
	
	
	
	
	itemsRelated = $$( '.itemsRelated' );
	
	if ( itemsRelated.length ) {
		
		$$( '.itemsRelated ul li > a' ).addEvent( 'click', function() {
			
			if( $( this ).getParent( 'li' ).hasClass( 'collapsed' ) ) {
				$( this ).getParent( 'li' ).removeClass( 'collapsed' ).addClass( 'expanded' );
			}
			
			else if( $( this ).getParent( 'li' ).hasClass( 'expanded' ) ) {
				$( this ).getParent( 'li' ).removeClass( 'expanded' ).addClass( 'collapsed' );
			}
			
			return false;
			
		});
		
	}
	pics=$$('.pics ul a');
	if ( pics.length ) {
		
		pics.addEvent( 'click', function() {
			var a=$('photo');
			a.href=$(this).href;
			a.getElement('img').src=$(this).rel;
			return false;
		});
		
	}
	
	if($('odbior_osobisty')){
		
		$$( ".payment input" ).addEvent( "click", function() {
			if($(this).id=="f_order_payment_self_collection"){				
				$each( $$( ".delivery .del input" ), function( el ) {
					if(el.id!="odbior_osobisty")
						el.disabled='disabled';
					else el.checked=1;
				
				});				
			}
			else{
				$each( $$( ".delivery .del input" ), function( el ) {
					if(el.id!="odbior_osobisty")
						el.removeProperty('disabled');
					else el.checked=0;
				
				});
			}
		
	});
		
		$$( ".delivery .del input" ).addEvent( "click", function() {
				if(this.id=="odbior_osobisty"){
					$("f_order_payment_transfer").disabled='disabled';
					$("f_order_payment_online").disabled='disabled';
					$("f_order_payment_ondelivery").disabled='disabled';
					$("f_order_payment_self_collection").checked=1;
				}
				else{
					$("f_order_payment_transfer").removeProperty('disabled');
					$("f_order_payment_online").removeProperty('disabled');
					$("f_order_payment_ondelivery").removeProperty('disabled');
					$("f_order_payment_self_collection").checked=0;
				}
			
		});
	}
	
	$('filtruj_kategorie').addEvent("click", function(){
		if($(this).checked)
			var id = $('model').options[$('model').selectedIndex].value;
		else 
			var id='';
	    var zapytanie = odnosnik+"advajax/filtruj/"+id;
	    advAJAX.post({
	          url : zapytanie,
			  onLoading : function(obj) {

	            display_loading();

	        },
			  onSuccess : function(obj) {
	               hide_loading();	         
	     }
	     });
	});
	
	$('model').addEvent("change", function(){
		if(!$('filtruj_kategorie').checked) return;
		var id = $(this).options[$(this).selectedIndex].value;
	    var zapytanie = odnosnik+"advajax/filtruj/"+id;
	    advAJAX.post({
	          url : zapytanie,
			  onLoading : function(obj) {

	            display_loading();

	        },
			  onSuccess : function(obj) {
	               hide_loading();         
	     }
	     });
	});
	
	
	$('szukaj_producenci').addEvent("change", function(){
		var id = $(this).options[$(this).selectedIndex].value;
	    var zapytanie = odnosnik+"advajax/skutery/"+id;
	    advAJAX.post({
	          url : zapytanie,
			  onLoading : function(obj) {

	            display_loading();

	        },
			  onSuccess : function(obj) {
	               hide_loading();
	               $("model").innerHTML=obj.responseText;	         
	     }
	     });
	});
	
	labelToInput( 'f_s_query' );
	labelToInput( 'f_r_mail' );
	labelToInput( 'f_n_mail' );
	
	textToInput( 'f_l_login', 'nazwa użytkownika' );
	textToInput( 'f_l_pass', 'tajne hasło' );
	textToInput( 'f_order_coupon', 'wpisz kod' );

	if ( $( 'mainBanner' ) ) {
		new slideShow ( $( 'bannerPic' ), bannerImagesSubpage, bannerLinksSubpage );
	}
	
});

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;
	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) ].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' ) );
			}		
		});
	}
}
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 .info .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 .info .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');	
}

