XPE = {
	// common functions
	replaceContent : function ( placeholderSelector, content ){
				$( placeholderSelector ).removeClass( 'loading' ).replaceWith( content );
			},

	addCSSRule : function( selector, cssRule ){
				var styleSheets = document.styleSheets[0];

				if( styleSheets.insertRule ){
					// real browsers
					styleSheets.insertRule( selector + '{' + cssRule + '}', styleSheets.cssRules.length );
				} else {
					// IE
					styleSheets.addRule( selector, cssRule );
				}
			},

	// main functions
	makeExpandable : function( callerSelector, parentSelector, subitemsSelector, hideOtherElements ){
			function expand( e ){
				var parentElement = $( this ).parents( e.data.parentSelector );
				var subitems = $( e.data.subitemsSelector, parentElement );

				if( hideOtherElements ){
					$( '.expand ' + e.data.subitemsSelector, parentElement.parent() ).slideToggle();
					$( '.expand', parentElement.parent() ).toggleClass( 'expand' );
				}

				parentElement.toggleClass( 'expand' );
				subitems.slideToggle();

				if( $( this ).is( 'a' ) ) {
					e.preventDefault();
				}
			}

			$( callerSelector ).live( 'click', { parentSelector : parentSelector, subitemsSelector : subitemsSelector }, expand );
		},

	ajaxifyForm : function( callerSelector, placeholderSelector ){
			function ajaxForm( e ){
				e.preventDefault();

				$( placeholderSelector ).addClass( 'loading' );

				$.ajax( {
					url : $( this ).attr( 'action' ),
					type : $( this ).attr( 'method' ),
					data : $( this ).serialize() + '&AJAXLoad=' + placeholderSelector, 
					success : function( text ){
							XPE.replaceContent( placeholderSelector, text )
						}
					} );
			}

			$( callerSelector ).live( 'submit', ajaxForm );
		},
		
	ajaxifyInput : function(callerSelector, placeholderSelector, url ){
			function ajaxInput( e ){
				e.preventDefault();

				$( placeholderSelector ).addClass( 'loading' );

				$.ajax( {
					url : url,
					type : 'get',
					data : $( this ).serialize() + '&AJAXLoad=' + placeholderSelector, 
					success : function( text ){
							XPE.replaceContent( placeholderSelector, text )
						}
					} );
			}

			$( callerSelector ).live( 'keyup', ajaxInput );
		},
		
	ajaxifyCheckbox : function(callerSelector, placeholderSelector, url ){
			function ajaxCheckbox( e ){
				//e.preventDefault();

				$( placeholderSelector ).addClass( 'loading' );

				//alert($(this).attr('checked'))
				
				// zaznaczanie
				if ($( this ).attr('checked')) {
				$.ajax( {
					url : url,
					type : 'get',
					data : $( this ).serialize() + '&AJAXLoad=' + placeholderSelector, 
					success : function( text ){
							XPE.replaceContent( placeholderSelector, text )
						}
					} );
				}
				else {
				$.ajax( {
					url : url,
					type : 'get',
					data :  $( this ).attr('name')+'='+ $( this ).val() +'&'+ 'checked=false' + '&AJAXLoad=' + placeholderSelector, 
					success : function( text ){
							XPE.replaceContent( placeholderSelector, text )
						}
					} );	
				}
				// odznaczanie
					
				//$(this).attr('checked', 'checked');	
				//$(this).attr('moje', 'dane');	
				//return false;	
			}

			$( callerSelector ).live( 'click', ajaxCheckbox );
		},		

		
	ajaxifySelect : function(callerSelector, placeholderSelector, url ){
			function ajaxSelect( e ){
				e.preventDefault();

				$( placeholderSelector ).addClass( 'loading' );

				$.ajax( {
					url : url,
					type : 'get',
					data : $( this ).serialize() + '&AJAXLoad=' + placeholderSelector, 
					success : function( text ){
							XPE.replaceContent( placeholderSelector, text )
						}
					} );
			}

			$( callerSelector ).live( 'change', ajaxSelect );
		},	

	ajaxifyLink : function( callerSelector, placeholderSelector ){
			function ajaxLink( e ){
				e.preventDefault();

				$( placeholderSelector ).addClass( 'loading' );

				$.get( this.href, { AJAXLoad : placeholderSelector }, function( text ){
						XPE.replaceContent( placeholderSelector, text )
					} );
			}

			$( callerSelector ).live( 'click', ajaxLink );
		},

	customizeInputs : function( inputSelector, parentSelector ){
			function customInputClicked( e ){
				if( $( this ).is( 'input[type=radio]' ) ) {
					$( 'input[name=' + this.name + ']' ).parents( e.data.parentSelector ).removeClass( 'checked' );
					$( this ).parents( e.data.parentSelector ).addClass( 'checked' );
				}
				if ( $( this ).is( 'input[type=checkbox]' ) ) {
					$( this ).parents( e.data.parentSelector ).toggleClass( 'checked' );
				}
			}

			$( inputSelector ).live( 'click', { parentSelector : parentSelector }, customInputClicked );
		},

	ajaxMarkCurrent : function( parentSelector, elementToMark ){
			function markCurrent( e ){
				$( e.data.parentSelector + ' .current' ).removeClass( 'current' );
				$( this ).addClass( 'current' );
			}

			$( parentSelector + ' ' + elementToMark ).live( 'click',{ parentSelector : parentSelector, elementToMark : elementToMark }, markCurrent );
		},

	overlayCloseBtn : function( overlayPlaceholderSelector, overlayCloseButtonSelector ){
			$( overlayCloseButtonSelector ).live( 'click', function( e ){
					e.preventDefault();
					$( overlayPlaceholderSelector ).html( '' );
				} );
		},

	fixIEImageClick : function(){
			$( 'label img' ).live( 'click', function(){
					$( 'input', this.parentNode ).click();
				} );
		},
		
	
	addAjaxIndicator : function( indicatorElement ){
		$('#loadingLayer').ajaxStart( function(){
			$( this ).css('visibility', 'visible');
		} );

		$('#loadingLayer').ajaxComplete( function(){
			$( this ).css('visibility', 'hidden');
		} );
		/*
		if ( indicatorElement ){
			$( 'body' ).append( indicatorElement );
		}

		$( 'body' ).ajaxStart( function(){
			$( this ).addClass( 'loading' );
		} );

		$( 'body' ).ajaxComplete( function(){
			$( this ).removeClass( 'loading' );
		} );
		*/
	},

	// custom functions
	
	manageEditButton : function( callerSelector, parentSelector, placeholderSelector ){
			function addEditButton(){
				$( parentSelector + ' ' + placeholderSelector ).remove();
				$( this ).after( '<div id="' + placeholderSelector.replace( /#/, '' ) + '"></div>' );
				$.get( this.href, { AJAXLoad : placeholderSelector }, function( text ){
					XPE.replaceContent( parentSelector + ' ' + placeholderSelector, text )
				} );
			}

			$( callerSelector ).live( 'click', addEditButton );
		}
}

