﻿window.onload = LoadPage;
if( PageShowsPhotos() )
    window.onresize = Resize;


var fixingTile = false;
var resizeEventRebind = false;
var timerStarted = false;
var winHeight = 0;
var winWidth = 0;
var resizeTimer = false;


function LoadPage() 
{
	montre();
	if( PageShowsPhotos() )
	    TileImages("load");
}

/* -------------------------------------------------*/
/* [Duplicate Firefox Resize Event]                 */
/* -------------------------------------------------*/
             
function Resize()
{  
    if(resizeEventRebind)
    {
        resizeEventRebind = false;
        return;
    }
        
    if( !timerStarted && !fixingTile)
    {
        timerStarted = true;
        SetWindowSize();
        StartTimer();
    }  
}

function PageShowsPhotos()
{
    var pageUrl = window.location.href;
    
    if ( pageUrl.indexOf("profilephotos.aspx") > -1 || pageUrl.indexOf("photogallery.aspx") > -1 )
        return true;
    else
        return false;     
}

function SetWindowSize()
{
    winHeight=document.body.clientHeight;
    winWidth=document.body.clientWidth;
}

function StartTimer()
{
    resizeTimer = self.setTimeout("CheckWindowSize()", 500);
}

function StopTimer()
{
    clearTimeout(resizeTimer);
    timerStarted =false;  
}

function CheckWindowSize()
{  
    var newWinHeight=document.body.clientHeight;
    var newWinWidth=document.body.clientWidth;
    
    //the window has stopped resizing
    if( newWinHeight==winHeight && newWinWidth==winWidth)
    {
        if( !fixingTile)
        {          
            // turn off the timer
            StopTimer();                      
            
            fixingTile = true;
            TileImages("resize");  
            return;
         }
    } 
    else
    {
        SetWindowSize(); 
        StartTimer();
    }
 }


function TileImages( method)
{            
    fixingTile = true;
    window.onresize = null;
    
    var allDivs = document.getElementsByTagName('div');
    var divTop = -1;
    
    var lastPlaceHolder = null;
    
    for (var i=0;i<allDivs.length;i++)
	{
	    var divClass = allDivs[i].className;
	    
        if( divClass == "photoPlaceHolder" || divClass == "photoPlaceHolderNewLine")
	       lastPlaceHolder =  allDivs[i]; 
	       
	    if( divClass == "photoPlaceHolderNewLine") lastPlaceHolder.className = "photoPlaceHolder";
	    
	    if( divClass == "photoBorder")
	    {
	        var photoTop = allDivs[i].offsetTop;
	        if(divTop == -1)
	            divTop = photoTop;
	            
	         if( divTop !=  photoTop)
	         {
	            lastPlaceHolder.className = "photoPlaceHolderNewLine";
	            photoTop = allDivs[i].offsetTop;
	            divTop = photoTop;
	         }                   
	    } 

	}
	
	resizeEventRebind = true;
	window.onresize = Resize;
	fixingTile = false;
}



/* -------------------------------------------------*/
/* [Other]                                          */
/* -------------------------------------------------*/

function montre(id) 
{
    var d = document.getElementById(id);
	    for (var i = 1; i<=10; i++) {
		    if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	    }
    if (d) {d.style.display='block';}
}

function ConfirmDeletePhoto()
{
  if (confirm("Are you sure you want to permanently delete this photo?")==true)
    return true;
  else
    return false;
}

function ConfirmDeleteNews()
{
  if (confirm("Are you sure you want to permanently delete this news item?")==true)
    return true;
  else
    return false;
}

function keyp(e, buttonControl) 
{
    if (e.type == "keypress" && e.keyCode=="13") 
    {
        var cbutton =document.getElementById(buttonControl);
        
        if(cbutton != null)
            cbutton.click();    
        e.returnValue = false;          
    }
}

function FixYear()
{
	var now = new Date();
	
	var yearText = document.getElementById("year");
	if( yearText!=null)
		yearText.innerHTML = now.getFullYear().toString();
}
