var fbicon = '<img border="0" src="/images/fbconnect/fb_logo2.png" style="position: relative; top: 0px; left: -14px;">';
var currentTab = 1;
var regionlanguageOfRemix = "";
$(document).ready(function () {
	initMember();
});
function shareit(memberId, remixId) {
	if (memberId <= 0) {
		popupLoginFirst();
		return;
	}
	return GB_showCenter("", "/remix/share_remix.jsp?remixId=" + remixId, 460, 880);
}
function historyChange(newLocation, historyData) {
	if (newLocation == "" || newLocation == undefined) {
		newLocation = "allComment:1:otherRemix:1:allFriendComment:1:tab:1";
	}
	currentTab = historyData;
	var temp = newLocation.split(":");
	var allCommentPageIndex = temp[1];
	var otherRemixPageIndex = temp[3];
	var allFriendCommentIndex = temp[5];
	var tabIndex = temp[7];
	handleHistoryChange(remixId, allCommentPageIndex, otherRemixPageIndex, allFriendCommentIndex, tabIndex);
}
var allCommentPageIndex;
var otherRemixPageIndex;
var allFriendCommentIndex;
var tabIndex;
function handleHistoryChange(movieId, _allCommentPageIndex, _otherRemixPageIndex, _allFriendCommentIndex, _tabIndex) {
	allCommentPageIndex = _allCommentPageIndex;
	otherRemixPageIndex = _otherRemixPageIndex;
	allFriendCommentIndex = _allFriendCommentIndex;
	tabIndex = _tabIndex;
	remixAction.gerMovieIdByRemixId(remixId, handleHistoryChangeCallBack);
}
function handleHistoryChangeCallBack(_movieId) {
	movieId = _movieId;
	if (tabIndex == 1) {
		initTabMenu("tabMenu", 1);
		getAllComment(remixId, regionlanguage, allCommentPageIndex, allCommentPageSize, false);
		$("#allCommentTable").fadeIn("slow");
		$("#myCommentTable").hide();
		$("#addMyCommentTable").hide();
		$("#notLoginMsgTable").hide();
		$("#notLoginMsg").hide();
		$("#allFriendCommentTable").hide();
	} else {
		if (tabIndex == 2) {
			initTabMenu("tabMenu", 2);
			onMouseOut("tab_1");
			$("#allCommentTable").hide();
			$("#allFriendCommentTable").hide();
			if (memberId == 0) {
				$("#notLoginMsgTable").fadeIn("slow");
				$("#notLoginMsg").fadeIn("slow");
			} else {
				$("#myCommentTable").fadeIn("slow");
				$("#addMyCommentTable").fadeIn("slow");
				initGetMyComment();
				FCKEditingArea_CompleteStart();
			}
		} else {
			if (tabIndex == 3) {
				initTabMenu("tabMenu", 3);
				onMouseOut("tab_1");
				$("#myCommentTable").hide();
				$("#addMyCommentTable").hide();
				$("#notLoginMsgTable").hide();
				$("#notLoginMsg").hide();
				$("#allCommentTable").hide();
				if (memberId == 0) {
					$("#notLoginMsgTable").fadeIn("slow");
					$("#notLoginMsg").fadeIn("slow");
				} else {
					$("#allFriendCommentTable").fadeIn("slow");
					getAllFriendComment(remixId, memberId, regionlanguage, allFriendCommentIndex, allCommentPageSize, false);
				}
			}
		}
	}
	getOtherMovieRemix(movieId, otherRemixPageIndex, otherMovieRemixPageSize, false);
}
function getCurrentLocation() {
	var currentLocation = dhtmlHistory.getCurrentLocation();
	var locationArray = currentLocation.split(":");
	if (locationArray.length == 0) {
		currentLocation = "allComment:1:otherRemix:1:allFriendComment:1:tab:1";
		return currentLocation;
	}
	if (locationArray[0] != "allComment") {
		locationArray[0] = "allComment";
		locationArray[1] = 1;
	}
	if (locationArray[2] != "otherRemix") {
		locationArray[2] = "otherRemix";
		locationArray[3] = 1;
	}
	if (locationArray[4] != "allFriendComment") {
		locationArray[4] = "allFriendComment";
		locationArray[5] = 1;
	}
	if (locationArray[6] != "tab") {
		locationArray[6] = "tab";
		locationArray[7] = 1;
	}
	var newCurrentLocation = "";
	for (var i = 0; i < locationArray.length; i++) {
		if (i == 0) {
			newCurrentLocation = locationArray[i];
		} else {
			newCurrentLocation = newCurrentLocation + ":" + locationArray[i];
		}
	}
	return newCurrentLocation;
}
function addToHistory(allCommentIndex, otherRemixIndex, allFriendCommentIndex, tabIndex) {
	var currentLocation = getCurrentLocation();
	var temp = currentLocation.split(":");
	if (allCommentIndex == -1) {
		if (temp[1] == undefined) {
			allCommentIndex = 1;
		} else {
			allCommentIndex = temp[1];
		}
	}
	if (otherRemixIndex == -1) {
		if (temp[3] == undefined) {
			otherRemixIndex = 1;
		} else {
			otherRemixIndex = temp[3];
		}
	}
	if (allFriendCommentIndex == -1) {
		if (temp[5] == undefined) {
			allFriendCommentIndex = 1;
		} else {
			allFriendCommentIndex = temp[5];
		}
	}
	if (tabIndex == -1) {
		if (temp[7] == undefined) {
			tabIndex = 1;
		} else {
			tabIndex = temp[7];
		}
	}
	historyIndex = currentTab;
  		//alert("allReview:"+allCommentIndex+":allRemix:"+otherRemixIndex+','+historyIndex);
	dhtmlHistory.add("allComment:" + allCommentIndex + ":otherRemix:" + otherRemixIndex + ":allFriendComment:" + allFriendCommentIndex + ":tab:" + tabIndex, historyIndex);
}
function initMember(rtMember) {
	useLoadingImage("/images/ajax-loader.png");
	memberAction.getCurrentMember(initMemberCallBack);
}
function initMemberCallBack(rtMember) {
        if (rtMember != null) {
			member = rtMember;
			memberId = member.memberId;
			initRemixInfo();
		} else {
    		//Not login yet
			memberId = 0;
			initRemixInfo();
		}
}

