/**
 * @fileoverview collaborate 스크립트
 * @author n017213@netville.co.kr
 * @version 1.0.0
 * @since 2009.06.11
 */

/*
 * 이벤트 경로 전역변수 선언 
 */
var g_blogId;

/**
 * 플래그 전역변수 선언 
 */
var g_check=false;

/**
 * 이벤트 메인 경로 설정
 */
function collaborateUrl(blogId){
    
    if(g_check ==false){
        location.href = getTeamBlogUrl(blogId, 0, COLLABORATE_LEVEL);
        
    }else{
        location.href = getTeamBlogUrl(g_blogId, 0, COLLABORATE_LEVEL);
    }

}

/**
 * 현재 진행 중인 collaborate HTML을 화면에 보여준다
 */
function makeCollaborateInfoHTML(idx){
    
    var size = nv.$("size" + idx) ? nv.$("size" + idx).value : 0;
    var blogId = nv.$("blogId" + idx).value;
    var title = nv.$("title" + idx).value;
    var startDate = nv.$("startDate" + idx).value;
    var endDate = nv.$("endDate" + idx).value;
    var memberCount = nv.$("memberCount" + idx).value;
    var logCount = nv.$("logCount" + idx).value;
    var description = nv.$("description" + idx).value;
    var collaborateNumber = nv.$("collaborateNumber_"+idx);
    //선택 전에 class 명을 지운다
    for(var i=0; i< size; i++){
        nv.$("collaborateNumber_"+i).className = "";
    }

    collaborateNumber.className = "current";

    var collaborateInfoDiv = nv.$("collaborateInfoDiv");
    
    var htmlString = "";
    htmlString += "<div><h3>"
    htmlString += "    <a href=\""+ WEB_SERVER + getTeamBlogUrl(blogId, 0, COLLABORATE_LEVEL) +"\">"; 
    htmlString += title; 
    htmlString += "</a>";
    htmlString += "</h3></div>";
    htmlString += "<p class=\"summary\">";    
    htmlString += "    기간 <strong>| " + startDate + " ~ " + endDate + "</strong>";    
    htmlString += "    &nbsp;&nbsp;&nbsp;&nbsp;";
    if(memberCount > 0) {
        htmlString += "    참여자수 <strong>| " + memberCount + "</strong>";
        
    }
    htmlString += "    &nbsp;&nbsp;&nbsp;&nbsp;";
    if(logCount > 0) {
        htmlString += "    업로드된 사진 <strong>| " + logCount + "</strong>";
    }
    htmlString += "</p>";
    htmlString += "<p class=\"text\">";
    htmlString += description;
    htmlString += "</p>";
    collaborateInfoDiv.innerHTML = htmlString;
    
    //bestphoto의 background 이미지로 들어간 로고 이미지를 변경 한다
    var currentCollaborateMainImgDiv = nv.$("currentCollaborateMainImgDiv");

    currentCollaborateMainImgDiv.style.backgroundImage = "url("+ LOGO_DOWNLOAD_URL+ "?blogId=" + blogId+ "&imageType="+ORIGINAL_LOGO_IMAGE+")";
    
    g_blogId =  blogId;
    g_check =true;
    BlogDWR.getLogListByBlogId(blogId, makeInitBlogLogHTML); 
}

/**
 * 현재 진행 중인 collaborate HTML을 화면에 보여준 후 해당 collaborate의 photo를 화면에 보여준다
 */
