﻿///<reference path=lib/jquery-vsdoc.js />
///<reference path=WebUtils1.js />

function DisableControl(control) {
    if (Page_ClientValidate()) {
        var c = $get(control);
        c.disabled = true;
    }
}

function DisableControl(control, group) {
    if (Page_ClientValidate(group)) {
        var c = $get(control);
        c.disabled = true;
    }
}

function AjaxCall(url) {
    new Ajax.Request(url);
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function AddAjaxEndRequestHandlerWeb() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InitFlashPlayer);
}

var lastVideoUrl = null;
function InitFlashPlayer() {
    var hdnVideoUrl = $("#hdnVideoUrl")
    if (hdnVideoUrl != null) {
        var autoPlay = markSelectedPanel();
        if (lastVideoUrl != hdnVideoUrl.val()) {
            lastVideoUrl = hdnVideoUrl.val();
            var clipUrl = hdnVideoUrl.val();
            var playerUrl = 'FlashPlayer.swf';
            flashembed('videoPlaceHolder',
            { src: playerUrl, width: 168, height: 165, wmode: 'transparent' },
            {
                config: {
                    autoPlay: autoPlay,
                    controlBarBackgroundColor: '0x3b3a3a',
                    initialScale: 'scale',
                    autoRewind: true,
                    loop: false,
                    showFullScreenButton: false,
                    showStopButton: true,
                    showPlayButton: true,
                    showScrubber: true,
                    showVolumeSlider: false,
                    showMuteVolumeButton: false,
                    showMenu: false,
                    showPlayListButtons: false,
                    videoFile: clipUrl
                }
            });
        }
    }
}

function markSelectedPanel() {
    var autoPlay = false;

    var currentVideoPanel = getVideoPanel("hdnCurrentVideoPanelId");
    var prevVideoPanel = getVideoPanel("hdnPrevVideoPanelId");

    if (currentVideoPanel != null) {
        autoPlay = true;
        //currentVideoPanel.className = "pnlSelected";
    }

//    if (currentVideoPanel != prevVideoPanel) {
//        if (prevVideoPanel != null) {
//            prevVideoPanel.className = "pnlVideo";
//        }
//    }

    return autoPlay;
}

function getVideoPanel(hdnId) {
    var panel = null;
    var panelId = $("#" + hdnId).val();
    if (panelId != null && panelId != "") {
        panel = $("#" + panelId);
    }

    return panel;
}

function getPanel(panelObj) {
    var panel = null;
    if (panelObj != null) {
        if (panelObj.value != "") {
            panel = $(panelObj.value);
        }
    }

    return panel;
}

function ClosePanels(animPanel1, animPanel2) {
    $find(animPanel1).Start();
    $find(animPanel2).Start();
}

function OpenWindow(url) {
    window.open(url)
}

function HidePanel(panel) {
    $("#" + panel).css({ 'visibility': 'hidden',
        'display': 'none'
    });
}

function ShowOnePanel(panel) {
//    if (panel != 'panelEditVideo') {
//        HidePanel('panelEditVideo');
//    }
//    if (panel != 'addCommentPanel') {
//        HidePanel('addCommentPanel');
//    }
//    if (panel != 'tagPanel') {
//        HidePanel('tagPanel');
//    }

    HidePanel("userLinks");
    ShowPanel(panel)
}

function ShowPanel(panel) {
    $("#" + panel).css({
        'height': '100%',
        'display': 'block',
        'visibility': 'visible'
    });
}

function ShowAjaxControl(urlParam, placeToAppend) {
    $.ajax({
        url: urlParam,
        cache: false,
        success: function(html) {
            onAjaxSuccess(html, placeToAppend);
        }
    });
}

function onAjaxSuccess(html, placeToAppend) {
    $("#" + placeToAppend).append(html);
}

function HideAjaxControlPopup(controlDivId) {
    $("#" + controlDivId).remove();
}

function callWebService(data, method, onWSSuccess, onWSFailure) {
    $.ajax({
        type: "POST",
        url: "WebServices/VmagineWS.asmx/" + method,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onWSSuccess,
        error: onWSFailure
    });
}

function IncreaseNumberThumbsUp(contentId, imgThumbsUpId, lblNumberOfThumbsUpId) {
    func = function(result) { onIncreaseThumbsSuccess(result, imgThumbsUpId, lblNumberOfThumbsUpId); };
    callWebService("{contentId:" + contentId + "}", "IncreaseNumberThumbsUp", func, null);
}

function onIncreaseThumbsSuccess(result, imgThumbsUpId, lblNumberOfThumbsUpId) {
    $("#" + lblNumberOfThumbsUpId).text(result.d);
    $("#" + imgThumbsUpId).attr("disabled", true);
}