function initRemixInfo() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
	isRemixExist();	
}

function isRemixExist() {
	remixAction.isRemixExist(remixId, isRemixExistCallBack);
}

function isRemixExistCallBack(isExist) {
	
	if(isExist) {
		ratingAction.getMemberRemixRating(remixId, memberId, getMemberRemixRatingCallBack);
		getMyRemixTag();
		remixAction.getRemixByRemixId(remixId, getRemixByRemixIdCallBack);
		remixAction.gerMovieIdByRemixId(remixId, gerMovieIdByRemixIdCallBack);
		getTagOfThisRemix();
		if (dhtmlHistory.isFirstLoad()) {
			initPage();	
		}
	}
	else {
		alert(no_remix_message);
		window.location = "/remix/remixindex.do";
	}
}

function initPage() {

	var currentLocation = getCurrentLocation();
	var temp = currentLocation.split(":");
	var tabIndex = temp[7];

	if (tabIndex == 1) {
		initTabMenu("tabMenu", 1);
		initGetAllComment();
		$("#allCommentTable").fadeIn("slow");
		$("#myCommentTable").hide();
		$("#addMyCommentTable").hide();
		$("#notLoginMsgTable").hide();
		$("#notLoginMsg").hide();
		$("#allFriendCommentTable").hide();
	} else {
		if (tabIndex == 2) {
			initTabMenu("tabMenu", 2);
			onMouseOut("tab_1");
			$("#allCommentTable").hide();
			$("#allFriendCommentTable").hide();
			if (memberId == 0) {
				$("#notLoginMsgTable").fadeIn("slow");
				$("#notLoginMsg").fadeIn("slow");
			} else {
				$("#myCommentTable").fadeIn("slow");
				$("#addMyCommentTable").fadeIn("slow");
				initGetMyComment();
				FCKEditingArea_CompleteStart();
			}
		} else {
			if (tabIndex == 3) {
				initTabMenu("tabMenu", 3);
				onMouseOut("tab_1");
				$("#myCommentTable").hide();
				$("#addMyCommentTable").hide();
				$("#notLoginMsgTable").hide();
				$("#notLoginMsg").hide();
				$("#allCommentTable").hide();
				if (memberId == 0) {
					$("#notLoginMsgTable").fadeIn("slow");
					$("#notLoginMsg").fadeIn("slow");
				} else {
					$("#allFriendCommentTable").fadeIn("slow");
					initGetAllFriendComment();
				}
			}
		}
	}
}
var movieId;
function gerMovieIdByRemixIdCallBack(_movieId) {
	movieId = _movieId;
	initGetOtherMovieRemix();
}
function initGetOtherMovieRemix() {
		var currentLocation = getCurrentLocation();
		var temp = currentLocation.split(":");
		var pageIndex = temp[3];
		getOtherMovieRemix(movieId, pageIndex, otherMovieRemixPageSize, false);
}
function getOtherMovieRemix(_movieId, _pageIndex, _pageSize, isAddToHistory) {
	if (isAddToHistory == true) {
		addToHistory(-1, _pageIndex, -1, currentTab);
	}
	remixAction.getRemixByMovieId(_movieId, remixId, _pageIndex, _pageSize, getOtherMovieRemixCallBack);
}
function pageNavOtherMovieRemix(pageIndex) {
		addToHistory(-1, pageIndex, -1, currentTab);
		remixAction.getRemixByMovieId(movieId, remixId, pageIndex, otherMovieRemixPageSize, getOtherMovieRemixCallBack);
}
var otherMovieRemixLineHtml = "<tr id='otherMovieRemixLine' class='otherMovieRemixLine'><td height='6' colspan='2' align='left' valign='middle'><img src='/images/wight_line3.png' width='245' height='8' /></td></tr>";
function downLoadRemix(event) {
	remixDownload(memberId, event.data.remixId, event.data.remixPath);
}
function linkToOtherRemix(event) {
	window.location.href = event.data.remixUrl;
}
function getOtherMovieRemixCallBack(recordArray) {
	$("tr.otherMovieRemixLine").remove();
	$("tr.real_OtherMovieRemix").remove();
	$("#otherMovieRemixPageList").empty();
	$("#otherMovieRemixTable").hide();
	var otherRemixCount = 0;
	for (i = 0; i < recordArray.size; i++) {
		
		var temp = $("tr.temp_otherMovieRemix").clone();
		temp.find("#otherMovieRemixTitle").html(recordArray.items[i].remixName);
		temp.find("#otherMovieRemixTitle").attr("href", "remix_info.jsp?remixId=" + recordArray.items[i].remixId);
		temp.find("#othetMovieRemixDownloadCount").html("" + recordArray.items[i].downloadCount);
		temp.find("#otherMovieRemixCommentCount").html("" + recordArray.items[i].commentCount);
		temp.find("#otherMovieRemixGlobalRatingCount").html("" + recordArray.items[i].globalRatingCount);
		temp.find("#otherMovieRemixMemberName").html(recordArray.items[i].member.displayName);
		temp.find("#otherMovieRemixMemberAvatar").attr("src", recordArray.items[i].member.avatar);
		temp.find("#linkToMemberProfile").bind("click", {url:"/member/member-profile.do?memberId=" + recordArray.items[i].member.memberId}, linkToMemberProfile);
		
		temp.find("#otherMovieRemixDownloadLink").attr("href", "javascript:remixDownload(" + memberId + "," + recordArray.items[i].remixId + ",'" + recordArray.items[i].remixFile + "')");
				//movie remix global rating
		var remixGlobalRatingScore;
		remixGlobalRatingScore = recordArray.items[i].ratingAvg;
		temp.find("#otherMovieRemixGlobalRating").attr("class", "otherMovieRemixGlobalRating");
		temp.find("#otherMovieRemixGlobalRating").html("" + remixGlobalRatingScore);
	 		
           	//temp.find("#review_MemberName").html(recordArray.items[i].member.diaplayname);
		temp.attr("class", "real_OtherMovieRemix");
		temp.show();
		temp.fadeIn("slow");
		$("#otherMovieRemixPageListTable").before(temp);
		$("#otherMovieRemixPageListTable").before(otherMovieRemixLineHtml);
	}
	initRating("otherMovieRemixGlobalRating", false, 's_rating');
	$("#otherMovieRemixTable").fadeIn("slow");
	$("#otherRemixCount").html("" + recordArray.totalSize);
	var pList = new SmallPageList(recordArray.pageIndex, recordArray.totalPage, recordArray.totalSize, "pageNavOtherMovieRemix").displayList();
	$("#otherMovieRemixPageList").append(pList);
}
function initGetAllFriendComment() {
	var currentLocation = getCurrentLocation();
	var temp = currentLocation.split(":");
	var pageIndex = temp[5];
	getAllFriendComment(remixId, memberId, regionlanguage, pageIndex, allCommentPageSize, false);
}
function getAllFriendComment(_remixId, _memberId, _regionlanguage, _pageIndex, _pageSize, isAddToHistory) {
		if (isAddToHistory == true) {
			addToHistory(-1, -1, _pageIndex, currentTab);
		}
   		//alert(_remixId+','+_regionlanguage+','+_pageIndex+','+_pageSize);
		friendAction.listAllFriendsComment(_remixId, memberId, _regionlanguage, _pageIndex, _pageSize, getAllFriendCommentCallback);
}
function pageNavAllFriendComment(pageIndex) {
	addToHistory(-1, -1, pageIndex, currentTab);
	friendAction.listAllFriendsComment(remixId, memberId, regionlanguage, pageIndex, allCommentPageSize, getAllFriendCommentCallback);
}

