/// <reference path="../common/js/jquery-ui-1.8.23.custom.min.js" />

/// <reference path="../common/js/common.js" />

/// <reference path="contentview_VarDef.js" />

/// <reference path="contentview_CallApi.js" />

/// <reference path="contentview_GetData.js" />

/// <reference path="contentview_InitObjects.js" />

/// <reference path="contentview_Events.js" />


/* ----------------------------------- Create memo --------------------------------*/

var memoObjects = [];

/* constructor */
var MemoObject = function (id, text, x, y, width, heigth, imageUrl, action) {

};

/* setup each properties */
MemoObject.prototype.setup = function (id, text, x, y, w, h, imageUrl) {
    this.text = text;
    this.id = id;
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
    this.imageUrl = imageUrl;
};

/* prototype hitTest */
MemoObject.prototype.hitTest = function (px, py) {
    if (this.x <= px && px <= (this.x + this.width)) {
        if (this.y <= py && py <= (this.y + this.height)) {
            return true;
        }
    }
    return false;
};

/* draw Memo */
MemoObject.prototype.drawMemo = function (context, opt) {
    var posX = this.x;
    var posY = this.y;
    var objWidth = this.width;
    var objHeigth = this.height;
    var imageObj = new Image();

    imageObj.onload = function () {
        context.drawImage(imageObj, posX, posY, objWidth, objHeigth);
        flip(opt);
    };
    imageObj.src = this.imageUrl;

};

/* create memo object */
var memo = function (id, text, x, y, w, h, imageUrl) {
    this.setup(id, text, x, y, w, h, imageUrl);
    this.action = function () {
        EditMemo(id, 0, 0, $('#divDialogMemo'), function () {
            /*set flag change memo */
            ClientData.isChangedMemo(true);
        });
    };
};

memo.prototype = new MemoObject();

/*click showEditMemo() */
function showEditMemo() {
    objEditMemo.action();
};

/*click copyMemo() */
function copyMemo() {
    //isCopyMemo = true;
    ClientData.memo_copyText(objEditMemo.text);
    //disableControlsCopyMemo();
    $("#pop_up_memo").hide();
};


/* ----------------------------------- End create memo -----------------------------*/


/* ----------------------------------- Create webGetContentType4 --------------------------------*/

var webGetContentType4Objects = [];

/* constructor */
var WebGetContentTyp4Object = function (id, x, y, width, heigth, imageUrl, action) {

};

/* setup each properties */
WebGetContentTyp4Object.prototype.setup = function (id, x, y, w, h, imageUrl) {
    this.id = id;
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
    this.imageUrl = imageUrl;
};

/* prototype hitTest */
WebGetContentTyp4Object.prototype.hitTest = function (px, py) {
    if (this.x <= px && px <= (this.x + this.width)) {
        if (this.y <= py && py <= (this.y + this.height)) {
            return true;
        }
    }
    return false;
};

/* draw Memo */
WebGetContentTyp4Object.prototype.drawObject = function (context, opt) {
    var posX = this.x;
    var posY = this.y;
    var objWidth = this.width;
    var objHeigth = this.height;
    var imageObj = new Image();
    imageObj.onload = function () {
        context.globalAlpha = 0.4;

        context.drawImage(imageObj, posX, posY, objWidth, objHeigth);
        
        //Start Function : No.4 - Editor: Long - Date : 08/14/2013 - Summary : handle lazy loading
        flip(opt);
       //Start Function : No.4 - Editor: Long - Date : 08/14/2013 - Summary : handle lazy loading   
        context.globalAlpha = 1;
    };
    imageObj.src = this.imageUrl;

};

/* create memo object */
var webContentType4 = function (id, x, y, w, h, imageUrl, linkKind, destURI, destPageNumber) {
    this.setup(id, x, y, w, h, imageUrl);
    this.action = function () {
        if (linkKind == "1") {
            //START TRB00070
            window.onbeforeunload = null;
            //window.open(destURI, "_blank", "new window");
            window.open(destURI, "_self");
            
            setTimeout(function(){ToogleLogoutNortice();}, 200);
            
            //END TRB00070
            
        } 
        else if (linkKind == "0") {
            changePage(changePageNo(destPageNumber));
        }
    };
};
webContentType4.prototype = new WebGetContentTyp4Object();

/* ----------------------------------- End create webGetContentType4 -----------------------------*/

/**
* PageObject Class Definition 
*/

/* constructor */
var PageObject = function (id, imageUrl, x, y, w, h, visible, actionFunction, displayBorder) {
};

/* setup each properties */
PageObject.prototype.setup = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, actionFunction, displayBorder) {

    this.mediaType = mediaType;
    this.actionType = actionType;
    this.id = id;
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
    this.displayBorder = displayBorder;
    this.visible = visible;
    this.imageUrl = imageUrl;
    this.image = new Image();

    this.image.src = this.imageUrl;
};

