﻿var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used  
var browser = {
    version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
    safari: /webkit/.test(userAgent),
    opera: /opera/.test(userAgent),
    msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
    mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
};
//alert('Browser Version=' + browser.version.toString() + ' Safari=' + (browser.safari ? true : false) + ' Opera=' + (browser.opera ? true : false) + ' IE=' + (browser.msie ? true : false) + ' FF=' + (browser.mozilla ? true : false));  
//end browser detection 
//needed to check for page
function isNullOrUndefined(obj) {
    try {
        return ((obj == null) || (obj == undefined) || (typeof (obj) == "undefined"));
    }
    catch (e) {
        return true;
    }
}
var Constants = {
    isIE: browser.msie ? true : false,
    isMoz: !this.isIE,
    mode: "prod"
};
function characterRemeiningCounter(counterId, TotalCharactersLength, CurrentCharactersLength) {
    var valueToCounter = 0;
    if (TotalCharactersLength - CurrentCharactersLength > 0) {
        valueToCounter = TotalCharactersLength - CurrentCharactersLength;
    }
    $('#' + counterId).html(valueToCounter);

}


function addComment(index) {  // index is the area where you pressed

    var commentTextArea = document.getElementById(index);
    if (commentTextArea == null || commentTextArea.value == '') {
        return;
    }

    // check against html

    if ((commentTextArea.value.indexOf("<") > -1) || (commentTextArea.value.indexOf(">") > -1)) {
        return;
    }

    if ((commentTextArea.value.indexOf("&lt;") > -1) || (commentTextArea.value.indexOf("&gt;") > -1)) {
        return;
    }

    var ReplaceComment = document.getElementById('ReplaceComments');

    var url = appPathScript + "/Animations/Player/UserControls/Comments/CommentServiceHq.aspx";
    var params = "command=2";

    params = params + "&type=" + CommentsUC.Type;
    params = params + "&source=" + CommentsUC.Source;
    params = params + "&text=" + commentTextArea.value;
    params = params + "&sid=" + Math.random();
    params += "&IsHiddenDeleteOption=" + CommentsUC.IsHiddenDeleteOption;
    params += "&ShowBorder=" + CommentsUC.ShowBorder;
    params += "&CommentsPerPage=" + CommentsUC.CommentsPerPage;

    if (CommentsUC.IsLongCommentText != '0') {
        params += "&isLong=1";
    }

    updateCommentsNumberBoomHeader(true);

    AJAX.replaceContent(ReplaceComment, url, params, false, false);

    commentTextArea.value = "";
}

// Add Replay -> after you press add in the enw replay window
// id of text box is the newly created
// id is NewReplay_CommentId
function addReplay(idOfTextBox) {


    try {
        var commentTextArea = document.getElementById(idOfTextBox);

        if (commentTextArea == null || commentTextArea.value == '') {
            return;
        }
        if ((commentTextArea.value.indexOf("<") > -1) || (commentTextArea.value.indexOf(">") > -1)) {
            return;
        }

        if ((commentTextArea.value.indexOf("&lt;") > -1) || (commentTextArea.value.indexOf("&gt;") > -1)) {
            return;
        }
    }
    catch (e) {
        return;
    }

    var commentId = CommentsUC.commentIdToDelete;
    var ReplaceComment = document.getElementById('ReplaceComments');

    var url = appPathScript + "/Animations/Player/UserControls/Comments/CommentServiceHq.aspx";
    var params = "command=3";
    params = params + "&type=" + CommentsUC.Type;
    params = params + "&source=" + CommentsUC.Source;
    params = params + "&text=" + commentTextArea.value;
    params = params + "&commentId=" + commentId;
    params = params + "&sid=" + Math.random();
    params += "&IsHiddenDeleteOption=" + CommentsUC.IsHiddenDeleteOption;
    params += "&ShowBorder=" + CommentsUC.ShowBorder;
    params += "&CommentsPerPage=" + CommentsUC.CommentsPerPage;
    if (CommentsUC.IsLongCommentText != '0') {
        params += "&isLong=1";
    }
    updateCommentsNumberBoomHeader(true);

    AJAX.replaceContent(ReplaceComment, url, params, false, false);
    commentTextArea.value = "";

}
function NextPage(pageId) {
    var ReplaceComment = document.getElementById('ReplaceComments');

    var url = appPathScript + "/Animations/Player/UserControls/Comments/CommentServiceHq.aspx";
    var params = "command=4";
    params = params + "&type=" + CommentsUC.Type;
    params = params + "&source=" + CommentsUC.Source;
    params = params + "&sid=" + Math.random();
    params = params + "&page=" + pageId;
    params += "&IsHiddenDeleteOption=" + CommentsUC.IsHiddenDeleteOption;
    params += "&ShowBorder=" + CommentsUC.ShowBorder;
    params += "&CommentsPerPage=" + CommentsUC.CommentsPerPage;
    if (CommentsUC.IsLongCommentText != '0') {
        params += "&isLong=1";
    }
    AJAX.replaceContent(ReplaceComment, url, params, false, false);
}
function ActualDeleteComment(CommentIdToDelete) {
    var commentId = CommentIdToDelete;
    debugger;
    var url = appPathScript + "/Animations/Player/UserControls/Comments/CommentServiceHq.aspx";
    var params = "command=0";
    params = params + "&commentId=" + commentId;
    params = params + "&type=" + CommentsUC.Type;
    params = params + "&source=" + CommentsUC.Source;
    params = params + "&sid=" + Math.random();
    params += "&ShowBorder=" + CommentsUC.ShowBorder;
    params += "&CommentsPerPage=" + CommentsUC.CommentsPerPage;
    if (CommentsUC.IsLongCommentText != '0') {
        params += "&isLong=1";
    }
    var ReplaceComment = document.getElementById('ReplaceComments');

    updateCommentsNumberBoomHeader(false);

    AJAX.replaceContent(ReplaceComment, url, params, false, false);
}

