﻿//var copyLine1Width = copyLine1.offsetWidth;
//alert(copyLine1.scrollWidth + ', ' + copyLine1.clientWidth);
//alert(copyLine1.scrollHeight + ', ' + copyLine1.clientHeight);

var cntr = 0;
function up() {
    copyLine1.style.fontSize = cntr + 'px';
    copyLine1.style.height = cntr++ + 'px';
    alert(copyLine1.scrollHeight + ', ' + copyLine1.clientHeight);
}


//Minimum Body width
var MINBODYWIDTH = 920;

//COPY constants
var origCopyWidth = 844;
var origCopySmallBorder = 56 - 15;
var origCopyBigBorder = 85;
var origCopyLine1Width = origCopyWidth - origCopyBigBorder - origCopySmallBorder;
var origCopyLine2Width = origCopyLine1Width;
var origTopMargin = 115;
var copyLine1 = document.getElementById('copyLine1');
var copyLine2 = document.getElementById('copyLine2');
var copyLetter1 = document.getElementById('copyLetter1');

//IMAGES constants
var RATIO = 5/4; //images to copy ratio
var SPACER = 10;
var MAXIMAGEWIDTH = 500;
var SCROLLBARWIDTH = 18;
var TOPANDBOTTOMSPACER = 20;

var MAXIMAGESWIDTH = MAXIMAGEWIDTH + SPACER + (MAXIMAGEWIDTH - SPACER)/2 + SPACER;
var MAXCOPYWIDTH = (1 / RATIO) * MAXIMAGESWIDTH;
var MAXWINDOWWIDTH = MAXIMAGESWIDTH + MAXCOPYWIDTH;

var copyWidth;
var imagesWidth;
var IE = /*@cc_on!@*/false;
 
window.onresize = resizeEverythingResize;
window.onload = resizeEverythingLoad;
//resizeEverythingInitial();

//hbdev
var initialFinished = false;

