/*
 * xWindow 1.0 - Plugin for jQuery
 * 
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Depends:
 *   jquery.js
 *	 ui.base.js
 * 	 ui.draggable.js
 *   ui.resizable.js
 * 
 *
 *  Copyright (c) 2008 Oleg Slobodskoi (ajaxsoft.de)#
 
 *  Updated for HUB by morris10
    at line 		this.minimize = function()
    added functionality for windowd to be minimized to the window bar
 
 */


;(function($) {


    $.fn.extend({
        xWindow: function(options) {

            var args = Array.prototype.slice.call(arguments, 1);
            var xWindowClass;

            this.each(function() {
                var method = (typeof options == 'string') ? options : 'init',
					xWindow = $.data(this, 'xWindow') || $.data(this, 'xWindow', new $.xWindow(this, options));
                xWindow[method].apply(xWindow, args);
                xWindowClass = xWindow;
            });
            return xWindowClass;

        }
    });




    $.xWindow = function(elem, options) {

        /**************************************************************************/
        //Defaults

        var d = {
            title: '',
            status: '',
            position: 'center',
            width: 320,
            height: 240,
            content: null,
            html_page: null,
            draggable: true,
            resizable: true,
            closable: true,
            maximizable: true,
            minimizable: true,
            autoShow: true,
            close: function() { },
            resize: function() { },
            restoreAfterDestroy: false,
            modal: false,
            overlayOpacity: 0.7,
            stayInView: false,
            draggableOptions: {
                cancel: '.xWindow-content,.xWindow-titlebar-btn',
                helper: 'clone', //or original || clone
                opacity: 0.7,
                scroll: false,
                scrollSensitivity: 80,
                scrollSpeed: 20
            },
            resizableOptions: {
                cancel: '.xWindow-content',
                maxWidth: null,
                maxHeight: null,
                minWidth: 200,
                minHeight: 100,
                containment: null,
                helper: 'xWindow-resizable-proxy'
            },
            titlebar: true,
            statusbar: true,
            animate: false,
            showAnimation: { opacity: "show" },
            closeAnimation: { opacity: "hide" },
            mimimizeAnimation: null,
            maximizeAnimation: null,
            speed: 200,
            extension: false,
            buttons: null,
            bgiframe: false,
            counter: null,
            unbind_events: false,
            page_name: 'box',
            script_to_run: '',
            type_of_content: '',
            window_menu: '',
            scrolling: 'auto',
            closefunction: null,
            get_url: ''
        };
        $.extend(d, options);

        /**************************************************************************/
        //Privat variables

        var 
			$elem = $(elem),
			$xWindow,
			$title,
			$titlebar,
			$titlebarButtons,
			$buttonsPanel,
			$status,
			$statusbar,
			$overlay,
			$bgiframe,
			$iframe,
			$wnd = $(window),
			$doc = $(document),
			self = this,
			size = {
			    width: d.width,
			    height: d.height
			},
			pos = d.position,
			savedPos = {},
			savedSize = {},
			statusbarHeight,
			titlebarHeight,
			buttonsPanelHeight,
			scoll_panel_container_box,
			iframe_body

		;


        /**************************************************************************/
        //Public variables

        this.$elem = $elem;
        this.$xWindow = $xWindow;
        this.$iframe = $iframe;
        this.mimimized = false;
        this.maximized = false;
        this.contentSize = {};
        this.buttons = {};

        /**************************************************************************/
        //Public methods
        this.init = function() {

            create();
            d.autoShow && self.show();

            //fix selectboxes in IE6
            if (d.bgiframe && $.fn.bgiframe) {
                $xWindow.bgiframe();
                $bgiframe = $xWindow.children('.bgiframe');
            };

            $doc.keydown(function(e) {
                if ($xWindow.hasClass('selected') && e.keyCode) {
                    var ESC = 27, enter = 13;
                    e.keyCode == ESC && self.destroy();
                    e.keyCode == enter && $buttonsPanel && $buttonsPanel.children('.focus').click();
                };
            });


        };

        this.object = function(callback) {
            createCallback(callback, [self]);
        };

        this.defaults = function(param) {
            typeof param == 'object' ? $.extend(d, param) : createCallback(param, [d]);
        };

        this.title = function(param) {
            /^[\s[]?function/.test(param + "") ? createCallback(param, [$title.text()]) : $title.text(param);
        };

        this.status = function(param) {
            /^[\s[]?function/.test(param + "") ? createCallback(param, [$status.text()]) : $status.text(param);
        };

        this.position = function(param) {
            typeof param == 'object' ? position(param) : createCallback(param, [pos]);
        };

        this.size = function(param) {
            typeof param == 'object' ? resize(param) : createCallback(param, [size]);
        };

        //toggle minimize
        this.minimize = function() {
            //restore normal state 
            self.maximized && self.maximize();

            //minimize window
            if (!self.minimized) {
                saveSize(size);
                /* Removed by morris10
				
				$statusbar && $statusbar.hide();
                $buttonsPanel && $buttonsPanel.hide();
                resize({width: size.width , height: $titlebar.height()});
                */
                $xWindow.hide();
                self.minimized = true;
                windowMinimized(d.counter);
            }
            //restore window
            else {
                restoreSize();
                resize(size);
                /*$statusbar && $statusbar.show();
                $buttonsPanel && $buttonsPanel.show();*/
                $xWindow.show();
                self.minimized = false;
                windowMaximized(d.counter);
            };
        };
        // Added function by stephen morrissey 
        // Is the window minimized	
        this.ishidden = function() {
            return self.minimized;

        };
        //set the callback function if window is resized
        this.setResizeCallBackFunction = function(function_name) {
            d.resize = function_name;

        };

        this.getWindowIframe = function() {
            return $iframe;

        };
        this.getResizeDetails = function() {

            custom_details_arr = new Array();
            custom_details_arr['height'] = size.height - 60;
            custom_details_arr['width'] = size.width - 60;
            return custom_details_arr;

        };

        //toggle fullscreen
        this.maximize = function() {
            //restore normal state 
            self.minimized && self.minimize();

            //fullscreen window
            if (!self.maximized) {
                saveSize(size);
                savePos(pos);
                position({ top: 5, left: 5 });
                resize({ width: $wnd.width() - 10, height: $wnd.height() - 10 });
                self.maximized = true;
            }
            //restore window
            else {
                restoreSize();
                restorePos();
                $xWindow.css(pos)
                resize(size);
                self.maximized = false;
            };

        };

        this.show = function() {
            d.animate && d.showAnimation ? $xWindow.animate(d.showAnimation, d.speed) : $xWindow.show();
            self.select();
            titlebarHeight = $titlebar ? $titlebar.height() : 0;
            buttonsPanelHeight = $buttonsPanel ? $buttonsPanel.height() : 0;
            statusbarHeight = $statusbar ? $statusbar.height() : 0;
            resize({ width: d.width, height: d.height });
            position(d.position);
        };

        this.destroyFinish = function() {

            closeWindowTitle(d.counter);
            d.restoreAfterDestroy && $elem.removeClass('xWindow-content').hide().appendTo('body');
            $.removeData($elem);
            $titlebar && $.removeData($titlebar, "ui-mouse");
            $statusbar && $.removeData($statusbar, "ui-mouse");
            $overlay && $overlay.remove();
            $.removeData($elem.get(0), 'xWindow');

            d.animate && d.closeAnimation ? $xWindow.animate(d.closeAnimation, d.speed, function() {
                $xWindow.remove();
                createCallback(d.onClose, [self]);
            }) : $xWindow.remove();
            createCallback(d.close, [self]);
        };

        this.getAnElementWindow = function(element_name) {

            return $xWindow.find(element_name);
        };

        this.destroy = function() {
            if (d.closefunction) {
                createCallback(d.closefunction, [self]);

            }
            else {
                self.destroyFinish();
            }


        };

        this.hide = function() {
            d.animate && d.closeAnimation ? $xWindow.animate(d.closeAnimation, d.speed) : $xWindow.hide();
            createCallback(d.onClose, [self]);
        };

        this.select = function() {
            $('.xWindow:visible').removeClass('selected');
            $xWindow.addClass('selected');
        };

        this.buttonEnabled = function(buttonName, sw) {
            var action = sw ? 'removeClass' : 'addClass';
            self.buttons[buttonName].$elem[action]('disabled');
        };


        /**************************************************************************/
        //Private methods
        function create() {
            var loader_message = false;
            $xWindow = $('<div class="xWindow selected"/>');
            d.extension && $xWindow.addClass('xWindow-extended ' + d.extension);

            //add titlebar
            d.titlebar && titlebar(d.title || $elem.attr('title'));

            //add user container
            $elem.addClass('xWindow-content').show().
			appendTo($xWindow);

            //add buttons
            d.buttons && buttons(d.buttons);

            //add status Bar
            d.statusbar ? statusbar(d.status) : $xWindow.addClass('no-xWindow-statusbar');

            //set on top by click on each element
            $xWindow.children().mousedown(self.select);

            //add content

            if (d.content != '' && d.content != null) {
                $elem.append(d.content)

                if (d.script_to_run != '') {
                    eval(d.script_to_run);

                }
            }
            else if (d.get_url != '') {
                $.ajax({
                    type: "GET",
                    url: d.get_url,
                    data: "",
                    success: function(html) {
                        $elem.append(html);
                        //$('#ajax_loader_image').fadeOut('slow');
                    },
                    error: function(res, textStatus, errorThrown) {
                    }
                });

            }
            else {
                var iframe_name = 'float_window_iframe_' + d.counter;
                var loader_message = true;
                if (d.type_of_content == 'external') {
                    $elem.append(d.window_menu);

                }

                //$elem.append('<iframe class= "float_window_iframe" id = "' + iframe_name + '" src = "' + d.html_page + ' "  scrolling="yes" ></frame>');

                $elem.append('<iframe class= "float_window_iframe" src = "/jquery/plugins/xWindow/loading_page.html" id = "' + iframe_name + '"   scrolling="' + d.scrolling + '" onLoad = "javascript:parent.removeIframeLoader(\'' + iframe_name + '\')"><html><head></head><body>dsdfdf</body></head></iframe>');
                //src = "' + d.html_page + ' "

            }
            //stayInView
            (d.stayInView || d.modal) && $doc.scroll(function() { position('center') });

            //make it draggable
            d.draggable && $.fn.draggable && draggable();

            //make it resizable
            d.resizable && $.fn.resizable && $statusbar && resizable();

            //show overlay if modal
            if (d.modal) {
                $overlay = $('<div class="xWindow-overlay"/>').
				css({ 'opacity': d.overlayOpacity, 'height': $doc.height() }).
				appendTo('body');
                d.bgiframe && $.fn.bgiframe && $overlay.bgiframe();
            };
            $xWindow.appendTo('body');



            $iframe = $('#' + iframe_name);
            if (loader_message) {
                parent.addIframeLoader(iframe_name);
                $iframe.attr("src", d.html_page);
            }



        };
        function setiFrameSize() {
            $iframe.css("width", size.width - 15).css("height", size.height - 50);
        }



        function resize(s) {
            $.extend(size, s);
            $xWindow.css(size);
            setiFrameSize();
            initContentSize();
            createCallback(d.resize, [self]);
        };

        function resize_scroll_panel() {


            if ($('#jScrollPaneContainer').html()) {
                var inside_html = $('#sitemap_ul');
                $('#jScrollPaneContainer').remove();
                $('#box' + d.counter).append('<div id = "scroll_pane_div"></div>');
                $('#scroll_pane_div').html($(inside_html));
                $('#scroll_pane_div').jScrollPane({ scrollbarWidth: 20, scrollbarHeight: 10, wheelSpeed: 50 });

            }
        }
        function initContentSize() {
            self.contentSize.height = size.height - titlebarHeight - statusbarHeight - buttonsPanelHeight;


        };

        function titlebar(title) {
            var id_from_title = title.replace(/ /g, '');
            $titlebar = $('<div id = "' + id_from_title + '" class="xWindow-titlebar"/>').appendTo($xWindow);
            $title = $('<span class="title">' + title + '</span>').appendTo($titlebar);
            d.title = title;

            //add panel for titlebar buttons
            if (d.closable || d.minimizable || d.maximizable)
                $titlebarButtons = $('<div class="xWindow-titlebar-buttons-panel"/>').
				appendTo($titlebar);

            d.closable &&
			$('<div id = "close_' + id_from_title + '" class="xWindow-close-btn xWindow-titlebar-btn"/>').
			appendTo($titlebarButtons).
			click(self.destroy);

            $('<div id = "straight_close_' + id_from_title + '" class="straight_close_window_button" style="display:none"/>').
			appendTo($titlebarButtons).
			click(self.destroyFinish);

            d.maximizable &&
			$('<div class="xWindow-maximize-btn xWindow-titlebar-btn"/>').
			appendTo($titlebarButtons).
			click(self.maximize);

            d.minimizable &&
			$('<div class="xWindow-minimize-btn xWindow-titlebar-btn"/>').
			appendTo($titlebarButtons).
			click(self.minimize);

        };

        function buttons(btns) {
            if (!btns) return;

            $buttonsPanel = $('<div class="xWindow-buttons-panel"/>');
            var $container = $('<div/>'), $button;

            var i = 0;
            $.each(btns, function(name, val) {
                $button = $('<a class="button" href="#">' + val.title + '</a>');
                val.focus && $button.addClass('focus').focus();
                val.disabled && $button.addClass('disabled');
                i > 0 && $button.addClass('margin');

                $button.click(function() {
                    !$(this).hasClass('disabled') && val.onclick(self);
                    return false;
                }).appendTo($container);
                $.extend(self.buttons[i], val, { $elem: $button });
                i++;
            });

            $container.appendTo($buttonsPanel);
            $buttonsPanel.appendTo($xWindow);
        };

        function statusbar(status) {
            $statusbar = $('<div class="xWindow-statusbar"/>').
			appendTo($xWindow);
            $status = $('<span>' + status + '</span>').appendTo($statusbar);
        };

        function draggable() {
            var dragHandles = [];
            $titlebar && dragHandles.push($titlebar.get(0));
            $statusbar && dragHandles.push($statusbar.get(0));
            if (dragHandles.length > 0) {
                $.extend(d.draggableOptions, {
                    handle: dragHandles,
                    start: function(e, obj) {
                        obj.helper.children('.xWindow-content').remove();
                        $xWindow.hide();
                        $bgiframe && $bgiframe.remove();
                    },
                    stop: function(e, obj) {

                        position({ top: obj.position.top - $doc.scrollTop(), left: obj.position.left - $doc.scrollLeft() });
                        $xWindow.show();
                        //d.bgiframe && $xWindow.bgiframe();
                    }
                });
                $xWindow.draggable(d.draggableOptions);
            };

        };


        function resizable() {

            $.extend(d.resizableOptions, {
                handles: {

                    se: $('<div class="ui-resizable-se ui-resizable-handle"/>').appendTo($statusbar)
                },
                start: function(e, obj) {
                    var iframe_floating_window = '#float_window_iframe_' + d.counter;
                    $(iframe_floating_window).hide();
                },
                stop: function(e, obj) {
                    var iframe_floating_window = '#float_window_iframe_' + d.counter;
                    $(iframe_floating_window).show();
                    resize({ width: obj.size.width, height: obj.size.height });
                }
            });

            $xWindow.resizable(d.resizableOptions);
        };



        function position(p) {
            var scrTop = $doc.scrollTop(), scrLeft = $doc.scrollLeft();

            p = (typeof p == 'object') ?
				{
				    left: p.left ? scrLeft + p.left : null,
				    top: p.top ? scrTop + p.top : null,
				    right: p.right ? scrLeft + p.right : null,
				    bottom: p.bottom ? scrLeft + p.bottom : null
				}
			:

				(p == 'center') ?
				{
				    top: Math.round(scrTop + ($wnd.height() - size.height) / 2),
				    left: Math.round(scrLeft + ($wnd.width() - size.width) / 2)
} : p;

            pos = $.extend({ position: 'absolute' }, p);
            $xWindow.css(pos);
        };

        function savePos(p) {
            $.extend(savedPos, p);
        };

        function saveSize(s) {
            $.extend(savedSize, s);
        };

        function restorePos() {
            var prevPos = $.extend({}, savedPos);
            $.extend(savedPos, pos);
            $.extend(pos, prevPos);
        };

        function restoreSize() {
            var prevSize = $.extend({}, savedSize);
            $.extend(savedSize, size);
            $.extend(size, prevSize);
        };

        function createCallback(fn, params) {
            typeof fn == 'string' && eval('fn=' + fn);
            typeof fn == 'function' && fn.apply(self, params);
        };


    };



})(jQuery);		


	




