
	/* lang vars */
	var lang = {
		more : 'więcej',
		less : 'mniej',
		prev : 'poprzednia',
		next : 'następna',
		close: 'zamknij'
	};

/*
 jQuery('img.photo',this).imagesLoaded(myFunction)
 execute a callback when all images have loaded.
 needed because .load() doesn't work on cached images

 mit license. paul irish. 2010.
 webkit fix from Oren Solomianik. thx!

 callback function is passed the last image to load
 as an argument, and the collection as `this`
*/
$.fn.imagesLoaded = function (callback) {
  var elems = this.filter('img'),
    len = elems.length;
  elems.bind('load', function () {
    if (--len <= 0) {
      callback.call(elems, this);
    }
  }).each(function () {
    // cached images don't fire load sometimes, so we reset src.
    if (this.complete || this.complete === undefined) {
      var src = this.src;
      // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
      this.src = '#';
      this.src = src;
    }
  });
};

	
function css() {
	/* 
	 * nav mod
	 */
	 $('#nav>div').each(function(index){
		var item = { act: $(this).find('.act').text() }
                if ($(this).find('.act').length) {
                  $(this).prepend('<strong>' + (index ? '&ndash;' : '') + item.act + '</strong>');
                } else {
                  $(this).prepend('<strong>' + (index ? '&mdash;' : '') + '</strong>');
                }
		item.width = 0;
		$('li',this).each(function(){
			item.width += $(this).outerWidth() + 5;
		});
		
                /*$('ul',this).width(item.width);*/
	 });
	$('#nav li').mouseover(function(){
	
		var offsetA = $(this).parent().offset();
		var offsetB = $(this).offset();
		var offsetC = $(this).width();
		
		$('#nav ul').css( 'background-position', (-600 + offsetC + (offsetB.left - offsetA.left)) + 60 + 'px 0' );
	});	
	$('#nav strong').mouseover(function(){
	
		var offsetA = $(this).next().offset();
		var offsetB = $(this).next('ul').find('li:first').offset();
		var offsetC = $(this).next('ul').find('li:first').width();
		
		$('#nav ul').css( 'background-position', (-600 + offsetC + (offsetB.left - offsetA.left)) + 60 + 'px 0' );
	});
	
	
	/* 
	 * filter menu
	 */  
	$('.menu-frm input:checked').parent().addClass('act');
	$('#team-list').menuFilter().team();
        $('#media-list').menuFilter('div');
        $('#client-list').menuFilter('.clients-entries', true);
	$('.clients-entries').menuFilter();
	
	/* 
	 * intro page background scale
	 */
	if($('body').is('.intro')){
		$(window).resize(function(){
			iResize();
		});
		
		function iResize(){
			windowHeight = $(window).height()-24;
			$('#intro-img').css({
				'height':windowHeight
			});
		}
		iResize();
		
		$('#subnav ul').prepend('<li>zwiń top</li>')
		$('#subnav li:first, #intro-img').click(function(){
			$('#page').slideToggle();
			$('#intro-img span').toggle();
			
		});
                $('#intro-img span').css('background-image', 'url(' + $('#intro-img span img').attr('src') + ')');
                $('#intro-img span img').remove();
	}
	
	/*
	 * lista projektów
	 */
	$('#projects-list ul').each(function(){
		_w = 0;
		$(this).children('li').each(function(){
			_w += $(this).width();
		});
		$(this).width(_w);
	});
	 
	 
	$('#projects-list').menuFilter().find('a').cloud();
	 
	$('.projects-menu ul').each(function(){
		_w = 0;
		/*if($(this).children('li').size() > 6){
			$(this).children('li:gt(5)').addClass('hidden').hide();
			$(this).append('<li class="trigger">...</li>').after('<a href="#" class="link-more>'+lang.more+'</a>');
			
		}*/
		
		$(this).children('li').each(function(){
			_w += $(this).height();
			
			if($(this).is('.act'))
				return false;
		});
		$(this).css('background-position', '8px '+ (-240+_w-5) + 'px');
	 });
	 $('.projects-menu  .link-more').bind('click',function(e){
		e.preventDefault();
		$(this).prev().find('.trigger').toggle();
		$(this).prev().find('.hidden').toggle();
		$(this).toggleClass('act');
		$(this).is('.act') ? $(this).text(lang.less) : $(this).text(lang.more);
	 });	
	 
	 
         $('.project-data > div:first-child > small').unbind().bind('click',function(){
                $(this).parent().toggle().next('div').toggle();
         });
	 $('.project-data .link-close').unbind().bind('click',function(e){
		e.preventDefault();
		$(this).parent().toggle().prev('div').toggle();
		
	 });
	 $('#gallery-mod .gallery').gallery();
	 $('#gallery-mod .gallery2').gallery2();
         if ($(window).popeye) {
           $('#gallery-mod .gallery3').popeye({
             caption:    false,
             navigation: 'hover',
             direction:  'right'
           });
           /*$('.ppyc').popeye();*/
         }

	if ($(window).fancybox) {
		$('#gallery-mod .gallery a, #gallery-mod .gallery2 a, .news-entries a').fancybox();
        }
	if (window.EYE) {
		$('#gallery-mod .gallery a, #gallery-mod .gallery2 a'/* + ', .news-entries a'*/).zoomimage({centered: true});
                /*$('.news-files').each(function() {
                  $(this).find('a').zoomimage({centered: true});
                });*/
        }
	if ($(window).lightbox) {
		$('#gallery-mod .gallery a, #gallery-mod .gallery2 a, .news-entries a, .media-entries a, .clients-entries a').lightbox({
                    fitToScreen: true,
                    /*loopImages: true,*/
                    disableNavbarLinks: true
                });
        }



	$('.link-print').bind('click',function(){ window.print(); });
	$('.frm select').select();
	$('input:checkbox').checkbox();
		 

}