function getAllFriendCommentCallback(recordArray) {
		if (recordArray.size > 0) {
			$("tr.lineHtml").remove();
			$("tr.reportHtml").remove();
			$("tr.real_allFriendComment").remove();
			$("#friend_page_index").empty();
			$("#allFriendCommentTable").hide();
			for (i = 0; i < recordArray.size; i++) {
				if (recordArray.items[i] != null) {
					var reportHtml = "<tr class='reportHtml'><td align='right' valign='top'><span class='cursor' onclick='reportComment(" + recordArray.items[i].remixCommentId + ");'><span class='right_r'>"+report+"</span></span></td></tr>";
					var temp = $("tr.temp_allFriendComment").clone();
					temp.find("#allFriendComment_text").html(shortDisplay(recordArray.items[i].remixComment.remixCommentId, recordArray.items[i].remixComment.comment, 150));
					temp.find("#allFriendComment_date").html(recordArray.items[i].remixComment.displayDateTime);
					
					var _avatar = recordArray.items[i].friend.avatar;
					temp.find("#allFriendComment_MemberAvatar").attr("src", _avatar);
					
					if(_avatar && _avatar.indexOf('fbcdn.net') >= 0) {
						temp.find('#allFriendComment_MemberAvatar').after(fbicon);
					}
					
					temp.find("#allFriendComment_MemberName").html(recordArray.items[i].friend.displayName);
					temp.find("#linkToMemberProfile").bind("click", {url:"/member/member-profile.do?memberId=" + recordArray.items[i].friend.memberId}, linkToMemberProfile);
					temp.find("#isNew").attr("id", "isNew_" + recordArray.items[i].remixComment.remixCommentId);
					temp.attr("class", "real_allFriendComment");
					temp.fadeIn("slow");
					$("#friend_page_index_list_table").before(temp);
					$("#friend_page_index_list_table").before(reportHtml);
					$("#friend_page_index_list_table").before(lineHtml);
	            	//alert(recordArray.items[i].remixCommentId+','+memberId);
					if (memberId != 0) {
	            		//remixCommentAction.isViewComment(recordArray.items[i].remixComment.remixCommentId, memberId, isViewCommentCallBack);
	            		//remixCommentAction.setViewedComment(recordArray.items[i].remixComment.remixCommentId, memberId);
					}
				}
				$("#allFriendCommentTable").fadeIn("slow");
				$("#allFriendComment_count").html("" + recordArray.totalSize);
			}
			var pList = new PageList(recordArray.pageIndex, recordArray.totalPage, recordArray.totalSize, "pageNavAllFriendComment").displayList();
			$("#friend_page_index").append(pList);
			$("#friend_page_index_list_table").fadeIn("slow");
		} else {
			$("#reviewEmptyWordingTable_2").fadeIn("slow");
		}
		$('#myCommentLine').fadeOut("fast");
}
function initGetAllComment() {
	var currentLocation = getCurrentLocation();
	var temp = currentLocation.split(":");
	var pageIndex = temp[1];
	getAllComment(remixId, regionlanguage, pageIndex, allCommentPageSize, false);
}
function getAllComment(_remixId, _regionlanguage, _pageIndex, _pageSize, isAddToHistory) {
		if (isAddToHistory == true) {
			addToHistory(_pageIndex, -1, -1, currentTab);
		}
   		//alert(_remixId+','+_regionlanguage+','+_pageIndex+','+_pageSize);
		remixCommentAction.getRemixCommentsByRemixId(_remixId, _regionlanguage, _pageIndex, _pageSize, getAllCommentCallback);
}
function pageNavAllComment(pageIndex) {
	addToHistory(pageIndex, -1, -1, currentTab);
	remixCommentAction.getRemixCommentsByRemixId(remixId, regionlanguage, pageIndex, allCommentPageSize, getAllCommentCallback);
}
var lineHtml = "<tr class='lineHtml'><td height='2' colspan='2' align='left' valign='middle'><img src='/images/wight_line.png' width='592' height='4' /></td></tr>";
function reportComment(reviewId) {
	if (memberId != 0) {
		return GB_showCenter("", "/report-form.do?reportTarget=comment&targetId=" + reviewId, 278, 580);
	} else {
		popupLoginFirst();
	}
}
function getAllCommentCallback(recordArray) {
		if (recordArray.totalSize > 0) {
			$("tr.lineHtml").remove();
			$("tr.reportHtml").remove();
			$("tr.real_allComment").remove();
			$("#page_index").empty();
			$("#allCommentTable").hide();
			for (i = 0; i < recordArray.size; i++) {
				if (recordArray.items[i] != null) {
					var reportHtml; 
					var temp = $("tr.temp_allComment").clone();
					temp.find("#allComment_text").html(shortDisplay(recordArray.items[i].remixCommentId, recordArray.items[i].comment, 150));
					temp.find("#allComment_date").html(recordArray.items[i].displayDateTime);
					
					var _avatar = recordArray.items[i].member.avatar;
					temp.find("#allComment_MemberAvatar").attr("src", _avatar);
					
					if(_avatar && _avatar.indexOf('fbcdn.net') >= 0) {
						temp.find('#allComment_MemberAvatar').after(fbicon);
					}
					
					temp.find("#allComment_MemberName").html(recordArray.items[i].member.displayName);
					temp.find("#linkToMemberProfile").bind("click", {url:"/member/member-profile.do?memberId=" + recordArray.items[i].member.memberId}, linkToMemberProfile);
					temp.find("#isNew").attr("id", "isNew_" + recordArray.items[i].remixCommentId);
					temp.attr("class", "real_allComment");
					temp.fadeIn("slow");
					$("#page_index_list_table").before(temp);
					if(memberId == recordArray.items[i].memberId) {
						reportHtml = "<tr class='reportHtml'><td align='right' valign='top'><span class='cursor' onclick=\"popEditCommentWin(" + recordArray.items[i].remixCommentId + ", " + memberId + ",'" + recordArray.items[i].updateTime.getFullYear() + "/" + (recordArray.items[i].updateTime.getMonth() + 1) + "/" + recordArray.items[i].updateTime.getDate() + "');\"><span class='right_r'>" + edit + "</span></span>&nbsp;|&nbsp;<span class='cursor' onclick=\"deleteComment("+ recordArray.items[i].remixCommentId +");\"><span class='right_r'>" + deleteText + "</span></span>&nbsp;|&nbsp;<span class='cursor' onclick='reportComment("+recordArray.items[i].remixCommentId +");'><span class='right_r'>"+report+"</span></span></td></tr>";
					}
					else {
						reportHtml = "<tr class='reportHtml'><td align='right' valign='top'><span class='cursor' onclick='reportComment(" + recordArray.items[i].remixCommentId + ");'><span class='right_r'>"+report+"</span></span></td></tr>";						
					}
					$("#page_index_list_table").before(reportHtml);
					$("#page_index_list_table").before(lineHtml);
	            	//alert(recordArray.items[i].remixCommentId+','+memberId);
					if (memberId != 0) {
						remixCommentAction.isViewComment(recordArray.items[i].remixCommentId, memberId, isViewCommentCallBack);
					}
				}
				$("#allCommentTable").fadeIn("slow");
				$("#allComment_count").html("" + recordArray.totalSize);
			}
			var pList = new PageList(recordArray.pageIndex, recordArray.totalPage, recordArray.totalSize, "pageNavAllComment").displayList();
			$("#page_index").append(pList);
			$("#page_index_list_table").fadeIn("slow");
		} else {
			/*
			changeTab(2);
			initTabMenu("tabMenu", 2);
			onMouseOut("tab_1");
			onMouseOut("tab_3");
			if (memberId != 0) {
				$("#reviewEmptyWording").fadeIn("slow");
				$("#reviewEmptyWordingTable").fadeIn("slow");
			}*/
			//initTabMenu("tabMenu", 1);
			//onMouseOut("tab_2");
			//onMouseOut("tab_3");
			$("#allCommentTable").hide();
			$("#allComment_count").hide();
			$("#reviewEmptyWording").fadeIn("slow");
			$("#reviewEmptyWordingTable").fadeIn("slow");
		}
		$('#myCommentLine').fadeOut("fast");
}
function isViewCommentCallBack(result) {
	var _commentId = result[0];
	if (result[1] == true) {
		$("#isNew_" + _commentId).hide();
	} else {
		$("#isNew_" + _commentId).fadeIn("slow");
		remixCommentAction.setViewedComment(_commentId, memberId);
	}
}
function getMemberRemixRatingCallBack(rating) {
	var myRatingScore;
	if (rating != null && memberId != 0) {
		myRatingScore = rating.score;
		$("#clearRating").fadeIn("slow");
	} else {
		myRatingScore = "0";
		$("#clearRatingEmptyWording").fadeIn("slow");
	}
	$("#myRating").html("" + myRatingScore);
	if (memberId != 0) {
		initRating("myRating", true);
		$("span.myRating > img").bind("click", submitRating);
	} else {
		initRating("myRating", false);
	}
}
function submitRating(evt) {
	var tmp = evt.target.parentNode.rating;
	ratingAction.setRemixRatingFromPage(remixId, memberId, tmp, afterRatingCallBack);
}
function afterRatingCallBack() {
	ratingAction.getMemberRemixRating(remixId, memberId, refreshMyRating);
	remixAction.getRemixByRemixId(remixId, refreshGlobalRating);
}
function refreshGlobalRating(remix) {
	var globalRatingScore = remix.ratingAvg;
	$(document).find("#movieRemix_globalRating").html("" + globalRatingScore);
	$(document).find("#movieRemix_globalRatingCount").html("" + remix.globalRatingCount);
	initRating("movieRemix_globalRating", false);
}
function refreshMyRating(rating) {
	var myRatingScore = 0;
	if (rating != null) {
		myRatingScore = rating.score;
		$("#clearRatingEmptyWording").hide();
		$("#clearRating").fadeIn("slow");
	} else {
		$("#clearRating").hide();
		$("#clearRatingEmptyWording").fadeIn("slow");
	}
	$(document).find("#myRating").html("" + myRatingScore);
	initRating("myRating", true);
	document.getElementById("myRating").myRating = myRatingScore;
	$("span.myRating>img").bind("click", submitRating);
}
var directorMemberId;
function getRemixByRemixIdCallBack(remix) {
	document.title = remix.remixName;
	$(document).find("#movieRemixTitle").html(remix.remixName);
	var relatedMovie = link_to_movie_info2(remix.discSetId,remix.regionLanguage);
	$(document).find("#relatedMovieNameLink").html(encodeHtmlString(remix.movieName));
	$(document).find("#relatedMovieNameLink").attr("href", relatedMovie);
	$(document).find("#movieRemixDescription").html(remix.remixDesp);
	$(document).find("#dvdRegion").html("" + remix.disc.region);
	//$(document).find("#remixCommentCount").html("" + remix.commentCount);
	$(document).find("#remixDownloadCount").html("" + remix.downloadCount);
	var _remixFile = remix.remixFile;
	if(_remixFile.indexOf("?") != -1) {
    remix.remixFile = _remixFile.substring(0, _remixFile.lastIndexOf("/") + 1) + remix.remixName + ".Rmx";
  }
	$(document).find("#movieRemixDownloadLink").bind("click", {remixPath:remix.remixFile, remixId:remix.remixId}, downLoadRemix);
   	$(document).find("#movieRemixDownloadLink2").bind("click", {remixPath:remix.remixFile, remixId:remix.remixId}, downLoadRemix);
    directorMemberId = remix.memberId;
    regionlanguageOfRemix = remix.regionLanguage;
    remixAction.getRemixCommentCountByRemixIdAndLang(remixId, regionlanguageOfRemix, getRemixCommentCountByRemixIdAndLangCallBack);
    if(directorMemberId == memberId) {
    	$('#button').css("display","block");
    	$('#edit').bind('click',{directorId: directorMemberId, remixId: remix.remixId},editRemix);
    	$('#delete').bind('click', {directorId: directorMemberId, remixId: remix.remixId},deleteRemix);
    }
    else {
    	$('#delete').remove();	
    }
	memberAction.getMemberById(directorMemberId, getMemberByIdCallBack);
	$(document).find("#movieRemix_globalRating").html("" + remix.ratingAvg);
	initRating("movieRemix_globalRating", false);
	$(document).find("#movieRemix_globalRatingCount").html("" + remix.globalRatingCount);
	if (remix.movie.coverArt != null) {
		$(document).find("#coverArtUrl").attr("src", remix.movie.coverArt.url[1]);
	}
	if (remix.updateDate != null) {
		$('#remixUpdateDate').html(toLocalDate(remix.updateDate));
	}
	else {
		$('#remixUpdateDate').html(toLocalDate(remix.createDate));
	}
	$("#byLink").bind("click", popBuyPage);
	$("#updateDate").html(remix.displayCreateDate);
}

