var timer;

jQuery(window).load(function () {
	if (jQuery.browser.msie) {
		jQuery('ul#menu ul').each(function(i, item){
			jQuery(item).css( 'margin-left', '-'+(jQuery(item).prev().prev().width()+53)+'px' );
		});
	}

	jQuery('.customize').click(function() {
		if (jQuery(this).parent().hasClass('hidden')) {
			jQuery(this).parent().removeClass('hidden').prev().removeClass('hidden');

			jQuery(this).next().show();
			jQuery('.customizebox_shadow').height( jQuery('.customizebox').height()+1 );
		} else {
			jQuery(this).parent().addClass('hidden').prev().addClass('hidden');
			jQuery(this).next().hide();
		}

		return false;
	});

	if (jQuery.browser.msie) {
		jQuery('.customizebox fieldset legend').css('margin-left', '-3px');
	}

	jQuery('.hidecustomizebox').click(function() {
		jQuery(this).parent().parent().parent().prev().click();
		return false;
	});

	jQuery('a.mtip').mtip();

	jQuery('.toogle').click(function () {
		jQuery('div#shadow').remove();

		if(jQuery(this).next().css('display') == 'block'){
			jQuery('ul#menu ul').slideUp('fast');
			return false;
		}

		jQuery('ul#menu ul').slideUp('fast');

		shadow = jQuery('<div id="shadow" style="display: none; position: absolute; opacity: 0.19; filter: alpha(opacity=19); -moz-opacity: 0.19; background: #000; z-index: 190; width: 100px; height: 100px; margin-top: '+(parseInt(jQuery(this).next().css('margin-top'))+2)+'px; margin-left: '+(parseInt(jQuery(this).next().css('margin-left'))+2)+'px; height: '+(jQuery(this).next().height()+4)+'px; width: '+jQuery(this).next().width()+'px;"></div>');
		shadow.insertBefore( jQuery(this).next() );
		jQuery(this).next().slideDown("fast");
		jQuery(this).next().next().slideDown("fast");

		clearTimeout(timer);
		timer = setTimeout(function(){
        	jQuery('div#shadow, ul#menu ul').slideUp('fast');
        }, 2500);

		return false;
	});

    jQuery('ul#menu ul').mouseout(function(){
    	timer = setTimeout(function(){
        	jQuery('div#shadow, ul#menu ul').slideUp('fast');
        }, 500);
    });

	jQuery('ul#menu ul').mouseover(function(){
		clearTimeout(timer);
	});

	jQuery('#messagesContainer p').click(function(){
		jQuery('#messagesContainer').slideUp('slow');
    });
 /*
    jQuery( 'form:not("#registration")' ).submit( function(){
    	jQuery( this ).find( 'input[type="image"], input[type="submit"]' ).attr( 'disabled', 'true' );
    });
 */

	jQuery('.tjs .tabsbox').each(function(i, el){
		if( (i != 0) && ( !jQuery(el).hasClass( 'selected' ) ) ) {
			jQuery(el).hide();
		}
	});

	jQuery('.tjs .tabs a').click(function() {
		jQuery('.tjs .tabs li').attr('class', null);
		jQuery(this).parent().attr('class', 'selected');
		jQuery('.tjs .tabsbox').hide();

		jQuery('.tjs #tab'+jQuery(this).attr('href').substring(1)).show();

		return false;
	});

	/**
	 * Pozwala na otwieranie w nowych oknach dla linków bez łamania xhtml'a
	 */
	jQuery('a[rel$="external"]').live('click', function(){
		jQuery(this).attr('target', '_blank');
	});

	/**
	 * Czyści wartość inputa
	 */
	jQuery('input.clear-value').bind('focus', function(){
		var name = jQuery(this).attr('name');
		var data = jQuery(this).data(name);
		if ( data == undefined ) {
			jQuery(this).data(name, jQuery(this).val());
			jQuery(this).val('');
		}
	});
	jQuery('input.clear-value').bind('blur', function(){
		var name = jQuery(this).attr('name');
		if ( jQuery(this).val() == '' ) {
			jQuery(this).val(jQuery(this).data(name));
			jQuery(this).removeData(name);
		}
	});
});

/**
 * showThumb
 *
 * @author	Józek
 */
