(function($){
    /**
     * jQuery delayed event execution.
     */
    $.fn.delay = function(options) {
        var timer;
        var delayImpl = function(eventObj) {
            if (timer != null) {
                clearTimeout(timer);
            }
            var newFn = function() {
                options.fn(eventObj);
            };
            timer = setTimeout(newFn, options.delay);
        }
       
        return this.each(function() {
            var obj = $(this);
            obj.bind(options.event, function(eventObj) {
                 delayImpl(eventObj);  
            });
        });
    };
})(jQuery);

function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;} function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageWidth();} function posBottom() {return posTop()+pageHeight();}

var docW = pageWidth();
var docH = pageHeight(); //dodac warunek sprawdzajacy czy H jest wieksze od wysokosci strony

function checkLength(o,n,min,max) 
{
	if ( o.length > max || o.length < min ) {
		return false;
	} else {
		return true;
	}
}

function checkRegexp(o,regexp,n) 
{
	if ( !( regexp.test( o ) ) ) {
		return false;
	} else {
		return true;
	}

}

$(document).ready(function () 
{
	/*$("a[rel^='prettyPhoto']").fancybox({
		'centerOnScroll' : true,
		'onStart' : function(){
			$('#fancybox-wrap').animate({top : '800'}, 200);
		}
	});
	
	$("a[rel^='fancybox']").fancybox();
	*/
	
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_rounded',
		default_width: 750,
		allow_resize: false,
		default_height: 460,
		allowresize: false,
		wmode: 'opaque',
		hideflash: true,
		callback: function() {
			//alert('help frame');
		}
	});
	
	
	
	// newsletter
	$('#newsletter_form input#mail').live("click", function() {
		if ($(this).val() == 'adres@email')
		{
			$(this).val('');
		}
	});	
	$('#newsletter_form input#mail').live("blur", function() {
		if ($(this).val() == '')
		{
			$(this).val('adres@email');
		}
	});
	$('#newsletter_form .buttons input').live("click", function() {
		var typ = $(this).attr('id') == "subscribe" ? 1 : 2;
		var email = $(this).parents('#newsletter_form').find('#mail').val();
		var bValid = (email != 'twoj.adres@email');
		bValid = bValid && checkLength(email,"email",6,80);
		// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
		bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Niepoprawny adres e-mail.");

		if (bValid) 
		{
			$.ajax(
			{
				type: "POST",
				url: "/save_newsletter.php",
				data: 'email='+email+'&type='+typ,
				dataType: 'json',
				success: function(msg)
				{
					if (msg.status == 'ok') {
						$.prettyPhoto.close();
					}
					alert(msg.msg);
				},
				error: function(msg)
				{
					alert('Wystąpił nieoczekiwany błąd, prosimy spróbować ponownie');
				}
			});	
		}
		else
		{
			alert('Nieprawidłowy adres e-mail');
		}
	});
	
	// pomoc
	$('#help_form input#send').live("click", function() {
		var name = $(this).parents('#help_form').find('#name').val();
		var email = $(this).parents('#help_form').find('#mail').val();
		var content = $(this).parents('#help_form').find('#question').val();
		
		var bValid = (email != 'twoj.adres@email');
		bValid = bValid && checkLength(email,"email",6,80);
		// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
		bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Niepoprawny adres e-mail.");
		bValid = bValid && (name.length > 2);
		bValid = bValid && (content.length > 10);
		
		if (bValid) 
		{
			$.ajax(
			{
				type: "POST",
				url: "/send_mail.php",
				data: 'name='+name+'&email='+email+'&content='+content,
				dataType: 'json',
				success: function(msg)
				{
					if (msg.status == 'ok') {
						$.prettyPhoto.close();
					}
					alert(msg.msg);
				},
				error: function(msg)
				{
					alert('Wystąpił nieoczekiwany błąd, prosimy spróbować ponownie');
				}
			});	
		}
		else
		{
			alert('Nie wypełniono wszystkich wymaganych pól lub wprowadzono nieprawidłowe dane');
		}
	});
	
	$('#close3dview').click(function(){ $('#view3dwindowHolder').html('');$('#transparent').css('display','none');$('#view3dwindow').css('display','none'); return false; });
	$('#closeDetails').click(function(){ $('#detailsHolder').html('');$('#transparent').css('display','none');$('#details').css('display','none'); return false; });
		
});
