//Public vars/config
var menuItems = [["bio", "/site/bio"], ["publicidade", "/site/publicidade"], ["projetos", "/site/projetos"], ["blog", "http://www.crisberger.com.br/"], ["contato", "/site/contato"]];
var selectedMenu = '';
var footerColor = 'black';
var isMobile = navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null || navigator.userAgent.match(/iPod/i) != null;
var slideshowContentList = [];
var slideshowWindowResizeTimeout;
var slideshowCurrentIndex = 0;
var slideshowShowPages = true;
var contentResizeTimeout;
var categoriesList = [];
var categoriesPages;
var categoriesCurrentPageIndex = 0;
var backLink = "";

//Transformie config
/*Transformie.inlineCSS = false;
Transformie.trackChangesFor = false;
Transformie.stylesheets = true;*/

//Footer
function configureFooter(selectedMenu, footerColor) {
    if (!footerColor) {
        footerColor = "black";
    }
	//Create menu
	var link;
	var cursor;
	var top;
	for (var i = 0; i < menuItems.length; i++)
	{
		if (menuItems[i][0] == selectedMenu)
		{
			link = "";
			cursor = "";
			top = "-20px";
		}
		else
		{
			if (menuItems[i][1].indexOf("http://") > -1)
			{
				link = " onclick=\"javascript: { window.open('" + menuItems[i][1] + "', '_blank'); }\"";
			}
			else
			{
				link = " onclick=\"javascript: { document.location = '" + menuItems[i][1] + "';  }\"";
			}
			
			cursor = "cursor: pointer;";
			top = "0px";
		}
	
		$('.menu').append("<div class='item' id='" + menuItems[i][0] + "' style='" + cursor + "' " + link + "><div style='top: " + top + ";'></div></div>");
	}
	
	if (!isMobile)
	{
		$('.menu .item').hover(function ()
		{
			if (this.id != selectedMenu)
			{
				$(this).find('div').stop().animate({ top: -20 }, 400, "easeInOutExpo");
			}
		}, function ()
		{
			if (this.id != selectedMenu)
			{
				$(this).find('div').stop().animate({ top: 0 }, 400, "easeInOutExpo");
			}
		});
	}

	//Apply the selected item
	$('.menu').children().each(function ()
	{
		if (this.innerHTML == selectedMenu)
		{
			var link = $(this);
			link.addClass("selected");
			link.removeAttr("href");
			link.css("cursor", "default");
		}
	});
	
	//Apply the desired footer color
	if (footerColor == 'black')
	{
		$('.menu').addClass('menuBlack');
		if ($('.logo img')[0]){
			$('.logo img').attr("src",$('.logo img').attr("src").replace("White","Black"));
		}
	}
}

//Content
function configureCenteredDiv()
{
	$('.whiteBg').append("<img src='../_img/fullWhite.jpg' />");
	
	//RESIZE
	$(window).resize(function () {
		clearTimeout(contentResizeTimeout);
		contentResizeTimeout = setTimeout(contentResize, 80);
		contentResize();
	});
	contentResize();
}

function contentResize()
{
	$('.centeredDiv').css('display', 'none');

	var windowWidth = Math.max($(window).width(), $('.centeredDiv').width());
	var windowHeight = $(window).height();
		
	$('.whiteBg img').width(windowWidth).height(windowHeight);
	
	$('.centeredDiv').css('display', 'block').css({ left: Math.abs((windowWidth - $('.centeredDiv').width()) / 2), top: Math.abs((windowHeight - ($('.centeredDiv').height() + 48)) / 2) });
}

