function loadImage(imageLocation, imageId, mode, albumId)
{
	image_photo			= new Image();
	
	if (window.XMLHttpRequest) 
    {
        //codeforIE7+,Firefox,Chrome,Opera,Safari
        xmlhttp = new XMLHttpRequest();
    }
    else 
    {
        //codeforIE6,IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
		{
			var myUrl = xmlhttp.responseText;
			
			imageLocation 		= myUrl+'/album/'+imageLocation
    		image_photo.src 	= imageLocation;	
		}
	}
	
	xmlhttp.open("GET", "../php/popup.php?m=urlCheck", true);
    xmlhttp.send();
    
    var checkInterval;
    
    function checkStat()
    {
        if (image_photo.height != 0) 
        {
            clearInterval(checkInterval);
            
            if (typeof(window.innerWidth) == 'number') 
            {
                //Non-IE
                myWidth 	= window.innerWidth;
                myHeight 	= window.innerHeight;
            }
            else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
            {
                //IE 6+ in 'standards compliant mode'
                myWidth 	= document.documentElement.clientWidth;
                myHeight 	= document.documentElement.clientHeight;
            }
            else if (document.body && (document.body.clientWidth || document.body.clientHeight)) 
            {
                //IE 4 compatible
                myWidth 	= document.body.clientWidth;
                myHeight 	= document.body.clientHeight;
            }
            
			
            
            if (image_photo.height > myHeight && image_photo.width < myWidth) 
            {
                var photoHeightPercent 	= Math.floor(450 / image_photo.height * 100);
                var photo_height 		= 450;
                var photo_width 		= image_photo.width / 100 * photoHeightPercent;
            }
            
            else if (image_photo.height < myHeight && image_photo.width > myWidth) 
            {
            
                var photoWidthPercent 	= Math.floor(800 / image_photo.height * 100);
                var photo_width 		= 800;
                var photo_height 		= image_photo.height / 100 * photoWidthPercent;
            }
            
            else if (image_photo.height > myHeight && image_photo.width > myWidth) 
            {
            
                if (Math.floor(image_photo.height / image_photo.width * 100) > 100) 
                {
                    var photoHeightPercent 	= Math.floor(450 / image_photo.height * 100);
                    var photo_height 		= 450;
                    var photo_width 		= image_photo.width / 100 * photoHeightPercent;
                    
                    if (photo_width > myWidth) 
                    {
                        var diffrenceWidth 		= photo_width - myWidth + 60;
                        var photo_width 		= myWidth - diffrenceWidth - 100;
                        var photoWidthPercent 	= Math.floor(photo_width / image_photo.height * 100);
                        var photo_height 		= image_photo.height / 100 * photoWidthPercent;
                    }
                }
                else 
                {
					alert(Math.floor(image_photo.height / image_photo.width * 100));					
                    var photoWidthPercent 	= Math.floor(800 / image_photo.height * 100);
                    var photo_width 		= 800;
                    var photo_height 		= image_photo.width / 100 * photoWidthPercent;
                    
                    if (photo_height > myHeight) 
                    {
                        var diffrenceHeight 	= photo_height - myHeight + 60;
                        var photo_height 		= myHeight - diffrenceHeight - 100;
                        var photoHeightPercent 	= Math.floor(photo_height / image_photo.width * 100);
                        var photo_width 		= image_photo.width / 100 * photoHeightPercent;
                    }
                }
            }
            else 
            {
                var photo_height 	= image_photo.height;
                var photo_width 	= image_photo.width;
            }
            
			if (mode != "edit_photo") 
			{
				createPopup(photo_height + 20, photo_width + 20, "photo", albumId, imageLocation, photo_height, photo_width, imageId);
			}
			else
			{
				createPopup(photo_height + 20, photo_width + 20, "edit_photo", albumId, imageLocation, photo_height, photo_width, imageId);
			}
		}
    }
    
    checkInterval = setInterval(checkStat, 500);
    
}

function fileApeal()
{
    closePopup();
    createPopup(240, 500, "apeal", 1);
}

