function imageResize() {
    // get active image
    var activeImage = $('#stage > img.active,#stage > img.inactive');
    
    // calculate size and position
    var dimensions = imageCalcSize($(activeImage).width(), $(activeImage).height());
    var newWidth = dimensions[0];
    var newHeight = dimensions[1];
    var newBottom = dimensions[2];
    var newLeft = dimensions[3];
    
    // set new width / height
    $(activeImage).css('width', newWidth + 'px');
    $(activeImage).css('height', newHeight + 'px');
    
    return $(activeImage).addClass('scaled');
}
function imageCalcSize(imageWidth, imageHeight) {
    // get browser dimensions
    var browserWidth = $(window).width()-100;
    var browserHeight = $(window).height();
    
    // calculate ratio
    var ratio = imageHeight / imageWidth;
    
    // calculate new size
    var newWidth = 0;
    var newHeight = 0;
    
    if ((browserHeight / browserWidth) > ratio ) {
        newHeight = browserHeight;
        newWidth = Math.round(browserHeight / ratio);
    } else {
        newHeight = Math.round(browserWidth * ratio);
        newWidth = browserWidth;
    }
    
    // calculate new top and left position
    var newBottom = "0";
    var newLeft = Math.round((browserWidth - newWidth) / 2);
    
    var rcarr = [newWidth, newHeight, newBottom, newLeft];
    return rcarr;
}
function changeImage(selected) {
    var activeImg = $('#stage > img.active');
    
    $('.inactive').css({
        "left":$(window).width()+"px"
    });
    
    var newActive = $(selected).attr("id");
    var newLink = $(selected).attr("rel");
    
    var newActiveImg = newActive.replace("img","imgbig");
    var ActiveText = newActive.replace("img","text");
    $('.contenttext, .contenttext span').hide();
    
    $('.contentimage').fadeOut('fast',function() {
        $(this).addClass("contentimagehide");
        $('.contenttext').fadeIn('fast');
    });
    
    $('.contenttext .'+ActiveText).css({display:"block"}).find('a').attr("href",newLink);
    $('#'+newActiveImg).css({
        "z-index": "10"
    }).removeClass("inactive").addClass("active").click(function() {
        location.href= newLink;
    });
                
    $(activeImg).css({"z-index": "1"});
    if($(activeImg).attr("id") != newActiveImg) {
    
    $('#'+newActiveImg).animate({
        left: "0px"
    },500,
    function() {
        $(activeImg).css({
            left: $(window).width()+"px"
        }).removeClass("active").addClass("inactive");
    });
    
    }
    
}
function checkInnerHeight() {
    var browserHeight = $(window).height();
    var headerHeight = $('.headerchain').height();
    var footerHeight = $('.footerchain').height();
    var displayHeight = browserHeight - headerHeight - footerHeight;
    
    var contentImgHeight = 0;
    $('.maskimg').each(function() {
        contentImgHeight += $(this).height();
    });
    var contentImgWidth = $('.contentimage').width();    
    
    var p = displayHeight*100/contentImgHeight;
    
    var newContentImgHeight = Math.round(contentImgHeight*p/100);
    var newContentImgWidth = Math.round(contentImgWidth*p/100);
    
    if(displayHeight <= contentImgHeight) {
        $('.contentimage').css({
            width: newContentImgWidth+"px"
        });
        $('.maskholder').css({
            width: (newContentImgWidth+50)+"px"
        });
        
    } else {
        $('.contentimage').removeAttr("style");
    }
}
$(window).resize(function () {
    imageResize();
    checkInnerHeight();
});
$(window).load(function () {
    
    imageResize();
    checkInnerHeight();

    if($('.slider').length > 0) {

        $('.maskimg').each(function() {
            sTarget = $(this).attr("href");
            $(this).attr("rel",sTarget).attr("href","javascript:void(0);");
        }).
        click(function() {
            changeImage(this);
        });
    }

    $('.startimg').css({
        left: $(window).width()+"px"
    });
    
    $('.startimg:first').css({
        left: "0px",
        display: "none"
    }).fadeIn("fast");
    
    $('.contenttext img').click(function() {
        
        $('.contenttext').fadeOut('fast',function() {
            $('.contentimage').removeClass("contentimagehide").fadeIn('fast');
        });
    });
});