function popBuyPage() {
   	return GB_showCenter("", "/movie/buypage.jsp?movieTitle=" + $('#relatedMovieNameLink').text() + "&locale=" + regionlanguage.substr(3,2), 600, 600);
}

function deleteRemix(info) {
	if(info.data.directorId == memberId) {
		popupConfirm(deleteRemixMsg, info.data.remixId);
	}
	else {
		alert(notAllowed);
		return;
	}
}

function deleteRemixCallBack(result) {
	if(result != -1) {
		var handleYes = function() {
			this.hide();
			window.location = "/remix/remixindex.do";
			return true;
		};
		popupInfo(deleteRemixSuccess, handleYes);
	}
	else {
		var handleYes = function() {
			this.hide();
			return true;
		};
		popupInfo(notAllowed, handleYes);
	}
}

function editRemix(info) {
	if(memberId <= 0) {
		popupLoginFirst();
	}
	else {
		if(info.data.directorId == memberId) {
			return GB_showCenter("", "/remix/editRemix.jsp?remixid="+info.data.remixId+"&directorid="+info.data.directorId+"&memberid="+memberId, 310, 580, editRemixCallBack);
		}
		else {
			alert(notAllowed);
			return;
		}
	}
}

function editRemixCallBack() {
	var handleYes = function() {
		this.hide();
		initRemixInfo();
		return true;
	};
	popupInfo(updateRemixSuccess, handleYes);
}