(function($) {
	$.fn.thumb = function( options ) {
		var settings = {};
		var item = null;

		$(this).live('mouseover', function(e) {
			if (!$(this).data('init')) {
				$(this).data('init', true);
				init( options );

				item = $(this);

				imgAttrs = {};
				if ( !imgAttrs.src ) {
					imgAttrs.src = item.attr('rel');
					if ( !imgAttrs.src ) {
						return false;
					}
				}

				if ( !imgAttrs.alt ) {
					imgAttrs.alt = item.attr('title');
					if ( !imgAttrs.alt ) {
						imgAttrs.alt = '';
					}
				}

				x = e.pageX+settings.offX;
				y = e.pageY+settings.offY;

				this.thumbDiv = $('<div style="position: absolute" />').attr(settings.wrapperAttrs).appendTo(settings.container);

				if ( settings.addShadow ) {
					this.divShadow = $('<div />').attr(settings.shadowAttrs);
					$(this.divShadow).append(this.thumbDiv);
				}

				$(this.thumbDiv).html( settings.formatOutput.call(item) );

				$(this.thumbDiv).show().css({left: x + "px", top: y + "px"});
				if ( settings.addShadow ) {
					$(this.divShadow).show().css({left: x + 20 + "px", top: y + 20 + "px"});
				}
			}
		} ).live('mouseout', function() {
			if( this.thumbDiv ) {
				$(this.thumbDiv).remove();
				this.thumbDiv = null;
				$(this).removeData('init');
				if ( settings.addShadow && this.divShadow ) {
					$(this.divShadow).remove();
					this.divShadow = null;
				}
			}
		} ).live('mousemove', function(e){
			$(this.thumbDiv).css({
				left: (e.pageX+settings.offX) + 'px',
				top: (e.pageY+settings.offY) + 'px'
			});
		} );

		function init( options ) {
			settings = $.extend({
				wrapperAttrs: {
					'class': 'thumb'
				},
				addShadow: false,
				shadowAttrs: {
					'class': 'thumbShadow'
				},
				dimensions: {
					width: 206,
					height: 133
				},
				offX: 16,
				offY: 16,
				container: 'body',
				formatOutput: function( obj ) {
					return jQuery( '<img src="' + obj.attr( 'rel' ) + '" />' ).attr( settings.dimensions );
				}
			}, options);
		}

		return this;
	}
})(jQuery);

/**
 * tooltip
 */
jQuery.fn.mtip = function() {
	this.mouseover(
		function(e) {
			x = jQuery(this).position().left;
			y = jQuery(this).position().top+40;

			if(!this.tooltipdiv) {
				// create a div and style it
				var div = document.createElement("div");
				this.tooltipdiv = div;
				jQuery(div).css({
					padding: '5px 0 0 0',
					font: '11px Tahoma',
					background: 'transparent url(\'/images/layout/arr_pink_top.gif\') 20px 0 no-repeat',
					position: 'absolute'
				})
				.html('<p style="background-color: #c10055; padding: 7px; margin: 0; color: #fff;">'+(this.title || this.alt)+'</p>');

				this.title = "";
				this.alt = "";

				jQuery("body").append(div);
			}

			jQuery(this.tooltipdiv).show().css({left: x + "px", top: y + 3 + "px"});
		}
	).mouseout(
		function() {
			if(this.tooltipdiv) {
				jQuery(this.tooltipdiv).hide();
			}
		}
	);
	return this;
}

/**
 * Zwykla funkcja. Jest to JSowy odpowiednik helpera widoku parseToUrl z PHP. Moze sie przydac, zwlaszcza przy AJAXie
 *
 * @author Michał Piotrowski
 */
function parseToUrl( sString )
{
	aPl = new Array('ę', 'ó', 'ą', 'ś', 'ł', 'ż', 'ź', 'ć', 'ń', 'Ę', 'Ó', 'Ą', 'Ś', 'Ł', 'Ż', 'ű', 'Ć', 'Ń', ' ');
	aStd = new Array('e', 'o', 'a', 's', 'l', 'z', 'z', 'c', 'n', 'e', 'o', 'a', 's', 'l', 'z', 'z', 'c', 'n', 'asdfghjkl');

	var iCounter = aPl.length;
	for( i = 0; i < iCounter; i++ ) {
		sString = sString.replace( aPl[i], aStd[i] );
	}

	sString = sString.replace( 'asdfghjkl', '_' );
	return sString;
}

/**
 * Zwykla funkcja. Jest to JSowy odpowiednik helpera widoku stringSplit
 *
 * @author Michał Piotrowski
 */
function stringSplit( sString, iCount ){
	if( sString.length <= iCount) {
		return sString;
	}

	aString = sString.split(' ');
	iCounter = aString.length;
	sString = '';

	for(x=0;x<iCounter;x++){
		var sTmpString = sString + aString[x] + ' ';

		if (sTmpString.length >= iCount) {
		    if( sString != '' ) {
		    	return sString;
		    } else {
		    	return sTmpString.substring(0, iCount );
		    }
	   } else {
		   sString += aString[x] + ' ';
	   }
	}
}

function htmlentities( s ){
    // Convert all applicable characters to HTML entities
    // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    var div = document.createElement('div');var text = document.createTextNode(s);div.appendChild(text);return div.innerHTML;
}
function html_entity_decode(str){
    // original by: john (http://www.jd-tech.net)
	var tarea=document.createElement('textarea'); tarea.innerHTML = str; return tarea.value; tarea.parentNode.removeChild(tarea);
}
function trim( str ){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

/**
 * ustawia ciastko na expiredays dni (gdy null to do zabicia przegladarki)
 */
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	';path=/;host=.poszkole.pl;';
}

/**
 * pobiera cookie, zwraca pusty ciag jezeli nie znalazl
 * @author Marek
 * @reutrn string
 */
function getCookie(c_name){
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