//Categories
function configureCategories(categoriesList)
{
	//CREATE
	var itemData;
	var html = "<div class='container' style='left: 0px;'><table cellspacing='0' cellpadding='0' style='table-layout:fixed; overflow:hidden; white-space: nowrap;'><tr>";
	
	categoriesPages = 0;
	for (var i = 0; i < categoriesList.length; i++)
	{
		itemData = categoriesList[i];
		itemData.index = i;
		if (i % 12 == 0)
		{
			html += "<td><table cellspacing='0' cellpadding='0' id='page" + categoriesPages + "'><tr>";
		}
		
		html += "<td>";
		html += "<div class='item' id='item" + itemData.index + "_" + itemData.ProjetoId + "'>";
		
		//.thumb
		html += "<div class='thumb'><img src='" + itemData.ImagemThumbUrl + "' />";
		
		if (!isMobile)
		{
			html += "<div class='over'></div>";	
		}
		
		html += "</div>";
		
		//.label(over)
		if (!isMobile)
		{
			html += "<div class='label'><div class='bg'></div><div class='text'><table cellspacing='0' cellpadding='0'><tr><td>";
			
			if (itemData.hasVideo)
			{
				html += "<img src='../_img/play.png' /><br>";
			}
			
			html += "" + itemData.Titulo + "</td></tr></table></div></div>";
		}
		
		html += "</div>";
		html += "</td>";
		
		if (i % 4 == 3)
		{
			html += "</tr><tr>";
		}
		
		if (i % 12 == 11 || i == (categoriesList.length - 1))
		{
			categoriesPages++;
			html += "</tr></table></td>";
		}
	}
	
	html += "</tr></table></div>";
	$('.projects').append(html);

	//Item behavior
	if (!isMobile)
	{
		$('.projects').find(".item").hover(function ()
		{
			//Over this item
			var over = this;
			
			$(this).find(".label").stop().animate({ marginTop: -100 }, 300, "easeOutQuint");
			
			//Hidden other items of this current page
			$('#page' + categoriesCurrentPageIndex).find('.item').each(function () 
			{
				if (over.id != this.id)
				{
					$(this).find(".over").stop().animate({ opacity: 0.7 }, 400, "easeOutQuint");
				}
			});
		}, function ()
		{
			$(this).find(".label").stop().animate({ marginTop: 0 }, 300, "easeOutQuint");
		
			//Return all items to normal
			$('#page' + categoriesCurrentPageIndex).find('.item').each(function ()
			{
				$(this).find(".over").stop().animate({ opacity: 0 }, 400, "easeOutQuint");
			});
		})
		
		$('.projects').find(".item").find(".over").stop().animate({ opacity: 0 }, 0);
		$('.projects').find(".item").find(".bg").stop().animate({ opacity: 0.64 }, 0);
	}

	$('.projects').find(".item").click(function() {
	    var index = this.id.split("item").join("");
	    var codes = index.split("_");	    
	    document.location = "Detalhe.aspx?id=" + codes[1] + "&page=" + Math.floor(codes[0] / 12);
	});

	//Navigation
	if (!isMobile)
	{
		$('#arrowLeft, #arrowRight').hover(function ()
		{
			if ($(this).data("enabled"))
			{
				$(this).data("over", true);
				
				var targetX = -20;
				if (this.id == "arrowRight")
				{
					targetX = 0;
				}
				
				$(this).find(".content").stop().animate({ left: targetX }, 400, "easeInOutExpo");
			}
		}, function ()
		{
			$(this).data("over", false);
		
			if ($(this).data("enabled"))
			{			
				var targetX = 0;
				if (this.id == "arrowRight")
				{
					targetX = -20;
				}
				
				$(this).find(".content").stop().animate({ left: targetX }, 400, "easeInOutExpo");
			}
		});
	}
	
	$('#arrowLeft, #arrowRight').click(function ()
	{
		if ($(this).data("enabled"))
		{
			if (this.id == "arrowLeft")
			{
				categoriesNavigateBy(-1);
			}
			else
			{
				categoriesNavigateBy(1);
			}
		}
	});
	
	categoriesNavigateBy(0, false);
}

function categoriesNavigateBy(tmp_offset, tmp_animated)
{
	var targetIndex = categoriesCurrentPageIndex + tmp_offset;
	
	targetIndex = Math.min(categoriesPages - 1, Math.max(0, targetIndex));

	if (tmp_animated == null)
	{
		tmp_animated = true;
	}
	
	categoriesCurrentPageIndex = targetIndex;
	
	var duration = 600;
	if (!tmp_animated)
	{
		duration = 0;
	}
	
	$('.categories .projects .container').stop().animate({ left: -(categoriesCurrentPageIndex * 724) }, duration, "easeInOutExpo");
	
	$('.categories .pages').text((categoriesCurrentPageIndex + 1) + "/" + categoriesPages);
	
	//Show/hide navigation arrows
	if (targetIndex <= 0)
	{
		if (!isMobile)
		{
			$('#arrowLeft').find(".content").stop().animate({ left: 20 }, (tmp_animated ? 200 : 0), "easeInOutExpo");
			$('#arrowLeft').css("cursor", "default");
		}
		else
		{
			$('#arrowLeft .content').toggle(false);
		}
		
		$('#arrowLeft').data("enabled", false);
	}
	else if (!$('#arrowLeft').data("over"))
	{	
		if (!isMobile)
		{
			$('#arrowLeft').find(".content").stop().animate({ left: 0 }, (tmp_animated ? 200 : 0), "easeInOutExpo");
			$('#arrowLeft').css("cursor", "pointer");
		}
		else
		{
			$('#arrowLeft .content').toggle(true);
		}
		
		$('#arrowLeft').data("enabled", true);
	}
	
	if (targetIndex >= (categoriesPages - 1))
	{	
		if (!isMobile)
		{
			$('#arrowRight').find(".content").stop().animate({ left: -40 }, (tmp_animated ? 200 : 0), "easeInOutExpo");
			$('#arrowRight').css("cursor", "default");
		}
		else
		{
			$('#arrowRight .content').toggle(false);
		}
		
		$('#arrowRight').data("enabled", false);
	}
	else if (!$('#arrowRight').data("over"))
	{	
		if (!isMobile)
		{
			$('#arrowRight').find(".content").stop().animate({ left: -20 }, (tmp_animated ? 200 : 0), "easeInOutExpo");
			$('#arrowRight').css("cursor", "pointer");
		}
		else
		{
			$('#arrowRight .content').toggle(true);
		}
		
		$('#arrowRight').data("enabled", true);
	}
}