function getRemixCommentCountByRemixIdAndLangCallBack(commentCount) {
	$(document).find("#remixCommentCount").html("" + commentCount);
}

function reportRemix() {
	if (memberId != 0) {
		return GB_showCenter("", "/report-form.do?reportTarget=remix&targetId=" + remixId, 278, 580);
	} else {
		alert(loginfirst);
	}
}
function openBuyLink(event) {
	window.open(event.data.url);
}
function clearRating() {
	if (memberId != 0) {
		ratingAction.clearMemberRemixRating(remixId, memberId, afterRatingCallBack);
	} else {
		popupLoginFirst();
	}
}
function getMemberByIdCallBack(member) {
	//$(document).find("#remixDirectorName").html(director);
    $(document).find("#remixDirectorName").attr("href","/member/member-profile.do?memberId=" + member.memberId );
    $(document).find("#remixDirectorName").html(member.displayName );
    $(document).find("#remixDirectorAvatarLink").attr("href", "/member/member-profile.do?memberId=" + member.memberId );    	
    if(member.friendCount == 0) friend = "0";
    else friend = member.friendCount;
    $(document).find("#friendCount").html(friend);
    $(document).find("#remixDirectorAvatar").attr("src", member.avatar);
	$(document).find("#movieRemixCount").html(member.remixCount);
	if(member.memberId != memberId) {
		friendAction.checkFriendStatus(memberId, member.memberId, checkFriendStatusCB);
	}
	else {
		$('#statusIcon').remove();
	}
	$(document).find("#linkToMemberProfile").bind("click", {url:"/member/member-profile.do?memberId=" + member.memberId}, linkToMemberProfile);
}

function checkFriendStatusCB(status) {
	if(status) {
		// visitor has any relationship to remix maker
		if(status == -2) {
			$('#statusIcon').attr('src', '/images/add_friend_s.jpg');
			$('#directorFriendStatus').html('<a href="javascript:requestFriend('+memberId+','+directorMemberId+')">' + document.getElementById(status+'').innerHTML + '</a>');
		}
		else {
			$('#statusIcon').attr('src', '/images/icon1.jpg');
			$('#directorFriendStatus').text(document.getElementById(status+'').innerHTML);
		}
		
	}
}

