﻿$(function() {
	/* Admin section */
	if ($('#administrator').text()) {
		$('#container').css('margin-top', '24px');
	}

	$('#administrator img').click(function() {
		if($('#administrator ul').is(':hidden')) {
			$('#administrator ul').slideDown('slow');
		} else {
			$('#administrator ul').slideUp();
		}
	});
	
	/* Focus forms */
	$('input, textarea').focus(function() {
		$(this).addClass('form-focus');
	}).focusout(function() {
		$(this).removeClass('form-focus');
	});
	
	/* Language text */
	$('#nav-lang li').hover(function() {
		$('#nav-version').text($(this).text());
	}, function() {
		$('#nav-version').text('');
	});
	
	/* External links */
	$('a[href^=http]').click(function() {
		window.open($(this).attr('href'));

		return false;
	});
	
	/* Acording menu */
	$(window).load(function() {
		var search = $('#main-content h1').text();

		if (search != '') {
			$('.sidebar-submenu li a').filter(function() {
				return $(this).text().substr(2) == search;
			}).css('font-weight', 'bold').parent().parent().show();
		}
	});
	
	$('#sidebar-menu ul').hide();

	$('#sidebar-menu li a').click(function() {
		if (($(this).next().is('ul')) && ($(this).next().is(':visible'))) {
			return false;
		}

		if (($(this).next().is('ul')) && (!$(this).next().is(':visible'))) {
			$('#sidebar-menu ul:visible').slideUp('normal');
			$(this).next().slideDown('normal');

			return false;
		}
	});
	
	$('.sidebar-submenu li:last-child').css('border', 'none !important');
	$('.sidebar-submenu li:last-child').children().css('border', 'none !important');

	/* Slider header */
	setInterval(function() {
		$($('#show-slides li:first').clone()).appendTo('#show-slides');

		$('#show-slides li:first').animate({
			width: 0
		}, 'slow', function() {
			$(this).remove();
		});
	}, 5000);
	
	var element = null;

	/* Photo zooming */
	$('a img').click(function() {
		if ($(this).parents('#main-content').length > 0 || $(this).parents('#content-news').length > 0) {
			element = new Image();

			extension = /[^.]+$/.exec($(this).parent('a').attr('href'));

			if(extension == 'pdf') {
				return true;
			}

			element.src = $(this).parent('a').attr('href');

			showModal();

			return false;
		}
	});
	
	/* Photo zooming */
	$('a.zoom').click(function() {
		element = new Image();
		element.src = $(this).attr('href');

		showModal();

		return false;
	});
	
	/* Modal window */
	function showModal() {
		if (element) {
			if ($('#content-opacity').length > 0) {
				// ...
			} else {
				$('<div id="content-opacity" title="Zamknij"></div>').css({
					'width': $(document).width(),
					'height': $(document).height()
				}).appendTo('body').click(function() {
					$(this).remove();
					$('#content-modal').remove();
					element = null;
				});
			}

			// Create modal box
			$('<div id="content-modal"></div>').appendTo('body').css({
				'top': ($(window).height() - 450) / 2,
				'left': ($(window).width() - 500) / 2
			});

			element.onload = function() {
				$('#content-modal').animate({
					'width': element.width,
					'height': element.height,
					'top': ($(window).height() - element.height) / 2, // + $(window).scrollTop(),
					'left': ($(window).width() - element.width) / 2
				});

				if($('#offer-photo a[href="'+ element.src.slice(-19) +'"]').prev().length > 0) {
					$('<img src="./img/icon-prev.png" class="prev" />').appendTo('#content-modal');
				}

				$(element).appendTo('#content-modal').hide().fadeIn(1000);

				if($('#offer-photo a[href="'+ element.src.slice(-19) +'"]').next().length > 0) {
					$('<img src="./img/icon-next.png" class="next" />').appendTo('#content-modal');
				}
			}
			
			// Scroll position of modal box when window scrolling
			/*
			$(window).scroll(function() {
				$('#content-modal').css({
					'top': ($(window).height()-$('#content-modal').height())/2 + $(window).scrollTop(),
					'left': ($(window).width()-$('#content-modal').width())/2 + $(window).scrollLeft()
				});
			});
			*/

			// Resizing opacity background and position of modal box when window resizing
			$(window).resize(function() {
				$('#content-opacity').css({
					'width': $(document).width(),
					// 'height': $(document).height()
				});

				$('#content-modal').css({
					'top': ($(window).height() - $('#content-modal').height()) / 2,
					'left': ($(window).width() - $('#content-modal').width()) / 2
				});
			});
			
			return false;
		}
	}

	/* Prev and Next Modal window */
	$('#content-modal img').live('click', function() {
		current = $('#content-modal img::not([class])').attr('src');

		if ($(this).hasClass('prev')) {
			$('#content-modal').remove();

			if ($('#offer-photo a[href="' + current + '"]').prev().length > 0) {
				element = new Image();
				element.src = $('#offer-photo a[href="' + current + '"]').prev().attr('href');

				showModal();
			}
		} else if ($(this).hasClass('next')) {
			$('#content-modal').remove();

			if ($('#offer-photo a[href="' + current + '"]').next().length > 0) {
				element = new Image();
				element.src = $('#offer-photo a[href="' + current + '"]').next().attr('href');

				showModal();
			}
		}
	});
	
	/* Srcoll to top */
	$('a[href="#top"]').click(function() {
		$('html, body').animate({
			scrollTop: 0
		}, 'slow');

		return false;
	});
	
	/* Tooltip display */
	$('img, input, textarea, label, span, a, div, acronym').hover(function() {
		if($(this).attr('title')) {
			$(this).after('<span class="tip">' + $(this).attr('title') + '</span>');
			$(this).removeAttr('title');
		}

		tip = $(this).next('.tip');
		tip.show();
	}, function() {
		tip.hide();
	}).mousemove(function(e) {
		var mousex = e.pageX + 20;
		var mousey = e.pageY + 20;
		var tipWidth = tip.width();
		var tipHeight = tip.height();

		var tipVisX = $(window).width() - (mousex + tipWidth);
		var tipVisY = $(window).height() - (mousey + tipHeight);

		if (tipVisX < 20) {
			mousex = e.pageX - tipWidth - 20;
		}

		if (tipVisY < 20) {
			mousey = e.pageY - tipHeight - 20;
		}

		tip.css({
			'top': mousey,
			'left': mousex
		});
	});
	
	/* Rotation logos */
	$('#main-center a:first').css('display', 'block');

	setInterval(function() {
		var activea = $('#main-center a.active');

		if($('#main-center .active').next().length > 0) {
			nexta = $('#main-center .active').next();
		} else {
			nexta = $('#main-center a:first');
		}

		activea.fadeOut(function() {
			activea.removeClass('active');
			nexta.fadeIn().addClass('active');
		});
	}, 3500);
	
	/* Inputs type file */
	num = 0;
	
	$('input[type="file"]').each(function() {
		num++;
		
		if($(this).parent('fieldset')) {
			$(this).parent('fieldset').css({
				'width': '560px',
				'position': 'relative'
			});
		}
		
		if($(this).prev('label')) {
			$(this).prev('label').css({
				'width': '550px'
			});
		}
		
		$(this).css({
			'position': 'relative',
			'top': '0px',
			'width': '550px',
			'height': '28px',
			'opacity': '0',
			'z-index': '2',
			'cursor': 'pointer',
			'border': '1px solid red'
		}).after('<input type="text" disabled="disabled" id="file_v'+ num +'" /> <a href="" class="button" id="file_b'+ num +'">Wybierz plik</a>');
		
		$('#file_b'+ num).css({
			'position': 'absolute',
			'top': '30px',
			'left': '0px',
			'z-index': '1',
			'cursor': 'pointer',
			'margin-left': '0px'
		}).click(function() {
			return false;
		});
		
		$('#file_v'+ num).css({
			'position': 'absolute',
			'width': '425px',
			'top': '30px',
			'right': '0px',
			'z-index': '1'
		});
		
		$(this).change(function() {
			$('#file_v'+ num).val(/[^\\]+$/.exec($(this).val()));
			$('#file_b'+ num).text('Zmień plik');
		});
	});
	
	/* NIP number */
	$('input[id$=Nip]').keypress(function(e) { 
		if(e.which != 8 && e.which != 0 && e.which != 45 && (e.which < 48 || e.which > 57)) {
			return false;
		}  
    
        var length = $(this).val().length;
        var value = $(this).val();
        
        if (length == 3) {
            $(this).val(value +'-');
        } else  if (length == 7) {
            $(this).val(value +'-');
        } else  if (length == 10) {
            $(this).val(value +'-');
        }
    });
	
	/* Phone number */
	$('input[id$=Phone]').keypress(function(e) { 
		if(e.which < 48 || e.which > 57) {
			return false;
		}
    });
	
	// todo: # links
	$('a[href=#]').click(function() {
		alert('W przygotowaniu...');

		return false;
	});
	
	$('.button').click(function() {
		//showModal();
	})
});