/* prototype hitTest */
PageObject.prototype.hitTest = function (px, py) {
    if (this.x <= px && px <= (this.x + this.width)) {
        if (this.y <= py && py <= (this.y + this.height)) {
            return true;
        }
    }
    return false;
};
var check;
/* draw page Object */
//Start Function: No.4 - Editor : Long - Date: 08/09/2013 - Summary : Edit function to draw object on next page
PageObject.prototype.drawPageObject = function (context, opt) {
    
    var nObjectCnt = 0;
    
    //Check which pageObject to be count
    if(opt == null || opt == 0){
        nObjectCnt = pageObjects.length;
    }
    else if(opt == 1){
        nObjectCnt = nextPageObjects.length;
    }
    else if(opt == 2){
        nObjectCnt = prevPageObjects.length;
    }
    
    if (this.visible) {
        /* init data */
        var mediaType = this.mediaType;
        var displayBorder = this.displayBorder;
        var x = this.x;
        var y = this.y;
        var w = this.width;
        var h = this.height;
        var tempUrl = this.imageUrl;
        var isUsedTempImg = false;
        
        //START TRB00046 - Editor : Long - Date: 09/19/2013 - Summary : Fix for draw video object at center
        if(mediaType == '6' && tempUrl == 'img/iPad_video.png'){
            isUsedTempImg = true;
        }
        //END TRB00046 - Editor : Long - Date: 09/19/2013 - Summary : Fix for draw video object at center
        
        if (mediaType == '5') {
            nCountObjectLoad++;
            context.strokeStyle = "black";
            context.lineWidth = "1";
            context.strokeRect(x, y, w, h);
            
            flip(opt);
        }

        if (this.imageUrl != '') {
            /* init image  */
            var img = new Image();

            img.onload = function () {
                nCountObjectLoad++;


                /* create object store value image */
                var objTemp = [];
                objTemp.mediaType = mediaType;
                objTemp.img = img;
                objTemp.x = x;
                objTemp.y = y;
                objTemp.w = w;
                objTemp.h = h;
                objTemp.displayBorder = displayBorder;

                /* push to array */
                arrImage.push(objTemp);

                if (mediaType == '2') {/*  media */
                    img.width = 50;
                    img.height = 50;

                    if (img.width > w) {
                        context.drawImage(img, x, y, w, h);
                    } else {
                        /*draw image and border */
                        context.drawImage(img, x + ((w - img.width) / 2), y + ((h - img.height) / 2), img.width, img.height);
                        if (displayBorder == '1') {/* draw border */
                            context.strokeStyle = "black";
                            context.lineWidth = "1";
                            context.strokeRect(x, y, w, h);
                        }
                    }

                } 
                else if (mediaType == '3') {/* audio */
                    img.width = 50;
                    img.height = 50;
                    
                    //START TRB00035 - Editor : Long - Date: 09/17/2013 - Summary : Fix for draw audio object at center
                    context.drawImage(img, x + (w/2 - img.width/2), y + (h/2 - img.height/ 2), img.width, img.height);                 
                    //END TRB00035 - Editor : Long - Date: 09/17/2013 - Summary : Fix for draw audio object at center                                      
                    
                }
                else if(mediaType == '6' && isUsedTempImg){
                    img.width = 50;
                    img.height = 50;
                    
                    //START TRB00046 - Editor : Long - Date: 09/19/2013 - Summary : Fix for draw video object at center
                    context.drawImage(img, x + (w/2 - img.width/2), y + (h/2 - img.height/ 2), img.width, img.height);   
                                        
                    isUsedTempImg = false;              
                    //END TRB00046 - Editor : Long - Date: 09/19/2013 - Summary : Fix for draw video object at center  

                }
                else {
                    context.drawImage(img, x, y, w, h);
                }
                
                flip(opt);
                
                if (nCountObjectLoad >= nObjectCnt) {                   	

                    /* draw marking */
                    drawMarkingOnScreen(opt);

                    /*set status is loading page */
                    //Start Function : No.4 - Summary : make sure all page is loaded
                    if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                        if(totalPage > 1){
                            if(opt != 0 && opt != null){
                                isLoadingObject = false;
                            }
                        }
                        else{
                            isLoadingObject = false;
                        }                                          
                    }
                    else{
                         isLoadingObject = false;
                    }
                    //End Function : No.4 - Summary : make sure all page is loaded

                    /* enable controls in page */
                    if (ClientData.IsAddingMarking() == true) {
                        disableAllControl();
                        //Start Function : No.4 - Summary : make sure all page is loaded
                        if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                            if(totalPage > 1){
                                if(opt != 0 && opt != null){
                                    enableButtonMarking();
                                }   
                            }
                            else{
                                enableButtonMarking();
                            }                     
                        }
                        else{
                             enableButtonMarking();
                        }
                        //End Function : No.4 - Summary : make sure all page is loaded                       
                    } 
                    else {
                        //Start Function : No.4 - Summary : make sure all page is loaded
                        if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                            if(totalPage > 1){
                                if(opt != 0 && opt != null){
                                    enableAllControl();
                                }
                            }
                            else{
                                enableAllControl();
                            }                     
                        }
                        else{
                             enableAllControl();
                        }
                        //End Function : No.4 - Summary : make sure all page is loaded  
                    }

                    /*finish loading */
                     //Start Function : No.4 - Summary : make sure all page is loaded
                    if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                        if(totalPage > 1){
                            if(opt != 0 && opt != null){
                                 $('#divImageLoading').css('display', 'none');
                            }
                        }
                        else{
                            $('#divImageLoading').css('display', 'none');
                        }                                                  
                    }
                    else{
                          $('#divImageLoading').css('display', 'none');
                    }
                    //End Functi

                    /* set pos video */
                    zoomVideo();
                }
            };
            img.src = this.imageUrl;
        } 
        else {
            if (nCountObjectLoad >= nObjectCnt) {
                /* draw marking */
                drawMarkingOnScreen(opt);

                /*set status is loading page */
                isLoadingObject = false;
                /* enable controls in page */
                if (ClientData.IsAddingMarking() == true) {
                    disableAllControl();
                     //Start Function : No.4 - Summary : make sure all page is loaded
                    if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                        if(totalPage > 1){
                            if(opt != 0 && opt != null){
                                 enableButtonMarking();
                            }
                        }
                        else{
                            enableButtonMarking();
                        }                       
                    }
                    else{
                          enableButtonMarking();
                    }
                    //End Function : No.4 - Summary : make sure all page is loaded  
                } else {
                    //Start Function : No.4 - Summary : make sure all page is loaded
                    if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                        if(totalPage > 1){
                            if(opt != 0 && opt != null){
                                 enableAllControl();
                            }     
                        }
                        else{
                           enableAllControl(); 
                        }                   
                    }
                    else{
                          enableAllControl();
                    }
                    //End Function : No.4 - Summary : mak
                }

                /*finish loading */ 
                //Start Function : No.4 - Summary : make sure all page is loaded
                if(contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile){
                    if(totalPage > 1){
                        if(opt != 0 && opt != null){
                             $('#divImageLoading').css('display', 'none');
                        }    
                    }
                    else{
                        $('#divImageLoading').css('display', 'none');
                    }                    
                }
                else{
                      $('#divImageLoading').css('display', 'none');
                }
                //End Func
                /* set pos video */
                zoomVideo();
            }
        }
    }
};
//End Function: No.4 - Editor : Long - Date: 08/09/2013 - Summary : Edit function to draw object on next page

//Start Function : No.9

/*mediaType = 8 3d object*/
var _isEnableRotate = false;
var object3d = function (mediaType, actionType, id, imageUrl, x, y, w, h, hCnt, vCnt, visible, imageObjects){
    
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function (imagePt){      
                    
       if(_3dAction == _3dActionType.TouchStart_MouseDown){
           OnMouseDown3D(id, imagePt, x, y);
       }
       else if(_3dAction == _3dActionType.TouchMove_MouseMove){
           OnMouseMove3D(id, imagePt, x, y, w, h, hCnt, vCnt);
       }
       else if(_3dAction == _3dActionType.TouchEnd_MouseUp){
           OnMouseEnd3D(id, hCnt, vCnt);
       }              
    };
};
object3d.prototype = new PageObject();

//End Function : No.9

/* mediaType = 7 write text object*/
var text = function (mediaType, actionType, id, x, y, w, h, visible, actionFunction, content) {
    //var imageUrl = displayText(x,y,w,h,id,content);
    var imageUrl = getTextObjectImage(w, h, content);
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, actionFunction, null);
    this.action = function () {
    };
};
text.prototype = new PageObject();

