/**
 * Resizes an image to best fit it's parent element
 */
function resizeImageToParent(img) {

    if ((!img.width) ||(img.width() == 0)) {

            setTimeout( function() {
                resizeImageToParent(img);
            },50);
    
    } else {

        //var c = $(img).parent(); //parent element

        var c = $(img).closest("div"); //parent element

        var cHeight = $(c).css("height").replace("px","");
        var cWidth= $(c).css("width").replace("px","");
        var cAspect = cWidth/cHeight;
        var pAspect = img.width()/img.height();

        if (pAspect < cAspect) {
        
            $(img).css("height",(cHeight+'px'));
                                
        } else {
            var newHeight = (cWidth/img.width())*img.height();
            var newTopPad = (cHeight - newHeight)/2;
            $(img).css("margin-top",(newTopPad+'px'));
            $(img).css("width",(cWidth+'px'));
        }
        
    }
}


/**
 *  Homepage slideshow functions
 */

var REFRESH_TIME_MS = 15000;
var FADE_TIME_MS = 400;
        
breaker = false;
        

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);},
            function() { $(this).removeClass(c); }
        );
    });
};    
        
function loopItems(num) {

    var countSize = $('.pagerItem').length;
    // special case if only one item, do not loop;
    if (countSize < 2) {
            breaker = true;
    } else {
        if (breaker != true) {
            makeDisplay(num);
            var nextItem = ((num + 1) < countSize) ? num + 1 : 0;
            var nextFunction = "loopItems(" + nextItem + ")"; 
            setTimeout(nextFunction,REFRESH_TIME_MS);            
        }
    }
}

function breakAndShow(num, linky) {
    $('.pageNumberLink').css('color','#b6aa4e');
    $(linky).css('color','#58585a');            
    breaker = true;
    makeDisplay(num);
}        

function makeDisplay(num) {
    
    $('.pagerItem').fadeOut(100);
    var displayItem = "#pagerItem" + num;
    var headingItem = "#pagerHead" + num;
    $(displayItem).fadeIn(FADE_TIME_MS);
}

function resizeBodyElement() {
  //  var h = $(window).height();
  //  var w = $(window).width();
  //  $("#elementToResize").css('height',(h < 1024 || w < 768) ? 500 : 400);
}