function ActualDeleteReplay(ReplayIdToDelete) {
    var ReplayId = ReplayIdToDelete;

    var url = appPathScript + "/Animations/Player/UserControls/Comments/CommentServiceHq.aspx";
    var params = "command=1";
    params = params + "&commentId=" + ReplayId;
    params = params + "&type=" + CommentsUC.Type;
    params = params + "&source=" + CommentsUC.Source;
    params = params + "&sid=" + Math.random();
    params += "&ShowBorder=" + CommentsUC.ShowBorder;
    params += "&CommentsPerPage=" + CommentsUC.CommentsPerPage;
    if (CommentsUC.IsLongCommentText != '0') {
        params += "&isLong=1";
    }
    var ReplaceComment = document.getElementById('ReplaceComments');

    updateCommentsNumberBoomHeader(false);

    AJAX.replaceContent(ReplaceComment, url, params, true, true);
}

//Just open pop up
function deleteComment(commentId) {
    CommentsUC.commentIdToDelete = commentId;
    showPopup('popup_confirm_deletion_comment');
}

// just open pop up
function deleteReplay(replayid) {
    CommentsUC.commentIdToDelete = replayid;
    showPopup('popup_confirm_deletion_replay');
}

function openReplyTextArea(commentId) {

    CommentsUC.commentIdToDelete = commentId;

    var divId = "replyTextboxDiv_" + commentId;

    divToDisplay = document.getElementById(divId);

    if (divToDisplay === null) {
        //id's are installed from ajax, hance they dont have the prefix
        //id of the current control
        divId = "CommentsUserControl_replyTextboxDiv_" + commentId;
        divToDisplay = document.getElementById(divId);
    }


    if (divToDisplay != null) {
        if (divToDisplay.style.display == 'none') {
            divToDisplay.style.display = '';
            divToDisplay.appendChild(buildTextArea(commentId));
        }
        else {
            divToDisplay.innerHTML = "";
            divToDisplay.style.display = 'none';
        }
    }
}
function switchImageIdOver(elem, imgPath) {

    elem.src = imgPath;

}
function switchImageIdOut(elem, imgPath) {
    elem.src = imgPath;
}

