﻿function filebrowser(field_name, url, type, win) {

            fileBrowserURL = applicationpath + "/Providers/HtmlEditorProviders/TinyMCE/Includes/FileManager/Filemanager.aspx";

            tinyMCE.activeEditor.windowManager.open({
                title: "Ajax File Manager",
                url: fileBrowserURL,
                width: 950,
                height: 650,
                inline: 0,
                maximizable: 1,
                close_previous: 0
                }, {
                    window: win,
                    input: field_name,
                    sessionid: sessionid
                }
		    );
        }
        var $j = jQuery.noConflict();


        function getLayerPosition(id) {
            var layer = jQuery('#' + id);
            var totX = jQuery(window).scrollLeft() + jQuery(window).width();
            var totY = jQuery(window).scrollTop() + jQuery(window).height();

            var x, y, height = layer.height(), width = layer.width();
            if (width == 0 || height == 0) {
                var obj = { width: 0, height: 0 };
                getLargestChild(layer, obj);
                width = obj.width;
                height = obj.height;
            }
            var pos = layer.attr("position") !== undefined ? layer.attr("position") : "center";
            
            switch (pos) {
                case "mousecenter":
                    layer.css("margin", "0px");
                    layer.css("padding", "0px");
                    x = window.MouseX + width > totX ? totX - width : window.MouseX - (width / 2);
                    y = window.MouseY + height > totY ? totY - height : window.MouseY;
                    break;
                case "mouse":
                    layer.css("margin", "0px");
                    layer.css("padding", "0px");
                    x = window.MouseX + width > totX ? totX - width : window.MouseX;
                    y = window.MouseY + height > totY ? totY - height : window.MouseY;
                    break;
                case "center":
                    layer.css("margin", "0px");
                    layer.css("padding", "0px");
                    x = jQuery(window).scrollLeft() + (jQuery(window).width() / 2 - width / 2);
                    y = jQuery(window).scrollTop() + (jQuery(window).height() / 2 - height / 2);
                    if (y < 0) y = jQuery(window).scrollTop() + 50;
                    break;
                case "topcenter":
                    layer.css("margin", "0px");
                    layer.css("padding", "0px");
                    x = jQuery(window).scrollLeft() + (jQuery(window).width() / 2 - width / 2);
                    y = 50;
                    break;
                default:
                    x = parseInt(layer.css("left"));
                    y = parseInt(layer.css("top"));
                    if (y < 0) y = 10;
                    break;
            }
            return { X: x, Y: y };
        }


        function getLargestChild(el, obj) {
            el.children().each(function() {
            if (jQuery(this).height() > obj.height) obj.height = jQuery(this).height();
                if (jQuery(this).width() > obj.width) obj.width = jQuery(this).width();
                getLargestChild(jQuery(this), obj) 
            });
        }

       

        function Reload() {
            window.location.reload();
        }

        function ReloadHome() {
            window.location = "/";
        }

        function loggedIn(isIn) {
            if (!isIn) {

                alert('Du &auml;r inte inloggad. Du m&aring;ste vara inloggad f&ouml;r att kunna anv&auml;nda den h&auml;r funktionen');
                GetRadWindowManager().GetWindowByName("ModalWindow").Hide();
            }
            return false;
        }

        //this function is necessary since there is an IE bug that does not 
        //hide select boxes in layers below another layer  so we have to show/hide them
        function hideSelect() {
            jQuery(".modalwindow").each(
                function() {
                    var modalvisible = null;
                    var zi = 0;
                    if (jQuery(this).is(":visible")) {
                        if (parseInt(jQuery(this).css("z-index")) > zi) {
                            zi = parseInt(jQuery(this).css("z-index"));
                            modalvisible = jQuery(this).id;
                        }
                    }
                    if (modalvisible != null) {
                        jQuery("select").hide();
                        jQuery(modalvisible + " select").show();
                    }
                    else {
                        jQuery("select").show();
                    }
                }
            );
        }


       function toModal() {
            jQuery(".movetobody").each(function() {
                if (jQuery(this).parent().get(0).tagName != "FORM") jQuery(this).prependTo(jQuery('form'));
            });
            jQuery(".modalwindow").each(function() {

                var ismodal = jQuery(this).attr("modal") == undefined || jQuery(this).attr("modal") == null ? (jQuery(this).hasClass("modal") ? true : false) : eval(jQuery(this).attr("modal"));
                var overlay = jQuery(this).attr("overlay") == undefined || jQuery(this).attr("overlay") == null ? 1 : jQuery(this).attr("overlay");
                
                jQuery(this).jqm({
                    modal: ismodal,
                    overlay: overlay,
                    onHide: function(h) {

                        h.o.remove(); // remove overlay                     
                        h.w.fadeOut(200); // hide window
                        setTimeout("hideSelect()", 300);

                    },
                    onShow: function(h) {
                        var pos = getLayerPosition(h.w.attr("id"));
                        h.w.css("position", "absolute");
                        h.w.css("left", pos.X + "px");
                        h.w.css("top", pos.Y + "px");
                        hideSelect();
                        h.w.fadeIn(200); // show window
                    }
                });
                //jQuery(this).jqDrag(".draggable");
            });
        }

        

        function actionSetActionValue(e) {
            e.stopPropagation();
            if (jQuery(this).attr("control")) {
                jQuery("#" + jQuery(this).attr("control")).val(jQuery(this).attr("id"));
                __doPostBack(jQuery(this).attr("control"), '');
            }
        }

        function saveMousePos(e) {
            if (e != null && e != undefined) {
                window.MouseX = e.pageX;
                window.MouseY = e.pageY;
            }
        }


        function progressBar(layerid) {
            var obj = jQuery("#" + layerid);
            var height = obj.height() != null && obj.height() != undefined && obj.height() > 0 ? obj.height() : (parseInt(obj.css("height")) > 0 ? parseInt(obj.css("height")) : "100%");
            var width = obj.width() != null && obj.width() != undefined && obj.width() > 0 ? obj.width() : (parseInt(obj.css("width")) > 0 ? parseInt(obj.css("width")) : "100%");

            var html = obj.html();
            var img = height > 60 && width > 130 && (height + "").indexOf('%') < 0 && (width + "").indexOf('%') < 0 ? "loading_big.gif" : "loading_small.gif";

            var image = new Image();
            image.src = applicationpath + "/images/" + img;
            
            var margintop = (height+"").indexOf('%') > 0 ? "auto" : ((height - image.height) / 2) + "px";
            var marginleft = (width + "").indexOf('%') > 0 ? "auto" : ((width - image.width) / 2) + "px";
            
            
            height = (height + "").indexOf('%') > 0 ? height : height + "px";
            width = (width + "").indexOf('%') > 0 ? width : width + "px";

            obj.html("<div style=\"width:" + width + ";height:" + width + ";\"><img src=\"" + image.src + "\" style=\"margin-left:" + marginleft + ";margin-top:" + margintop + "\" /></div>");
            return html;
        }

        function resetProgressBar(html, layerid) {
            if (layerid != null && layerid!=undefined && layerid!="") {
                jQuery("#" + layerid).html(html);
            }
        }

        

        function ajaxFullPostProgressBar(path, methodname, args, successmethod, errormethod, completemethod, tabmoduleid, headers, windowname, layerid) {
            disWindow = windowname == undefined ? null : windowname;
            var html = "";
            jQuery.ajax
              (
                  {
                      type: "POST",
                      url: path + "/" + methodname,
                      data: args,
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      success: function(msg, textStatus) {
                          resetProgressBar(html, layerid);
                          if (successmethod != null && successmethod != undefined) {
                              if (msg.d != undefined && msg.d != null)
                                  successmethod(msg.d);
                              else
                                  successmethod(msg);
                          }
                      },
                      error: function(request, status, error) {
                          resetProgressBar(html, layerid);
                          InkaAlert(request.status + ' ' + request.statusText + "\n" + request);
                          if (errormethod != null && errormethod != undefined)
                              errormethod(request);
                      },
                      beforeSend: function(req) {
                          if (disWindow != null && disWindow != "" && disWindow != true) jQuery('#' + disWindow + '').jqmShow();
                          else if (disWindow) jQuery('#contactingserver').jqmShow();
                          else if (layerid != null) {
                              html = progressBar(layerid);

                          }
                          req.setRequestHeader('TID', activetab);
                          if (tabmoduleid != null) req.setRequestHeader('TMID', tabmoduleid);
                          if (headers != null) {
                              for (var key in headers) {
                                  req.setRequestHeader(key, headers[key]);
                              }
                          }

                      },
                      complete: function(xhr, status) {
                          if (disWindow != null && disWindow != "" && disWindow != true) jQuery('#' + disWindow + '').jqmHide();
                          else if (disWindow) jQuery('#contactingserver').jqmHide();

                          if (completemethod != null && completemethod != undefined)
                              completemethod(xhr.d);
                      }
                  }
              );
        }
        
        function ajaxFullPost(path, methodname, args, successmethod, errormethod, completemethod, tabmoduleid, headers, windowname) {
            ajaxFullPostProgressBar(path, methodname, args, successmethod, errormethod, completemethod, tabmoduleid, headers, windowname)
        }

        function ajaxPost(methodname, args, successmethod, errormethod, completemethod) {
            ajaxFullPost(surl, methodname, args, successmethod, errormethod, completemethod);
        }

        function ajaxWebMethod(webservice, methodname, args, successmethod, errormethod, completemethod, windowname) {
            ajaxFullPost(webservice, methodname, args, successmethod, errormethod, completemethod, null, null, windowname);
        }

        function ajaxWebMethodLayerProgressBar(webservice, methodname, args, successmethod, errormethod, completemethod, layerid)         {
            ajaxFullPostProgressBar(webservice, methodname, args, successmethod, errormethod, completemethod, null, null, null, layerid);
        }

        function ajaxWebMethodNoModal(webservice, methodname, args, successmethod, errormethod, completemethod) {
            ajaxFullPost(webservice, methodname, args, successmethod, errormethod, completemethod, null, null, null);
        }

        function ajaxModuleWebMethod(webservice, methodname, args, successmethod, errormethod, completemethod, tabmoduleid) {
            ajaxFullPost(webservice, methodname, args, successmethod, errormethod, completemethod, tabmoduleid);
        }

        function ajaxHeaderWebMethod(webservice, methodname, args, successmethod, errormethod, completemethod, tabmoduleid, headers) {
            ajaxFullPost(webservice, methodname, args, successmethod, errormethod, completemethod, null, headers);
        }
        function ajaxCustomWindowWebMethod(webservice, methodname, args, successmethod, errormethod, completemethod, windowname) {
            ajaxFullPost(webservice, methodname, args, successmethod, errormethod, completemethod, null, null, windowname);
        }
    
        /*******Log function********/
        var rowcounter = 1;
        function log(msg)
        {
            jQuery("#log").html(rowcounter + " " + msg + "<br />" + jQuery("#log").html());
            rowcounter++;
        }
        /*******End log function********/

        function xfilemanager_callback(field_name, url, type, win) {
            if (type == "media") {
                type = win.document.getElementById("media_type").value;
            }
            var cmsURL = applicationpath + "/Providers/HtmlEditorProviders/TinyMCE/Includes/xfilemanager/default.aspx?tinymce=true";
            switch (type) {
                case "image":
                    type = "images";
                    break;
                case "media":
                case "qt":
                case "wmp":
                case "rmp":
                    type = "media";
                    break;
                case "shockwave":
                case "flash":
                    type = "flash";
                    break;
                case "file":
                    type = "files";
                    break;
                default:
                    return false;
            }
            if (cmsURL.indexOf("?") < 0) {
                //add the type as the only query parameter
                cmsURL = cmsURL + "?type=" + type;
            }
            else {
                //add the type as an additional query parameter
                // (PHP session ID is now included if there is one at all)
                cmsURL = cmsURL + "&type=" + type;
            }

            var windowManager = tinyMCE.activeEditor.windowManager.open({
                file: cmsURL,
                width: screen.width * 0.7,  // Your dimensions may differ - toy around with them!
                height: screen.height * 0.7,
                resizable: "yes",
                inline: 0,  // This parameter only has an effect if you use the inlinepopups plugin!
                close_previous: "no"
            }, {
                window: win,
                input: field_name
            });
            //if (window.focus) { windowManager.focus() }
            return false;
        }
        
        jQuery(window).load(
        function() {


            windowheight = jQuery(window).height();
            windowwidth = jQuery(window).width();

            toModal();


            jQuery("span.InkaSkinObject li").bind('mouseenter mouseleave', function() { jQuery(this).toggleClass('skinOver'); });
            jQuery(".modulemenuoptionlayer").bind('click', function(e) {
                var layer = jQuery("#" + jQuery(this).attr("window"));
                saveMousePos(e);
                layer.jqmShow();
            });
            jQuery(document).bind('mousemove', saveMousePos);
            jQuery(".modulemenuoption").bind("click", actionSetActionValue);


            jQuery(".inka_admin_ui").addClass("ui-dialog ui-widget ui-widget-content ui-corner-all");
            jQuery(".inka_admin_button").each(
                function() {
                    jQuery(this).addClass("ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only").html("<span class=\"ui-button-text\">" + jQuery(this).html() + "</span>"); jQuery(this).show();
                }
            );
            //jQuery(".inka_admin_button").addClass("ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only").show();
            jQuery(".inka_admin_container").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all").show();


            jQuery(".inka_admin_header").each(
                function() {
                    jQuery(this).addClass("ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix").html("<span class=\"ui-dialog-title\">" + jQuery(this).html() + "</span>"); jQuery(this).show();
                }
            );
            //jQuery(".inka_admin_header").addClass("ui-widget-header ui-helper-clearfix ui-corner-all").show();
            jQuery(".inka_admin_error").each(
                function() {
                    jQuery(this).addClass("ui-widget").html("<div class=\"ui-state-error ui-corner-all\" style=\"padding: 0pt 0.7em;\"><p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 0.3em;\"></span><strong>Alert:</strong>" + jQuery(this).html() + "</p></div>");
                }
            );
            jQuery(".inka_admin_confirm").each(
                function() {
                    jQuery(this).addClass("ui-widget").html("<div class=\"ui-state-highlight ui-corner-all\" style=\"margin-top: 20px; padding: 0pt 0.7em;\"><p><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: 0.3em;\"></span>" + jQuery(this).html() + "</p></div>");
                }
            );



        }
    );