/* mediaType=4  list image object : extends PageObject */
var listImage = function (mediaType, id, imageUrl, x, y, w, h, visible, imageObjects, triggerType) {
    this.setup(mediaType, null, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        if (triggerType == '1') {
            mediaType4_changeImage++;

            /* check index bigger than length object */
            if (mediaType4_changeImage > (imageObjects.length - 1)) {
                mediaType4_changeImage = 0;
            }

            /* draw image */
            var imageObj = new Image();
            imageObj.onload = function () {
                var canvasObject = document.getElementById("offscreen");
                var contextObject = canvasObject.getContext("2d");
                contextObject.drawImage(imageObj, x, y, w, h);
                flip();
            };
            imageObj.src = imageObjects[mediaType4_changeImage].fileName;
        }
    };
};
listImage.prototype = new PageObject();

/* mediaType=5 Replace Video object : extends PageObject */
var listVideo = function (mediaType, id, imageUrl, x, y, w, h, visible, videoObjects) {
    this.setup(mediaType, null, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
    };
};
listVideo.prototype = new PageObject();

/* mediaType=6 Trigger object : extends PageObject */
var trigger = function (mediaType, actionType, id, imageUrl, x, y, w, h, index, target, visible, actionFunction) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, actionFunction, null);
    this.action = function () {
        // draw object		

        for (var nIndex = 0; nIndex < objType4_5.length; nIndex++) {
            for (var nIndex1 = 0; nIndex1 < objType4_5[nIndex].dataObjects.length; nIndex1++) {
                if (objType4_5[nIndex].dataObjects[nIndex1].index == index
					&& objType4_5[nIndex].id == target) {
                    if (objType4_5[nIndex].mediaType == '4') {	/*image*/
                        changeImageType4(objType4_5, nIndex, nIndex1);
                    } else {   /* video */
                        stopAllAudio();
                        showVideoObject(objType4_5[nIndex].x, objType4_5[nIndex].y, objType4_5[nIndex].width, objType4_5[nIndex].height, objType4_5[nIndex].dataObjects[nIndex1].fileName, false);
                    }
                    break;
                }
            }
        }
    };
};
trigger.prototype = new PageObject();

//Start Function : No.9 - Editor : Long - Date: 08/16/2013 - Summary : 
var htmlLinkButton = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, resourceUrl) {

    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        /*stop audio on page */
        stopAllAudio();
        
        if (resourceUrl != "") {
            
            showHtml(resourceUrl);
            showDialog(true);
        }        
    };
};
htmlLinkButton.prototype = new PageObject();
function showHtml(url){
    var $container = $('#dialog');
    
    $container.html('');
    $container.attr('style', 'background-color: white; width:100%; left:0% !important'); 
                 
   //START TRB00076 - EDITOR : Long - Date : 09/24/2013 - Summary : Fix for scrolling on ipad
    if(avwUserEnvObj.os == "ipad"){       
        $container.html(
          '<h1>' 
        + '<img src="img/viewer/x.png" style="margin:3px 3px 0px 21px" id="btnClose" class="align_right" ></img>' 
        + '</h1>'
        + '<div id="html-wrap" style="height: 95%; position: absolute; width: 100%;">'
        + '<iframe src="'+ url +'" style="position: absolute; width: 100%; height: 100%; ">'
        + '</iframe>'
        + '</div>'
        );
        
        $('#html-wrap').css('overflow', 'scroll');
        $('#html-wrap').css('-webkit-overflow-scrolling', 'touch');
    }
    else{
        $container.html(
          '<h1>' 
        + '<img src="img/viewer/x.png" style="margin:3px 3px 0px 21px" id="btnClose" class="align_right" ></img>' 
        + '</h1>'
        + '<iframe src="'+ url +'" style="position: absolute; width: 100%; height: 95%; ">'
        + '</iframe>'
        );
    }
    //END TRB00076 - EDITOR : Long - Date : 09/24/2013 - Summary : Fix for scrolling on ipad 
    
    $('#dialog h1 img').click(function(){
        $container.removeAttr('style');
        hideDialog();
    });
}

var anket = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, questionNo, replyLimit, fullscreen, saveAs, objectId) {

    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        /*stop audio on page */
        stopAllAudio();
         
        var url = getURLPageImage("webEnqueteReply/init") + "/?abObjectId="+objectId + "&sid=" + ClientData.userInfo_sid() + '&pid=' + pid;
        
        var isFullScreen = false;
        if(parseInt(fullscreen) == 1){
            isFullScreen = true;
        }
        
        showAnket(url, isFullScreen);
        showDialog(true);
           
    };
};
anket.prototype = new PageObject();

//End Function : No.9 - Editor : Long - Date: 08/16/2013 - Summary : 

/* mediaType=3  Audio object : extends PageObject */
var audioType3 = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, audioFile, playType) {

    if (playType == "0") {
        this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
        this.action = function () {
            createAudio(audioFile, playType);
        };
    }
    else {
        //START : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
        if(playType == '2' || playType == 2){
            
        }
        else{
            createAudio(audioFile, playType);
        }
        //END : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
        this.setup(mediaType, actionType, id, null, x, y, w, h, visible, null, null);
        this.action = function () {
            
        };
    }
};
audioType3.prototype = new PageObject();

/* mediaType = 1 and actionType = 5 jumpPage object*/
var jumpPage = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, jumpPage) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        if ((Number(jumpPage) - 1) != getPageIndex()) {            
            createLockLayout(true);
            changePage(Number(jumpPage) - 1);                       
        }
    };
};

jumpPage.prototype = new PageObject();

/* mediaType = 1 and actionType = 6 send mail object*/
var sendMail = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, emailSubject, emailAddress) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        MailTo(emailAddress, emailSubject);
    };
};
sendMail.prototype = new PageObject();

/* mediaType = 1 and actionType = 8 open popup object*/
var openPopUp = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, content) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        isOpenPopUpText = true;
        var pt1 = imageToScreen(x, y + h / 2);

        if (userScale > 1) {
            OpenPopupText(posXPopupMemo, posYPopupMemo, content, $('#dialogPopUp'), $('#arrow'));
        } else if ($("#header_toolbar").is(":hidden")) {
            OpenPopupText(pt1.x, pt1.y - 65, content, $('#dialogPopUp'), $('#arrow'));
        } else {
            OpenPopupText(pt1.x, pt1.y, content, $('#dialogPopUp'), $('#arrow'));
        }
    };
};
openPopUp.prototype = new PageObject();

var moveContentParam = {};

/* mediaType = 1 and actionType = 10 move to Content object*/
var moveToContent = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, contentId, pageNo) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        var $dialog = $('#contentAudioConfirm');
        var $overlay = $('#overlay');
        
        $dialog.find("#txtContentAudio").html(i18nText("txtPreventLink"));
        $dialog.find("#txtContentAudio").attr("lang",'txtPreventLink');
        
        $dialog.center();
        $dialog.show();
        $overlay.show();
        $('#contentAudioConfirm_OK').click(function(){
            $overlay.hide();
            $dialog.hide();
            $dialog.find("#txtContentAudio").attr("lang",'txtContentAudio');
        });          
    };
    
};
moveToContent.prototype = new PageObject();
//START TRB00033 - EDITOR: Long - Date : 09/12/2013 - Summary : limit content
var alertMessageLevel = {
    ShowAlert : '1',
    RequirePassword : '2',
    None : '0'  
};

