﻿var middleHeight = 140;
var Timer;
var timeInterval = 10;
var movePixelStep = 10;
function Initial()
{
    BuildImgTest();
    GetAlbums();
}
function BuildImgTest()
{
    imgTest = document.createElement('img');
    imgTest.style.position = 'absolute';
    imgTest.style.left = '-9999px';
    imgTest.style.top = '-9999px';
    document.body.insertBefore(imgTest,document.body.firstChild);
}
function GetAlbums()
{    
    Clozone.Web.WebService.PhotoAlbum.GetAlbumsAndThumbnails(prefix, GetAlbumsSuccess);
}
function GetAlbumsSuccess(result)
{
    if(result == null || result.length != 2)
    {
        return;
    }
    albums = result[0];
    albumThumbnails = result[1];
    if(albums == null || albums.length == 0)
    {
        return;
    }
    BuildAlbums();
    if(albumIDToShow != 0)
    {
        CheckPrivilege();
    }
    ChangeAlbumDefault();
}
function BuildAlbums()
{
    albumHTML = '<div style="width:200px;">';
    for(var i = 0; i < albums.length; i++)
    {
        albumHTML += '<div onmouseover="SetBgColor(this)" onmouseout="RemoveBgColor(this)" onclick="ChangeSelectedAlbum(' + albums[i].AlbumID + ',\'' + albums[i].Title.toString() + '\')" style="cursor:pointer;">';
        albumHTML += '<table>';
        albumHTML += '<tr>';
        albumHTML += '<td rowspan="2"><img src="http://www.clozone.com/' + albumThumbnails[i] + '" style="width:60px;height:45px;"/>';
        albumHTML += '</td>';
        albumHTML += '<td style="font-weight:bold;">' + albums[i].Title + '(' + albums[i].CountPhoto + ')';
        albumHTML += '</td>';
        albumHTML += '</tr>';
        albumHTML += '<tr>';
        albumHTML += '<td>';
        albumHTML += '<div title="' + albums[i].Description + '">' + GetString(albums[i].Description,10) + '</div>';
        albumHTML += '</td>';
        albumHTML += '</tr>';
        albumHTML += '</table>';
        albumHTML += '</div>';
    }
    albumHTML += '</div>';
}
function CheckPrivilege()
{
    hasPrivilege = false;
    for(var i = 0; i < albums.length; i++)
    {
        if(albums[i].AlbumID == albumIDToShow)
        {
            hasPrivilege = true;
            break;
        }
    }
    if(!hasPrivilege)
    {
        albumIDToShow = albums[0].AlbumID;
        ShowResultBox('没有权限浏览该相册，系统将自动为您加载其他相册',false);
    }
}
function ToggleAlbum(control)
{
    var left = GetOffsetLeft(control);             
    var top = GetOffsetTop(control) + control.offsetHeight;
    TogglePopupMenu(albumHTML,left,top,200,0,true);
}
function ChangeAlbumDefault()
{
    if(albumIDToShow == 0)
    {
        albumIDToShow = albums[0].AlbumID;
        albumNameToShow = albums[0].Title;
    }
    else
    {
        for(var i = 0; i < albums.length; i++)
        {
            if(albums[i].AlbumID == albumIDToShow)
            {
                albumNameToShow = albums[i].Title;
            }
        }
    }
    ChangeSelectedAlbum(albumIDToShow,albumNameToShow);
}
function ChangeSelectedAlbum(albumID, albumName)
{
    $('spanAlbum').innerHTML = albumName;
    for(var i = 0; i < albums.length; i++)
    {
        if(albums[i].AlbumID == albumID)
        {
            $('imgAlbumThumbnail').src = 'http://www.clozone.com/' + albumThumbnails[i];
            $('divAlbumDescription').innerHTML = GetString(albums[i].Description,68);
            $('divAlbumDescription').title = albums[i].Description;
        }
    }
    ChangeAlbum(albumID);
}
function ChangeAlbum(albumID)
{
    $('divPhotoSlide').style.top = middleHeight + 'px';
    $('divPhotoSlide').innerHTML = "正在加载相册图片...";
    Clozone.Web.WebService.PhotoAlbum.GetPhotosAndTagsInAlbum(albumID, GetPhotosSuccess);
}
function GetPhotosSuccess(result)
{
    photos = result;
    if(photos == null)
    {
        ShowResultBox('获取图片时有错误发生',false);
        return;
    }
    BuildPhotoList();
    ShowPhoto();
}
function BuildPhotoList()
{
    $('divPhotoSlide').innerHTML = '';
    for(var i = 0 ; i < photos.length ; i ++)
    {
        if(i % 3 == 0)
        {
            var divPhotoRow = document.createElement('div');
            divPhotoRow.style.height = '70px';
            $('divPhotoSlide').appendChild(divPhotoRow);
        }
	    var photo = document.createElement("img");
	    photo.id = "photo" + i;
	    photo.src = 'http://www.clozone.com/' + photos[i].Path.replace("UserFile","UserThumbnail");
	    photo.title = photos[i].Name;
	    photo.className = 'imgThumbnail';
	    photo.onclick = PhotoClick;
	    
	    if(i == 0)
	    {
	        photo.className = 'imgThumbnailSelected';
            photoSelected = photo;
	    }
	    
	    var divPhoto = document.createElement("div");
	    divPhoto.style.height = "70px";
	    divPhoto.style.width = "90px";
	    SetFloatLeft(divPhoto);
	    divPhoto.appendChild(photo);
	    
	    divPhotoRow.appendChild(divPhoto);
    }
}
function ShowPhoto()
{
    if(photos.length > 0)
    {
        if(photoIDToShow != 0)
        {
            for(var i = 0; i < photos.length; i ++)
            {
                if(photos[i].PhotoID == photoIDToShow)
                {
                    photoIndex = i;
                    break;
                }
            }
            if(i == photos.length)
            {
                //有权限提示，无权限不重复提示
                if(hasPrivilege)
                {
                    ShowResultBox("您浏览的图片不存在",false);
                }
                photoIndex = 0;
            }
            else
            {
                var photo = $("photo" + photoIndex);
                var divPhoto = photo.parentNode;
                if(photoSelected != null)
                {
                    photoSelected.className = 'imgThumbnail';
                }
                photo.className = 'imgThumbnailSelected';
                photoSelected = photo;
                $('divPhotoSlide').style.top = middleHeight - Math.floor(photoIndex / 3) * 70 + 'px';
            }
            photoIDToShow = 0;
        }
        else
        {
            photoIndex = 0;
        }
        ChangePhoto();        
    }
    else
    {
        $('imgPhoto').src = "http://www.clozone.com/images/BlankPhotoBig.jpg";
        divPhotoDetail.innerHTML = '';
        ClearComments();
    }
}
function PhotoClick(e)
{
    if(document.all)
    {
        e = window.event;
    }
    var photo = e.srcElement;
    photoIndex = photo.id.substring(5,photo.id.length);
    var divPhoto = photo.parentNode;
    if(photoSelected != null)
    {
        photoSelected.className = 'imgThumbnail';
    }
    photo.className = 'imgThumbnailSelected';
    photoSelected = photo;
    topExpect = middleHeight - Math.floor(photoIndex / 3) * 70;
    MovePhotoSlideTopTo(topExpect);
	ChangePhoto();	
}
function ChangePhoto()
{    
    imgTest.onload = TestPhotoDone;
    imgTest.src = 'http://www.clozone.com/' + photos[photoIndex].Path;
    var html = '';
    html += '<div style="text-align:right;"><a href="http://www.clozone.com" onclick="SavePhoto(this,' + photos[photoIndex].PhotoID + ');return false;">收藏到我的相册</a>';
    if(photos[photoIndex].HasOriginal)
    {
        var srcOriginal = imgTest.src.replace('UserFile','UserOriginal');
        html += '&nbsp;&nbsp;<a href=' + srcOriginal + ' target="_blank">查看原图</a>';
    }
    html += '</div>';
    html += '<span class="title">名称：' + photos[photoIndex].Name + '</span>';
    html += '<div>标签：';
    for(var i = 0; i < photos[photoIndex].Tags.length; i++)
    {
        html += '<a href="http://tag.clozone.com/photo/' + photos[photoIndex].Tags[i] + '.html" target="_blank" style="margin-right:5px">' + photos[photoIndex].Tags[i] + '</a>';
    }
    html += '</div>';
    html += '<div>描述：' + photos[photoIndex].Description + '</div>';
    var url = 'http://' + prefix + '.clozone.com/photo/' + photos[photoIndex].PhotoID + '.html';
    html += '<div>地址：<input type="text" class="txt4" style="width:300px;" value="' + url + '" />　　<a href="http://www.clozone.com" onclick="CopyUrl(this);return false;">复制地址</a></div>';
    $('divPhotoDetail').innerHTML = html;
    GetComments(photos[photoIndex].PhotoID, 9);
    Clozone.Web.WebService.PhotoAlbum.AddCountVisit(photos[photoIndex].PhotoID);
}
function CopyUrl(control)
{
    var oInputs = control.parentNode.getElementsByTagName('INPUT');
    CopyToClipboard(oInputs[0]);
    ShowResultBox('已经复制到剪贴板！', true);
}
function TestPhotoDone()
{
    imgTest.onload = null;//Fix IE gif onload bug
    $('imgPhoto').onload = LoadPhotoDone;
    $('imgPhoto').src = imgTest.src;
}
function LoadPhotoDone()
{
    var imgWidth = imgTest.offsetWidth;
    var imgHeight = imgTest.offsetHeight;
    var divWidth = $('divImage').offsetWidth;
    var divHeight = $('divImage').offsetHeight;    
    if(imgWidth <= divWidth && imgHeight <= divHeight)
    {
        $('imgPhoto').style.height = imgHeight + 'px';
        $('imgPhoto').style.width = imgWidth + 'px';
        $('imgPhoto').style.left = Math.floor((divWidth - imgWidth) / 2) + 'px';
        $('imgPhoto').style.top = Math.floor((divHeight - imgHeight) /2) + 'px';
    }
    else if(imgHeight/imgWidth >= divHeight/divWidth)
    {
        $('imgPhoto').style.height = divHeight + 'px';
        $('imgPhoto').style.width = Math.floor(divHeight * imgWidth / imgHeight) + 'px';
        $('imgPhoto').style.left = Math.floor((divWidth - divHeight * imgWidth / imgHeight) / 2) + 'px';
        $('imgPhoto').style.top = '0px';
    }
    else
    {
        $('imgPhoto').style.width = divWidth + 'px';
        $('imgPhoto').style.height = Math.floor(divWidth * imgHeight /imgWidth) + 'px';
        $('imgPhoto').style.left = '0px';
        $('imgPhoto').style.top = Math.floor((divHeight - divWidth * imgHeight /imgWidth) / 2) + 'px';
    }
}
function MovePhotoSlideTopTo(top)
{
    topExpect = top;
    var topNow = $('divPhotoSlide').offsetTop;
    if(topExpect >= topNow)
	{
	    clearInterval(Timer);
	    Timer = setInterval("MoveDown()",timeInterval);
	}
	else
	{
	    clearInterval(Timer);
		Timer = setInterval("MoveUp()",timeInterval);
	}
}
function MoveUp()
{
    if($('divPhotoSlide').offsetTop <= topExpect)
    {
        clearInterval(Timer);
        return;
    }
    $('divPhotoSlide').style.top = $('divPhotoSlide').offsetTop - movePixelStep + "px";
}
function MoveDown()
{
    if($('divPhotoSlide').offsetTop >= topExpect)
    {
        clearInterval(Timer);
        return;
    }
    $('divPhotoSlide').style.top = $('divPhotoSlide').offsetTop + movePixelStep + "px";
}
function MovePhotos(e)
{
    if(document.all)
    {
        e = event;
    }
    else
    {
        e.wheelDelta = - e.detail * 50;
    }
    if(($('divPhotoSlide').offsetTop > $('divPhotos').offsetHeight && e.wheelDelta > 0) || ($('divPhotoSlide').offsetTop + $('divPhotoSlide').offsetHeight < 0 && e.wheelDelta < 0))
    {
        return;
    }
    $('divPhotoSlide').style.top = $('divPhotoSlide').offsetTop + (Math.floor(e.wheelDelta / 100) * 10) + "px";
    CancelEvent(e);
}
function Up()
{
    var newTop = $('divPhotoSlide').offsetTop - $('divPhotos').offsetHeight;
    if(newTop + $('divPhotoSlide').offsetHeight < 0)
    {
        newTop = - $('divPhotoSlide').offsetHeight;
    }
    topExpect = newTop;
    clearInterval(Timer);
    Timer = setInterval("MoveUp()",timeInterval);
}
function Down()
{
    var newTop = $('divPhotoSlide').offsetTop + $('divPhotos').offsetHeight;
    if(newTop > $('divPhotos').offsetHeight)
    {
        newTop = $('divPhotos').offsetHeight;
    }
    topExpect = newTop;
    clearInterval(Timer);
    Timer = setInterval("MoveDown()",timeInterval);
}
function SavePhotoContext(control,photoID)
{
    this.Control = control;
    this.PhotoID = photoID;
}
SavePhotoContext.prototype.GetAllAlbumsSuccess = function(result)
{
    HideProcess();
    if(result == null)
    {
        ShowResultBox('获取相册信息时出错，请重试',false);
    }    
    userAlbums = result;
    ShowSelectAlbumToSave(this.Control,this.PhotoID);
}
function SavePhoto(control,photoID)
{
    if(!isAuthenticated)
    {
        ToggleLoginBox();
        return;
    }
    if(!window.userAlbums)
    {        
        var context = new SavePhotoContext(control,photoID);
        Clozone.Web.WebService.PhotoAlbum.GetAlbums(null,Function.createDelegate(context,context.GetAllAlbumsSuccess));
        ShowProcess('正在获取您的相册信息，请稍候...');
        return;
    }
    ShowSelectAlbumToSave(control,photoID);
}
var _control = null;
function ShowSelectAlbumToSave(control,photoID)
{
    var html = '<div style="padding:10px;width:155px;">';
    html += '收藏到：<select id="selectAlbumToSave" style="width:100px">';
    for(var i = 0; i < userAlbums.length; i++)
    {
        html += '<option value=' + userAlbums[i].AlbumID + '>' + userAlbums[i].Title + '</option>';
    }
    html += '</select>';
    html += '<br /><br />';
    html += '<input type="button" class="btnCommon2" style="float:right;margin-top:5px;" value="取消" onclick="HideSelectAlbumToSave()" />';
    html += '<input type="button" class="btnCommon2" style="float:right;margin-top:5px;" value="确定" onclick="DoSavePhoto(' + photoID + ')" />';
    html += '<div style="clear:both"></div>';
    html += '</div>';
    if(!window.albumSelectionBox)
    {
        albumSelectionBox = new CommonBox(html,'选择相册',0,0,180,100,false,true,null);
    }
    albumSelectionBox.SetContent(html);
    albumSelectionBox.Show();
    _control = control;
}
function DoSavePhoto(photoID)
{
    var albumID = parseInt($('selectAlbumToSave').value);
    HideSelectAlbumToSave();
    Clozone.Web.WebService.PhotoAlbum.SavePhoto(albumID,photoID,SavePhotoSuccess);
    ShowProcess('正在收藏图片，请稍候...');
}
function SavePhotoSuccess(result)
{
    HideProcess();
    if(result == null)
    {
        ShowResultBox('有错误发生，请重试',false);
    }
    else if(result != "")
    {
        ShowResultBox(result,false);
    }
    else
    {
        _control.style.visibility= 'hidden';
    }
}
function HideSelectAlbumToSave()
{
    albumSelectionBox.Hide();
}
function ToggleAddPhotoBox()
{
    var width = 400;
    var height = 370;
    var src = '/AddPhoto.aspx';
    if(window.isGroup)
    {
        src = '/AddPhoto_Group.aspx';
        height = 400;
    }
    var html = '<iframe src="' + src + '" frameborder="0" width="' + width + 'px" height="' + height + 'px" scrolling="no" ></iframe>';
    boxAddPhoto = new CommonBox(html,'添加图片',0,0,width,0,false,true,null);
    boxAddPhoto.Toggle();
}