var num = 0;
var currentPhotoId = 0;

function nextPhoto()
{
    getCurrentPhotoId();
    currentPhotoId++;
    if ( currentPhotoId >= aryImages.length )
    {
        currentPhotoId = 0;
    }
    LoadPhoto(currentPhotoId);
    SetImageWidth(currentPhotoId);

    return 0;
}

function prevPhoto()
{
    getCurrentPhotoId();
    currentPhotoId--;
    if ( currentPhotoId < 0 )
    {
        currentPhotoId = aryImages.length - 1;
    }
    LoadPhoto(currentPhotoId);
    SetImageWidth(currentPhotoId);

    return 0;
}

function getCurrentPhotoId()
{
    var currentImage = document.images['imgPhoto'].src;
    debug(currentImage, 'currentImage');
    if ( currentImage )
    {
        for ( i=0; i<aryImages.length; i++ )
        {
            if ( aryImages[i] == currentImage )
            {
                currentPhotoId = i;
                break;
            }
        }
    }

    return 0;
}

function LoadPhoto(num)
{
    document.getElementById('imgPhoto').src = aryImages[num];
    document.getElementById('hrefPhoto').href = aryImages[num];
    window.scrollTo(0,0);

    return 0;
}

function SetImageWidth(num)
{
    img = new Image();
    img.src = aryImages[num];
	//alert(img.width);
    // if the image width will overstep the boundry, limit the width, but not the height
    if ( (0 + img.width) > 150 )
    {
        document.getElementById('imgPhoto').width = '150';
    }
    else
    {
        if ( (0 + img.width) > 0 )
        {
            document.getElementById('imgPhoto').width = img.width;
        }
    }

    // unborder any thumbnails
   /* for ( i=0; i<aryImages.length; i++ )
    {
       document.getElementById('thumbNailClip' + i).className = 'thumbNailClip';
    }

    document.getElementById('thumbNailClip' + num).className = 'thumbNailClipHightlighted';*/

     // if the no primary photo panel is on the page, hide it
     /*
    if (document.getElementById('pnlPrimaryPhotoAlt'))
    {
        document.getElementById('pnlPrimaryPhotoAlt').style.display = 'none';
    }
    */

    return 0;
}

function addMatchWord() {
	var matchword = document.getElementById('txtMatchWord').value.toLowerCase().replace(/ /g, '');

    // force input lower case, no space
    document.getElementById('txtMatchWord').value = matchword;

    if(matchword == '') return;

	MatchWebProj.profile.ShowProfile.AddMatchWord(profileIdHash , userIdHash, matchword, callback_addMatchWord);

}
function callback_addMatchWord(res)
{
    var matchword = document.getElementById('txtMatchWord').value;

    var retVallue = res.value.split(',');     //res format: returnValue + "," + tagID

    var oUL;
    var oNewNode;
    if (retVallue[0] == '1')   // 1=whitelist add to other list
    {

        oUL = document.getElementById('listOtherMatchWords');

        oNewNode = document.createElement('LI');

        oNewNode.innerHTML = '<a href=\"../matchwords/' + matchword +' id=\"word' +  matchword + '\" >' +  matchword + '</a>';

        oUL.appendChild(oNewNode);
    }
    else if (retVallue[0] == '0' ) // && retVallue[1] != '0' )    // 0,num=unreviewed, 0,0=invalid tag
    {
        document.getElementById('divPendingLable').style.display = '';

        oUL = document.getElementById('listPendingMatchWords');

        oNewNode = document.createElement('LI');
        oNewNode.innerHTML = '<span>' + matchword + '</span>';

        oUL.appendChild(oNewNode);
    }

    document.getElementById('txtMatchWord').value = '';
}

function addMatchWordToMyProfile(matchWord)
{    
    MatchWebProj.profile.ShowProfile.AddMatchWordToLoggedInUsersProfile(userIdHash, matchWord, callback_addMatchWordToMyProfile);
}
function callback_addMatchWordToMyProfile(res)
{    
    var retVallue = res.value.split(',');     //res format: returnValue + "," + tagID + "," + Tag

    switch (retVallue[0]) {
        case '1':
        case '3':
        case '255':
            document.getElementById('listWord' + retVallue[2]).className = "highlight";
            break;
        case '5':
            alert("You have added too many MatchWords to your profile.  Please remove some MatchWords from your profile before adding more.");
            break;
        default: 
            alert("Error::193879");
    }
    /*
    if (retVallue[0] == '1')   // 0 unreviewed, 1 whitelist, 2 blacklist, 3 personal, 255 user already has tag, 5 user has too many tags
    {
        document.getElementById('listWord' + retVallue[2]).className = "highlight";
        alert(retVallue[2] + " has been added to your profile");
    }
    */

}

