/*
 * WYMeditor : what you see is What You Mean web-based editor
 * Copyright (c) 2005 - 2009 Jean-Francois Hovinne, http://www.wymeditor.org/
 * Dual licensed under the MIT (MIT-license.txt)
 * and GPL (GPL-license.txt) licenses.
 *
 * For further information visit:
 *        http://www.wymeditor.org/
 *
 * File Name:
 *        jquery.wymeditor.fullscreen.js
 *        Fullscreen plugin for WYMeditor
 *
 * File Authors:
 *        Luis Santos (luis.santos a-t openquest dotpt)
 *        Jonatan Lundin (jonatan.lundin a-t gmail dotcom)
 *        Gerd Riesselmann (gerd a-t gyro-php dot org) : Fixed issue with new skin layout
 */

//Extend WYMeditor
WYMeditor.editor.prototype.fullscreen = function() {
    var wym = this,
        $box = jQuery(this._box),
        $iframe = jQuery(this._iframe),
        $overlay = null,
        $window = jQuery(window),
        
        editorMargin = 15;     // Margin from window (without padding)

    //construct the button's html
    var html = "<li class='wym_tools_fullscreen'>"
             + "<a name='Fullscreen' href='#' title='Cijeli ekran'"
             + " style='background-image:"
             + " url(" + wym._options.basePath +"plugins/fullscreen/icon_fullscreen.gif)'>"
             + "Fullscreen"
             + "</a></li>";

    //add the button to the tools box
    if ($box.find('.wym_tools_fullscreen').length == 0) $box.find(wym._options.toolsSelector + wym._options.toolsListSelector)
        .append(html);
        
   //handle click event
    $box.find('li.wym_tools_fullscreen a').click(function() {
		var par = null;
		var bW = 0;
		var bH = 0;
		var ifH = 0;
        if (!$window.data('isFull')) {
			par = wym._element.parent();
			bW = $box.width();
			bH = $box.height();
			ifH = $iframe.height();
			var html;
			html = $('.wym_iframe iframe')[0];
			html = html.contentWindow.document;
			html = $("body",html).html();			

			$window.data('isFull', true);
            // Store previous inline styles
            $box.data('wym-inline-css', $box.attr('style'));
            $iframe.data('wym-inline-css', $iframe.attr('style'));
            
            // Possition the editor
            
            // Bind event listeners
            //$window.bind('resize', resize);
            			
            // Force resize
            //resize();
			$.fancybox({
				content: '<div id="fb_editor" style="background-color:white;overflow:hidden;margin:0;padding:0;position:relative;width:' + parseInt(parseInt($(window).width())-100) + 'px;height:' + parseInt(parseInt($(window).height())-100) + 'px;background-color:#C0C683;"></div>',
				onComplete: function() {
					wym._element.appendTo($('#fb_editor'));
					$box.remove();
					$window.data('isFull', true);
					WYMeditor.INSTANCES.pop();
					wym._element.wymeditor({		
						skin: 'compact',
						lang: 'hr',		
						basePath: "/js/",
						html: html,
						toolsItems: [
						{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'}, 
						{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
						{'name': 'Superscript', 'title': 'Superscript',
							'css': 'wym_tools_superscript'},
						{'name': 'Subscript', 'title': 'Subscript',
							'css': 'wym_tools_subscript'},
						{'name': 'InsertOrderedList', 'title': 'Ordered_List',
							'css': 'wym_tools_ordered_list'},
						{'name': 'InsertUnorderedList', 'title': 'Unordered_List',
							'css': 'wym_tools_unordered_list'},
						{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
						{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
						{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
						{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
						{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
						{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
						{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'},
						{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'},
						{'name': 'Paste', 'title': 'Paste_From_Word',
							'css': 'wym_tools_paste'},
						{'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'}
						],
						stylesheet: '/skin/editor_styles.css',
						postInit: function(wym) {
								wym.fullscreen();							
								setTimeout(function(){$("body",$('.wym_iframe iframe', $box)[0].contentWindow.document).html(html);},250);								
						}									
					});					
					//$iframe.height($('#fb_editor').height() - $iframe.position().top -10 );
					
					
				},
				onCleanup: function() {
					$window.data('isFull', false);
					wym._element.appendTo(par);
					html = $('.wym_iframe iframe')[0];
					html = html.contentWindow.document;
					html = $("body",html).html();
					alert(html);
					//$box.appendTo(window.editorparent);
		            $box.css({
						'position': 'static', 
						'z-index': '', 
						'z-index': '', 
						'width': bW + 'px', 
						'height' : bH + 'px',
						'top': '', 
						'left': '',				
					});
					$iframe.css('height', '');
					$iframe.height(ifH);
					WYMeditor.INSTANCES.pop();
					wym._element.wymeditor({		
						skin: 'compact',
						lang: 'hr',		
						basePath: "/js/",
						html: html,
						toolsItems: [
						{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'}, 
						{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
						{'name': 'Superscript', 'title': 'Superscript',
							'css': 'wym_tools_superscript'},
						{'name': 'Subscript', 'title': 'Subscript',
							'css': 'wym_tools_subscript'},
						{'name': 'InsertOrderedList', 'title': 'Ordered_List',
							'css': 'wym_tools_ordered_list'},
						{'name': 'InsertUnorderedList', 'title': 'Unordered_List',
							'css': 'wym_tools_unordered_list'},
						{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
						{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
						{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
						{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
						{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
						{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
						{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'},
						{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'},
						{'name': 'Paste', 'title': 'Paste_From_Word',
							'css': 'wym_tools_paste'},
						{'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'}
						],
						stylesheet: '/skin/editor_styles.css',
						postInit: function(wym) {
								wym.fullscreen();							
						}									
					});
				}
			});
        } else {
            $.fancybox.close();
        }

        return false;
    });
};