function configureDetails()
{
	if (!isMobile)
	{
		$('.close .button').hover(function ()
		{
			$(this).children('.content').stop().animate({ top: -15 }, 400, "easeInOutExpo");
		}, function ()
		{
			$(this).children('.content').stop().animate({ top: 0 }, 400, "easeInOutExpo");
		});
	}

	$('.close .button').click(function() {

	    if (document.location.toString().indexOf("page=") != -1) {
	        var tipo = "";
	        if (document.location.toString().indexOf("/projetos") != -1) {
	            tipo = "projetos/?";
	        }

	        if (document.location.toString().indexOf("/publicidade") != -1) {
	            tipo = "publicidade/?";
	        }
	        var page = document.location.toString().substring(document.location.toString().indexOf("page="));
	    }
	    window.open("/site/" + tipo + page, "_parent");
	});
}



//Slideshow
function configureSlideshow() {
    
   
	//CREATE
	var itemData;
	var itemChild;
	var html = "<table cellspacing='0' cellpadding='0' style='table-layout:fixed; overflow:hidden; white-space: nowrap;'><tr>";
	var htmlThumbs = "<table cellspacing='0' cellpadding='0'><tr>";

	for (var i = 0; i < slideshowContentList.length; i++) {
	    
		itemData = slideshowContentList[i];
		
		html += "<td><div id='tdWrapper' style='overflow: hidden;'>"
		htmlThumbs += "<td>";
		
		switch (itemData.type)
		{
		    case 'image':

		        html += "<div id='image' style='position: relative;'><img src='" + ((isMobile) ? itemData.imageMobileURL : itemData.imageURL) + "' /></div>";
		        if (!isMobile) {
		            
		            //html += "<div id='shadow' style='position: absolute; left: 0; top: 0;'><img src='/site/_img/fullShadow.png' id='shadow' /></div>";
		        }
		        
		        


		        //html += "</div>";

		        htmlThumbs += "<img src='" + itemData.thumbURL + "' />";

		        if (!isMobile) {
		            
		            if (!itemData.imageWidth) {
		                itemData.imageWidth = 1440;
		            }

		            if (!itemData.imageHeight) {
		                itemData.imageHeight = 900;
		            }
		        }
		        else {
		            itemData.imageWidth = itemData.imageMobileWidth;
		            itemData.imageHeight = itemData.imageMobileHeight;

		            if (!itemData.imageWidth) {
		                itemData.imageWidth = 1024;
		            }

		            if (!itemData.imageHeight) {
		                itemData.imageHeight = 768;
		            }
		        }

		        break;
			case 'video':
				if (!itemData.width)
				{
					itemData.width = 640;
				}

	            if (!itemData.height)
				{
				    itemData.height = 480;
				}

				html += '<div id="video" style="position: relative; margin-left: 0px; margin-top: 0px;"><iframe width="' + itemData.width + '" height="' + itemData.height + '" src="http://www.youtube.com/embed/' + itemData.videoUrl + '" frameborder="0" allowfullscreen></iframe></div>';
				
				htmlThumbs += "<img src='../_img/thumbVideo.jpg' />";
				
				break;
		}
		
		html += "</div></td>";
		htmlThumbs += "</td>";
	}
	
	html += "</tr></table>";
	htmlThumbs += "</tr></table>";
	
	$('#divSlideshowWindow').addClass("slideshow");
	$('#divSlideshowWindow').css({ width: 100, height: 100, overflow: "none" });
	$('#divSlideshowWindow').prepend("<div id='slideshowContainer' style='position: relative;'>" + html + "</div>");

	
	//Mobile navigation behavior
	if (isMobile)
	{
		//Remove thumb preview / arrows over/click
		$('.nav .thumbs').remove();
		$('.nav .mouseHint').remove();
		$('.nav .pages').remove();
		
		//Detect swaping gesture
		$('#divSlideshowWindow').swipe({ swipeLeft: function ()
		{
			//slideshowNavigateBy(1);
		}, swipeRight: function ()
		{
			//slideshowNavigateBy(-1);
		}, swipeStatus: function (event, phase, direction, distance)
		{
			if ((direction == "left" && (slideshowCurrentIndex < slideshowContentList.length - 1)) || (direction == "right" && slideshowCurrentIndex > 0))
			{
				if (phase == "start")
				{
					$('#divSlideshowWindow #slideshowContainer').stop();
				}
				else if (phase == "move")
				{
					var signal = 1;
					if (direction == "left")
					{
						signal = -1;
					}
					
					$('#divSlideshowWindow #slideshowContainer').css({ left: -(slideshowCurrentIndex * $(window).width()) + (distance * signal) });
				}
				else if (phase == "end")
				{
					if (distance > ($(window).width() / 5))
					{
						if (direction == "left")
						{
							slideshowNavigateBy(1, true);
						}
						else
						{
							slideshowNavigateBy(-1, true);
						}
					}
					else
					{
						slideshowNavigateBy(0, true);
					}
				}
			}
		}, triggerOnTouchEnd: true, allowPageScroll: false, fingers: 1, threshold: 0 });
	}
	//Desktop browser navigation behavior
	else
	{
		if (!slideshowShowPages)
		{
			$('.nav .pages').remove();
		}
	
		$('.nav .thumbs').append("<div id='thumbsContainer' style='position: relative;'>" + htmlThumbs + "</div>");
		
		$(".nav").hover(function ()
		{
			var item = $(this);
		
			if (item.data("enabled"))
			{
				item.find(".thumbs").stop().animate({ width: 256 }, 400, "easeInOutExpo");
				item.find(".pages").stop().animate({ width: 31 }, 400, "easeInOutExpo");
				
				item.stop().animate({ width: 298 }, 400, "easeInOutExpo");
			}
		}, function ()
		{
			var item = $(this);
			
			item.find(".thumbs").stop().animate({ width: 0 }, 400, "easeInOutExpo");
			item.find(".pages").stop().animate({ width: 0 }, 400, "easeInOutExpo");
			
			item.stop().animate({ width: 80 }, 400, "easeInOutExpo");
		});
		
		$('.nav').width(80);
		
		$('#navLeft').click(function ()
		{
			slideshowNavigateBy(-1, true);
		});
		
		$('#navRight').click(function ()
		{
			slideshowNavigateBy(1, true);
		});
	}
	
	//RESIZE
	$(window).resize(function () {
		clearTimeout(slideshowWindowResizeTimeout);
		slideshowWindowResizeTimeout = setTimeout(slideshowResize, 80);
	});
	
	slideshowNavigateBy(0, false);
	slideshowResize();
}

