var div = $('#divImg');
var img = $('#imgBackground');
var height;
var width;
var ratio;
var resizeID = 0;
var imageName = '';
var imagePath;
var imageSize = 'small';
var qeueCount = 0;
var imageQueue = []
var isLoaded = false;

$(document).ready(function ()
{
    if (document.location.href.search('project') <= 0)
        PageStart();

    $(window).resize(scale);
});

document.getElementById('imgBackground').onload = function ()
{
    if (!isLoaded)
        return;

    $(this).css('display', 'inline-block');
    height = img.height();
    width = img.width();
    ratio = width / height;
    scale();

    if (imageSize == 'small')
    {
        ImageLoad('medium');
    }
    else if (imageSize == 'medium')
        ImageLoad('large');
    else if (imageSize == 'large')
        ImageCache();
};

function ClearQueue()
{
    for (var item in imageQueue)
    {
        imageQueue[item].src = '';
    }
}

function PageStart()
{
    var hdPath = $('#hdImagePath');
    var hdName = $('#hdImageName');
    
    if (hdPath.length > 0)
        imagePath = hdPath.val();
    else
        imagePath = 'images/BackgroundImages/';
    if (hdName.length > 0)
        imageName = hdName.val();
    else
        imageName = document.location.pathname.replace('/', '').replace('.aspx', '');

    ImageLoad('small');
}

function ImageCallback()
{
    $('iframe').remove();
    HideLoader();
    $('#imgBackground').attr('src', imagePath + imageName + '_' + imageSize + '.jpg');
    $('iframe').remove();
}

function ImageLoad(size)
{
    isLoaded = true;

    ShowLoader();
    imageSize = size;

    imgCached = new Image();

    imgCached.onerror = ImageError;
    imgCached.onload = ImageCallback;

    imgCached.src = imagePath + imageName + '_' + size + '.jpg';

    imageQueue[imageQueue.length] = imgCached;
}

function scale()
{
    if (!ratio || ratio == undefined)
        return;

    var contentWindow = $(document.body);
    var contentWidth = contentWindow.width();
    var contentHeight = contentWindow.height();
    var documentHeight = $(window).height();
    var documentWidth = $(window).width();

        contentHeight = documentHeight;
        contentWidth = documentWidth;

    var windowRatio = contentWidth / contentHeight;


    img.width(ratio < windowRatio ? contentWidth : contentHeight * ratio)
    .height(ratio < windowRatio ? contentWidth / ratio : contentHeight);
    div.width(contentWidth)
    .height(contentHeight);

    if(typeof(Scale) != "undefined")
        Scale(img);
}

function ShowLoader()
{
    if (qeueCount++ == 0)
        $('#Bg_load').css('display', 'inline-block');
}

function HideLoader()
{
    if (qeueCount > 0 && --qeueCount == 0)
        $('#Bg_load').css('display', 'none');
}

function ImageError(a, b, c)
{
    HideLoader();
    return;
    if (imageName == 'default' || imageSize != 'small')
    {
        return;
    }
    else
    {
        imageName = 'default';
        ImageLoad('small');
    }
}
    


