/**
 *
 * BIP new
 * skrypty ogólne
 * 
 * @lastmodified 
 * @requires JQuery 1.4.2+
 * @project bip_new
 *
 */


/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}}; 
 
 
/* ************************************************
   Document ready 
*/
$(function(){

	
	/**
	 * in-field label 
	 */
	$(':input[title]').each(function() {
		var $this = $(this);
		if($this.val() === '') {
			$this.val($this.attr('title'));
		}
		$this.focus(function() {
			if($this.val() === $this.attr('title')) {
				$this.val('');
			}
		});
		$this.blur(function() {
			if($this.val() === '') {
				$this.val($this.attr('title'));
			}
		});
	});
	
	
	/**
	 * Zmiana rozmiaru tekstu i trybu tekstowego
	 */
	$('#readability .size > a').click( function () {
		var cl = 'font'+$(this).attr('class');
		$('body').removeClass('fontsize-1 fontsize-2').toggleClass(cl)
		$.cookie('readability_size', cl);
		return false;
	} );
	
	$('#readability .contrast > a').click( function () { 
		// kontrast:
		$('body').toggleClass('contrast');
		$.cookie('readability_contrast', $('body').hasClass('contrast') ? 'contrast' : '');
		return false;
	} );
	// Włączenie trybu na start strony wedle zapisanych ciastek:
	$('body').addClass($.cookie('readability_size'));
	$('body').addClass($.cookie('readability_contrast'));	
	
	
	/**
	 * Pokazywanie / ukrywanie - np. sekcji w formularzach
	 */
	(function(){
		var $toggler = $('.showhide-toggler');
		
		$toggler
		.click( function () {
			var $wrapper = $(this).closest('.showhide');
			$wrapper.find('.showhide-content').slideToggle('fast', function() {
				$wrapper.toggleClass('showhide_collapsed').toggleClass('showhide_expanded');
			} );
			return false
		} )
		.end()
		
	})(); 
	/**/

	
	/**
	 * Aktywacja colorboxa
	 * @requires jquery.colorbox
	 */
	if (typeof($.fn.colorbox) !== 'undefined') { 
		$("a[rel^='gallery']").colorbox(config.colorbox);
	}
	
	
	/**
	 * Tooltipy
	 * 
	 * @url http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips
	 * @todo przerobić na plugin
	 */
	var $liveTip = $('<div id="livetip"><i></i><div></div></div>').hide().appendTo('body');
	var tipTitle = '';

	$('.has-tooltip[title]')
	//.add('.mod_quicklaunch.ql_compact')
	.bind('mouseover mouseout mousemove', function(event) {
		var $link = $(event.target).closest('a');
		if (!$link.length) { $link = $(event.target).closest('.has-tooltip') }
		
		if (!$link.length) { return; }
		var link = $link[0];
		
		
		if (event.type == 'mouseover' || event.type == 'mousemove') {
			$liveTip.css({
			  top: event.pageY + 12,
			  left: event.pageX + 12
			});
		}

		if (event.type == 'mouseover') {
			tipTitle = link.title;
			link.title = '';
			//$liveTip.html('<div>' + tipTitle + '</div><div>' + link.href + '</div>')
			$liveTip.find('div').html(tipTitle)
			.end()
			.show();
		}

		if (event.type == 'mouseout') {
			$liveTip.hide();
			if (tipTitle) {
					link.title = tipTitle;
				}
		}
	}); // Tooltipy	

	
}) // Document ready


/**
 * Modal dialogs wrapper
 *
 * @requires jquery.simplemodal
 */
var ModalDialog = {
	open: function(el, opt) {
		if ( typeof($.fn.modal) === 'function' ) {
			if (typeof(opt) === 'object') {
				if (opt.onCloseEffect) {
					opt.onClose = function(dialog) {
						dialog.data.fadeOut('slow', function () {
							dialog.container.hide(0, function () {
								dialog.overlay.fadeOut('fast', function () {
									$.modal.close();
								});
							});
						});
					}
				}
			}
			$(el).modal( $.extend( '', opt, {  } ));
		}		
	},
	close: function() {
		$.modal.close();
	}
};
/**/


if (typeof(config) === 'undefined') var config = {};
config.colorbox = {
	opacity: .64,
	transition: 'none',
	current: "<b>{current}</b> z {total}"
}