function resizeEverythingResize() {
    //debugClear();
    //debug('resizing');
    resizeEverything();
}
function resizeEverythingLoad() {
    //while (!initialFinished) { }
    //debug('Load:');
    resizeEverything();
}
function resizeEverythingInitial() {
    //debug('pure call:');
    resizeEverything();
    initialFinished = true;
}
function resizeEverything() {

    //hbdev
    //debugClear();

    var windowWidth = $(window).width();
    if (windowWidth > 0) {



        if (windowWidth > SCROLLBARWIDTH + MAXWINDOWWIDTH) { }
        else if ((windowWidth > MAXWINDOWWIDTH) && (createsScrollBars(windowWidth) == true)) {
            windowWidth -= SCROLLBARWIDTH;
        }
        else if (createsScrollBars(windowWidth) == true) {
            windowWidth -= SCROLLBARWIDTH;
            if (createsScrollBars(windowWidth) == false) {
                var i = 1;
                while (!createsScrollBars(windowWidth + i)) {
                    i++;
                }
                windowWidth = windowWidth + i;
            }
        }
        windowWidth = Math.min(windowWidth, MAXWINDOWWIDTH);
        windowWidth = Math.max(windowWidth, MINBODYWIDTH);

        setDivWidths(windowWidth);
        //alert('CHOSEN windowWidth = ' + windowWidth + ', copyWidth=' + copyWidth + ', imagesWidth = ' + imagesWidth);
        $('#content').width(copyWidth + imagesWidth);
        var smallerImageWidth = Math.floor((imagesWidth - 3 * SPACER) / 3);

        $('#imgCopy').width(Math.round(copyWidth));
        $('#copyContainer').width(Math.round(copyWidth));
        $('#imageContainer').width(Math.round(imagesWidth));

        $('#imgFeatured1').width(smallerImageWidth * 2 + SPACER);
        $('#imgFeatured2').width(smallerImageWidth);
        $('#imgFeatured3').width(smallerImageWidth);
        $('#imgFeatured4').width(smallerImageWidth);
        $('#imgFeatured5').width(smallerImageWidth);
        $('#imgFeatured6').width(smallerImageWidth);

        //now on to COPY
        maxLineWidth = origCopyLine1Width * (copyWidth / origCopyWidth);
        maxLineWidth = Math.round(maxLineWidth);
        var oFontSize = 1;

        copyLine1.style.fontSize = oFontSize + 'px';
        copyLetter1.style.fontSize = (oFontSize * 2) + 'px';
        copyLine2.style.fontSize = oFontSize + 'px';

        //alert('current width: ' + copyLine1.clientWidth + 'max width: ' + maxLineWidth);

        while (copyLine1.clientWidth <= maxLineWidth && copyLine2.clientWidth <= maxLineWidth) {
            oFontSize++;
            copyLine1.style.fontSize = oFontSize + 'px';
            copyLetter1.style.fontSize = (oFontSize * 2) + 'px';
            copyLine2.style.fontSize = oFontSize + 'px';
            //alert('eval.  ' + copyLine1.clientWidth + ', ' + copyLine2.clientWidth + ' -- where max is ' + maxLineWidth);
            //debug('eval.  ' + copyLine1.clientWidth + ', ' + copyLine2.clientWidth + ' @font= ' + oFontSize + ' -- where max is ' + Math.round(maxLineWidth) + ', window: ' + windowWidth);

        }
        //debug('the clientWidth that sets it off: ' + copyLine1.clientWidth + ', ' + copyLine2.clientWidth + ', maxclientWidth: ' + Math.round(maxLineWidth) + ', fontsize: ' + oFontSize + ', window: ' + windowWidth);

        oFontSize--;


        //hbrandom change:  this fixes legibility on IE.  Fontsize=0 is a crazy random bug that kept coming up for a reason i can't fathom.  
        //debug('font size is: ' + oFontSize);
        if (oFontSize <= 16) {
            oFontSize = 17;
        }

        copyLine1.style.fontSize = oFontSize + 'px';
        copyLetter1.style.fontSize = (oFontSize * 2) + 'px';
        copyLine2.style.fontSize = oFontSize + 'px';
        copyLine2.style.top = '-' + (copyLine1.clientHeight / 5) + 'px';
        var newSmallBorder = origCopySmallBorder * (copyWidth / origCopyWidth);
        var newBigBorder = origCopyBigBorder * (copyWidth / origCopyWidth);
        copyLine1.style.marginLeft = newSmallBorder + 'px';
        copyLine2.style.marginLeft = newBigBorder + 'px';
        copyLine1.style.marginTop = (origTopMargin * (copyWidth / origCopyWidth)) + 'px';
        var copyAboutUs = document.getElementById('copyAboutUs');

        //alert(Iterations);

        copyAboutUs.style.width = (copyWidth - 2 * newBigBorder) + 'px';
        copyAboutUs.style.marginLeft = newBigBorder + 'px';

        var copyFontSize = oFontSize * (14 / 21);
        copyAboutUs.style.fontSize = copyFontSize + 'px';
        copyAboutUs.style.lineHeight = (copyFontSize * 1.7) + 'px';

        debug('font size is: ' + oFontSize);

        $('#homePageLoadingIndicator').css('display', 'none');
    }
}
function createsScrollBars(pWindowWidth) {
   //if (!$.browser.webkit) {
    var docHeight = IE ? document.documentElement.scrollHeight : $(document).height();
    //are there presently scroll bars?
    if ($(window).height() < docHeight) {
    //alert('presently, no scroll bars b/c ' + docHeight + ' >= ' + $(window).height());
        return false;
    }
    var staticElementsHeight = $('#header').height() +
                                $('#navigation').height() +
                                $('#footer').height() + 
                                2*TOPANDBOTTOMSPACER;
    var imagesHeight = getImagesHeight(pWindowWidth);
           //alert('checking for scroll bars.  window: ' + $(window).height() + ', static elements: ' + staticElementsHeight + ', imagesheight: ' + imagesHeight);
    if ($(window).height() < staticElementsHeight + imagesHeight) {
        //alert('creates scroll bars!' + ' window: ' + $(window).height() + ', stuff: ' + (imagesHeight) + 'pWindowWidth: ' + pWindowWidth);
        //debug('SCROLLBARS CREATED');
        return true;
    }
    else {
        return false;
    }
    //}
    //else {
    //    return false;
    //}
}
function getImagesHeight(pWindowWidth) {
    var oCopyWidth = pWindowWidth / (1 + RATIO);
    var oImagesWidth = pWindowWidth - oCopyWidth;
    var oSmallerImage = Math.floor((oImagesWidth - 3 * SPACER) / 3);
    var oImagesHeight = oSmallerImage * 3 + SPACER * 2 + 2 * TOPANDBOTTOMSPACER;
    //alert('getting Images height with ' + pWindowWidth + ' as the windowWidth: ' + oImagesHeight);
    return (oImagesHeight);
}

function setDivWidths(windowWidth) {
    copyWidth = windowWidth / (1 + RATIO);
    imagesWidth = windowWidth - copyWidth;
    //alert('window width: ' + windowWidth + ', copy: ' + copyWidth + ', images: ' + imagesWidth);
}
function debug(text) {
    var debugDiv = document.getElementById('debug');
    debugDiv.innerHTML += (text + '<br/>');
}
function debugClear() {
    var debugDiv = document.getElementById('debug');
    debugDiv.innerHTML = '';
}