function showContentConfirmDialog(type, msg) {  
    
    if(type == alertMessageLevel.ShowAlert){
        createAlertTypeDialog(msg);
        showDialog(true);
    }
    else if(type == alertMessageLevel.RequirePassword){
        createPwdRequiredTypeDialog();
        showDialog(true);
    }
};

function createAlertTypeDialog(msg){
    var $container = $('#dialog');
    $container.html('');
    $container.addClass('sectionLimitAccess');

    $container.draggable({ handle: "h1" });
    $container.html(
    '  <h1 class="lang" lang="txtContentWarning">'+ i18nText("txtContentWarning") +'</h1>'
    +'  <p class="message">'
    +       msg     
    +'  </p>'
    +'  <p class="deletebtn">'
    +'      <a lang="dspOK" class="ok lang" id="contentAlertOk">OK</a>'
    +'      <a lang="dspCancel" class="cancel lang" id="contentAlertCancel">キャンセル</a>'
    +'  </p>'
    );               
    $container.center(); 
    $('#dialog a#contentAlertCancel').click(function(){
        hideDialog();        
        var removeTimeOut = setTimeout(function(){
            $container.removeClass('sectionLimitAccess');
        }, 200);      
    });
    
    $('#dialog a#contentAlertOk').click(function(){
        
        ClientData.common_preContentId(moveContentParam.preContentId);
        ClientData.common_prePageNo(moveContentParam.prePageNo);
        
        /* store old page */                                      
        var dataJump = ClientData.JumpQueue();                
        dataJump.push(moveContentParam.oldContent);                      
        ClientData.JumpQueue(dataJump);                  
        avwScreenMove(ScreenIds.ContentView);        
    });
};

function createPwdRequiredTypeDialog(){
    var $container = $('#dialog');
    $container.html('');
    $container.addClass('sectionLimitAccess');
    $container.draggable({ handle: "h1" });
    $container.html(
    '  <h1 class="lang" lang="txtContentPWTitle">'+ i18nText("txtContentPWTitle") +'</h1>'
    +' <p class="message">'
    +'  <label class="text lang" lang="txtContentPWMsg">'+ i18nText("txtContentPWMsg") +'</label>'
    +'      <input type="password" />'
    +'      <label class="error" id="lblMessageLimitError"></label> '  
    +'  </p>'
    +'  <p class="deletebtn">'
    +'      <a lang="dspOK" class="ok lang" id="contentAlertOk">OK</a>'
    +'      <a lang="dspCancel" class="cancel lang" id="contentAlertCancel">キャンセル</a>'
    +'  </p>'
    );           
    $container.center();       
    
    $('#dialog a#contentAlertCancel').click(function(){
        hideDialog();        
        var removeTimeOut = setTimeout(function(){
            $container.removeClass('sectionLimitAccess');
        }, 200);
        
    });

    
    $('#dialog a#contentAlertOk').click(
        function () {
            var password = $('.sectionLimitAccess .message input').val();
            if (!ValidationUtil.CheckRequiredForText(password)) {
                $('#lblMessageLimitError').html(i18nText('msgPwdEmpty')).show();
                return;
            }

            // start login
            var params = {
                previousSid: ClientData.userInfo_sid(),
                loginId: ClientData.userInfo_loginId_session(),
                password: password,
                urlpath: ClientData.userInfo_accountPath()
            };
            // Get url to login
            var sysSettings = avwSysSetting();
            var apiLoginUrl = sysSettings.apiLoginUrl;

            avwCmsApiSyncWithUrl(apiLoginUrl, null, 'webClientLogin', 'GET', params,
        function (data) {
            if (data.result == 'success') {                
                ClientData.common_preContentId(moveContentParam.preContentId);
                ClientData.common_prePageNo(moveContentParam.prePageNo);
                
                /* store old page */                                      
                var dataJump = ClientData.JumpQueue();                
                dataJump.push(moveContentParam.oldContent);                      
                ClientData.JumpQueue(dataJump);        
                
                
                // update sid id
                ClientData.userInfo_sid(data.sid);  
                        
                avwScreenMove(ScreenIds.ContentView);                                         
            }
            else {
                $('#lblMessageLimitError').html(format(i18nText('msgLoginErrWrong'), data.errorMessage).toString()).show();
            }
        },
        function (xhr, statusText, errorThrown) {
            var errorCode = 'E001';
            if (xhr.responseText && xhr.status != 0) {
                errorCode = JSON.parse(xhr.responseText).errorMessage;
            }
            $('#lblMessageLimitError').html(format(i18nText('msgLoginErrWrong'), errorCode).toString()).show();
        });

        }
    );
};
//END TRB00033 - EDITOR: Long - Date : 09/12/2013 - Summary : limit content

/* mediaType=2  Video object : extends PageObject */
var videoType2 = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, mediaFile, media, embed) {

    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, embed);
    this.action = function () {
        /*stop audio on page */
        stopAllAudio();
        if (embed == "1") {
            showVideoObject(x, y, w, h, mediaFile, false);
        } 
        else {
            if (media != '') {
                $('#playvideo').children().remove();

                createVideoFulScreen(mediaFile, id);

                $("#btnCloseVideo").click(function (e) {
                    hideDialog();
                    /*play audio if exist */
                    playAllAudio();
                });

                showDialog(true);
            }
        }
    };
};
videoType2.prototype = new PageObject();

/* mediaType = 1 and actionType = 0 linkURL object*/
var imageNoAction = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        //no action
    };
};
imageNoAction.prototype = new PageObject();

/* mediaType=1  Video object : extends PageObject */
var videoType1 = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, mediaFile, media) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        if (media != '') {
            /* stop all audio on page */
            stopAllAudio();

            createVideoFulScreen(mediaFile, id);

            $("#btnCloseVideo").click(function (e) {
                hideDialog();

                /* play audio  */
                playAllAudio();
            });

            showDialog(true);
        }
    };
};
videoType1.prototype = new PageObject();

/* mediaType=1 and actionType = 2  Audio object : extends PageObject */
var audioType1 = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, audioFile) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {
        createAudio(audioFile, "0")
    };
};
audioType1.prototype = new PageObject();

/* mediaType = 1 and actionType = 3 linkURL object*/
var linkURL = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, actionFunction, linkUrl, browserType) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, actionFunction, null);
    this.action = function () {
        window.open(linkUrl, "_blank", "new window");
    };
};
linkURL.prototype = new PageObject();

/* mediaType = 1 and actionType = 4 imagePreview object*/
var imagePreview = function (mediaType, actionType, id, imageUrl, x, y, w, h, visible, imagePreview) {
    this.setup(mediaType, actionType, id, imageUrl, x, y, w, h, visible, null, null);
    this.action = function () {

        createImagePreview();

        showImagePreview($('#divImagePreview'), imagePreview);

        $("#btnClose").live('click', hideDialog);


        showDialog(true);
    };
};
imagePreview.prototype = new PageObject();

