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;
}

