/** 
 *  Preloader obrazków w linku
 *  
 *  @date 10.07.2008
 *  @author Daniel Nowacki
 *  @company EPRO IT    
 */

/**
 *  Rozszerzenie metody dodwane tylko na linki
 *  
 *  Jako parametr metody nalezy podac tylko nazwe pliku z hoverem. Plik ma sie znajodwac w tym samym katalogu co plik bazowy   
 */ 
Element.addMethods('A', {
  rollOver: function(objLink, hoverImg)
  {    
    if (objLink) /* link musi istnieć */
    {
      var objImgArray = objLink.select('img'); /* preloadery działają na obrazkach - pobieram */
      
      if (objImgArray[0]) /* jako pierwsze dziecko musi zawierać obrazek */
      {
        var objImg = objImgArray[0];
        
        var actPath = objImg.readAttribute('src') + ""; // zamiana na string
        
        var pattern = /[a-zA-Z_-]+\.[gpj][inp][fg]$/; // wyrazenie - pobiera nazwe pliku mozliwe rozszerzenia jpg, gif, png
        
        var hoverPath = actPath.replace(pattern, "") + hoverImg; // sciezka z obrazkeim do preloadera - obrazek z hoverem musi byc w tym samym katalogu co bazowy
        
        /* buforuję obrazek hovera - wymuszam pobranie z serwera */        
        new Image().src = hoverPath;
                

        /* zapamietuje sciezki */              
        objImg.writeAttribute('rollOver', hoverPath);            
        objImg.writeAttribute('rollOut', objImg.readAttribute('src'));    
              
        // dodaje sluchaczy na linki
        objLink.observe('mouseover', function()
        {                    
          objImg.writeAttribute('src', objImg.readAttribute('rollOver'));          
        });
        
        objLink.observe('mouseout', function()
        {
          objImg.writeAttribute('src', objImg.readAttribute('rollOut')); 
        });
      }            
    }    
    return objLink;
  }
});


/* 
	ładowanie dużych zdjęć 
*/
function eproLoadBigImage(objSender, imgPath, idGallery, noMin, bigImgTarget, bindGalleryAction, bigImagePath,galleryTitle)
{
	$(objSender).up().up().up().select("a").each(function(item, index){	
		item.removeClassName("selected");
	});
	
	
	$(objSender).addClassName("selected");
	
	var bigImageContainer = $('big_image_container');
	
	if (bigImgTarget) bigImageContainer = $(bigImgTarget);
	
	var childs = bigImageContainer.select('img');
	
	var height = 100;
	
	if (childs[0])
	{
		var actImg = childs[0];		
		var height = actImg.getHeight() > 0 ? actImg.getHeight() : 100;
	}
	

	var spacer = new Element("div");
	spacer.setStyle({
		width: "100",
		margin: "auto",
		height: height + "px",
		background: "url('image/card/loader.gif') no-repeat center center"
	});
	
	spacer.update("&nbsp;");
	
	bigImageContainer.update("");
	bigImageContainer.appendChild(spacer);
	
	var img = new Image();
	img.src = imgPath;
	new PeriodicalExecuter(function(pe)
	{
		if (img.complete)
		{
			var tag = new Element("img");
			tag.writeAttribute("src", imgPath);		
			tag.setStyle({ cursor: 'pointer'});	
			bigImageContainer.update("");
			bigImageContainer.appendChild(tag);
			
			if (bindGalleryAction == undefined || bindGalleryAction == true)
			{
				tag.observe('click', function()
				{
					displayAjaxGallery(idGallery, bigImagePath, noMin, galleryTitle);
				});
			}
			
			pe.stop();
		}
		
	}, 0.1);	
	
	return false;
} 

var EproScreenClass = Class.create({
	getDocumentHeight: function()
		{
			$(this).innerHeight = (document.height != undefined) ? document.height : document.body.offsetHeight;
			return $(this).innerHeight;
		},
	
	getWindowWidth: function()
		{
			$(this).windowWidth = document.viewport.getWidth();
			return $(this).windowWidth; 
		},
		
	getWindowHeight: function()
		{
			if (Prototype.Browser.Opera)
			{
				$(this).windowHeight = window.innerHeight;	
			}	
			else
			{
				$(this).windowHeight = document.viewport.getHeight();
			}		
						
			return $(this).windowHeight;
		},
		
	getTopOffset: function()
		{
			$(this).pageOffset = document.viewport.getScrollOffsets();
			return $(this).pageOffset.top;
		},
		
	getLeftOffset: function()
		{
			$(this).pageOffset = document.viewport.getScrollOffsets();
			return $(this).pageOffset.left;
		},
		
	getMaxHeight: function()
		{
			$(this).maxHeight = ($(this).getDocumentHeight() > $(this).getWindowHeight()) ? $(this).getDocumentHeight() : $(this).getWindowHeight();
			return $(this).maxHeight;
		}
});

var EproScreen = new EproScreenClass();