function slideshowNavigateBy(tmp_offset, tmp_animated)
{
	var targetIndex = slideshowCurrentIndex + tmp_offset;
	
	targetIndex = Math.min(slideshowContentList.length - 1, Math.max(0, targetIndex));

	if (tmp_animated == null)
	{
		tmp_animated = true;
	}
	
	slideshowCurrentIndex = targetIndex;
	
	var duration = 600;
	if (isMobile)
	{
		duration = 400;
	}
	
	if (!tmp_animated)
	{
		duration = 0;
	}
	
	var animation = "easeInOutExpo";
	if (isMobile)
	{
		animation = "easeOutQuint";
	}
	$('#divSlideshowWindow #slideshowContainer').stop().animate({ left: -(slideshowCurrentIndex * $(window).width()) }, duration, animation);
	
	if (!isMobile)
	{
		$('#navLeft .thumbs #thumbsContainer').stop().animate({ left: -(Math.max(0, (slideshowCurrentIndex - 1)) * 256) }, 600, "easeInOutExpo");
		$('#navRight .thumbs #thumbsContainer').stop().animate({ right: 0 - (slideshowContentList.length - 1) * 256 + (Math.min(slideshowContentList.length - 1, (slideshowCurrentIndex + 1)) * 256) }, 600, "easeInOutExpo");
		$('.nav .pages').html((slideshowCurrentIndex + 1) + '<br>/<br>' + slideshowContentList.length);
    }

    //$('.description .year span').html(slideshowContentList[slideshowCurrentIndex].year);
    $('.description .year span').hide();
    $('.description .title span').html(slideshowContentList[slideshowCurrentIndex].title);
    $('.description .text span').html(slideshowContentList[slideshowCurrentIndex].description);

	currentItemData = slideshowContentList[slideshowCurrentIndex];
	
	if (currentItemData != null && currentItemData.type == "video")
	{
		$('.description').stop().animate({ opacity: 0 }, 400, "easeOutQuint");
	}
	else
	{
		$('.description').stop().animate({ opacity: 1 }, 400, "easeOutQuint");
	}
	
	//Show/hide navigation arrows
	if (targetIndex <= 0)
	{
		if (!isMobile)
		{
			$('#navLeft').stop().animate({ left: 0 }, (tmp_animated ? 200 : 0), "easeInOutExpo");
			$('#navLeft').data("enabled", false);
			$('#navLeft .thumbs').stop().animate({ width: 0 },  (tmp_animated ? 400 : 0), "easeInOutExpo");
			$('#navLeft .pages').stop().animate({ width: 0 }, (tmp_animated ? 400 : 0), "easeInOutExpo");
			$('#navLeft .mouseHint').toggle(false);
		}
		else
		{
			$('#navLeft').toggle(false);
		}
	}
	else
	{	
		if (!isMobile)
		{
			$('#navLeft').stop().animate({ left: 0 }, (tmp_animated ? 200 : 0), "easeInOutExpo");	
			$('#navLeft').data("enabled", true);
			$('#navLeft .mouseHint').toggle(true);
		}
		else
		{
			$('#navLeft').toggle(true);
		}
	}
	
	if (targetIndex >= (slideshowContentList.length - 1))
	{	
		if (!isMobile)
		{
			$('#navRight').stop().animate({ right: -10, width: 80 }, (tmp_animated ? 200 : 0), "easeInOutExpo");
			$('#navRight').data("enabled", false);
			$('#navRight .thumbs').stop().animate({ width: 0 }, (tmp_animated ? 400 : 0), "easeInOutExpo");
			$('#navRight .pages').stop().animate({ width: 0 }, (tmp_animated ? 400 : 0), "easeInOutExpo");
			$('#navRight .mouseHint').toggle(false);
		}
		else
		{
			$('#navRight').toggle(false);
		}
	}
	else
	{	
		if (!isMobile)
		{
			$('#navRight').stop().animate({ right: 0 }, (tmp_animated ? 400 : 0), "easeInOutExpo");
			$('#navRight').data("enabled", true);
			$('#navRight .mouseHint').toggle(true);
		}
		else
		{
			$('#navRight').toggle(true);
		}
	}
}