function insertAfter(newElement, targetElement)
{
    //target is what you want it to go after. Look for this elements parent.
    var parent = targetElement;
    
    //if the parents lastchild is the targetElement...
    if (parent.lastchild == targetElement) 
    {
        //add the newElement after the target element.
        parent.appendChild(newElement);
    }
    else 
    {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

function closePopup()
{
    var targetBg = document.getElementById('popup_bg');
    var targetCt = document.getElementById('popup_content');
    var targetBt = document.getElementById('popup_close_btn');
    
    targetBg.parentNode.removeChild(targetBg);
    targetCt.parentNode.removeChild(targetCt);
    targetBt.parentNode.removeChild(targetBt);
}

function rzCC(s)
{
    // thanks http://www.ruzee.com/blog/2006/07/\
    // retrieving-css-styles-via-javascript/
    for (var exp = /-([a-z])/; exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()));
    return s;
}

function _setStyle(element, declaration)
{
    if (declaration.charAt(declaration.length - 1) == ';') declaration = declaration.slice(0, -1);
   
    var k, v;
	var splitted = declaration.split(';');
	
    for (var i = 0, len = splitted.length; i < len; i++) 
    {
        k = rzCC(splitted[i].split(':')[0]);
        v = splitted[i].split(':')[1];
        eval("element.style." + k + "='" + v + "'");
    }
}

function createPopup(popupHeight, popupWidth, mode, messageNum, imageLocation, photo_height, photo_width, photo_id)
{
	var topParent = document.getElementById('html-body');
	
	if (mode != "edit_photo") 
	{
		var siteHeight;
		
		var docBody = document.body, docElement = document.documentElement;
		
		var h1 = 0, h2 = 0, h3 = 0, h4 = 0;
		
		if (docBody) 
		{
			h1 = docBody.offsetHeight;
			h2 = docBody.scrollHeight;
		}
		if (docElement) 
		{
			h3 = docElement.offsetHeight;
			h4 = docElement.scrollHeight;
		}
		
		siteHeight = Math.max(h1, h2, h3, h4);
		
		var bgStr = "height:" + siteHeight + "px;";
		
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id', "popup_bg");
		newdiv.setAttribute('class', "hide", 0);
		_setStyle(newdiv, bgStr);
	}
	
	if (typeof(window.innerWidth) == 'number') 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) 
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	var popupMarginTop = Math.floor(popupHeight / 2);
	var popupMarginLeft = Math.floor(popupWidth / 2);
	var popupCloseMarginTop = popupMarginTop + 16;
	var popupCloseMarginLeft = popupMarginLeft - 11;
	
	if (mode != "edit_photo") 
	{
		popupStr = "margin-top: -" + popupMarginTop + "px;margin-left: -" + popupMarginLeft + "px;height: " + popupHeight + "px;width: " + popupWidth + "px;";
		popupCloseStr = "margin-top: -" + popupCloseMarginTop + "px;margin-left: " + popupCloseMarginLeft + "px;";
		
		var contentWindow = document.createElement('div');
		contentWindow.setAttribute('id', "popup_content");
		contentWindow.setAttribute('class', "hide", 0);
		_setStyle(contentWindow, popupStr);
		
		var closeBtn = document.createElement('a');
		closeBtn.setAttribute('id', "popup_close_btn");
		closeBtn.setAttribute('href', "javascript:closePopup();");
		closeBtn.setAttribute('onclick', "closePopup();return false;");
		_setStyle(closeBtn, popupCloseStr);
		closeBtn.setAttribute('class', "hide", 0);
		
		insertAfter(newdiv, topParent);
		insertAfter(contentWindow, topParent);
		insertAfter(closeBtn, topParent);
	}
	else
	{
		var contentWindow 				= document.getElementById('popup_content');
		var closeBtn 					= document.getElementById('popup_close_btn');
		
		contentWindow.style.marginTop 	= "-"+popupMarginTop+"px";
		contentWindow.style.marginLeft 	= "-"+popupMarginLeft+"px";
		contentWindow.style.height		= popupHeight+"px";
		contentWindow.style.width		= popupWidth+"px";
		
		closeBtn.style.marginTop		= "-"+popupCloseMarginTop+"px";
		closeBtn.style.marginLeft		= popupCloseMarginLeft+"px";
	}

	
    if (window.XMLHttpRequest) 
    {
        //codeforIE7+,Firefox,Chrome,Opera,Safari
        xmlhttp = new XMLHttpRequest();
    }
    else 
    {
        //codeforIE6,IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
            document.getElementById("popup_bg").className = "";
            document.getElementById("popup_content").className = "";
            document.getElementById("popup_close_btn").className = "";
            
            if (mode != "photo" && mode != "edit_photo") document.getElementById("popup_content").innerHTML = xmlhttp.responseText;
            else 
            {
                document.getElementById("popup_content").innerHTML = '<img src="' + imageLocation + '" height="' + photo_height + '" width="' + photo_width + '" id="photo_holder" />';
            	
				var btnParent 			= document.getElementById("popup_content");
				var image_array 		= eval(xmlhttp.responseText);
				var amountOfImages 		= image_array.length;
				var currentImageNumber 	= 0;
				
				for(var i=0;i<amountOfImages;i++)
				{
					if(image_array[i][0] == photo_id)
					{
						currentImageNumber = i;
						
						nextImageLocation = i < amountOfImages-1 ? image_array[i+1][2] : "";
						prevImageLocation = i > 0 ? image_array[i-1][2] : "";
						
						if(i > 0 && i < amountOfImages-1)
						{
							if(document.getElementById('popup_previous'))
							{
								var new_data_prev = document.getElementById('popup_previous')
								new_data_prev.setAttribute('href','javascript:loadImage("'+prevImageLocation+'", "'+image_array[i-1][0]+'", "edit_photo",'+messageNum+');');
								new_data_prev.setAttribute('onclick','loadImage("'+prevImageLocation+'", "'+image_array[i-1][0]+'", "edit_photo",'+messageNum+');return false;');
							}
							else
							{
								var previousLink = document.createElement('a');
								previousLink.setAttribute('id','popup_previous');
								previousLink.setAttribute('href','javascript:loadImage("'+prevImageLocation+'", "'+image_array[i-1][0]+'", "edit_photo",'+messageNum+');');
								previousLink.setAttribute('onclick','loadImage("'+prevImageLocation+'", "'+image_array[i-1][0]+'", "edit_photo",'+messageNum+');return false;');
								btnParent.appendChild(previousLink);
							}
							
							if (document.getElementById('popup_next')) 
							{
								var new_data_next = document.getElementById('popup_next')
								new_data_next.setAttribute('href', 'javascript:loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');');
								new_data_next.setAttribute('onclick', 'loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');return false;');
							}
							else 
							{
								var nextLink = document.createElement('a');
								nextLink.setAttribute('id', 'popup_next');
								nextLink.setAttribute('href', 'javascript:loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');');
								nextLink.setAttribute('onclick', 'loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');return false;');
								btnParent.appendChild(nextLink);
							}
						
						}
						else if(i == 0)
						{
							if (document.getElementById('popup_previous')) 
							{
								var targetP 	= document.getElementById('popup_previous');
								
								targetP.parentNode.removeChild(targetP);
							}
							
							if (document.getElementById('popup_next')) 
							{
								var new_data_next = document.getElementById('popup_next')
								new_data_next.setAttribute('href', 'javascript:loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');');
								new_data_next.setAttribute('onclick', 'loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');return false;');
							}
							else 
							{
								var nextLink = document.createElement('a');
								nextLink.setAttribute('id', 'popup_next');
								nextLink.setAttribute('href', 'javascript:loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');');
								nextLink.setAttribute('onclick', 'loadImage("' + nextImageLocation + '", "' + image_array[i + 1][0] + '", "edit_photo",'+messageNum+');return false;');
								btnParent.appendChild(nextLink);
							}
						}
						else if(i == amountOfImages-1)
						{
							if (document.getElementById('popup_next')) 
							{
								var targetN 	= document.getElementById('popup_next');
								
								targetN.parentNode.removeChild(targetN);
							}
							
							if (document.getElementById('popup_previous')) 
							{
								var new_data_prev = document.getElementById('popup_previous')
								new_data_prev.setAttribute('href', 'javascript:loadImage("' + prevImageLocation + '", "' + image_array[i - 1][0] + '", "edit_photo",'+messageNum+');');
								new_data_prev.setAttribute('onclick', 'loadImage("' + prevImageLocation + '", "' + image_array[i - 1][0] + '", "edit_photo",'+messageNum+');return false;');
							}
							else 
							{
								var previousLink = document.createElement('a');
								previousLink.setAttribute('id', 'popup_previous');
								previousLink.setAttribute('href', 'javascript:loadImage("' + prevImageLocation + '", "' + image_array[i - 1][0] + '", "edit_photo",'+messageNum+');');
								previousLink.setAttribute('onclick', 'loadImage("' + prevImageLocation + '", "' + image_array[i - 1][0] + '", "edit_photo",'+messageNum+');return false;');
								btnParent.appendChild(previousLink);
							}
						}
					}
				}
			}
        }
    }
	
	
	
    xmlhttp.open("GET", "../php/popup.php?m=" + mode + "&msg=" + messageNum + "", true);
    xmlhttp.send();
}