function searchBlock()
{
    var btnString;
    btnString = document.getElementById('hlnkSearchBlockbottom').innerHTML;

    if (btnString)
    {
        if ( btnString.indexOf('Block') == 0 )
        {
            //if ( ProfileDisplayAjax.BlockFromSearching(profileHandle, userIdHash) == 0 )
            //{
            //    document.getElementById('hlnkSearchBlocktop').innerHTML = btnString.replace('Block', 'Unblock');
            //    document.getElementById('hlnkSearchBlockbottom').innerHTML = btnString.replace('Block', 'Unblock');
            //}
            //else
            //{
                gotoSearchBlockPage('block');
            //}
        }
        else
        {
            //if ( ProfileDisplayAjax.UnBlockFromSearching(profileHandle, userIdHash) == 0 )
            //{
            //    document.getElementById('hlnkSearchBlocktop').innerHTML = btnString.replace('Unblock', 'Block');
            //    document.getElementById('hlnkSearchBlockbottom').innerHTML = btnString.replace('Unblock', 'Block');
            //}
            //else
            //{
                gotoSearchBlockPage('unblock');
            //}
        }
    }
    return;

}

function gotoSearchBlockPage(context)
{
    if ( context == 'block' )
    {
        document.getElementById('hlnkSearchBlockbottom').href = removeThisProfile;
    }
    else
    {
        document.getElementById('hlnkSearchBlockbottom').href = undoRemoveThisProfile;
    }

    document.getElementById('hlnkSearchBlockbottom').click();
    return;
}


function blockContact()
{
    gotoBlockContactPage();

    var btnString;
    btnString = document.getElementById('hlnkContactBlockbottom').innerHTML;

    if (btnString)
    {
        if ( btnString.indexOf('Block') == 0 )
        {
            //if ( ProfileDisplayAjax.BlockFromContact( profileIdHash, userIdHash ) == 0 )
            //{
            //    document.getElementById('hlnkContactBlocktop').innerHTML = btnString.replace('Block', 'Unblock');
            //    document.getElementById('hlnkContactBlockbottom').innerHTML = btnString.replace('Block', 'Unblock');
            //}
            //else
            //{
                gotoBlockContactPage('block');
            //}
        }
        else
        {
            //if ( ProfileDisplayAjax.UnBlockFromContact( profileIdHash, userIdHash ) == 0 )
            //{
            //    document.getElementById('hlnkContactBlocktop').innerHTML = btnString.replace('Unblock', 'Block');
            //    document.getElementById('hlnkContactBlockbottom').innerHTML = btnString.replace('Unblock', 'Block');
            //}
            //else
            //{
                gotoBlockContactPage('unblock');
            //}
        }
    }
    return;

}

function gotoBlockContactPage(context)
{
    if ( context == 'block' )
    {
        document.getElementById('hlnkContactBlockbottom').href = contactBlock;
    }
    else
    {
        document.getElementById('hlnkContactBlockbottom').href = contactUnblock;
    }
    document.getElementById('hlnkContactBlockbottom').click();
    return;
}

function showMatchWordsHelp(show)
{
	document.getElementById('matchWordsHelp').style.display = (show) ? '' : 'none';
}

function openCompatWin(sec)
{
	myWin = window.open('../html/custom/template.blank.html', 'compatWin', 'width=715,height=550,status=0,titlebar=0,toolbar=0,scrollbars=1,resizable=1');
	myWin.name = 'compatWin';
	document.forms['matchMainForm'].target = 'compatWin';
	document.forms['matchMainForm'].action = compatWin + '&sec=' + sec;
	document.forms['matchMainForm'].submit();
}

function debug( test )
{
    if (!test) alert('test failed');
}
function debug( test, comment )
{
    if (!test) alert('test failed:: ' + comment);
}

