/**
 * @fileOverview  Js for home page
 * 
 */
var photoArr = [];
$(document).ready(function() {
    Whl.UA.initData();
    // Create Alm Object 
    new Whl.UA.Alm(alm.almUrl, alm.allotment, alm.childPolicy);

    $('#uxPhysicalRating').click(function(){
        arguments[0].preventDefault();
        window.open(this.href, 'caf', 'width=400,height=600,toolbar=1,status=1,scrollbars=1,location=1');
        return false;
    });
    $('#uxCultureRating').click(function(){
        arguments[0].preventDefault();
        window.open(this.href, 'caf', 'width=400,height=600,toolbar=1,status=1,scrollbars=1,location=1');
        return false;
    });
    $('#uxAdventureRating').click(function(){
        arguments[0].preventDefault();
        window.open(this.href, 'caf', 'width=400,height=600,toolbar=1,status=1,scrollbars=1,location=1');
        return false;
    });
    
    $('#dlgPhoto').dialog({
        bgiframe: false,
        autoOpen: false,
        height: 'auto',
        width: 500,
        modal: true,
        buttons: {
            Close: function(){
                $(this).dialog('close');
            }
        }
    });
    $('img[id^="img-"]').each(function(){
        $(this).css('cursor','pointer').click(ShowPhoto.bindEvent(null,this.id));
        photoArr.push(this.id);
    });
    $('div.photo-detail').hide();
    $('#cancel-policy-dlg').dialog({
        bgiframe: false,
        autoOpen: false,
        height: 'auto',
        width: 500,
        modal: false,
        buttons: {
            Close: function(){
                $(this).dialog('close');
            }
        }
    });
    $('#cancel-policy-lnk').click(function(){
        arguments[0].preventDefault();
        $('#cancel-policy-dlg').dialog('open');
        return false;
    });
    PhotoPaging();
});
function ShowPhoto(imgId){
    if(imgId){
        $('div.photo-detail').hide();
        $('#'+imgId+'-full').show();
        $('#dlgPhoto').dialog('close').dialog('option','height','auto').dialog('open');
        $('#photoNav_' + imgId + '_current' ).focus();
    }
    arguments[1].preventDefault();
}
function PhotoPaging(){
    var iTotalPage = photoArr.length;
    for(var iPage=0; iPage<iTotalPage; iPage++){
        var paging = $('#' + photoArr[iPage] + '-paging').empty();
        first = iPage-2;
        if( first<0 ){
            first = 0;
        }
        last = first+4;
        if( last>=iTotalPage ){
            last = iTotalPage-1;
            first = last - 4;
            if( first<0 ){
                first = 0;
            }
        }
        prev = iPage>0 ? iPage-1 : 0;
        next = iPage<(iTotalPage-1) ? iPage+1 : iTotalPage-1;
        paging.append($('<a id="prevBtn_' + photoArr[iPage] + '" href="#">&nbsp;Previous&nbsp;</a>'));
        $('#prevBtn_' + photoArr[iPage]).click(ShowPhoto.bindEvent(null,photoArr[prev]));
        for(p=first; p<=last; p++){
            if( iPage==p ){
                paging.append($('<a id="photoNav_' + photoArr[iPage] + '_current" href="#" style="color:red">&nbsp;<b>' + (p+1) + '</b>&nbsp;</a>'));
                $('#photoNav_' + photoArr[iPage] + '_current' ).click(ShowPhoto.bindEvent(null,photoArr[p]));
            }else{
                paging.append($('<a id="photoNav_' + photoArr[iPage] + '_' + p + '" href="#">&nbsp;' + (p+1) + '&nbsp;</a>'));
                $('#photoNav_' + photoArr[iPage] + '_' + p ).click(ShowPhoto.bindEvent(null,photoArr[p]));
            }
        }
        paging.append($('<a id="nextBtn_' + photoArr[iPage] + '" href="#">&nbsp;Next&nbsp;</a>'));
        $('#nextBtn_' + photoArr[iPage]).click(ShowPhoto.bindEvent(null,photoArr[next]));
    }
}