function requestFriend(requestorId, responserId) {
	if(requestorId == 0) {
		popupLoginFirst();
	}
	else {
		friendAction.requestFriend(requestorId, responserId, popUpRequestFriendResult);
	}
}

function popUpRequestFriendResult() {
	var handleYes = function() {
			this.hide();
			$('#statusIcon').attr('src', '/images/icon1.jpg');
			$('#directorFriendStatus').text(document.getElementById('-1').innerHTML);
			return true;
		};
	popupInfo(requestfriendok, handleYes);
}

function linkToMemberProfile(event) {
	window.location.href = event.data.url;
}
function changeTab(tabNo, isAddToHistory) {
	if (isAddToHistory == null) {
		isAddToHistory = false;
	}
		switch (tabNo) {
		  case 1:
			if (isAddToHistory == true) {
				addToHistory(-1, -1, -1, 1);
			}
			currentTab = 1;
			$("#allCommentTable").fadeIn("slow");
			$("#myCommentTable").hide();
			$("#addMyCommentTable").hide();
			$("#notLoginMsgTable").hide();
			$("#notLoginMsg").hide();
			$("#allFriendCommentTable").hide();
			$("#reviewEmptyWordingTable").hide();
			$("#reviewEmptyWordingTable_2").hide();
			initGetAllComment();
			break;
		  case 2:
			if (isAddToHistory == true) {
				addToHistory(-1, -1, -1, 2);
			}
			currentTab = 2;
			$("#allCommentTable").hide();
			$("#allFriendCommentTable").hide();
			$("#reviewEmptyWordingTable").hide();
			$("#reviewEmptyWordingTable_2").hide();
			if (memberId == 0) {
				$("#notLoginMsgTable").fadeIn("slow");
				$("#notLoginMsg").fadeIn("slow");
			} else {
				$("#myCommentTable").fadeIn("slow");
				$("#addMyCommentTable").fadeIn("slow");
				initGetMyComment();
				FCKEditingArea_CompleteStart();
			}
			break;
		  case 3:
			if (isAddToHistory == true) {
				addToHistory(-1, -1, -1, 3);
			}
			currentTab = 3;
			$("#reviewEmptyWordingTable").hide();
			$("#reviewEmptyWordingTable_2").hide();
			$("#myCommentTable").hide();
			$("#addMyCommentTable").hide();
			$("#notLoginMsgTable").hide();
			$("#notLoginMsg").hide();
			$("#allCommentTable").hide();
			if (memberId == 0) {
				$("#notLoginMsgTable").fadeIn("slow");
				$("#notLoginMsg").fadeIn("slow");
			} else {
				$("#allFriendCommentTable").fadeIn("slow");
				initGetAllFriendComment();
			}
			break;
		}
}
// "this" here is FCKEditingArea.Window
function FCKEditingArea_CompleteStart() {
	if (!document.all) {
		this.setTimeout(FCKEditingArea_CompleteStart, 50);
		return;
	}
	if (!document.all) {
		var oEditor = FCKeditorAPI.GetInstance("add_my_comment_text");
		if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG) {
			oEditor.MakeEditable();
		}
	}
}
function initGetMyComment() {
	var pageIndex = 1;
	var pageSize = 3;
	getMyComment(remixId, regionlanguage, memberId, pageSize, pageIndex);
}
function getMyComment(_remixId, _regionLang, _memberId, _pageSize, _pageIndex) {
	remixCommentAction.getMemberRemixCommentByRemixId(_memberId, _remixId, _pageIndex, _pageSize, getMyCommentCallback);
}
function getMyCommentCallback(recordArray) {
	$("tr.real_my_comment").remove();
	$("#myCommentTable").hide();
	if (recordArray.items[0] != null) {
		$("#reviewEmptyWording").hide();
		$("#reviewEmptyWordingTable").hide();
		var temp = $("tr.temp_my_comment").clone();
		temp.find("#my_comment_text").html(shortDisplay("my" + recordArray.items[0].reviewid, recordArray.items[0].comment, 150));
		//temp.find("#my_review_text").html(recordArray.items[0].review + "...");
		temp.find("#my_comment_date").html(recordArray.items[0].displayDateTime);
		temp.attr("class", "real_my_comment");
		temp.show();
		temp.fadeIn("slow");
		$("#myCommentTable").append(temp);
	}
	$("#myCommentTable").fadeIn("slow");
	$('#myCommentLine').fadeIn("slow");
}
function addMyComment() {
	//var oEditor = FCKeditorAPI.GetInstance("add_my_comment_text");
	//var commentText = oEditor.GetXHTML(true);
	var commentText = document.getElementById("add_my_comment_text").value;
	var txtCaptcha = document.getElementById("txtCaptcha").value;
	remixCommentAction.insertUpdateRemixComment(remixId, regionlanguageOfRemix, memberId, commentText, txtCaptcha, addMyCommentCallBack);
}
function addMyCommentCallBack(result) {
	document.getElementById("txtCaptcha").value = "";
	if(result.rerult != -1){
    	initGetMyComment();
		remixCommentAction.getRemixCommentCount(remixId, regionlanguageOfRemix, getCommentCountCallBack);
		$('#add_my_comment_text').attr("value","");
    }
    $('#captchaMessage').html(result.rtnString);
    $('#captchaMessage').fadeIn("slow");
	changeCaptchaImage();
}
function getCommentCountCallBack(count) {
	$("#remixCommentCount").html("" + count);
}
function showAllTag() {
	remixTagAction.getRemixTagByRemixId(remixId, 0, 0, getTagOfThisRemixCallBack);
	$("#moreTag").hide();
	$("#first20Tag").show();
}
function getTagOfThisRemix() {
	var allTagPageSize = 20;
	$("#first20Tag").hide();
	$("#moreTag").show();
	remixTagAction.getRemixTagByRemixId(remixId, 1, allTagPageSize, getTagOfThisRemixCallBack);
}
function getTagOfThisRemixCallBack(recordArray) {
	var tagString;
	$("span.real_allRemixTag").remove();
	for (i = 0; i < recordArray.size; i++) {
		tagString = "";
		tagString = "<a href='/get-remixtag-action.do?tagId=" + recordArray.items[i].remixTagId + "&pageIndex=1&pageCount=10&orderField=create_date&order=1' class='tag_2'>";
		tagString = tagString + "<nobr>" + recordArray.items[i].remixTagName + "</nobr>";
		/*
           if(recordArray.items[i].rank<=10){
           	tagString="<a href='/get-remixtag-action.do?tagId=" + recordArray.items[i].remixTagId + "&pageIndex=1&pageCount=10&orderField=create_date&order=1' class='tag_5'>"
			tagString = tagString + "<nobr>"+recordArray.items[i].remixTagName+"</nobr>";
		}
		else if(recordArray.items[i].rank<=20){
			tagString="<a href='/get-remixtag-action.do?tagId=" + recordArray.items[i].remixTagId + "&pageIndex=1&pageCount=10&orderField=create_date&order=1' class='tag_4'>"
			tagString = tagString + "<nobr>"+recordArray.items[i].remixTagName+"</nobr>";
		}
		else if(recordArray.items[i].rank<=30){
			tagString="<a href='/get-remixtag-action.do?tagId=" + recordArray.items[i].remixTagId + "&pageIndex=1&pageCount=10&orderField=create_date&order=1' class='tag_3'>"
			tagString = tagString + "<nobr>"+recordArray.items[i].remixTagName+"</nobr>";
		}
		else if(recordArray.items[i].rank<=40){
			tagString="<a href='/get-remixtag-action.do?tagId=" + recordArray.items[i].remixTagId + "&pageIndex=1&pageCount=10&orderField=create_date&order=1' class='tag_2'>"
			tagString = tagString + "<nobr>"+recordArray.items[i].remixTagName+"</nobr>";
		}
		else if(recordArray.items[i].rank<=50){
 		tagString="<a href='/get-remixtag-action.do?tagId=" + recordArray.items[i].remixTagId + "&pageIndex=1&pageCount=10&orderField=create_date&order=1' class='tag_1'>"
			tagString = tagString + "<nobr>"+recordArray.items[i].remixTagName+"</nobr>";
		}*/
		tagString = tagString + "</a>";
           //if(i%5 == 0 && i/5 >=1)
			//tagString = tagString + "<br />";
		var temp = $("span.temp_allRemixTag").clone();
		temp.html(tagString + "&nbsp;&nbsp; ");
		temp.attr("class", "real_allRemixTag");
		temp.show();
		$("#allRemixTagTable").append(temp);
	}
	if (recordArray.totalSize <= 20) {
		$("#first20Tag").hide();
		$("#moreTag").hide();
	}
	$("#allRemixTagTable").fadeIn("slow");
}
function deleteTag(remixId, memberId, tagId) {
	remixTagAction.delRemixTag(remixId, memberId, tagId, refreshMovieTag);
}
function getMyRemixTag() {
	remixTagAction.getMyRemixTag(memberId, remixId, 0, 0, getMyRemixTagCallBack);
}
function getMyRemixTagCallBack(recordArray) {
	$("span.real_myRemixTag").remove();
	for (i = 0; i < recordArray.size; i++) {
		var temp = $("span.temp_myRemixTag").clone();
		var index = i + 1;
		temp.html("<span class=\"black10\"><nobr>" + recordArray.items[i].remixTagName + "</nobr></span> <a href=\"#\" onclick=\"deleteTag(" + remixId + "," + memberId + "," + recordArray.items[i].remixTagId + ");return false;\"> <span class=\"black10_line\">(x)<br /></span></a></span>");
		temp.attr("class", "real_myRemixTag");
		temp.show();
		$("#myRemixTagList").append(temp);
	}
	$("#myRemixTagList").fadeIn("slow");
}
function addMyRemixTag() {
	if (memberId == 0) {
		popupLoginFirst();
	} else {
		var tagName = document.getElementById("myRemixTag").value;
		if (checkTagLength(tagName)) {
			remixTagAction.addTag(remixId, memberId, tagName, regionlanguage, refreshMovieTag);
		}
	}
}
function refreshMovieTag() {
	getMyRemixTag();
	getTagOfThisRemix();
}
function clearAddTagTip() {
	document.getElementById("myRemixTag").value = "";
}