/* rect object */
var Rect = function (x, y, w, h) {
    this.left = Math.floor(x);
    this.top = Math.floor(y);
    this.right = Math.floor(x + w);
    this.bottom = Math.floor(y + h);
    this.width = w;
    this.height = h;
};
Rect.prototype.add = function (x, y) {
    this.left = Math.floor(this.left + x);
    this.right = Math.floor(this.right + x);
    this.top = Math.floor(this.top + y);
    this.bottom = Math.floor(this.bottom + y);
    this.width = this.right - this.left;
    this.height = this.bottom - this.top;
};
Rect.prototype.center = function () {
    var x = (this.left + this.right) / 2;
    var y = (this.top + this.bottom) / 2;
    return new Point(Math.floor(x), Math.floor(y));
};

/**
* Content Class Definition 
*/
var Content = function () {

    this.pageCount = 0;
    this.pageIndex = 0;
    this.pageImages = null;
    this.pageObjects = null;
    this.currentPage = null;

};

/* ste page images (json data) */
Content.prototype.setPageImages = function (pageCount, imageUrls) {
    this.pageCount = pageCount;
    this.pageImages = imageUrls;
    return this;
};

/* set page objects data (json data) */
Content.prototype.setPageObjects = function (pageObjects) {
    this.pageObjects = pageObjects;
    return this;
};

/* Set up page */
Content.prototype.setUpPage = function (pageIndex, opt) {
    this.currentPage = createPage(
 							this.pageIndex,
							this.pageImages,
							this.pageObjects,
							function () {
							    adjustPage(opt);
							});
    return this;
};

/* check next page exists */
Content.prototype.hasNextPage = function () {

    if ((this.pageIndex + 1) > (this.pageCount - 1)) {
        return false;
    }
    return true;
};

/* setup next page */
Content.prototype.nextPage = function () {
    this.pageIndex++;
    if (this.pageIndex > (this.pageCount - 1)) {
        
        this.pageIndex = this.pageCount - 1;
    }
    this.setUpPage(this.pageIndex, 1);
    return this;
};

/* check previous page exists */
Content.prototype.hasPreviousPage = function () {
    if ((this.pageIndex - 1) < 0) {
        return false;
    }
    return true;
};

/* setup previous page */
Content.prototype.previousPage = function () {
    this.pageIndex--;
    if (this.pageIndex < 0) {
        this.pageIndex = 0;
    }
    this.setUpPage(this.pageIndex, 2);
    return this;
};

/* setup previous page */
Content.prototype.toPage = function (index) {
    this.pageIndex = eval(index);
    if (this.pageIndex < 0) {
        this.pageIndex = 0;
    }
    this.setUpPage(this.pageIndex);
    
    //START : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
    createPageBGM();
    //END : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
    return this;
};

/**
* Transition Class Definition 
*/
var Transition = function () { };

