﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

var OriginalImageSize = new Array();

$(document).ready(function() {

    window.onload = GetImageSize;
   
  
    
   


    //If the User resizes the window, adjust the #container height
    $(window).bind("resize", resizeWindow);
    function resizeWindow(e) {
        GetImageSize();
    }

});

function scaleSize(maxW, maxH, currW, currH) {

    var ratio = currH / currW;

    if (currW >= maxW && ratio <= 1) {
        currW = maxW;
        currH = currW * ratio;
    }
    else if (currH >= maxH) {
        currH = maxH;
        currW = currH / ratio;
    }

    return [currW, currH];
}

function resizeImageWithCss(maxW, maxH, currW, currH, img) {
    var window_height = maxH;
    var window_width = maxW;
    var image_width = currW;
    var image_height = currH;
    var height_ratio = image_height / window_height
    var width_ratio = image_width / window_width
    if (height_ratio > width_ratio) {
        img.style.width = "auto"
        img.style.height = "100%"
    }
    else {
        img.style.width = "100%"
        img.style.height = "auto"
    }
}

var resize = function(img, maxh, maxw) {
    var ratio = maxh / maxw;
    if (img.height / img.width > ratio) {
        // height is the problem
        if (img.height > maxh) {
            img.width = Math.round(img.width * (maxh / img.height));
            img.height = maxh;
        }
    } else {
        // width is the problem
        if (img.width > maxh) {
            img.height = Math.round(img.height * (maxw / img.width));
            img.width = maxw;
        }
    }
};

function ResizeImageNew(image, maxwidth, maxheight, OriginalWidth, OriginalHeight) {

    w = image.width;
    h = image.height;

//    if (w == 0 || h == 0) {
//        image.width = maxwidth;
//        image.height = maxheight;
//    }
    //    else
    /*Working
    if (w > maxwidth) {
    image.width = maxwidth;
        
    }
    else {
    //        image.width = OriginalWidth;
    image.width = maxwidth;
    }*/

    if (OriginalWidth >= maxwidth) {
        image.width = maxwidth;

    }
    else {
                image.width = OriginalWidth;
        //image.width = maxwidth;
    }
    
//    if (h > maxheight) {
//        image.height = maxheight;
//    }
//    else {
//        image.height = OriginalHeight;
    //    }

//    else if (w > maxwidth) {
//        if (w > h) image.width = maxwidth;
//    }
//    else {
//        if (h > maxheight) image.height = maxheight;
//    }


}

function ResizeWithBroserWidth(image, maxwidth, maxheight, OriginalWidth, OriginalHeight) {
    image.width = maxwidth;
    //Added by Hemant to show image without footer height
    //image.height = maxheight-21;
}

function GetImageSize() {
    //alert('function call');

    
    $("#page-background").find("img").each(function(index, elmt) {

        var img = elmt;
        //alert('src:' + $(this).attr('src'));
        //or however you get a handle to the IMG
        var width = $(this).width();
        var height = $(this).height(); ; // 555; // img.clientHeight;

       
        OriginalImageSize.push($(this).attr('src') + '|' + width + '|' + height);

        //        var winHeight = $(window).height();
        //        var winWidth = $(window).width();
        //        //alert('winHeight:' + winHeight + 'winWidth:' + winWidth);
        //        var newSize = scaleSize(winWidth, winHeight, width, height);

        //        $(this).attr('height', newSize[0] / 2);
        //        $(this).attr('width', newSize[1]);

    });

    $("#page-background").find("img").each(function(index, elmt) {


        var img = elmt;
        var imgOriginal = OriginalImageSize[index];
        var partArray = imgOriginal.split('|');
        //or however you get a handle to the IMG
        var width = partArray[1];
        var height = partArray[2];  // 555; // img.clientHeight;


        //Current height and width
        var winHeight = $(window).height();
        var winWidth = $(window).width();
        //alert('winHeight:' + winHeight + 'winWidth:' + winWidth+' imgheight:'+height+' imgWidth:'+width );
        /*Working solution
        var newSize = scaleSize(winWidth, winHeight, width, height);

        $(this).attr('height', newSize[1] );
        $(this).attr('width', newSize[0]);*/

        //        $(this).attr('height', winHeight-50 );
        //        $(this).attr('width', winWidth-50);

        //Resize image with height and width
        //resizeImageWithCss(winWidth, winHeight, width, height, img);

        //resize working
        //ResizeImageNew(img, winWidth, winHeight, width, height);

        //resize width only
        ResizeWithBroserWidth(img, winWidth, winHeight, width, height);
    });
}