function showhide(objectToShowHide) {
    $("#" + objectToShowHide).toggleClass("divHideClass");
}

function DidNotGetMail(userMail, lblMessagesId) {
    var selectedValue = $('input[name=radioMail]:checked').val();
    var userCurMail = userMail;
    var mailToSend = userCurMail;

    if (selectedValue == "Change") {
        mailToSend = $("#txtNewMail").val();

        if (mailToSend == "") {
            setUserMessage("Please enter email", lblMessagesId);
            return;
        }
    }

    func = function(result) { onResendMailSuccess(result, lblMessagesId); };
    callWebService("{userCurEmail:'" + userCurMail + "', mailToSend:'" + mailToSend + "'}", "ResendMail", func, null);
}

function onResendMailSuccess(result, lblMessagesId) {
    setUserMessage(result.d, lblMessagesId);
}

function setUserMessage(msg, lblMessagesId) {
    $("#" + lblMessagesId).text(msg);
}

function setActiveTab(triggerId, containerId) {
    clearContainerElements();
    clearMessagesLabels();
    $("#" + triggerId).removeClass("Tab1");
    $("#" + triggerId).addClass("Tab0");

    $("#" + containerId).removeClass("divHideClass");
    $("#" + containerId).addClass("divShowClass");
}

function setMessagesLabel(text) {
    var lblMessagesId = $("#hdnPublishSettings").val();
    $("#" + lblMessagesId).text(text);
}

function clearMessagesLabels() {
    var lblMessages1Id = $("#hdnPublishSettings").val();
    var lblMessages2Id = $("#hdnLblAccessSettings").val();
    var lblMessages3Id = $("#hdnLblGeneralSettings").val();
    var lblMessages4Id = $("#hdnLblPersonSettings").val();
    $("#" + lblMessages1Id).text("");
    $("#" + lblMessages2Id).text("");
    $("#" + lblMessages3Id).text("");
    $("#" + lblMessages4Id).text("");
}

function clearContainerElements() {    
    $("#generalInfo").removeClass("divShowClass");
    $("#accessInfo").removeClass("divShowClass");
    $("#personalInfo").removeClass("divShowClass");
    $("#publishInfo").removeClass("divShowClass");

    $("#generalInfo").addClass("divHideClass");
    $("#accessInfo").addClass("divHideClass");
    $("#personalInfo").addClass("divHideClass");
    $("#publishInfo").addClass("divHideClass");

    $("#generalSettingsTrigger").removeClass("Tab0");
    $("#accessSettingsTrigger").removeClass("Tab0");
    $("#personalSettingsTrigger").removeClass("Tab0");
    $("#publishSettingsTrigger").removeClass("Tab0");

    $("#generalSettingsTrigger").addClass("Tab1");
    $("#accessSettingsTrigger").addClass("Tab1");
    $("#personalSettingsTrigger").addClass("Tab1");
    $("#publishSettingsTrigger").addClass("Tab1");
}

function TransferToPage(page) {
    location.href = page;
}

//////////////////////////////////////Publish Content//////////////////////////////////////////
function PublishContent(userId, contentId, service) {
    var publishServices = service;
    
    if (service == null) {
        publishServices = getPublishSelections('span.chkBoxPublish input');
    }    

    if (publishServices != null) {
        funcSuccess = function(result) { onVideoUploadCallSuccess(result); };
        funcFailure = function(result) { onVideoUploadCallFailure(result); };
        callWebService("{userIdStr:'" + userId + "', contentIdStr:'" + contentId + "', services:'" + publishServices + "'}", "PublishVideo", funcSuccess, funcFailure);

        ShowPopup('Popup/GeneralPopup.aspx?pt=6', 'Publishing video', 400, 100);
    }
}

function ShowRedirectToSettings(chkBoxId) {
    ShowPopup('Popup/GeneralPopup.aspx?pt=8', 'Publish video', 400, 100);
    chkBox = $("#" + chkBoxId);
    chkBox[0].checked = false;
}

function getPublishSelections(selectionClass) {
    var publishServices = null;

    $(selectionClass).each(function() {
    var element = this;
        if (element.checked) {
            if (publishServices != null) {
                publishServices += "-" + element.value;
            }
            else {
                publishServices = element.value;
            }
        }
    });

    return publishServices;
}

function onVideoUploadCallSuccess(result) {
    if (result.d == true) {
        HidePopup();
        ShowPopup('Popup/GeneralPopup.aspx?pt=9', 'Publish video', 400, 100);
    }
    else {
        actionOnError();
    }
}

function onVideoUploadCallFailure(result) {
    actionOnError();
}

function actionOnError() {
    HidePopup();
    ShowPopup('Popup/GeneralPopup.aspx?pt=7', 'Publish video', 400, 100);
}