//Start Functon : No.4 - Editor : Long - Date : 08/09/2013 - Summary : 
/* Flip Next Page Transition */
Transition.prototype.flipNextPage = function () {
    
    var w = $('#canvasWrapper').width();
    removeObject();
    if(animateType == animateTypeKeys.Type_FadeIn){
        $('#mainNext').css('left', marginX + 'px');
        $('#mainNext').css('display', 'block');
        $('#main').css('z-index', '1');
    }
    else{
        $('#mainNext').css('display', 'block');
    }
         
    if (getContent().hasNextPage()) {
       
       if(animateType == animateTypeKeys.Type_Slide){
           $('#canvasWrapper').stop(true).animate(
                { left: '-' + w + 'px' },
                {
                    duration: animatePeriod,
                    complete: function () {                                                                                                                            
                              
                        assignCurrentContentPage(1);
                                                                                    
                        //Change id of canvas
                        switchCanvas(1);
                        
                        //restore canvas event
                        setDefaultEvent();
                        
                        //init image bookmark                       
                        initImageBookmark();
                                                                        
                        //Change slider
                        changeSlider(getContent().pageIndex);
                        
                        
                        //Draw marking on current screen
                        //drawMarkingOnScreen();                                            
                        
                        //Correct position of canvas for next flipping
                        correctCanvasPosition();
                       
                       //START : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
                       createPageBGM();
                       //END : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM                             
                        if(getPageIndex() < totalPage - 1){
                            
                            //Get Next Page Object
                            getNextPageObjectsByPageIndex(pageObjectsData, getPageIndex() + 1);
                                                        
                            //Load Next
                            renderNextPage();                                     
                            
                        }else{
                            //Enable all control
                            enableAllControl(); 
                            $('#divImageLoading').css('display', 'none');
                            isLoadingObject = false;
                        }                                                 
                                                                                                        
                        /* init image had draw */
                        initDisplayMarking();
                        
                        /* change title of page */
                        if (dataPageTitle[getContent().pageIndex]) {
                            if (dataPageTitle[getContent().pageIndex] != '') {
                                document.title = contentName + '_' + dataPageTitle[getContent().pageIndex] + ' | ' + i18nText('sysAppTitle');
                            } else {
                                document.title = contentName + ' | ' + i18nText('sysAppTitle');
                            }
                        } else {
                            document.title = contentName + ' | ' + i18nText('sysAppTitle');
                        }
                        /* set end log */
                        SetEndLog(contentID);
                        createLockLayout(false);
                        initImageCheckMarking();
                    }
                });
       }
       else if(animateType == animateTypeKeys.Type_FadeIn){
           $('#main').stop(true).fadeOut(
                {                   
                    duration: animatePeriod,
                    complete: function () {                                                                                                                            
                              
                        assignCurrentContentPage(1);
                                                                                    
                        //Change id of canvas
                        switchCanvas(1);
                        
                        //restore canvas event
                        setDefaultEvent();
                        
                        //init image bookmark                       
                        initImageBookmark();
                                                                        
                        //Change slider
                        changeSlider(getContent().pageIndex);
                        
                        //Draw marking on current screen
                        //drawMarkingOnScreen();                                            
                        
                        //Correct position of canvas for next flipping
                        correctCanvasPosition();
                                        
                                        
                        if(getPageIndex() < totalPage - 1){
                            
                            //Get Next Page Object
                            getNextPageObjectsByPageIndex(pageObjectsData, getPageIndex() + 1);
                                                        
                            //Load Next
                            renderNextPage();
                                         
                        }
                        else{
                            //Enable all control
                            enableAllControl(); 
                            $('#divImageLoading').css('display', 'none');
                            isLoadingObject = false;
                        }                                                
                                 
                        /* init image had draw */
                        initDisplayMarking();
                                      
                        /* change title of page */
                        if (dataPageTitle[getContent().pageIndex]) {
                            if (dataPageTitle[getContent().pageIndex] != '') {
                                document.title = contentName + '_' + dataPageTitle[getContent().pageIndex] + ' | ' + i18nText('sysAppTitle');
                            } else {
                                document.title = contentName + ' | ' + i18nText('sysAppTitle');
                            }
                        } else {
                            document.title = contentName + ' | ' + i18nText('sysAppTitle');
                        }
                        /* set end log */
                        SetEndLog(contentID);
                        createLockLayout(false);
                        initImageCheckMarking();
                    }
                });
        }
        initImageCheckMarking();          
    }
};
/* Flip Previous Page Transition */
Transition.prototype.flipPreviousPage = function () {
    var w = $('#canvasWrapper').width();
    removeObject();
    if(animateType == animateTypeKeys.Type_FadeIn){
        $('#mainPre').css('left', marginX + 'px');
        $('#mainPre').css('display', 'block');
        $('#main').css('z-index', '1');
    }
    else{
        $('#mainPre').css('display', 'block');
    }
    if (getContent().hasPreviousPage()) {       
		if(animateType == animateTypeKeys.Type_Slide){
			$('#canvasWrapper').stop(true).animate(
                { left: w + 'px' },
                {
                    duration: animatePeriod,
                    complete: function () {                                                                                                                            
                              
                        assignCurrentContentPage(2);
                                                                                    
                        //Change id of canvas
                        switchCanvas(2);
                        
                        //restore canvas event
                        setDefaultEvent();
                        
                        //init image bookmark                       
                        initImageBookmark();
                                                                        
                        //Change slider
                        changeSlider(getContent().pageIndex);
                        
                        //Draw marking on current screen
                        //drawMarkingOnScreen();                                            
                        
                        //Correct position of canvas for next flipping
                        correctCanvasPosition();
                        
                        //START : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM              
                        createPageBGM();
                        //END : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
                        if(getPageIndex() > 0){
                            
                            //Get Next Page Object
                            getPrevPageObjectsByPageIndex(pageObjectsData, getPageIndex() - 1);
                                                        
                            //Load Next
                            renderPrevPage();
                        }else{
                            //Enable all control
                            enableAllControl(); 
                            $('#divImageLoading').css('display', 'none');
                            isLoadingObject = false;
                        }                                                                            
                               
                        /* init image had draw */
                        initDisplayMarking();
                        /* change title of page */
                        if (dataPageTitle[getContent().pageIndex]) {
                            if (dataPageTitle[getContent().pageIndex] != '') {
                                document.title = contentName + '_' + dataPageTitle[getContent().pageIndex] + ' | ' + i18nText('sysAppTitle');
                            } else {
                                document.title = contentName + ' | ' + i18nText('sysAppTitle');
                            }
                        } else {
                            document.title = contentName + ' | ' + i18nText('sysAppTitle');
                        }
                        /* set end log */
                        SetEndLog(contentID);
                        createLockLayout(false);
                        initImageCheckMarking();
                    }
                });
        }
        else{
            $('#main').stop(true).fadeOut(
                {
                    duration: animatePeriod,
                    complete: function () {                                                                                                                            
                              
                        assignCurrentContentPage(2);
                                                                                    
                        //Change id of canvas
                        switchCanvas(2);
                        
                        //restore canvas event
                        setDefaultEvent();
                        
                        //init image bookmark                       
                        initImageBookmark();
                                                                        
                        //Change slider
                        changeSlider(getContent().pageIndex);
                        
                        //Draw marking on current screen
                        //drawMarkingOnScreen();                                            
                        
                        //Correct position of canvas for next flipping
                        correctCanvasPosition();
                                        
                                        
                        if(getPageIndex() > 0){
                            
                            //Get Next Page Object
                            getPrevPageObjectsByPageIndex(pageObjectsData, getPageIndex() - 1);
                                                        
                            //Load Next
                            renderPrevPage();
                        }else{
                            //Enable all control
                            enableAllControl(); 
                            $('#divImageLoading').css('display', 'none');
                            isLoadingObject = false;
                        }                                                                        
                                                       
                        /* init image had draw */
                        initDisplayMarking();
     
                        /* change title of page */
                        if (dataPageTitle[getContent().pageIndex]) {
                            if (dataPageTitle[getContent().pageIndex] != '') {
                                document.title = contentName + '_' + dataPageTitle[getContent().pageIndex] + ' | ' + i18nText('sysAppTitle');
                            } else {
                                document.title = contentName + ' | ' + i18nText('sysAppTitle');
                            }
                        } else {
                            document.title = contentName + ' | ' + i18nText('sysAppTitle');
                        }
                        /* set end log */
                        SetEndLog(contentID);
                        createLockLayout(false);
                        initImageCheckMarking();
                    }
                });
        }
        
    }
};
//Start Functon : No.4 - Editor : Long - Date : 08/09/2013 - Summary : 
/* Flip  Page Transition */
Transition.prototype.flipToPage = function (index) {

    var w = $('#main').attr('width');
    
    if (index == getContent().pageIndex) {
        /* init image had draw */
        initDisplayMarking();       
    } else if (index < getContent().pageIndex) {
        context_main.clearRect(0, 0, canvas_main.width, canvas_main.height);
        removeObject();
        $('#main').animate(
				{ left: w + 'px' },
				{
				    duration: animatePeriod,
				    complete: function () {
				        $('#main').css('left', -w + 'px');
				        $('#main').animate(
							{ left: marginX + 'px' },
							{ duration: animatePeriod,
							    complete: function () {
							        isFirstLoad = true;
							        //screenFit();
							    }
							});
				        //change pageIndex and image bookmark
				        userScale = 1;
                        //START TRB00097
                        //changeScale(userScale);   
                        srcRect = new Rect(0, 0, 0, 0);
                        srcRectNext = new Rect(0, 0, 0, 0);
                        srcRectPrev = new Rect(0, 0, 0, 0);
                        
                        destRect = new Rect(0, 0, 0, 0);
                        destRectNext = new Rect(0, 0, 0, 0);
                        destRectPrev = new Rect(0, 0, 0, 0);
                        
                        //END TRB00097  
				        checkDisableButtonZoom();
				        getContent().toPage(index);				         
				        
                        if(index > 0){
                            prevContent.pageIndex = getPageIndex();
                            getPrevPageObjectsByPageIndex(pageObjectsData, index - 1);               
                            renderPrevPage();
            
                        }
                        
                        if(index < totalPage - 1){
                            nextContent.pageIndex = getPageIndex();
                            getNextPageObjectsByPageIndex(pageObjectsData, index + 1);
                            renderNextPage();
                        }
				    
				        initImageBookmark();
				        checkExistNextPrePage();
				        changeSlider(index);
				        /* init image had draw */
				        initDisplayMarking();
				        /* change title of page */
				        if (dataPageTitle[index]) {				            
				            if (dataPageTitle[index] != '') {
				                document.title = contentName + '_' + dataPageTitle[index] + ' | ' + i18nText('sysAppTitle');
				            } else {
				                document.title = contentName + ' | ' + i18nText('sysAppTitle');
				            }
				        } else {
				            document.title = contentName + ' | ' + i18nText('sysAppTitle');
				        }
				        /* set end log */
				        SetEndLog(contentID);
				        initImageCheckMarking();
				        
				        createLockLayout(false);
				    }
				}
			 );
    } 
    else {
        context_main.clearRect(0, 0, canvas_main.width, canvas_main.height);
        removeObject();
        $('#main').animate(
				{ left: '-' + w + 'px' },
				{
				    duration: animatePeriod,
				    complete: function () {
				        $('#main').css('left', w + 'px');
				        $('#main').animate(
							{ left: marginX + 'px' },
							{ duration: animatePeriod,
							    complete: function () {
							        isFirstLoad = true;
							        //screenFit();
							    }
							});
				        //change pageIndex and image bookmark
				        userScale = 1;
				        //START TRB00097
                        //changeScale(userScale);   
                        srcRect = new Rect(0, 0, 0, 0);
                        srcRectNext = new Rect(0, 0, 0, 0);
                        srcRectPrev = new Rect(0, 0, 0, 0);
                        
                        destRect = new Rect(0, 0, 0, 0);
                        destRectNext = new Rect(0, 0, 0, 0);
                        destRectPrev = new Rect(0, 0, 0, 0);
                        
                        //END TRB00097  
                        checkDisableButtonZoom();
				        getContent().toPage(index);    
				        
				        if(index > 0){
				            prevContent.pageIndex = getPageIndex();
				            getPrevPageObjectsByPageIndex(pageObjectsData, index - 1);				  
				            renderPrevPage();
			
				        }
				        
				        if(index < totalPage - 1){
				            nextContent.pageIndex = getPageIndex();
				            getNextPageObjectsByPageIndex(pageObjectsData, index + 1);
				            renderNextPage();
				        }

				        initImageBookmark();
				        checkExistNextPrePage();
				        changeSlider(index);
				        /* init image had draw */
				        initDisplayMarking();
				        /* change title of page */
				        if (dataPageTitle[index]) {
				            if (dataPageTitle[index] != '') {
				                document.title = contentName + '_' + dataPageTitle[index] + ' | ' + i18nText('sysAppTitle');
				            } else {
				                document.title = contentName + ' | ' + i18nText('sysAppTitle');
				            }
				        } else {
				            document.title = contentName + ' | ' + i18nText('sysAppTitle');
				        }
				        /* set end log */
				        SetEndLog(contentID);
				        initImageCheckMarking();
				        
				        createLockLayout(false);
				    }
				});
    }   
    
};