function slideshowResize()
{
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    
    //Resize window
    $('#divSlideshowWindow').width(windowWidth).height(windowHeight);
    
    //Stop animation and set the current position
	$('#divSlideshowWindow #slideshowContainer').stop().css("left", -(slideshowCurrentIndex * windowWidth));
    
    //TODO: Resize overlay
    
    //Align nav
    //$('#divSlideshowNavigation').css('width', windowWidth);
    $('.nav').css("top", Math.round((windowHeight - 160) / 2));
    
    //Align all content sizes
    var itemIndex = 0;
    var newRect;
    var totalWidth = 0;
    var offset = 0;
    $('#divSlideshowWindow #slideshowContainer').find("td").each(function() {
        var data = slideshowContentList[itemIndex];
        var item = $(this);
        var video;

        switch (data.type) {
            case 'image':
                //newRect = resizeZoom(data.imageWidth, data.imageHeight, windowWidth, windowHeight);
                //newRect = resizeInside(data.imageWidth, data.imageHeight, windowWidth, windowHeight);
                newRect = resizeNormal(data.imageWidth, data.imageHeight, windowWidth, windowHeight);

                item.find("#image img").width(newRect.width).height(newRect.height);
                item.find("#image").css("top", newRect.y);
                item.find("#image").css("left", newRect.x);

                if (!isMobile) {
                    item.find("#shadow img").width(windowWidth).height(windowHeight);
                    
                    item.find("#shadow").css("left", totalWidth);
                }

                //item.width(windowWidth).height(windowHeight);
                item.find("#tdWrapper").width(windowWidth).height(windowHeight);
                //alert(item.find("#tdWrapper"));

                break;
            case 'video':
                newRect = { x: 0, width: windowWidth };

                //item.find("#shadow img").width(windowWidth).height(windowHeight);
                video = item.find("#video");
                var marginHorizontal = Math.round((windowWidth - data.width) / 2);
                video.css("margin-left", marginHorizontal);
                marginHorizontal = windowWidth - data.width - marginHorizontal;
                video.css("margin-right", marginHorizontal);

                var marginVertical = Math.round((windowHeight - data.height) / 2);
                video.css("margin-top", Math.round((windowHeight - data.height) / 2));
                marginVertical = windowHeight - data.height - marginVertical;
                video.css("margin-bottom", marginVertical);

                break;
        }

        totalWidth += newRect.width - (Math.abs(newRect.x) * 2);


        itemIndex++;
    });
    
	$('#divSlideshowWindow #slideshowContainer').stop().css("left", -(slideshowCurrentIndex * windowWidth));
}

