function slideshow(id, images){
	var cnt = $('#' + id);
	$.each(images, function(i, img){
		cnt.append( img );
	});
	cnt.nivoSlider();
};

function gotoUrl(url){
	top.location.href = url;
}


function closeBox(redir, msg){
	top.document.currBox.hide(function(){
		top.document.currBox = null;
		if( msg ){
			alert(msg);
		}
		if( redir === true ){
			top.document.location.reload();
		} else if( redir ){
			top.document.location = redir;
		}
	});
};

function showErrors( formId, err ){
	//formularz, z ktorego pochodza bledy
	var f = $('[value=' + formId + ']').closest('form');
	//pokaz wszystkie bledy
	$.each(err, function(name, errors){
		//div zawierajacy bledny element
		var d = f.find('[name=' + name + ']').closest('div.frmElm');
		var l = d.find('label:first');
		//pokaz wszystkie bledy
		$.each( errors, function(i,e){
			$('<span/>').addClass('error').css({
				'margin-left':	'170px',
				'margin-top':	'4px'
			}).text(e).appendTo(d);
		});
	});
};

$(function(){
	
	//mainmenu
	$('#nav li').each( function(){
		if ( $(this).children('ul')[0] ) {
 			$(this).append('<span/>').children('span').addClass('hasChildren');
 		}
	} ).children('ul').hide().end().hover(function() {
		$(this).find('> ul').show();	// mouseover
	}, function() {
		$(this).find('> ul').hide();	// mouseout
	});
	
	//zebra
	$('.subpost_left table').each(function(i, tbl){
		var rows = $(tbl).find('tr').css('background-color', '');
		if( rows.length > 2 ){
			rows.filter(':odd').css('background-color', '#dfdfdf');
		}
		
	});
	
	//boxes logic 1
	var showBox	= function(html, link){
		top.document.currBox = new Boxy( html, {
			'closeable':	true,
			'closeText':	'[Zamknij]',
			'unloadOnHide':	true,
			'modal':		true,
			'title':		link.attr('title')
		} );
		return false;
	};
	
	//boxes logic 2
	$('a.iframe').live('click', function(){
		var link	= $(this);
		var w		= link.attr('width') || 900;
		var h		= link.attr('height') || 500;
		var i		= '<iframe width="' + w + '" height="' + h + '" src="' + link.attr('href') + '"></iframe>';
		return showBox(i, link);
	});
	
	//boxes logic 3
	$('a.modal').live('click', function(){
		var link	= $(this);
		$.get( link.attr('href'), function(html){
			showBox( html, link );
		} );
		return false;
	});
	
	//special links
	var confirmLink = function(){
		var t = $(this).attr('title') || '';
		return t == '' || confirm(t);
	};
	
	//special links 2
	$('a.confirm').live('click', confirmLink);
	
	//special links 3
	$('a.ajax').live('click', function(){
		if( confirmLink.call(this) ){
			$.get( $(this).attr('href'), function(response){
				try {
					var u = eval(response);
					if( /^http\:\/\//.test(u) ){
						gotoUrl(u);
					} else {
						console.log( 'ajax response:', response );
					}
				} catch(err){
					console.log( 'ajax response:', response, ' error: ', err );
				}
			} );
		}
		return false;
	});
	
	$(document).ready( function(){
		$('a.thickbox').fancybox();
	} );
	
	$('form table th').attr('align', 'right');
});
