$(document).ready(function() {
	// IE6 block
	if($.browser.msie && parseInt($.browser.version) <= 6) {
		$(function(){
			$('<div>').addClass('body-enabled').css({
				'opacity': '0.75',
				'width': $(document).width(),
				'height': $(document).height()
			}).appendTo('body');
						
			$('<div><span><b>Uwaga!</b> Twoja przeglądarka jest w bardzo starej wersji.<br />Zaktualizuj ją natychmiast!</span><br /><br />Korzystasz z bardzo starej wersji programu Internet Explorera 6.0.<br />Twój program ukazał się w 2001r. zatem mineło wiele czasu od jego wydania. Twoja przeglądarka nie jest już wspierana przez producenta i stanowi potencjalne zagrożenie.<br /><br />Jeśli jesteś w pracy i widzisz tę wiadomość, skontaktuj się ze swoim administratorem i poproś o aktualizację przeglądarki.<br /><br /><a href="http://ie6.pl">Chcę dowiedzieć sie wiecej...</a></div>').addClass('enabled-message').appendTo('body');
		});
	}
								
	// Hide all links
	$('.layout-links a').css('visibility', 'hidden');
				
	// Background animation
	$('#layout').hover(function() {
		$(this).css('backgroundPosition', '0px -575px');
	}, function() {
		if ($('#opacity').css('opacity') != 0.9) {
			$(this).css('backgroundPosition', '0px 0px');
		}					
	});
				
	// Links showing
	$('.layout-links').hover(function() {
		$(this).children('a').css('visibility', 'visible');
		$(this).children('a').show();
	}, function() {
		$(this).children('a').hide();
	});
				
	// Links vibration
	$('#layout-gallery a, #layout-contact a, #layout-ads a, #layout-services a, #layout-about a').vibrate();				
			
	// Modal windows	
	$('.modal').click(function(e) {
		// Remove opened
		$('#layout-main').hide();
		$('.main-windows').hide();
					
		e.preventDefault();
					
		$($(this).attr('href')).show();
					
		$('#opacity').show().css({
			'width': $(window).width(),
			'height': $(document).height(),
			'opacity': 0.9});
	
		$('#layout-main').fadeIn(1000);	
	});		
	
	$('#opacity, #main-close').click(function () {
		$('#opacity').hide().css('opacity', 1.0);

		if ($(this).attr('id') == 'opacity') {
			$('#layout').css('backgroundPosition', '0px 0px');
		} else {						
			$('#layout').css('backgroundPosition', '0px -575px');
		}
					
		$('#layout-main').hide();
		$('.main-windows').hide();
	});				
				
	$('.cat-box:not(.cat-show)').parent().hide(); // Move to start	
							
	var z = 0;
	var inAnimation = false;
				
	// First show photos
	$('#gallery-1, #main-services a').click(function() {
		//z = 0;
					
		// Remove opened
		$('.photo-blocks').hide();
		$('div').removeClass('cat-show');
					
		$('#photo-cat4').show();				
		$('#photo-cat4 div:first').show().attr('class', 'cat-box cat-show');
					
		$('.cat-show a').each(function() {
			z++;
			
            $(this).css('z-index', z);
		});
	});
				
	// First show graphics
	$('#gallery-2, #main-ads a').click(function() {
		//z = 0;
					
		// Remove opened
		$('.cat-show').hide();					
		$('.graphic-blocks').hide();				
					
		$('#graphic-cat8').show();
		$('#graphic-cat8 .graphic-submenu').show();
		$('#graphic-cat8 div:first').show().attr('class', 'cat-box cat-show');
					
		// Hide other submenu element
		$('.cat-box:not(.cat-show)').hide();
	});				
				
	// Photo category menu				
	$('.photo-menu li, .graphic-menu li').click(function() {
		z = 0;
					
		// Remove opened					
		$('.photo-blocks, .graphic-blocks').hide();
				
		$('#'+ $(this).attr('class')).show();
		$('#'+ $(this).attr('class') +' .graphic-submenu').show();
		$('div').removeClass('cat-show');
		$('#'+ $(this).attr('class') +' div:first').show().attr('class', 'cat-box cat-show');
					
		// Hide other submenu element
		$('.cat-box:not(.cat-show)').hide();
					
		$('.cat-show a').each(function() {
			z++;
			
          	$(this).css('z-index', z);
		});
	});	

	// Photo subcategory menu
	$('.photo-submenu li, .graphic-submenu li').click(function() {
		z = 0;
					
		// Remove opened
		$('.cat-show').hide();
		$('#'+ $(this).attr('class')).show();
					
		$('div').removeClass('cat-show');
		$('#'+ $(this).attr('class')).attr('class', 'cat-box cat-show');
					
		$('.cat-show a').each(function() {
			z++;
        
        	$(this).css('z-index', z);
		});
	});
					
	// Animation
	function swapFirstLast(isFirst) {
		if(inAnimation) {
			return false;
		} else {
			inAnimation = true;
		}

		var processZindex, direction, newZindex, inDeCrease;
    
		if(isFirst) {
			processZindex = z; 
			direction = '-'; 
			newZindex = 1; 
			inDeCrease = 1; 
		} else { 
			processZindex = 1; 
			direction = ''; 
			newZindex = z; 
			inDeCrease = -1;
		}
    
		$('.cat-show a').each(function() { //process each image
			if($(this).css('z-index') == processZindex) { //if its the image we need to process
        		$(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
        			$(this).css('z-index', newZindex) //set new z-index
						.animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
						inAnimation = false; //reset the flag
					});
				});
			} else {
				$(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
					$(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
				});
			}
		});
    
		return false;
  	}

	$('.cat-prev').click(function() {
		return swapFirstLast(false);
	});
                
	$('.cat-next').click(function() {
		return swapFirstLast(true);
	});
				
	$('#tlo, #zoom').hide();
	
	var oldUrl = '';
				
	$('.cat-box a, #main-contact div a, #main-services div a, #main-ads div a, #main-about div a').click(function() {
		oldUrl = $(this).attr('href');
		$(this).attr('alt', oldUrl).removeAttr('href');
					
		$('#tlo').appendTo('body').css('opacity', 0.85).show();
		$('#zoom').css({
			'background': 'url('+ oldUrl.replace('zoom/', 'photos/big') +') center center no-repeat'
		}).show();
	});
				
	$('#zoom').click(function() {
		$('#tlo, #zoom').hide();
		$('a[alt="'+ oldUrl +'"]').attr('href', oldUrl);
					
		isInt = oldUrl.substring(5);
					
		if (isInt.match('^[a-z]{0,1}[1-9]{1,}')) {
			return swapFirstLast(true);
		}
	});

	// Form elmenets		
	$('#WorkBorder, #WorkAngle').attr('checked', 'checked');
        
	function isAuto() {
		if ($('#WorkAuto').is(':checked')) {
			$('[for=WorkThumb]').hide();
			$('[for=WorkBorder]').show();
			$('[for=WorkAngle]').show();            
		} else {                        
			$('[for=WorkThumb]').show();
			$('[for=WorkBorder], [for=WorkAngle]').hide();
 		}
	}
        
	isAuto();
        
	$('#WorkAuto').click(function() {
		isAuto();
	});
        
	$('[type=checkbox]').click(function() {
		if ($(this).is(':checked')) {
			$(this).next('b').text('Tak');
		} else {
			$(this).next('b').text('Nie');
		}
	});
});
