function ifpc_widget_resize(iframeId, verticalHeight) {
    if(verticalHeight <= 1 && verticalHeight > 0 && "canvas" === ifpc_current_surface){
        var a=document.body.scrollHeight;//scrolled content
        var b=document.documentElement.clientHeight;//no scroll
        var c=document.documentElement.scrollHeight;//ie7 friendly
        a = ((a>c)?a:c);
        //291 is height of header  + nav link + app title + footer
        //291 shows a scroll bar in IE, 295 doesn't
        //TODO:this won't work if the user has text magnified or if another language breaks text onto two lines
        verticalHeight = Math.floor((((a>b)?a:b) - 295) * verticalHeight);
    }
    else if (verticalHeight < 1) verticalHeight = 0;//< 1 for when a fraction is given, but not on canvas
    else{
        switch(ifpc_current_surface) {
            case "canvas":
                max_widget_height = 1000000;
                break;
            default:
                max_widget_height = 1000;
                break;                                        
        }
        if (verticalHeight > max_widget_height) verticalHeight = max_widget_height;
    }

    var iFrame = document.getElementById(iframeId);
    
    iFrame.style.height = verticalHeight;
    iFrame.height = verticalHeight;
}
_IFPC.registerService('resizeWidget', ifpc_widget_resize);

function ifpc_widget_requestNavigateTo(appId, ownerId, surface, params, sourcePanel) {
    ifpc_widget_requestNavigateToEx(appId, ownerId, surface, params, sourcePanel, false)
}

function ifpc_widget_requestNavigateToEx(appId, ownerId, surface, params, sourcePanel, isNewInstall) {
    //encode params accordingly
    var encodedParams = encodeURIComponent(gadgets.JSON.stringify(params));            

    var target = null;
    //navigate to the url
    switch(surface) {
        case "canvas":
            target = ifpc_canvas_urltemplate;
            break;
        case "profile":
            target = ifpc_profile_urltemplate;
            break; 
        case "home":
            target = ifpc_home_urltemplate;
            break;    
    }
    
    if(target != null) {
        target = target.replace("{0}", appId);
        target = target.replace("{1}", ifpc_ownerid);
        target = target.replace("{2}", encodedParams);
        if(isNewInstall) {
            target += "&newinstall=1";
        }                
        top.location = target;
    }
}
_IFPC.registerService('requestNavigateTo', ifpc_widget_requestNavigateTo);

function ifpc_widget_requestPermission(appId, permissions, reason, opt_callback) {
    if(permissions instanceof Array && permissions.length > 0) { 
        MySpace.UI.AppsPopup.ajaxPermissionsEx(appId, reason, "", MySpaceRes.AppManagement.RequestPermissionHeader, 
            function(status, p) {
                if(p){
                    p.add_button(MySpaceRes.AppManagement.RequestPermissionUpdate); //adds the update button
                    p.add_button(MySpaceRes.AppManagement.RequestPermissionCancel, true).isCancel = true;
                    p.show(
                        function(sender, args) {
                            if(args.target.isCancel) {
                                if(typeof opt_callback === "function") {
                                    opt_callback(null);
                                }
                                return;
                            }
                            var selectedPermissions = MySpace.UI.AppsPopup.getSelectedPermissions(sender);
                            var unselectedPermissions = MySpace.UI.AppsPopup.getUnselectedPermissions(sender);
                            var permissionState = {};
                            for(var i=0; i<selectedPermissions.length; i++) {
                                permissionState[selectedPermissions[i]] = true;
                            }
                            for(var i=0; i<unselectedPermissions.length; i++) {
                                permissionState[unselectedPermissions[i]] = false;
                            }
                            MySpace.Web.Services.Apps.Apps.UpdateApplicationSettings(appId,
                                selectedPermissions, unselectedPermissions,
                                function(ret) {
                                    switch(ret.status) {
                                        case 0:
                                            if(typeof opt_callback === "function") {
                                                opt_callback(permissionState);
                                            }
                                            return;
                                        default:
                                            if(typeof opt_callback === "function") {
                                                opt_callback(null);
                                            }
                                            return;
                                    }
                                },
                                function(ret) {
                                    if(typeof opt_callback === "function") {
                                        opt_callback(null);
                                    }
                                });                                   
                        }    
                    );
                }//end if(p)
            }
            , null, permissions[0] //TODO: currently only supports one permission request at a time
        ); 
    }
}
_IFPC.registerService('requestPermission', ifpc_widget_requestPermission);

var ifpc_widget_postTo_data = {};
var ifpc_widget_iframe_popup = null;
function ifpc_widget_postTo(os_token, post_type, subject, content, opt_recipientId, opt_recipientImage, opt_recipientName, opt_recipientProfile, opt_callback){
    ifpc_widget_postTo_data.os_token = os_token;
    ifpc_widget_postTo_data.post_type = post_type;
    ifpc_widget_postTo_data.subject = subject;
    ifpc_widget_postTo_data.content = content;
    ifpc_widget_postTo_data.opt_recipientId = opt_recipientId;
    ifpc_widget_postTo_data.opt_recipientImage = opt_recipientImage;
    ifpc_widget_postTo_data.opt_recipientName = opt_recipientName;
    ifpc_widget_postTo_data.opt_recipientProfile = opt_recipientProfile;
    ifpc_widget_postTo_data.callback = opt_callback;
    
    var target_supported = false;
    for(var i = 0; i < ifpc_supported_postto_targets.length; i++){
        if(ifpc_supported_postto_targets[i] === post_type){
            target_supported = true;
            break;
        }
    }
    
    if(target_supported){
        var url = "/Modules/MDPPostTo/Pages/MDPPostTo.aspx?opensocial_token=" + os_token + "&p=MDPPostTo", style = "appspopup_pt_";
        switch (post_type){
            case "PROFILE":
                url += "Profile";
                style += "pro";
                break;
            case "SEND_MESSAGE":
                url += "SendMessage";
                style += "send";
                break;
            case "COMMENTS":
                url += "Comment";
                style += "comm";
                break;
            case "BULLETINS":
                url += "Bulletin";
                style += "bull";
                break;
            case "BLOG":
                url += "Blog";
                style += "blog";
                break;
        }
        
        if("appspopup_pt_" !== style){
            var div = document.getElementById("post_to_container");
            if(div){
                var iframe = document.getElementById("post_to_iframe");
                if(iframe && style !== iframe.className){
                    div.parentNode.removeChild(div);
                    //div = null;
                    ifpc_widget_iframe_popup = new MySpace.UI.IframePopup("post_to_container","","post_to_iframe",url,"appspopup_box appspopup_box_pt",style);
                }
            }
            else{
                ifpc_widget_iframe_popup = new MySpace.UI.IframePopup("post_to_container","","post_to_iframe",url,"appspopup_box appspopup_box_pt",style);
            }
            ifpc_widget_iframe_popup.show();
            
            if(document.getElementById("post_to_container")){
                document.getElementById("post_to_container").style.position = "absolute";
                
                var vertScroll = 0;
                if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
                    vertScroll = document.body.scrollTop;
                }
                else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
                    vertScroll = document.documentElement.scrollTop;
                }
                document.getElementById("post_to_container").style.top = 125 + vertScroll + "px";
            }
        }
    }
}
_IFPC.registerService('postTo', ifpc_widget_postTo);


function ifpc_widget_requestLocale() {
    return [ifpc_locale.country, ifpc_locale.lang];
}
_IFPC.registerService('requestLocale', ifpc_widget_requestLocale);
