/*
 * Modal Window
 * Description: Shows the window on the screen with the defined content 
 * Author: Lukas Pesl
 * 
 */   

(function($){
	$.fn.extend({ 
		modalWindow: function(options) {

			var defaults = {
        opacity : 65,
        globalDataSelector : false,
        onShow : null
			};
			
			var options = $.extend(defaults, options);

			// az po nastaveni obsahu
      var centerWindow = function() {
        winH = $(window).height();
        winW = $(window).width();
        mask.css({'height': $(document).height()});

        var sc = $(document).scrollTop();
        var sc = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
        var top = (winH/2-mw.height()/2) + sc > sc + 10 ? (winH/2-mw.height()/2) + sc : sc + 10;
				mw.css({top : top, left : (winW/2-mw.width()/2)});
        mwb.css('width', mw.width()-8);
        mwt.css('width', mw.width()-8);
			};
      var showWindow = function() {
        centerWindow();
        mask.fadeIn(500, function(){ if (this.style.removeAttribute) this.style.removeAttribute('filter'); });
        mw.fadeIn(500);
			};
      var hideWindow = function() {
        mask.fadeOut(500, function(){ if (this.style.setAttribute) this.style.setAttribute('filter', 'alpha(opacity=' + options.opacity + ');'); });
        mw.fadeOut(0, function(){ if (this.style.removeAttribute) this.style.removeAttribute('filter'); });
			};
			var init = function() {
        var cssTag = '<style>#mw-cl { cursor: pointer; right: 0; top: 0; position: absolute; margin: 8px 3px 0 0; width: 9px; height: 9px; background: url(\'http://ihned.cz/img/11_0/zaviratko-krizek.png\') no-repeat; } #mw-t {  font-size: 0; display: block!important; background: white; margin: -4px 0 0 4px; width: 100%; height: 4px; sborder: 1px solid red; position: absolute; left: 0; top: 0; } #mw-b { font-size: 0; background: white; margin: 0 0 -4px 4px; width: 100%; height: 4px; sborder: 1px solid red; position: absolute; left: 0; bottom: 0; } #mw-r2 { font-size: 0; margin: 0 0 -4px 0; width: 4px; height: 4px; sborder: 1px solid green; position: absolute; right: 0; bottom: 0; background: url(\'http://ihned.cz/img/11_0/popup-zaobleny-pravy-spodni_roh.png\') no-repeat; } #mw-l2 { font-size: 0; margin: 0 0 -4px 0; width: 4px; height: 4px; sborder: 1px solid orange; position: absolute; left: 0; bottom: 0; background: url(\'http://ihned.cz/img/11_0/popup-zaobleny-levy-spodni_roh.png\') no-repeat; } #mw-r { font-size: 0; margin: -4px 0 0 0; width: 4px; height: 4px; sborder: 1px solid red; position: absolute; right: 0; top: 0; background: url(\'http://ihned.cz/img/11_0/popup-zaobleny-pravy-horni_roh.png\') no-repeat; } #mw-l { font-size: 0; margin: -4px 0 0 0; width: 4px; height: 4px; sborder: 1px solid red; position: absolute; left: 0; top: 0; background: url(\'http://ihned.cz/img/11_0/popup-zaobleny-levy-horni_roh.png\') no-repeat; }  #mw-mask { width: 100%; height: 100%; left: 0; position:absolute; z-index: 9000; background-color: black; display: none; opacity: 0.' + options.opacity + '; filter: alpha(opacity=' + options.opacity + '); } #mw-window { filter: alpha(opacity=0); display: none; background: white; position: absolute; left: 0; z-index: 9999; } #mw-content div { display: block!important; } </style>';
        var htmlTag = '<div id="mw-mask"></div><div id="mw-window"><div id="mw-t"><div id="mw-cl" class="pngfix"></div></div><div id="mw-l" class="pngfix"></div><div id="mw-r" class="pngfix"></div><div id="mw-b"></div><div id="mw-l2" class="pngfix"></div><div id="mw-r2" class="pngfix"></div><div id="mw-content"></div></div>';

        $('head').prepend(cssTag);
        $('body').prepend(htmlTag);

        mask = $('#mw-mask').click(function(){hideWindow();});
        mw = $('#mw-window'); 
        //mwc = $('#mw-content', mw).click(function(){hideWindow();}); 
        mwc = $('#mw-content', mw); 
        mwcl = $('#mw-cl', mw).click(function(){hideWindow();}); 
        mwb = $('#mw-b', mw); 
        mwt = $('#mw-t', mw); 
			};
			init();
		
  		return this.each(function() {
				var obj = $(this);				
        obj.click(function() {
  
          if (typeof options.dataSelector == 'function')
          {
            var data = options.dataSelector(obj);          
          } else
          {
            if (options.globalDataSelector)
              //var data = $('<div>').append($(options.dataSelector).clone()).remove().html();
              var data = $('<div>').append($(options.dataSelector).filter(function(){ return $(this).parent().attr('id') != 'mw-content'}).clone()).remove().html();
            else            
              var data = $('<div>').append($(options.dataSelector, obj).clone()).remove().html();
          }
  
          mwc.html(data);
          
					if (typeof options.onShow == 'function') options.onShow(); 
					
					showWindow();
          return false;
        });

   		});

    }
  });
})(jQuery);



