var timer;

jQuery(window).load(function () {
	// IE FIX!
	if (jQuery.browser.msie) {
		jQuery('ul#menu ul').each(function(i, item){
			jQuery(item).css( 'margin-left', '-'+(jQuery(item).prev().prev().width()+53)+'px' );
		});
	}
	// END OF IE FIX
	
	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(document).ready(function() {
	jQuery('a[rel$="external"]').attr('target', '_blank');
});

/**
 * 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] + ' ';
	   }
	   		   
	}
	
}