function toMyCommentTab(){
    changeTab(2);
    initTabMenu('tabMenu', 2);
    onMouseOut('tab_1');
    onMouseOut('tab_3');
}
YAHOO.namespace("example.container");

function infoInit(msg,icon, handleYes) {
	
	// Instantiate the Dialog
	YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1",
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
																			   close: false,
																			   text: msg,
																			   icon: icon,
																			   constraintoviewport: true,
																			   buttons: [ { text:"OK", handler:handleYes, isDefault:true }
																						   ]
																			 } );
	// Render the Dialog
	YAHOO.example.container.simpledialog1.render("yui_popinfo_container");
}

function confirmInit(msg,icon,remixId) {
	
	// Define various event handlers for Dialog
	var handleYes = function() {
		this.hide();
		//alert(remixId);
		remixAction.deleteRemixById(remixId, deleteRemixCallBack);
		return true;
	};
	var handleNo = function() {
		this.hide();
		return false;
	};

	// Instantiate the Dialog
	YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1",
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
																			   close: false,
																			   text: msg,
																			   icon: icon,
																			   constraintoviewport: true,
																			   buttons: [ { text:"Yes", handler:handleYes, isDefault:true },
																			   			  { text:"No", handler:handleNo, isDefault:false }	
																						   ]
																			 } );

	// Render the Dialog
	YAHOO.example.container.simpledialog1.render("yui_popinfo_container");
}