function ie() {
	/* go on for IE in generall */
	if($.browser.msie){
		
		if($.browser.version <= 7) {
			/* take care IE 7 & lower */
			
			$('input').ieClass('type');
			
			$('.col-1-lay #content>.column:first-child').addClass('first');
			
			$('.quote p').cssBefore('„');
			$('.quote p').cssAfter('”');
			$('.news-entries .date').cssBefore('/');
			$('.frm button[type=submit] span').cssAfter('>');
			
			$('#team-data a[href^=mailto] ').addClass('href-mailto')
		}
		
		$('.intro #content .column:nth-child(3)').addClass('nth-child3');
		$('div:last-child, li:last-child, td:last-child').addClass('last-child');
		$('tr:odd').addClass('odd');
		$('tr:even').addClass('even');
		
	}
	
}

	/* 
	 * Load jQuery sets
	 */
$(document).ready(css);
$(document).ready(ie);


	/* 
	 * jQuery extras
	 */
$.fn.extend({
	ieClass: function(ieAttr) {
		return this.each(function(){
			$(this).addClass($(this).attr(ieAttr));
		});
	}, // ieClass	
	unwrap: function() {
		this.parent(':not(body)').each(function(){
			$(this).replaceWith( this.childNodes );
		});
		return this;
	}, // unwrap
	cssBefore: function(html) {
		this.prepend('<span class="before">'+html+'</span>')
		return this;
	}, // cssBefore
	cssAfter: function(html) {
		this.append('<span class="after">'+html+'</span>')
		return this;
	}, // cssAfter
	checkbox: function() {
		return this.each(function(){
			
			input_act = $(this).attr('checked') ? ' act' : '';
                        /*input_checkbox = document.createElement('checkbox');*/
			input_checkbox = $('<span class="checkbox"></span>');
			$(this).after(input_checkbox);
			$(this).hide().next().addClass(input_act);
			
			if ($.browser.msie || $.browser.opera) {
                                var _input = $(this);
				$(this).nextAll('span.checkbox, label').unbind().bind('click',function(){
					if(_input.attr('checked'))
						_input.attr('checked','');
					else
						_input.attr('checked','checked');
					
					_input.change().next().toggleClass('act');
				});
			} else {
				$(this).change(function(){
					$(this).next().toggleClass('act');
				});
                                var self = this;
                                input_checkbox.click(function() {
                                        $(self).attr('checked', ($(self).attr('checked') ? '' : 'checked')).change();
                                });
			}
			
		});
	}, // checkbox	
	select: function(){
		return this.each(function(){
			var _select = {
				strip : false,
				code : ''
			};
			
			$(this)	.children('option').each(function(){
				_text = $(this).text();
				_select.code += '<li data-val="'+$(this).val()+'">'+_text+'</li>';
			});
			$(this).after('<div class="select"><strong></strong><ul>'+_select.code+'</ul></div>').hide();
			
			
			_selected = $('.select li').eq(0);
				
			_select.code = _selected.text();
			
			if(_select.strip)
				$('.select strong').text(_select.code.substr(0, _select.strip) + '...');
			else
				$('.select strong').text(_select.code);
				
			_selected.addClass('act');
			
			$('.select li').unbind().bind('click',function(){
				_self = $(this).parents('.select');
				_value = $(this).attr('data-val');
				_text = $(this).text();
				
				$(this).siblings().removeClass('act');
				$(this).addClass('act');
				
				_self.prev('select').find('option').attr('selected','');
				_self.prev('select').find('option[value='+_value+']').attr('selected','selected')
				if(_select.strip)
					_self.find('strong').text(_text.substr(0, _select.strip) + '...');
				else
					_self.find('strong').text(_text);

				_self.find('ul').css('left','-5000em');
			});
			
			$('.select').mouseover(function(){ $(this).find('ul').css('left','0'); });
		
		});	// each
	}, // select
	menuFilter: function(selector, find){
		return this.each(function(){
                        selector = (selector ? selector : 'li');
			
			var mf = {
				root : $(this),
				items: (find ? $(this).find(selector) : $(this).children(selector)),
				input : $('.menu-frm input'),
				selection : $('.menu-frm input:checked')
			}
		
			/*if($.browser.msie){
				mf.input.parent().unbind().bind('click',function(){
					mf.input = $(this).find('input');
					
					
					if(mf.input.attr('checked'))
						mf.input.attr('checked','');
					else
						mf.input.attr('checked','checked');
					
					mfChange();
				});
				
			}
			else{*/
				mf.input.change(function(){
                                        if ($(this).attr('name') == 'all') {
                                                if ($(this).is(':checked')) {
                                                        mf.input.filter('[name!="all"]').attr('checked', 'checked').next().addClass('act');
                                                } else {
                                                        mf.input.filter('[name!="all"]').attr('checked', '').next().removeClass('act');
                                                }
                                        } else if (!$(this).is(':checked') && mf.input.filter('[name="all"]').is(':checked')) {
                                                mf.input.filter('[name="all"]').attr('checked', '').next().removeClass('act');
                                        } else if ($(this).is(':checked') && mf.input.filter(':not([name="all"]):checked').length == mf.input.length - 1) {
                                                mf.input.filter('[name="all"]').attr('checked', 'checked').next().addClass('act');
                                        }

					mfChange();
				});
			//}	
		
			function mfChange(){
				mf.input.parent().removeClass('act');
				
				mf.items.hide();
				
				mf.selection = $('.menu-frm input:checked');
				
				mf.selection.each(function(){
					mf.item = $(this).attr('name');
					if(mf.item == 'all'){
						mf.items.show();
						return false;
					}
					else{
						$('.'+mf.item).show();
					}
				}).parent().addClass('act');
			}
			
			if(mf.input.size() > 1)
				mfChange();
					
		});
	}, // menuFilter
	team : function(){
		$('.menu-frm').after('<div id="team-data" />');
		$(this).children('li').unbind('click').bind('click',function(){
                        if (window.EYE) {
                          $('div.zooimage').zoomimageClear();
                        }
			$('#team-data').empty();
			$(this).find('div').clone().appendTo('#team-data');
		});
		$('.menu-frm input').click(function() {
			$('#team-data').empty();
                        if (window.EYE) {
                          $('div.zooimage').zoomimageClear();
                        }
                });
                if (window.EYE) {
                  $('#team-list a').zoomimage({centered: true, beforeZoomIn: function() {
                    $(this).parent().click();
                  }});
                }
                if ($(window).lightbox) {
                        $('#team-list a').lightbox({
                            fitToScreen: true,
                            /*loopImages: true,*/
                            disableNavbarLinks: true
                        });
                }
	}, // team
	cloud: function() {
		if($('#cloud').size() == 0)
			$('body').prepend('<div id="cloud"></div>');
		
		var cloudText;
		
		$(this).mouseover(function(e){
		

			cloudText = $(this).attr('title') || $(this).text() || $(this).children().attr('alt');
			
			$('#cloud').html('<p>'+cloudText+'</p>');
			
			$(this).attr('title','');
			
		});
		
		$(this).mousemove(function(e){
			var x, y;
			try { x = e.pageX-20; y = e.pageY-30; } // FF
			catch(e) { x = event.clientX-20; y = event.clientY-40; } // IE
		
			y = y+20+'px';
			x = x+40+'px';
			if(cloudText)
				$('#cloud').css({ display: 'block', top: y, left: x})
		});
		
		$(this).mouseout(function(e){ 
			$('#cloud').removeClass().css({ display: 'none'}); 
			cloudText = $('#cloud p').text();
			$(this).attr('title',cloudText);
		});	
	}, // cloud()
	gallery:  function(set) {
		set = jQuery.extend({
			Step	: 650,
			Paging	: true
		}, set);	
		
		return this.each(function(i){	
		
			var g = {
				root	:	$(this),
				element	:	$(this).find('ul'),
				item	:	$(this).find('li'),
				size	:	$(this).find('li').size(),
				onhold	:	true,
				callback:	''
			}
	
				$(window).resize(function(){
					g.root.width( ( $('#page').width() - 304 ) );
				});			
			
			function init(){		
				g.item.eq(0).show();
				if(g.size > 1) {
                                        g.item.find('img').imagesLoaded(function() {
                                                buildGallery();
                                        });
                                }
			}
			
			function buildGallery(){
				
				g.root.attr('data-step',0).width( ( $('#page').width() - 304 ) );
			
                                /*g.element.width(g.size*set.Step);*/
                                var width = 0;
                                g.item.each(function() {
                                  width += $(this).outerWidth();
                                });
                                g.element.width(width);
				
				
				var _html = '';
				_html += '<div class="g-nav">';
				if(set.Paging){
					_html += '<ul>';
					
					t= 0;
					while(t < g.size){
						_html += '<li><a href="#" data-callback="'+t+'">'+(t+1)+'</a></li>';
						t++
					}
					
					_html += '</ul>';
				}
				_html += '<a href="#" data-callback="-1" class="g-step" rel="prev">'+lang.prev+'</a>';
				_html += '<a href="#" data-callback="1" class="g-step" rel="next">'+lang.next+'</a>';
				_html += '</div>';
				
				
				g.root.after(_html);
				g.nav = {
					root	:	$('.g-nav'),
					item	:	$('.g-nav').find('li'),
					step	:	$('.g-step'),
					prev	:	$('.g-step[rel=prev]'),
					next	:	$('.g-step[rel=next]')
				}
						
				navGallery();			
			}
			
			function navGallery(){
				g.nav.item.eq(0).addClass('act');
				
				g.nav.root.find('a').unbind().bind('click',function(e){
					e.preventDefault();
				
					step = $(this).is('.g-step') ? parseInt(g.root.attr('data-step')) : 0;
					g.callback = parseInt($(this).attr('data-callback')) + step;
				
					moveGallery(g.callback);
				});

			}	
					
			function moveGallery(t){
				if(g.onhold){
					g.nav.item.removeClass('act');
					
					if(t >= g.size)
						t = 0
						
					if(t < 0)
						t = g.size-1
						
					g.nav.item.eq(t).addClass('act');
					g.root.attr('data-step',t);
					
					g.onhold = false;
                                        var left = 0;
                                        g.item.each(function(index) {
                                          if (index < t) {
                                            left += $(this).outerWidth();
                                          }
                                        });
                                        /*g.element.animate({left: -1*t*set.Step+'px'},function(){*/
					g.element.animate({left: -1*left+'px'},function(){
					
						g.onhold = true;
					});
				}
			}
				
			init();
			
		}); // each
	}, // gallery
	gallery2:  function(set) {
		set = jQuery.extend({
                        /*Step	: 650,*/
			Paging	: true
		}, set);	
		
		return this.each(function(i){	
		
			var g = {
				root	:	$(this),
				element	:	$(this).find('ul'),
				item	:	$(this).find('li'),
				size	:	$(this).find('li').size(),
				onhold	:	true,
				callback:	''
			}
	
				$(window).resize(function(){
					g.root.width( ( $('#page').width() - 304 ) );
				});			
			
			function init(){		
				g.item.eq(0).show();
				if(g.size > 1)
					buildGallery();
			}
			
			function buildGallery(){
				
				g.root.attr('data-step',0).width( ( $('#page').width() - 304 ) );
                                g.item.filter(':gt(0)').css('display', 'none');
			
                                /*g.element.width(g.size*set.Step);*/
				
				
				var _html = '';
				_html += '<div class="g-nav">';
				if(set.Paging){
					_html += '<ul>';
					
					t= 0;
					while(t < g.size){
						_html += '<li><a href="#" data-callback="'+t+'">'+(t+1)+'</a></li>';
						t++
					}
					
					_html += '</ul>';
				}
				_html += '<a href="#" data-callback="-1" class="g-step" rel="prev">'+lang.prev+'</a>';
				_html += '<a href="#" data-callback="1" class="g-step" rel="next">'+lang.next+'</a>';
				_html += '</div>';
				
				
				g.root.after(_html);
				g.nav = {
					root	:	$('.g-nav'),
					item	:	$('.g-nav').find('li'),
					step	:	$('.g-step'),
					prev	:	$('.g-step[rel=prev]'),
					next	:	$('.g-step[rel=next]')
				}
						
				navGallery();			
			}
			
			function navGallery(){
				g.nav.item.eq(0).addClass('act');
				
				g.nav.root.find('a').unbind().bind('click',function(e){
					e.preventDefault();
				
					step = $(this).is('.g-step') ? parseInt(g.root.attr('data-step')) : 0;
					g.callback = parseInt($(this).attr('data-callback')) + step;
				
					moveGallery(g.callback);
				});

			}	
					
			function moveGallery(t){
				if(g.onhold){
					g.nav.item.removeClass('act');
					
					if(t >= g.size)
						t = 0
						
					if(t < 0)
						t = g.size-1
					
                                        g.nav.item.eq(t).addClass('act');
                                        var prev = g.root.attr('data-step');
                                        g.root.attr('data-step',t);

					g.onhold = false;
                                        g.item.eq(prev).fadeOut(400);
                                        g.item.eq(t).fadeIn(400, function(){
						g.onhold = true;
					});
				}
			}
				
			init();
			
		}); // each
	} // gallery2
});	