function buildTextArea(commentId) {
    var classLong = '';
    if (CommentsUC.IsLongCommentText != '0') {
        classLong = "maximum_width_Long_with";
    }

    var htmlstr = "<div><table cellpadding=\"0\" cellspacing=\"0\" width=\"96%\">"
				+ "<tr>"
				+ "<td style=\"background-color:#fafafa;border:1px solid #efefef;padding:5px;\" align=\"left\">"
				+ "<div>"
	            + "<textarea class=\"comments-replyTextBox\" id=\"replyTextArea_" + commentId + "\"></textarea>"
				+ "</div>"
				+ "</td>"
                + "</tr>"
                + "</table></div>"
                + "<div style=\"padding-top:10px;padding-right:20px;\">"

                + "<div style=\"width:80px;cursor:pointer;float:right; \" onclick=\"addReplay('replyTextArea_" + commentId + "');\">"
                + "<img onmouseout=\"switchImageIdOut(this,'http://icons.aniboom.com/Energy/Comments/PostButton.png');\" onmouseover=\"switchImageIdOver(this,'http://icons.aniboom.com/Energy/Comments/PostButton_over.png');\" style=\"\" src=\"http://icons.aniboom.com/Energy/Comments/PostButton.png\"/>&nbsp;"

                + "</div>"
                + "<div style=\"clear:both\"></div>"
                + "</div>"


    var span = document.createElement('span');
    span.innerHTML = htmlstr;
    return span;
}

function hidePopupsComments() {
    hidePopup('popup');
}
function revertCloseBtn(divObj) {
    divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('_over.gif', '.gif') : window.getComputedStyle(divObj, null).backgroundImage.replace('_over.gif', '.gif');
}


/*************************************************/
/*  set number of comment to header */
function updateCommentsNumberBoomHeader(IsPlus) {
    var numberComments;
    var objSpanCommentsNumber = document.getElementById('Booms_BoomComments_spanCommentsCount');
    if (!isNullOrUndefined(objSpanCommentsNumber)) {
        numberComments = parseInt(objSpanCommentsNumber.innerHTML);
        if (IsPlus) {
            objSpanCommentsNumber.innerHTML = numberComments + 1;
        }
        else {
            objSpanCommentsNumber.innerHTML = numberComments - 1;
        }
    }
}

/*************************************************/

function callHandleTabsComment(divObj, tabName, trId, isToDoCheckLeftBorderFunc) {
    handleTabsComment(divObj, tabName, document.getElementById(trId).childNodes[0].id, isToDoCheckLeftBorderFunc);
}

//like function handleTabs2(divObj, tabName) {     
var lastDivObj = -1;
var lastTabObj = -1;
function handleTabsComment(divObj, tabName, tdId, isToDoCheckLeftBorderFunc) {
    var tabObj = document.getElementById(tabName);
    if (tabObj.attributes["status"].value == 'open') {
        if (!isNullOrUndefined(tdId) && !isNullOrUndefined(isToDoCheckLeftBorderFunc) && (isToDoCheckLeftBorderFunc == 'True')) {
            checkLeftBorder(tdId, true);
        }

        new Effect.BlindUp(
	        tabObj,
	        {
	            duration: 0.5,
	            afterFinish: function() {
	                divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('minus', 'plus') : window.getComputedStyle(divObj, null).backgroundImage.replace('minus', 'plus'); ;
	                tabObj.attributes["status"].value = 'close';
	            }
	        }
	    );
    } else {
        if (lastDivObj != -1 && lastDivObj != divObj && lastTabObj.attributes["status"].value == 'open') {
            new Effect.BlindDown(
                            tabObj,
                            {
                                duration: 0.5,
                                afterFinish: function() {
                                    //   new Effect.ScrollTo(tabObj, {offset:-30});
                                    divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('plus', 'minus') : window.getComputedStyle(divObj, null).backgroundImage.replace('plus', 'minus');
                                    tabObj.attributes["status"].value = 'open';
                                    lastDivObj = divObj;
                                    lastTabObj = tabObj;
                                    if (!isNullOrUndefined(tdId) && !isNullOrUndefined(isToDoCheckLeftBorderFunc) && (isToDoCheckLeftBorderFunc == 'True')) {
                                        checkLeftBorder(tdId, false);
                                    }
                                }
                            }
                         );
        } else {

            new Effect.BlindDown(
	          tabObj,
	            {
	                duration: 0.5,
	                afterFinish: function() {
	                    divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('plus', 'minus') : window.getComputedStyle(divObj, null).backgroundImage.replace('plus', 'minus');
	                    tabObj.attributes["status"].value = 'open';
	                    lastDivObj = divObj;
	                    lastTabObj = tabObj;
	                    if (!isNullOrUndefined(tdId) && !isNullOrUndefined(isToDoCheckLeftBorderFunc) && (isToDoCheckLeftBorderFunc == 'True')) {
	                        checkLeftBorder(tdId, false);
	                    }
	                }
	            }
	         );
        }
    }
}
function loadDefault(obj) {
    obj.src = appPathAnimatorIcons + "/default.jpg";

}	