function makeInitBlogLogHTML(data){

    var collaboratePhotoDiv = nv.$("collaboratePhotoDiv");
    var htmlString = "";
    for(i=0; i<data.length; i++) {

        htmlString += "<li>";
        htmlString += "<a href=\""+ WEB_SERVER + getTeamBlogUrl(data[i].blogId, data[i].logId, COLLABORATE_LEVEL) +"\" class = \"disp_block\">";       
        
        
        //pick 일 경우
        if(data[i].bestYn == PICK_LOG){        
        htmlString += "    <img class=\"best\" src=\""+ IMAGE_SERVER +"/img/community/collaborate/icon_pick.gif\" alt=\"\" width=\"40\" height=\"40\" />";
        }
        htmlString += "    <img src=\""+IMAGE_DOWNLOAD_URL+"?attachmentId="+data[i].attachmentId+"&type="+THUMBNAIL_IMAGE_PHOTO_1+"\" width=\"75\" height=\"75\" alt=\"\"></img>"        
        htmlString += "</a>";
        htmlString += "<p>";
        htmlString += "    <a href=\""+ WEB_SERVER + getTeamBlogUrl(data[i].blogId, data[i].logId, COLLABORATE_LEVEL) +"\">"; 
        htmlString +=           nv.Str.cutString(data[i].title,10,"..."); 
        htmlString += "    </a>"
        htmlString += "    <span class=\"hint2\">[" + data[i].commentCount + "]</span>";
        //newContents 이미지 삽입될 span
        htmlString += "    <span id=\"newContentImg_"+i+"\">";
        htmlString += "    </span><br/>";
        htmlString += "    <a href=\"#\" onclick=\"javascript:showProfile('right', '" + data[i].registerId + "',event); return false;\" ><span class=\"name\">"+ data[i].registerName +"</span></a><br />";
//        htmlString += "    <span>"+data[i].locationAdd+"</span>"
        htmlString += "</p>";
        htmlString += "</li>";
    }
//    alert(htmlString);
    collaboratePhotoDiv.innerHTML = htmlString;

    //registerDate와 현재일 자를 비교하여 contents 에 new 이미지를 그린다
    for(i=0; i<data.length; i++) {
        //데이터 포멧 dwr 호출
        DateDWR.getMakeDateWithIndex(i, data[i].registerDate, "MMM dd, yyyy HH:ss" , getMakeDateAndWriteNewImgCallback); 
    }    

    
}   

/**
 * DateDWR.getMakeDateWithIndex의 콜백 함수 로서 리턴 값을 해당 날짜를 비교 하여 new 이미지를 화면에 보여준다
 */
function getMakeDateAndWriteNewImgCallback(data){
    if(data.dataSapce <= IS_NEW_PERIOD){        
        nv.$("newContentImg_"+data.index).innerHTML =  "<img src=\"" + IMAGE_SERVER + "/img/community/collaborate/icon_new.gif\" alt=\"\" width=\"12\" height=\"11\" />";
    }
}


/**
 * 로그인 유저가 참여하고 있는 collaborate 의 photo를 조회한다
 */
function makeMyPhotoInfoHTML(blogId, attachmentId){
    var myUploadPhoto = document.getElementById("myUploadPhoto");
    
    //포토 변경
    myUploadPhoto.src = IMAGE_DOWNLOAD_URL+"?attachmentId="+attachmentId;
    BlogDWR.getTeamBlogInfo(blogId, makeBlogInfoHTML); 

}

/**
 * photo 정보 변경 후 해당 photo가 업로드된 collaborate 정보를 화면에 보여준다
 */     
function makeBlogInfoHTML(data){
    //변경된 포토가 업로드된 collaborate 정보 변경
    var myCollaborateInfo = document.getElementById("myCollaborateInfo");
    var htmlString = "";
    htmlString += "participants : "+data.memberCount;
    htmlString += "you joined on : "+data.memberJoinDate;
    htmlString += "opened by : "+data.startDate;
    myCollaborateInfo.innerHTML = htmlString;
}

/**
 * 로그인 유저가 참여하고 있는 collaborate 의 photo를 모두 조회한다
 */
function makeMoreMyPhotoListHTML(){
    BlogDWR.getMyCollaborateList(makeListHTML);
}

/**
 * makeMoreMyPhotoListHTML에서 조회한 photo 목록을 div 페이지로 만든다
 */