//UTILS
function resizeZoom(tmp_original_width, tmp_original_height, tmp_bound_width, tmp_bound_height)
{
	var relX = tmp_original_width / tmp_bound_width;
	var relY = tmp_original_height / tmp_bound_height;
	
	var response = { x: 0, y: 0, width: 0, height: 0 };
	
	if (relX > relY)
	{
		response.width = tmp_original_width / relY;
		response.height = tmp_bound_height;
		
		if (response.width > tmp_bound_width)
		{
			response.x = (tmp_bound_width - response.width) / 2;
		}
	}
	else
	{
		response.width = tmp_bound_width;
		response.height = tmp_original_height / relX;
		
		if (response.height > tmp_bound_height)
		{
			response.y = (tmp_bound_height - response.height) / 2;
		}
	}
	
	return response;
}

function resizeInside(tmp_original_width, tmp_original_height, tmp_bound_width, tmp_bound_height) {
    /*var relX = tmp_original_width / tmp_bound_width;
    var relY = tmp_original_height / tmp_bound_height;

    var response = { x: 0, y: 0, width: 0, height: 0 };

    if (relX < relY) {
        response.width = tmp_original_width / relY;
        response.height = tmp_bound_height;

        if (response.width > tmp_bound_width) {
            response.x = (tmp_bound_width - response.width) / 2;
        }
    }
    else {
        response.width = tmp_bound_width;
        response.height = tmp_original_height / relX;

        if (response.height > tmp_bound_height) {
            response.y = (tmp_bound_height - response.height) / 2;
        }
    }*/

    return { x: 0, y: 0, width: tmp_bound_width, height: tmp_bound_height };

    //return response;
}

function resizeNormal(tmp_original_width, tmp_original_height, tmp_bound_width, tmp_bound_height) {

    /*
    var relX = 0;
    var relY = 0;
    var auxW = 0;
    var auxH = 0;

    var maxW = 0;
    var maxH = 0;
    if (tmp_bound_height > tmp_original_height) {
    auxH = (tmp_bound_height - tmp_original_height) / 2;
    }
    else {
    maxH = tmp_bound_height;
    }
    if (tmp_bound_width > tmp_original_width) {
    auxW = (tmp_bound_width - tmp_original_width) / 2;
    }
    else {
    maxW = tmp_bound_width;
    }
    relY = auxH;
    relX = auxW;

    return { x: relX, y: relY, width: tmp_original_width, height: tmp_original_height };
    */
    
    var relX = 0;
    var relY = 0;
    var auxW = 0;
    var auxH = 0;

    var maxW = 0;
    var maxH = 0;


    if (tmp_bound_height <= tmp_original_height) {
        maxW = (tmp_bound_height * tmp_original_width) / tmp_original_height;
        maxH = tmp_bound_height;
    } else {
        if (tmp_bound_width <= tmp_original_width) {
            maxH = (tmp_bound_width * tmp_original_height) / tmp_original_width;
            maxW = tmp_bound_width;
        }
        else {
            maxH = tmp_original_height;
            maxW = tmp_original_width;
        }
    }


    if (tmp_bound_height > maxH) {
        auxH = (tmp_bound_height - maxH) / 2;
    }
    if (tmp_bound_width > maxW) {
        auxW = (tmp_bound_width - maxW) / 2;
    }
    relY = auxH;
    relX = auxW;


   

    return { x: relX, y: relY, width: maxW, height: maxH };
    
}