function confirmCommentInit(msg,icon,commentid) {
	
	// Define various event handlers for Dialog
	var handleYes = function() {
		this.hide();
		//delete remix comment
		remixCommentAction.deleteComment(commentid, remixId, regionlanguage, deleteCommentCallBack);
		return true;
	};
	var handleNo = function() {
		this.hide();
		return false;
	};

	// Instantiate the Dialog
	YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1",
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
																			   close: false,
																			   text: msg,
																			   icon: icon,
																			   constraintoviewport: true,
																			   buttons: [ { text:"Yes", handler:handleYes, isDefault:true },
																			   			  { text:"No", handler:handleNo, isDefault:false }	
																						   ]
																			 } );

	// Render the Dialog
	YAHOO.example.container.simpledialog1.render("yui_popinfo_container");
}

function popupInfo(msg, handleYes){
    createPopInfoDiv();
    infoInit(msg,YAHOO.widget.SimpleDialog.ICON_INFO, handleYes);
    var popdiv = document.getElementById("simpledialog1");
    var iframe = document.createElement("iframe");
    iframe.className="menu_iframe";
    popdiv.appendChild(iframe);

    YAHOO.example.container.simpledialog1.show();
}

function popupConfirm(msg,remixId){
    createPopInfoDiv();
    confirmInit(msg,YAHOO.widget.SimpleDialog.ICON_INFO,remixId);
    var popdiv = document.getElementById("simpledialog1");
    var iframe = document.createElement("iframe");
    iframe.className="menu_iframe";
    popdiv.appendChild(iframe);

    YAHOO.example.container.simpledialog1.show();
}

function popupConfirmComment(msg,commentid){
    createPopInfoDiv();
    confirmCommentInit(msg,YAHOO.widget.SimpleDialog.ICON_INFO,commentid);
    var popdiv = document.getElementById("simpledialog1");
    var iframe = document.createElement("iframe");
    iframe.className="menu_iframe";
    popdiv.appendChild(iframe);

    YAHOO.example.container.simpledialog1.show();
}

function createPopInfoDiv() {
	var theBody = document.getElementsByTagName("body")[0];
	var theAlertDiv = document.createElement("div");
	theAlertDiv.setAttribute('id','yui_popinfo_container');
	theAlertDiv.setAttribute('class','yui-skin-sam');
	theAlertDiv.className='yui-skin-sam';
	theAlertDiv.style.visibility = 'hidden';
    theBody.appendChild(theAlertDiv);
}

function popEditCommentWin(commentid, commentorId, updateDate) {
	if(memberId <= 0) {
		popupLoginFirst();
	}
	else {
		if(memberId == commentorId) {
			return GB_showCenter("", "/remix/editRemixComment.jsp?commentid="+commentid+"&memberid="+commentorId+"&remixid="+remixId+"&regionlanguage="+regionlanguage+"&date="+updateDate, 310, 580, editRemixCommentCallBack);
		}
		else {
			alert(notAllowed);
			return;
		}
	}
}

function editRemixCommentCallBack() {
	initGetAllComment();
}

function deleteComment(commentid) {
	if(memberId <= 0) {
		popupLoginFirst();
	}
	else {
		popupConfirmComment(deleteCommentMsg,commentid);
	}
	
}

function deleteCommentCallBack(result) {
	if(result != -1) {
		initGetAllComment();
		remixCommentAction.getRemixCommentCount(remixId, regionlanguage, getCommentCountCallBack);
	}
	else {
		alert(notAllowed);
	}
}

var activeMenuItem = false;
var activeMenuItems = new Array();
function changeActiveItem(id,callBackFn,tabIndex) {
	
	var classIndex = id.split("_")[1];
	var prefix = id.split("_")[0];
	if (activeMenuItems[tabIndex] != classIndex) {
		var obj = document.getElementById(prefix+"_"+activeMenuItems[tabIndex]);
		var tempClass = obj.className.replace("-active","-idle");
		obj.className = tempClass;
		obj.onclick = new Function("evt", "changeActiveItem('" + prefix+"_"+activeMenuItems[tabIndex] +"',"+callBackFn+","+tabIndex+");");
		obj.onmouseover = new Function("evt", "onMouseOver('" + prefix+"_"+activeMenuItems[tabIndex] +"');");
		obj.onmouseout = new Function("evt", "onMouseOut('" + prefix+"_"+activeMenuItems[tabIndex] +"');");
	}
	//activeMenuItem = classIndex;
	activeMenuItems[tabIndex]=classIndex;
	var obj = document.getElementById(id);
	var tempClass = obj.className.replace("-rollover","-active");;
	tempClass = tempClass.replace("-idle","-active");
	obj.className = tempClass
	obj.onclick = new Function("evt", "");
	obj.onmouseover = new Function("evt", "");
	obj.onmouseout = new Function("evt", "");
	if(callBackFn!=null && isFunction(callBackFn))
		callBackFn(parseInt(classIndex), true);
	else
		changeTab(parseInt(classIndex), true);
}
function onMouseOver(id) {
	var obj = document.getElementById(id);
	var tempClass = obj.className.replace("-idle","-rollover");
	obj.className = tempClass;
}
function onMouseOut(id) {
	var obj = document.getElementById(id);
	var tempClass = obj.className.replace("-rollover","-idle");
	obj.className = tempClass;
}
// parameter : id of tab, defaultTabIndex
function initTabMenu(id, defaultTabIndex, callBackFn, tabIndex) {
	
	if(tabIndex==null)
		tabIndex=1;
	var mainMenuObj = document.getElementById(id);
	var menuItems = mainMenuObj.getElementsByTagName("div");

	for (var no = 0; no < menuItems.length; no++) {
		var classIndex = no + 1;
		if (no == defaultTabIndex - 1) {
			var tempClass = menuItems[no].className.replace("-idle","-active");
			menuItems[no].className = tempClass;
			//activeMenuItem = defaultTabIndex;
			activeMenuItems[tabIndex] = defaultTabIndex;
		} else {
			menuItems[no].onclick = new Function("evt", "changeActiveItem('" + menuItems[no].id + "',"+callBackFn+","+tabIndex+");");
			menuItems[no].onmouseover = new Function("evt", "onMouseOver('" +menuItems[no].id+ "');");
			menuItems[no].onmouseout = new Function("evt", "onMouseOut('" +menuItems[no].id+ "');");
		}
	}
}
function isFunction (obj) {
	return (typeof obj == "function");
}



