$(document).ready(function(){
    $('#animals').flash(
        { src: 'media/dom/gfx/animals.swf',
          width: 980,
		  height:403,
		  wmode: "transparent" },
        { version: 8 }
    );

    $(function() {
        $('#mini_gallery a').lightBox();
    });
	
	$(function() {
        $('#text a.lightbox').lightBox();
    });

	//$('#text a.lightbox').click();
	
    gallery_anim();
    
	if(navigator.userAgent.indexOf("IE")==-1){	 
		HTMLElement.prototype.contains = function(node)
		{
			if (node == null)
			{
				return false;
			}
			else if (node == this)
			{
				return true;
			}		
			return this.contains(node.parentNode);
		}		
	}
    
	$(".super_button").mouseout(function(e){
			//alert('this='+this+'; relatedTarget='+e.relatedTarget+'; contains='+this.contains(e.relatedTarget));
			//alert('this='+this+'; toElement='+e.toElement+'; contains='+this.contains(e.toElement));
		if (!this.contains(e.relatedTarget || e.toElement)) {
			$(this).children(".submenu").fadeOut(150);
			$(this).css("background-image","none");
		}
	});	
    
	$(".super_button").mouseover(function(e){
		if($(this).children(".submenu").length>0 && $(this).children(".submenu").css("display")!="block"){
			e.preventDefault();
			this.blur();
			$(this).children(".submenu").slideDown(150);
			$(this).css("background","url('media/dom/gfx/menu_hover.jpg') repeat-x");
		}
	});
});

function jezyk(short)
{
	$.post($("base").attr("href")+"welcome/session/LANGUAGE/"+short+'.html', {
	}, function(data){
			window.location.href = '';
	});
}

function font_size(size)
{
	$.post($("base").attr("href")+"welcome/session/FONT-SIZE/"+size+'.html', {
	}, function(data){
			window.location.href = '';
	});
}

function search()
{
	var str = $('#word').val();
	$.post($("base").attr("href")+'welcome/search_products/1.html', {
		str: str
	}, function(data){
		$("#container").fadeOut(500);
		setTimeout(function() {
			$("#container").html(data);
			$("#container").fadeIn(500);
			$('#products a').lightBox();
		},500);
	});
}

function site(nazwa){
	 if( $('#menu_prod_pl').length > 0 ) $('#menu_prod_pl').fadeOut();
	 if( $('#menu_prod_en').length > 0 ) $('#menu_prod_en').fadeOut();
	 if( $('#menu_prod_ru').length > 0 ) $('#menu_prod_ru').fadeOut();
	 if( $('#menu_prod_de').length > 0 ) $('#menu_prod_de').fadeOut();

	var url = false;
	var bool_search = false;
	if(nazwa == 'rodents' || nazwa == 'birds' || nazwa == 'dogs'){
		switch(nazwa){
			case 'rodents': kat = 1; break;
			case 'birds': kat = 2; break;
			case 'dogs': kat = 3; break;
			default: kat = 1; break;
		}
		url = $("base").attr("href")+"welcome/products/1/"+kat+'.html';
		bool_search = true;
		
	}
	else if(nazwa == 'company' || nazwa == 'contact'){
		url = $("base").attr("href")+"welcome/"+nazwa+".html";
	}
	else if(nazwa == 'new_products'){
		url = $("base").attr("href")+"welcome/"+nazwa+".html";
		bool_search = true;
	}
	
	if(url){
		$.post(url, {
		}, function(data){
			$("#container").fadeOut(500);
			if(bool_search) $('#search').fadeIn(500);
			else $('#search').fadeOut(500);
			setTimeout(function() {
				$("#container").html(data);
				$("#container").fadeIn(500);
				$('#products a').lightBox();
				$('#text a.lightbox').lightBox();
			},500);

			$.post($("base").attr("href")+"welcome/fotografie/"+nazwa+'.html', {
			}, function(data2){
				$("#mini_gallery").fadeOut(500);
				setTimeout(function() {
					$("#mini_gallery").html(data2);
					$("#mini_gallery").fadeIn(500);
					$('#mini_gallery a').lightBox();
					setTimeout(function() {gallery_anim();},200);
				},500);
			});
		});
	}
	else{
		alert('Błąd obsługi strony. Proszę skontaktować się z administratorem.');
	}

}