function clearServicesCheckboxes() {
     $('span.chkBoxPublish input').each(function() {
        var element = this;
        element.checked = false;
     });
}

function ClosePublishPanel() {
    clearServicesCheckboxes();
    HidePanel('publishVideoPanel');
    ShowPanel('userLinks');
}

var btnAddComment;
var btnEdit;
var btnCancelComment;
var btnCancelEdit;
var btnSend;
var btnCancelSend;

function SetBtnsCommentState(btnCommentId, btnCancelCommentId, active, validateGroup){
    btnAddComment = btnCommentId;
    btnCancelComment = btnCancelCommentId;
    
    if (!active) {
        if (Validate(validateGroup)){
            $("#" + btnCommentId).attr("disabled", "true");
            $("#" + btnCancelCommentId).attr("disabled", "true");
        }
    }
    else {
        $("#" + btnCommentId).removeAttr("disabled");
        $("#" + btnCancelCommentId).removeAttr("disabled");
    }
}

function SetBtnsComment(active){
    if (btnAddComment != null && btnCancelComment != null){
        SetBtnsCommentState(btnAddComment, btnCancelComment, active, null);     
    }
}

function SetBtnsEditState(btnEditId, btnCancelEditId, active){
    btnEdit = btnEditId;
    btnCancelEdit = btnCancelEditId;
    
    if (!active) {
        $("#" + btnEditId).attr("disabled", "true");
        $("#" + btnCancelEditId).attr("disabled", "true");
    }
    else {
        $("#" + btnEditId).removeAttr("disabled");
        $("#" + btnCancelEditId).removeAttr("disabled");
    }
}

function SetBtnsEdit(active){
    if (btnEdit != null && btnCancelEdit != null){
        SetBtnsEditState(btnEdit, btnCancelEdit, active);     
    }
}

function SetBtnsSendState(btnSendId, btnCancelSendId, active){
    btnSend = btnSendId;
    btnCancelSend = btnCancelSendId;
    
    if (!active) {
        if (Validate(sendValidationGroup)){
            $("#" + btnSendId).attr("disabled", "true");
            $("#" + btnCancelSendId).attr("disabled", "true");
        }
    }
    else {
        $("#" + btnSendId).removeAttr("disabled");
        $("#" + btnCancelSendId).removeAttr("disabled");
    }
}

function SetBtnsSend(active){
    if (btnSend != null && btnCancelSend != null){
        SetBtnsSendState(btnSend, btnCancelSend, active);
        clearTxtValues();     
    }
}

function Validate(group){
    var isValid = false;
    
    if((group != null) && (group != ""))
    {
        isValid = Page_ClientValidate(group);
    }
    
    return isValid;
}

function ValidateRegisterPage(){
    var drpCountryId = $("#hdnDrpCountryId").val();
    var selectedValue = $("#" + drpCountryId).val();
    
    if (selectedValue == "Select your country"){
        $("#lblReqCountry").text("Required field");
    }
    else {
        $("#lblReqCountry").text("");
    }
    
    isValid = Page_ClientValidate("RegUpdate");
    
    return isValid;
}

var sendValidationGroup = "SendEmail";

function SetActiveSendType(selectedValue){
    clearTxtValues();
    switch(selectedValue){
        case 1:
            HidePanel("sendTypeCellphone");
            ShowPanel("sendTypeEmail");
            sendValidationGroup = "SendEmail";
            break;
        case 2:
            HidePanel("sendTypeEmail");
            ShowPanel("sendTypeCellphone");
            sendValidationGroup = "SendCellphone";
            break;
    }
}

function clearTxtValues(){
    var txtEmailId = $("#hdnTxtEmailId").val();
    $("#" + txtEmailId).val("");
    
    var txtCellphoneId = $("#hdnTxtCellphoneId").val();
    $("#" + txtCellphoneId).val("");
}

function ResetHdnSendType(defaultSendTypeId){
     SetActiveSendType(1);
     $("#" + defaultSendTypeId).attr("checked", "checked"); 
}

function ShareVideoToFacebook(videoUrl, thumbUrl, isContentOwner, videoLink) {
    var onPublishFeed = function() {
        PublishShareVideoFeed(sessionObj.uid, videoUrl, thumbUrl, isContentOwner, videoLink);
    }

    connectFbUser(onPublishFeed);
}

var uploadVideoFunc;
var postVideoFunc;
function UploadOrPostVideo(onUploadRequest, onPostRequest) {
    uploadVideoFunc = onUploadRequest;
    postVideoFunc = onPostRequest;
    ShowPopup('Popup/GeneralPopup.aspx?pt=12', 'Share on facebook', 400, 100);
}

function UploadVideo() {
    HidePopup();
    uploadVideoFunc();
}

function PostToProfile() {
    HidePopup();
    postVideoFunc();
}