function makeListHTML(data){
    var collaborateList = nv.$("collaborateList");

    var htmlString = "";
    htmlString += "<div class=\"head\">";
    
    htmlString += "    <h4 title=\"collaborate list\"><img src=\""+IMAGE_SERVER+"/img/community/collaborate/tit_collaboratelist.gif\" alt=\"collaborate list\" width=\"104\" height=\"12\" /></h4>";
    htmlString += "    <a href=\"javascript:divDisplayNone('collaborateList');\" title=\"close\"><img src=\""+IMAGE_SERVER+"/img/community/challenge/btn_morecontentclose.gif\" class=\"closePopup\" alt=\"close popup\" width=\"9\" height=\"9\" /></a>";
    
    htmlString += "</div>";    
    htmlString += "<div class=\"body\">";
    htmlString += "	<div class=\"scrollWrap\">";
    htmlString += "    <ul class=\"clearfix\">";
    for(i=0; i<data.length; i++) {
        //loop
        htmlString += "    <li>";
        htmlString += "        <p class=\"img\">";
        htmlString += "            <a href=\""+ WEB_SERVER + getTeamBlogUrl(data[i].blogId, data[i].logId, COLLABORATE_LEVEL) +"\"><img class=\"type1Img\" src=\""+LOGO_DOWNLOAD_URL+"?blogId="+data[i].blogId+ "&imageType="+ORIGINAL_LOGO_IMAGE+"\" alt=\"\" width=\"78\" height=\"38\" /></a>";
        htmlString += "        </p";
        htmlString += "        <p class=\"txtHead\">";
        htmlString += "            <a href=\""+ WEB_SERVER + getTeamBlogUrl(data[i].blogId, 0, COLLABORATE_LEVEL) +"\">"+ data[i].title +"</a>";
        htmlString += "        </p>";
        htmlString += "        <p class=\"txtInfo\">";
        
        htmlString += "Participants";
        htmlString += "<img src=\""+IMAGE_SERVER+"/img/community/challenge/bullet_division.gif\" alt=\"\" width=\"1\" height=\"8\" />";
        htmlString += "<strong>"+data[i].memberCount+"</strong><br />";
        
        htmlString += "You joined on";
        htmlString += "<img src=\""+IMAGE_SERVER+"/img/community/challenge/bullet_division.gif\" alt=\"\" width=\"1\" height=\"8\" />";
        htmlString += "<strong>"
        htmlString += "    <span id=\"joinDateSpan_"+i+"\"></span>";
        htmlString += "</strong><br />";
        
        htmlString += "Opened till";
        htmlString += "<img src=\""+IMAGE_SERVER+"/img/community/challenge/bullet_division.gif\" alt=\"\" width=\"1\" height=\"8\" />";
        htmlString += "<strong>"
        htmlString += "    <span id=\"startDateSpan_"+i+"\"></span>";
        htmlString += "</strong><br />";
        
        htmlString += "        </p>";        
        htmlString += "    </li>";

    }
    htmlString += "    </ul>";
    htmlString += "  </div>"; 
    htmlString += "</div>"; 
    
    collaborateList.innerHTML = htmlString;  

    for(i=0; i<data.length; i++) {
        //memberJoinDate 와 startDate를 날짜 포멧에 마춰 화면에 보여준다
        DateDWR.getMakeDateWithIndex(i, data[i].memberJoinDate, "yyyy.MM.dd" , getMakeJoinDateCallback); 
		DateDWR.getMakeDateWithIndex(i, data[i].startDate, "yyyy.MM.dd" , getMakeStartDateCallback); 	
    }			
}

/**
 * DateDWR.getMakeDateWithIndex의 콜백 함수 (memberJoinoinDate 셋팅)
 */
function getMakeJoinDateCallback(data){
    nv.$("joinDateSpan_"+data.index).innerHTML = data.dataFormat;
}
/**
 * DateDWR.getMakeDateWithIndex의 콜백 함수 (startDate 셋팅)
 */
function getMakeStartDateCallback(data){
    nv.$("startDateSpan_"+data.index).innerHTML = data.dataFormat;
}

/**
 * div 페이지를 닫는다
 */
function divDisplayNone(divId){
    var divId = nv.$(divId);
    divId.style.display = "none";
}