/*

function site_old(nazwa){
	 if( $('#menu_prod_pl').length > 0 ) $('#menu_prod_pl').fadeOut();
	 if( $('#menu_prod_en').length > 0 ) $('#menu_prod_en').fadeOut();
	 if( $('#menu_prod_ru').length > 0 ) $('#menu_prod_ru').fadeOut();
	 if( $('#menu_prod_de').length > 0 ) $('#menu_prod_de').fadeOut();

	if(nazwa == 'rodents' || nazwa == 'birds' || nazwa == 'dogs'){
		switch(nazwa){
			case 'rodents': kat = 1; break;
			case 'birds': kat = 2; break;
			case 'dogs': kat = 3; break;
			default: kat = 1; break;
		}
		$.post($("base").attr("href")+"welcome/products/1/"+kat+'.html', {
		}, function(data){
			$("#container").fadeOut(500);
			$('#search').fadeIn(500);
			setTimeout(function() {
				$("#container").html(data);
				$("#container").fadeIn(500);
				$('#products a').lightBox();
			},500);

			$.post($("base").attr("href")+"welcome/fotografie/"+nazwa+'.html', {
			}, function(data2){
				$("#mini_gallery").fadeOut(500);
				setTimeout(function() {
					$("#mini_gallery").html(data2);
					$("#mini_gallery").fadeIn(500);
					$('#mini_gallery a').lightBox();
					setTimeout(function() {gallery_anim();},200);
				},500);
			});
		});
	}
	else if(nazwa == 'company' || nazwa == 'contact' || nazwa == 'new_products'){
		$.post($("base").attr("href")+"welcome/"+nazwa+".html", {
		}, function(data){
			$("#container").fadeOut(500);
			$('#search').fadeOut(500);
			setTimeout(function() {
				$("#container").html(data);
				$("#container").fadeIn(500);
				if(nazwa == 'new_products') $('#products a').lightBox();
			},500);

			$.post($("base").attr("href")+"welcome/fotografie/"+nazwa+'.html', {
			}, function(data2){
				$("#mini_gallery").fadeOut(500);
				setTimeout(function() {
					$("#mini_gallery").html(data2);
					$("#mini_gallery").fadeIn(500);
					$('#mini_gallery a').lightBox();
					setTimeout(function() {gallery_anim();},200);
				},500);
			});
		});
	}
	else if(nazwa == 'products'){
		$('#search').fadeIn(500);
		 if( $('#menu_prod_pl').length > 0 ) $('#menu_prod_pl').fadeIn();
		 if( $('#menu_prod_en').length > 0 ) $('#menu_prod_en').fadeIn();
		 if( $('#menu_prod_ru').length > 0 ) $('#menu_prod_ru').fadeIn();
		 if( $('#menu_prod_de').length > 0 ) $('#menu_prod_de').fadeIn();
	}

}
*/

function page_nr(nr){ //strona towarow
	//var kategoria = $('#kategoria').val();
	//var nowosci = $('#site_name').val()=='new_products' ? 1 : 0;

	var str = '';
	var url = '';
	if( $('#site_name').val()=='new_products' ) url = $("base").attr("href")+'welcome/new_products/'+nr+'.html';
	else if( $('#kategoria').val()=='search' ) {
		url = $("base").attr("href")+'welcome/search_products/'+nr+'.html';
		str = $('#szukaj').val();
	}
	else url = $("base").attr("href")+'welcome/products/'+nr+'/'+$('#kategoria').val()+'.html';
	
	
	$.post(url, {
		str: str
	}, function(data){
		$("#container").fadeOut(500);
		setTimeout(function() {
			$("#container").html(data);
			$("#container").fadeIn(500);
			$('#products a').lightBox();
		},500);
	});
}

