$(document).ready(function() {	
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
		//get galery ID and element ID
		var element_data = $(this).attr('id');
		element_mas = element_data.split("::");
		this_pi = element_mas[0];
		this_pie = element_mas[1];
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(100);	
		$('#mask').fadeTo("fast",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//grazina kiek paskrolinta
		var bs_top = $(window).scrollTop();
		var bs_left = $(window).scrollLeft();
		
		//parodo pelytes pozicija
		//leftVal=e.pageX;  
		//topVal=e.pageY;  

		//alert ('winH: ' + winH + ' winW: ' + winW + ' / BodyScrollHeight: ' + www + ' / leftVal: ' + leftVal + ' topVal: ' +topVal + ' BS: ' + bs);
		//Set the popup window to center
		$(id).css('top',  winH/2+bs_top-10-$(id).height()/2);
		$(id).css('left', winW/2+bs_left-$(id).width()/2);
	
		
		//transition effect
		$(id).fadeIn(500); 
		ShowWill(this_pi,this_pie);
	
	});
	
	$('a[name=configurator]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var conf_link = $(this).attr('href');
		//alert (conf_link);
		var id = "#configurator";
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(100);	
		$('#mask').fadeTo("fast",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//grazina kiek paskrolinta
		var bs_top = $(window).scrollTop();
		var bs_left = $(window).scrollLeft();
		
		//parodo pelytes pozicija
		//leftVal=e.pageX;  
		//topVal=e.pageY;  

		//alert ('winH: ' + winH + ' winW: ' + winW + ' / BodyScrollHeight: ' + www + ' / leftVal: ' + leftVal + ' topVal: ' +topVal + ' BS: ' + bs);
		//Set the popup window to center
		$(id).css('top',  winH/2+bs_top-10-$(id).height()/2);
		$(id).css('left', winW/2+bs_left-$(id).width()/2);
	
		
		//transition effect
		$(id).fadeIn(500); 
		$(id).load(conf_link);
		$("#configurator_content").load(conf_link, function(response, status, xhr) {
		  if (status == "error") {
		    var msg = "Sorry but there was an error: ";
		    $("#configurator_content").html(msg + xhr.status + " " + xhr.statusText);
		  }
		});

	});

	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	
});

	function ShowWill(pi,pie) {
		//alert("Hello World!" + pi +" " + pie + " ");
		
		if (pi > 0 && pie>0){
			$.ajax({
				type: "POST",
				url: "vit_ratai_el_ajax.php",
				data: "pi=" + pi + "&pie=" + pie + "&",
				cache: false,
				success: function(html){
					var rez = html.replace(/(\r\n|\n|\r)/gm,"");
					if (rez != ''){
						$('#big_img_content').html(rez);
					}else{
						$('#big_img_content').html('Informacijos nėra!');
					}

				}
			});
		}
		
  	}


