﻿///<reference path=jquery-vsdoc.js />
///<reference path=jquery.simplemodal.js />

//function showPopup(urlParam) {
//    var html = "<iframe id='popup_iframe' src='" + urlParam + "' width='483px' height='330px' scrolling='no' frameborder='0' />";
//    $.modal(html);
//}

//function showPopup(urlParam) {
//    $.ajax({
//        url: urlParam,
//        cache:false,
//        success: function(html) {
//        $.modal(html);
//        }
//    });
//}

//================= Popup ====================//
function OpenProfilePopup(param, userName) {
    ShowPopup('Popup/NonFriendProfilePopup.aspx?' + param, 'User Profile', 500, 200);
}

function ShowConfirmPopup(urlParam, header, width, height, onConfirm) {
    popupOnConfirm = onConfirm;
    ShowPopup(urlParam, header, width, height);
}

var popupOnConfirm;
function ConfirmAndHidePopup() {
    HidePopup();
    popupOnConfirm();
}

function ShowPopup(urlParam, header, popupWidth, popupHeight) {
    var defaultHeight = 140;
    var defaultWidth = 430;

    if (popupWidth != null) {
        width = popupWidth;
    }
    else {
        width = defaultWidth;
    }

    if (popupHeight != null) {
        height = popupHeight;
    }
    else {
        height = defaultHeight;
    }

    var html = getPopupHtml(urlParam, width, height, header);
    $("#popupContainer").html(html);
}

function getPopupHtml(urlParam, width, height, header) {
    iframeHeight = height - 20;
    iframeWidth = width - 20;
    bgHeight = $(document).height();
    
    var iframeHtml = '<iframe id="popup_iframe" name="popup_iframe" src="' + urlParam + '" width="' + iframeWidth + 'px" height="' + iframeHeight + 'px" scrolling="no" frameborder="0"></iframe>';

    var html = '<div class="popupBg" style="height:' + bgHeight + 'px"></div><div class="popup" style="width:' + width + 'px;height:' + height + 'px;"><center>';
    html += '<table class="popup_table" cellpadding="0" cellspacing="0">';
    html += '<tr><td class="popup_top_left" /><td class="popup_top_mid">' + header + '</td><td class="popup_top_right" onclick="HidePopup()"/></tr>';
    html += '<tr><td class="popup_mid_left" /><td style="background-color:white;" >';
    html += iframeHtml + '</td><td class="popup_mid_right" /></tr>';
    html += '<tr><td class="popup_bottom_left" /><td class="popup_bottom_mid" /><td class="popup_bottom_right" /></tr></table></center></div>';

    return html;
}


function HidePopup() {
    $("#popupContainer").html("");
}

function ConfirmDelete(filename, postbackControl) {
    var onConfirm = function() { __doPostBack(postbackControl, ''); 
    };

    ShowConfirmPopup('Popup/GeneralPopup.aspx?pt=10&v=' + filename, 'Delete video?', 400, 170, onConfirm);
}


function PlayVideo(clipUrl, placeHolderId, autoPlay, relativePath) {
    if (autoPlay == undefined) {
        autoPlay = true;
    }

    playerSrc = 'FlashPlayer.swf';
    if (relativePath != undefined) {
        playerSrc = relativePath + playerSrc;
    }

    flashembed(placeHolderId, { src: playerSrc, 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
        }
    });
}

var sessionObj = null;
var isFbInitialized = false;
var onSessionCreated;

function connectFbUser(onSessionCreatedFunc) {
    onSessionCreated = onSessionCreatedFunc;

    if (sessionObj == null) {
        if (isFbInitialized) {
            callFBConnectFunc(function() { loginUser });
        }
        else {
            initConnectUser(initSession, null, loginUser);
            isFbInitialized = true;
        }
    }
    else {
        onSessionCreatedFunc();
    }
}


function initSession(session) {
    sessionObj = session;
    onSessionCreated();
}

function SignInFBUser() {
    connectFbUser(getUserPermissions);
}

function loginUser()
{
    FB.Connect.requireSession();
}

function getUserNameAndTransfer(result, ex)
{
    location.href = "Facebook.aspx?uid=" + sessionObj.uid + "&UniqueData=" + sessionObj.session_key + "&NickName=" + result[0].username + "&RealName=" + result[0].name;
}


var checkedPermisions = false;

function getUserPermissions() 
{
    if (!checkedPermisions) 
    {
        var query = "select uid from permissions where status_update=1 and video_upload=1 and uid=" + sessionObj.uid;
        FB.Facebook.apiClient.fql_query(query, function(result, ex) { checkPermissions(result, ex); });
        checkedPermisions = true;
    }
}

function checkPermissions(result, ex) 
{
    if (result.length != undefined)
    {
        getUserName();
    }
    else
    {
        FB.Connect.showPermissionDialog("offline_access,video_upload", getUserName);
    }
}

function getUserName()
{
    var query = "select username, name from user where uid=" +  sessionObj.uid;
    FB.Facebook.apiClient.fql_query(query, function(result, ex) { getUserNameAndTransfer(result, ex); });
}

var isSignInDisplayed = false;
function ShowHideSignIn() {
    if (isSignInDisplayed) {
        HideSignIn();
    }
    else {
        ShowPanel("pnlSignIn");
        isSignInDisplayed = true;
    }
}

function HideSignIn() {
    HidePanel("pnlSignIn");
    isSignInDisplayed = false;
}