function gallery_anim()
{
	var total_gals = $(".mini_gal").length;
	var gal_numer = 0;

	if(total_gals>1){
		setInterval(function() {

		gal_next = (gal_numer < total_gals-1) ? gal_numer+1 : 1;

		$("#mini_gal_"+gal_numer).fadeOut(500);
		setTimeout(function() {
			$("#mini_gal_"+gal_next).fadeIn(500);
		},500);

		//$("#mini_gal_"+gal_next+" a").lightBox();

		gal_numer++;

	  }, 8000);
  }


}

/*
 * MAPKA
 */
function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;

}

function setOverlaySize() {
	var pageSize = getPageSize();
	$("#overlay").css("width", pageSize[0]+"px");
	$("#overlay").css("height", pageSize[1]+"px");
	$("#overlayBox").css({"left" : (pageSize[0]-$("#overlayBox").width())/2+"px", "top" : 100});
}

function setOverlayCellPos() {
	var cell_padding = $("#overlayCell").css("padding-left").replace(/px/, "");
	var cell_width = $("#overlayCell").width();
	var z = parseInt(cell_width/2) + parseInt(cell_padding);
	$("#overlayCell").css("left", "-"+z+"px");
}

function showMap(type) {
	
	$("body").append('<div id="overlay"></div><div id="overlayBox"><div id="overlayRow"><div id="overlayCell"></div></div></div>');
	$("embed").hide();
	setOverlaySize();
	$("#overlay").fadeIn(300, function() {
		if(type==1){
			$("#overlayCell").append('<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://mapy.google.pl/maps/ms?ie=UTF8&amp;hl=pl&amp;msa=0&amp;msid=112051267391135291207.00047383f968fa98b04f4&amp;ll=49.842521,19.981813&amp;spn=0.026569,0.054932&amp;z=14&amp;output=embed"></iframe>');
		}
		else if(type==2){
			$('#overlayCell').flash(
	            { src: 'media/dom/gfx/mapa.swf',
	              width: 640,
	    		  height:480,
	    		  wmode: "transparent" },
	            { version: 8 }
		    );
		    
		}
		$("#overlayCell").append('<div style="text-align: center;"><a href="javascript:hideMap();" title="Kliknij, aby ukryć mapkę" style="color: #fff;">ukryj mapkę</a></div>');
		setOverlayCellPos();
		$("#overlayCell").css("top", "100px").fadeIn(300);
	});
}

function hideMap() {
	$("#overlayCell").fadeOut(300, function() {
		$("#overlay").fadeOut(300, function() {
			$("#overlay").empty().remove();
			$("#overlayBox").empty().remove();
			$("embed").show();
		});
	});
}

function printLightbox(src){
	var WindowObject = window.open(src);
	WindowObject.print();
}

function contactMessage(){
	
	$('#form_contact').fadeOut(300);
	var email = $('input[name=email]').val();
	var reg_email = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	var valid_email=reg_email.test(email);
	
	if(valid_email){
		$('#email_error').css('color', '#024A5F');
		var name = $('input[name=name]').val();
		var surname = $('input[name=surname]').val();
		var phone = $('input[name=phone]').val();
		var message = $('textarea[name=message]').val();
		
		$.post($("base").attr("href")+"welcome/email_contact.html", {
			name: name,
			surname: surname,
			phone: phone,
			email: email,
			message: message
		}, function(data){
			if(data){
				$('#send_success').fadeIn(300);
			}
			else{
				$('#send_error').fadeIn(300);
			}
		});
	}
	else{
		$('#form_contact').fadeIn(300);
		$('#email_error').css('color', '#ff0000');
	}
}