function createTextConfirmAudio() {
    var text = i18nText('msgBGMPlayConfirm');
    $('#txtAudio').html(text);

    var text = i18nText('msgBGMPagePlayConfirm');
    $('#txtAudio_page').html(text);
};

/* create page */
function createPage(pageNumber, pageImage, pageObjects, onCompleteFunc) {

    var page = new ContentPage(pageNumber, pageImage);
    /* add page Object */
    //Start Function: No.12
    if (contentType == ContentTypeKeys.Type_PDF || contentType == ContentTypeKeys.Type_NoFile) {
        page.addPageObjects(pageObjects);
    }
    //End Function: No.12
    /* load page */
    page.load(onCompleteFunc);

    return page;
};

function createImagePreview() {
    $('#dialog').prepend(
		'<h1>' +
		'<img src="img/viewer/x.png" style="margin:3px 3px 0px 21px" id="btnClose" class="align_right" ></img>' +
		'</h1>' +
		'<div id="divImagePreview" class="content_image_preview">' +
		'</div>'
	);
    $('#dialog').css('box-shadow', '0px 0px 3px 1px #666');
    $('#dialog').css('border', 'solid 1px #666');

    $('#dialog').draggable({ handle: "h1" });
};

/* create video on page */
function createVideoFulScreen(video, id) {
    $('#dialog').css('box-shadow', '0px 0px 3px 1px #666');
    $('#dialog').css('border', 'solid 1px #666');
    $('#dialog').draggable({ handle: "h1" });

    $('#dialog').children().remove();
    if (ClientData.userOpt_videoMode() == "1") {/* loop video */
        $('#dialog').html(
			'<h1>' +
			'	<img src="img/viewer/x.png" style="margin:3px 3px 0px 21px" id="btnCloseVideo" class="align_right" ></img>' +
			'</h1>' +
			'		<div id="videoMedia' + id + '">' +
			'			<video style="height:90%; width:100%;" class="mov" id="videoOnPopUp" autoplay controls loop>' +
			'			 <source src="' + video + '" type="video/mp4"> ' +
	  		'    </video>' +
			'		</div>'
			);
    } else {
        $('#dialog').html(
			'<h1>' +
			'	<img src="img/viewer/x.png" style="margin:3px 3px 0px 21px" id="btnCloseVideo" class="align_right" width=25px" ></img>' +
			'</h1>' +
			'		<div id="videoMedia' + id + '">' +
			'			<video style="height:90%; width:100%;" id="videoOnPopUp" class="mov" autoplay controls>' +
			'			 <source src="' + video + '" type="video/mp4"> ' +
	  		'    </video>' +
			'		</div>'
			);
    }
    $('#videoOnPopUp').css('height', ($('#dialog').height() - 25) + "px");
    //showControlsVideo($('#videoOnPopUp'));
};

