var BezpieczneDrogi = {
	start: function() {
		/*
			Table of contents
		*/
		this.banner();
		this.funnyFacts();
		this.fancyFlashAnimations();
		this.fancyGallery();
    	this.miniGallery();
		this.beautifulForms();
		this.usableForms();
		this.ie();
	},
	banner: function(){
		/*
			Hide banner on x-click
		*/
		$banner = $('.banner');

		if( $banner.length ){
			$banner.find('.close').bind('click', function(){
				$(this).parent().animate({
					opacity: 0
				}, 800, function(){
					$(this).slideUp();
				});
				return false;
			});
		}
		
		/*
			right or left side?
		*/
		if( $.client.os == 'Mac' ){
			$banner.find('.close').addClass('leftHandSide');
		}
	},
	funnyFacts: function(){
		/*
			facts rotator for .funnyFacts
		*/
		
		$facts = $('.funnyfact');
		
		if( $facts.length ){
			$nav = $facts.find('.nav');
			$facts.find('.list').cycle({
				timeout: 0,
				prev: $nav.find('.prev a'),
				next: $nav.find('.next a'),
				fx: 'scrollHorz',
				easing: 'swing',
				speed: 300,
				height: 'auto',
				after: onAfter
			});
		}
		
		// callback for setting new height
		function onAfter(curr, next, opts, fwd){
			var ht = $(this).height();
			$(this).parent().animate({
				height: ht
			});
			
			if($.browser.msie && $.browser.version == '6.0'){
				/* IE 6 height fix */
				$(this).parent().parent().next('.nav').find('li').css('height', ht).find('a').css('height', ht);
			}
		}
	},
	fancyFlashAnimations: function(){
		/*
			adds flash animations
		*/
		
		// movie player
		$('#flashplayer').flash({
			src: 'flash/mainBaner.swf',
			width: 974,
			height: 374,
			flashvars: { mainBanerXMLPath: 'flash/data/movies.xml' },
			wmode: 'transparent',
			version: 10, 
			expressInstall: true
		});
		
		// games (bottom)
		$('#flashgames').flash({
			src: 'flash/kidsBaner.swf',
			width: 950,
			height: 360,
			wmode: 'transparent'
		});
		
	},
	fancyGallery: function(){
		$("a[rel=fancygallery]").fancybox({
						'transitionIn'		: 'none',
						'transitionOut'		: 'none',
						'titlePosition' 	: 'over',
						'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
						    return '<span id="fancybox-title-over">Zdjęcie ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
						}
			});
	},
	miniGallery: function(){
		/*
			picture rotator for .gallery
		*/

		$gallery = $('.gallery');
		
		if( $gallery.length ){
			$gallery.
				find('.editions li').
				each(function(){
					$zis = $(this)
					$(this).find('.pics ul').cycle({
						timeout: 0,
						prev: $zis.find('.prev a'),
						next: $zis.find('.next a'),
						speed: 500,
						fx: 'scrollHorz',
						easing: 'swing'
					});
				});
		}
	},
	beautifulForms: function(){
		/*
			advanced search
		*/
		$('.advsearch form .col1').jqTransform();
		
		var $multiselect = $('.multiselect');
		var $wojew = $('.wojew');
		
		$multiselect.find('input').focus(function(){
			return false;
		});
		
		
		$multiselect.find('label').each(function(){
			if($(this).next('input').attr('checked')){
				select($(this));
			}
			else{
				deselect($(this));
			}
		}).click(function(){
			toggle($(this));
			return false;
		});
		
		$('area').click(function(){
			what = $(this).attr('class');
			
			toggle(what);

			return false;
		});
		
		function toggle(what){
			if( typeof(what) == 'string' ){
				$that = $multiselect.find('label.'+what);
			}
			else if( typeof(what) == 'object'){
				$that = what;
			}

			if( $that.hasClass('selected') ){
				deselect($that);
			}
			else{
				select($that);
			}
		}
		function select(what){
			what.
				addClass('selected');
			what.
				next('input').
				attr('checked', 'checked');
			$wojew.
				find('.'+what.attr('for')).
				fadeIn();
		}
		function deselect(what){
			what.
				removeClass('selected');
			what.
				next('input').
				attr('checked', '');
			$wojew.
				find('.'+what.attr('for')).
				fadeOut();
		}
		
		// beautiful scroll
		$multiselect.find('ul').jScrollPane();
		
	},
	usableForms: function(){
		/* teaser */
		$input = $('.subscribe .i input');
		defaultText = $input.prev('label').text();
		handle($input, defaultText);

		/* mini-search */
		$input = $('.minisearch .i input');
		defaultText = $input.prev('label').text();
		handle($input, defaultText);

		// handler
		function handle(element, text){

			if(element.attr('value') && element.attr('value').length == 0)
			{
				element.attr('value', defaultText);
			}
			
			element.bind('focus', function(){
				if($(this).attr('value') == text){
					$(this).attr('value', '');
				}
			}).bind('blur', function(){
				if($(this).attr('value') == ''){
					$(this).attr('value', text);
				}
			});
		}
	},
	ie: function(){	
		/* ;( */
		
		if( $.browser.msie ){
			if($.browser.version == '6.0'){
				$('#nav li').mouseover(function(){
					$(this).addClass('hover');
				}).mouseout(function(){
					$(this).removeClass('hover');
				});
				
				
			}
		}
		
	}
};

$(function() {
	BezpieczneDrogi.start();
});