var sFadeInDur    = 2500;
var sFadeOutDur   = 1500;
var iIntvalDur    = 7000;

var aAllImages      = [];
var iCounter        = 0;


function cxcw_slideImagesPrepare(aImages1, aImages2, aImages3) {

    var iMaxLength = 0;

    if(aImages1.length > aImages2.length) {
        iMaxLength = aImages1.length;
    } else {
        iMaxLength = aImages2.length;
    }
    if(iMaxLength < aImages3.length) {
        iMaxLength = aImages3.length;
    }

    var i=0;

    var iLastIndex1 = 0;
    var iLastIndex2 = 0;
    var iLastIndex3 = 0;

    for (i=0;i<iMaxLength;i++) {

        aAllImages[i] = [];

        if(typeof aImages1[i] != 'undefined') {
            aAllImages[i][0] = i;
            iLastIndex1 = i;
        } else {
            aAllImages[i][0] = iLastIndex1;
        }
        if(typeof aImages2[i] != 'undefined') {
            aAllImages[i][1] = i;
            iLastIndex2 = i;
        } else {
            aAllImages[i][1] = iLastIndex2;
        }
        if(typeof aImages3[i] != 'undefined') {
            aAllImages[i][2] = i;
            iLastIndex3 = i;
        } else {
            aAllImages[i][2] = iLastIndex3;
        }
    }


    cxcw_slideImages(true);

    if(aAllImages.length > 1) {
        setInterval ( 'cxcw_slideImages(false)', iIntvalDur );
    }
}

function cxcw_slideImages(bIsFirst)
{
    if(typeof aAllImages[iCounter] == 'undefined') {
        iCounter = 0;
    }
    
    if(!bIsFirst) {
        $('.cxcw_headerImages img').fadeOut(sFadeOutDur);

        $('#cxcw_headerImg1 img').eq(aAllImages[iCounter][0]).fadeIn(sFadeInDur);
        $('#cxcw_headerImg2 img').eq(aAllImages[iCounter][1]).fadeIn(sFadeInDur);
        $('#cxcw_headerImg3 img').eq(aAllImages[iCounter][2]).fadeIn(sFadeInDur);

    } else {
        $('#cxcw_headerImg1 img').eq(aAllImages[iCounter][0]).fadeIn(1000);
        $('#cxcw_headerImg2 img').eq(aAllImages[iCounter][1]).fadeIn(1000);
        $('#cxcw_headerImg3 img').eq(aAllImages[iCounter][2]).fadeIn(1000);
    }

    iCounter++;
}

$(document).ready(function(){

    var aImages1 = [];
    var aImages2 = [];
    var aImages3 = [];

    $('#cxcw_headerImg1 img').each(function(index) {
        aImages1[index] = $('#cxcw_headerImg1 img').attr('src');

    });
    $('#cxcw_headerImg2 img').each(function(index) {
        aImages2[index] = $('#cxcw_headerImg1 img').attr('src');

    });
    $('#cxcw_headerImg3 img').each(function(index) {
        aImages3[index] = $('#cxcw_headerImg1 img').attr('src');

    });

    cxcw_slideImagesPrepare(aImages1, aImages2, aImages3);
});