/* create audio on page */
function createAudio(audio, type) {
    // if(avwUserEnvObj.browser == 'firefox'){
    // audio =  audio.replace("webResourceDownload/", "webResourceDownload");
    // }

    if (audio != '') {
        if (type == "0") { /* play when click */
            stopAllAudio();

            TypeAudio0 = type;
            srcAudioType0 = audio;

            if (ClientData.userOpt_musicMode() == "1") {
                if (avwUserEnvObj.browser == 'firefox') {
                    $('#playaudio').html(
						'	 <embed id="play_audio_0" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" loop="true" hidden="true"> </embed>'
					);
                } else if (avwUserEnvObj.os == "ipad") {
                    $('#playaudio').html(
						'	<audio  id="play_audio_0" loop="loop"  >' +
						'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
						'	</audio>'
						);
                } else {
                    $('#playaudio').html(
						'	<audio  id="play_audio_0" loop="loop"  autoplay ="autoplay">' +
						'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
						'	</audio>'
					);
                }

            } else {
                if (avwUserEnvObj.browser == 'firefox') {
                    $('#playaudio').html(
						'	 <embed id="play_audio_0" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" hidden="true"> </embed>'
					);
                } else if (avwUserEnvObj.os == "ipad") {
                    $('#playaudio').html(
						'	<audio  id="play_audio_0">' +
						'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
						'	</audio>'
						);
                } else {
                    $('#playaudio').html(
						'	<audio  id="play_audio_0" autoplay ="autoplay">' +
						'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
						'	</audio>'
					);
                }

            }

            if (avwUserEnvObj.os == "ipad") {
                document.getElementById("play_audio_0").load();
                document.getElementById("play_audio_0").play();
            }

        } 
        else if (type == "1") { /* play all page */        
           
            if (document.getElementById("play_audio_1") != undefined) {

                if (ClientData.userOpt_musicMode() == "1") {
                    if (avwUserEnvObj.browser == 'firefox') {
                        /*
                        $('#playaudioallpage').html(
                                                    '	 <embed id="play_audio_1" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" loop="true"  hidden="true"> </embed>'
                                                );*/
                        
                    } 
                    else {
                        if(!isPendingContentBGM){
                            document.getElementById("play_audio_1").play();
                        }                       
                    }
                }
                else {
                    if (avwUserEnvObj.browser == 'firefox') {
/*
                        $('#playaudioallpage').html(
							'	 <embed id="play_audio_1" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" hidden="true"> </embed>'
						);*/

                    } else {
                        if(!isPendingContentBGM){
                            document.getElementById("play_audio_1").play();
                        }
                    }
                }
            } 
            else {
                TypeAudio1 = type;
                srcAudioType1 = audio;

                if (ClientData.userOpt_musicMode() == "1") {                                        
                    if (avwUserEnvObj.browser == 'firefox') {
                        $('#playaudioallpage').html(
							'	 <embed id="play_audio_1" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" loop="true"  hidden="true"> </embed>'
						);

                    } 
                    else if (avwUserEnvObj.os == "ipad") {
                        $('#playaudioallpage').html(
							 '	<audio  id="play_audio_1" loop="loop" >' +
							 '		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
							 '	</audio>'
						 );
                    }
                    else {                      
                        if(isPendingContentBGM){
                            $('#playaudioallpage').html(
                                 '  <audio  id="play_audio_1" loop="loop" >' +
                                 '       <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
                                 '  </audio>'
                             );
                             document.getElementById('play_audio_1').load();
                             isPendingContentBGM = false;
                             
                        }
                        else{
                            $('#playaudioallpage').html(
                                 '  <audio  id="play_audio_1" loop="loop" autoplay="autoplay" >' +
                                 '       <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
                                 '  </audio>'
                             );
                        }
                    }
                }
                else {
                    if (avwUserEnvObj.browser == 'firefox') {
                        $('#playaudioallpage').html(
							'	 <embed id="play_audio_1" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" hidden="true"> </embed>'
						);
                    } else if (avwUserEnvObj.os == "ipad") {
                        $('#playaudioallpage').html(
							 '	<audio  id="play_audio_1" >' +
							 '		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
							 '	</audio>'
						 );
                    } else {
                        if(isPendingContentBGM){
                            $('#playaudioallpage').html(
                                 '  <audio  id="play_audio_1" >' +
                                 '       <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
                                 '  </audio>'
                             );
                             document.getElementById('play_audio_1').load();
                             isPendingContentBGM = false;
                             
                        }
                        else{
                            $('#playaudioallpage').html(
                                 '  <audio  id="play_audio_1" autoplay="autoplay" >' +
                                 '       <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
                                 '  </audio>'
                             );
                        }
                    }
                }
                // Check to play BGM on ipad
                if (avwUserEnvObj.os == "ipad") {
                    //START TRB00069
                    if(isPendingContentBGM){
                        document.getElementById('play_audio_1').load();
                        isPendingContentBGM = false;
                    }
                    else{
                        // Show confim message to play background music
                        $("#bgmConfirm_dspCancel").unbind('click');
                        $("#bgmConfirm_dspCancel").click(function () {
                            $("#bgmConfirm").hide();
                        });
                        $("#bgmConfirm_dspOK").unbind('click');
                        $("#bgmConfirm_dspOK").click(function () {
                            document.getElementById('play_audio_1').load();
                            document.getElementById('play_audio_1').play();
                            $("#bgmConfirm").hide();
                        });
                        $("#bgmConfirm").show();
                        $("#bgmConfirm").center();
                    }
                    //END TRB00069
                }
            }
        }
        else if (type == "2") { /* play on page */                    
           
            if (document.getElementById("play_audio_2") == undefined) {
                stopAllAudio();

                TypeAudio2 = type;
                srcAudioType2 = audio;

                if (ClientData.userOpt_musicMode() == "1") {
                    if (avwUserEnvObj.browser == 'firefox') {
                        $('#playaudiopage').html(
								'	 <embed id="play_audio_2" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" loop="true" hidden="true"> </embed>'
							);
                    } else if (avwUserEnvObj.os == "ipad") {
                        $('#playaudiopage').html(
							'	<audio  id="play_audio_2" loop="loop" >' +
							'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
							'	</audio>'
						);
                    } else {
                        $('#playaudiopage').html(
							'	<audio  id="play_audio_2" loop="loop" autoplay ="autoplay">' +
							'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
							'	</audio>'
						);
                    }
                } else {
                    if (avwUserEnvObj.browser == 'firefox') {
                        $('#playaudiopage').html(
								'	 <embed id="play_audio_2" src="' + audio + '&key=' + (new Date()).toIdString() + '" autostart="true" hidden="true"> </embed>'
							);
                    } else if (avwUserEnvObj.os == "ipad") {
                        $('#playaudiopage').html(
							'	<audio  id="play_audio_2" >' +
							'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
							'	</audio>'
						);
                    } else {
                        $('#playaudiopage').html(
							'	<audio  id="play_audio_2" autoplay ="autoplay">' +
							'		 <source src="' + audio + '&key=' + (new Date()).toIdString() + '" type="audio/mpeg" />' +
							'	</audio>'
						);
                    }
                }

                // Check to play BGM on ipad
                if (avwUserEnvObj.os == "ipad") {
                    // Show confim message to play background music
                    $("#bgmConfirm_dspCancel_page").unbind('click');
                    $("#bgmConfirm_dspCancel_page").click(function () {
                        $("#bgmConfirm_page").hide();
                    });
                    $("#bgmConfirm_dspOK_page").unbind('click');
                    $("#bgmConfirm_dspOK_page").click(function () {
                        document.getElementById('play_audio_2').load();
                        document.getElementById('play_audio_2').play();
                        $("#bgmConfirm_page").hide();
                    });
                    $("#bgmConfirm_page").show();
                    $("#bgmConfirm_page").center();
                }
            }
        }
    }
    else {
        stopAllAudio();
    }

};

//START : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM
function createPageBGM(){
    
    var currentPageObject = getContent().pageObjects;
    var audioFile  = '';
    var playType = '';
    $('#playaudiopage').html('');
    for(var i = 0; i < currentPageObject.length; i++){
        var BGMObject = currentPageObject[i];
        
        if(BGMObject.mediaType == '3' && BGMObject.actionType=='2' && BGMObject.playType == '2'){           
            
            audioFile = BGMObject.audioFile;
            playType = BGMObject.playType;
            
            createAudio(audioFile, playType);
            
            break;
        }
    }                
};
//END : TRB00028 - Editor: Long - Date: 09/10/2013 - Summary : Fix For Page BGM