$(document).ready(function(){
	
	//zewnetrzne linki
	$('a.external').live('click', function() {
		window.open(this.href);
		return false;
	});
	
	$('input.hinted').inputHints();
	
	$('.hidden').hide();
	$('.showing').show();
	
	//zaznacz/odznacz checkbox
	$('#checkboxSelectAll').click(function() {
		$('input[@type=checkbox]').attr({ checked: "checked"});
	});

	$('#checkboxDeselectAll').click(function() {
		$('input[@type=checkbox]').removeAttr("checked");
	});		
	
	// Add markItUp! to your textarea in one line 
	// $('textarea').markItUp( { Settings }, { OptionalExtraSettings } ); 
	//$('#post_text').markItUp(mySettings); 


 	//dodawanie/usuwanie z ulubionych
	$(".ajax a").click(function(){
		
		var title = $(this).attr('title');
		var href = $(this).attr('href');
		var parent = $(this).parent().get(0).className;
		
		if($(this).attr('class')=='add')
		{
			ajax_add(title,href,parent);
		}
		if($(this).attr('class')=='del')
		{
			ajax_del(title,href,parent);
		}		
		return false;
	});

 	//dodawanie do ulubionych
 	function ajax_add(title, href, parent)
	{
		$.ajax({
			type:   'POST',
			dataType: 'json',
			url:	href,
			data: 'type=ajax&title='+title,
			timeout: 5000,
			beforeSend:  function() {
				
			},
			error: function(msg) {
				$(".add").show();
				alert(msg);
			},
			success:  function(json) {
				if(json.error == 'true') 
				{ 
					alert(json.text); 
				}
				else
				{
					var add = "." + parent + " > .add";
					var del = "." + parent + " > .del";
					$(add).hide();
					$(del).show();					
				}
			}
		});		
	}

 	function ajax_del(title, href, parent)
	{
		$.ajax({
			type:   'POST',
			dataType: 'json',
			url:	href,
			data: 'type=ajax&title='+title,
			timeout: 5000,
			beforeSend:  function() {
				
			},
			error: function(msg) {
				$(".del").show();
				alert(msg);
			},
			success:  function(json) {
				if(json.error == 'true') 
				{ 
					alert(json.text); 
				}
				else
				{
					var add = "." + parent + " > .add";
					var del = "." + parent + " > .del";

					$(del).hide();
					$(add).show();					
				}
			}
		});		
	}


	// menu
	var submenu = '';
	var t;
	$('#menu a').bind('mouseover', function () {
		$('#menu a.selected').removeClass('selected');
		$(this).addClass('selected');
		
		clearTimeout(t);
		
		var div = $(this).attr('rel');
		if(div.length) {
			if(submenu.length)
				$('#'+submenu).hide();
			$('#submenu .active').hide();
			$('#'+div).show();
			submenu = div;
		}
		else {
			if(submenu.length)
				$('#'+submenu).hide();
			$('#submenu .active').hide();
		}
	});
	
	$('#menubar').bind('mouseleave', function () {
		t = setTimeout(
			function()
			{
				$('#menu a.selected').removeClass('selected');
				$('#menu a.current').addClass('selected');
				
				if(submenu.length)
					$('#'+submenu).hide();
				
				$('#submenu .active').show();
				submenu = '';
			},
			2000
		);
	});
	
	// tooltipsy
	//$('.form_error, .form_hint').tipsy({source: "html"});

	// dialog potwierdzajacy wykonanie zdarzenia
	$('a.delete').live('click', function(){
		confirm_dialog($(this).attr('href'));
		return false;
	});
	
	// zamykanie komunikatu
	$('.btn_close').live('click', function(){
		$(this).parent('div').hide('blind');
		return false;
	});
	
	// hover albumow
	$('ul.gallery li').live('mouseover', function(){
		$(this).addClass('hover');
	});
	$('ul.gallery li').live('mouseout', function(){
		$(this).removeClass('hover');
	});
	
	// lightbox

    $("a[rel*=lightbox]").lightBox({
			imageLoading: '/img/ico_loading_light.gif',
			imageBtnClose: '/img/lightbox/close.gif',
			imageBtnPrev: '/img/lightbox/prev.gif',
			imageBtnNext: '/img/lightbox/next.gif',
			txtImage: 'Image',
			txtOf: 'of'
    });

    $("a[rel*=lightbox]").live('click', function(){
		return false;
	});
	
	// przelaczanie tabow
	$('ul.tabswitch a').live('click', function() {
		var el = $(this);
		var item = $(this).parents('li');
		var target = $('#'+el.attr('rel'));
		
		item.addClass('active').siblings('li.active').removeClass('active');
		target.show().siblings().hide();
		
		return false;
	});
	
	// kropki paginacji
	$('p.pagination a.dots').live('click', function() {
		var item = $(this);
		var dots = item.next('span.dots');
		if(!dots.is(':visible'))
		{
			$('p.pagination span.dots').hide();
		}
		
		dots.css('top', item.offset().top + item.outerHeight() + 1).css('left', item.offset().left).toggle();
		return false;
	});
});

/**
 * Obsluga okna dialogowego dla potwierdzen
 *
 * @param url	strona na ktora ma byc przekierowanie po akceptacji
 * @is_form		czy przycisk nalezy do formularza
 *				jesli 'true' jako url nalezy podac sciezke do formularza
 */
function confirm_dialog(url, is_form)
{
	if (is_form == true)
	{
		$('#confirm_dialog').data('form', url);
	}
	else
	{
		$('#confirm_dialog').data('url', url);
	}

	$('#confirm_dialog')
		.dialog({
			big_frame: true,
			modal: true,
			minHeight: 50,
			buttons: {
				Yes: function() {
					if (form = $('#confirm_dialog').data('form'))
					{
						$(form).submit();
					}
					else
					{
						document.location.href = $('#confirm_dialog').data('url');
					}

					$(this).dialog('close').dialog('destroy');
				},
				No: function() {
					$(this).dialog('close').dialog('destroy');
				}
			}
		}).dialog('open');

		// przyciski z i18n
		var btn_yes = $("#confirm_dialog #btn_yes").text();
		var btn_no = $("#confirm_dialog #btn_no").text();
		$('#confirm_dialog').parent().find('.ui-dialog-buttonpane button:first').text(btn_yes);
		$('#confirm_dialog').parent().find('.ui-dialog-buttonpane button:last').text(btn_no);


    jQuery.cookie = function(name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            // CAUTION: Needed to parenthesize options.path and options.domain
            // in the following expressions, otherwise they evaluate to undefined
            // in the packed version for some reason...
            var path = options.path ? '; path=' + (options.path) : '';
            var domain = options.domain ? '; domain=' + (options.domain) : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    };
    
}


