$(document).ready(function() {
    $('body').ymhMini({
        'position_horizontal': 'left',
        'position_vertical': 'bottom',
    });
});

(function($) {
    $.fn.ymhMini = function(options) {

        return this.each(function() {
            var opts = $.extend({}, $.fn.ymhMini.defaults, options);
            var $this = $(this);

	    prepareLayout();
            addBanner(opts.position_horizontal, opts.position_vertical);
        });

    };

    function addBanner (horizontal, vertical) {
        var banner = document.createElement('a');
        banner.style.zIndex = '999';
        banner.style.width = '200px';
        banner.style.height = '65px';
        banner.style.position = 'absolute';
        banner.style.borderWidth = '0';
        banner.style.borderColor = '#fff';
        if(horizontal == 'left') banner.style.left = '0px';
            else banner.style.right = '0px';
        if(vertical == 'bottom') banner.style.bottom = '0px';
            else banner.style.top = '0px';
        banner.href = 'javascript:void(0)';

        $(banner).mouseover(function() {
            var colors = new Array();
            colors[0] = '#e2e1df';
            colors[1] = '#f29ac2';
            colors[2] = '#14a5c0';
            colors[3] = '#f4821f';
            colors[4] = '#ae1f25';
            colors[5] = '#83a6b9';

            var rand = Math.floor(Math.random()*6);
            banner.style.backgroundColor = colors[rand];
        });

        $(banner).click(function(){
            $("#modal").show();
            $("#modal_window").show();
        });

        var image = document.createElement('img');
        image.src = '/ymh-mcr400/banner.png';
        image.style.width = '200px';
        image.style.height = '65px';
        image.style.borderWidth = '0';
        image.style.borderColor = '#fff';
        banner.appendChild(image);

        var _body = document.getElementsByTagName('body')[0];
        _body.appendChild(banner);
    }

    function prepareLayout () {
        var _body = document.getElementsByTagName('body')[0];

        var modal = document.createElement('div');
        modal.id = 'modal';
        modal.style.zIndex = '9999';
        modal.style.position = 'absolute';
        modal.style.left = '0px';
        modal.style.top = '0px';
        modal.style.width = $(window).width() + 'px';
        modal.style.height = $(document).height() + 'px';
        modal.style.opacity = '0.85';
        modal.style.filter = 'alpha(opacity = 85)';
        modal.style.backgroundColor = '#000';
        modal.style.display = 'none';

        $(modal).click(function(){
            $(modal).hide();
            $(modal_window).hide();
        });

        _body.appendChild(modal);

        var modal_window = document.createElement('div');
        modal_window.id = 'modal_window';
        modal_window.style.zIndex = '99999';
        modal_window.style.position = 'absolute';
        modal_window.style.left = '50%';
        modal_window.style.marginLeft = '-495px';
        modal_window.style.top = Math.floor((parseInt($(window).scrollTop())+100)) + 'px';
        modal_window.style.width = '990px';
        modal_window.style.height = '665px';
        modal_window.style.backgroundColor = '#fff';
        modal_window.style.display = 'none';

        modal_window.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="990" height="650" id="site_008" align="top"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="/ymh-mcr400/site_008.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="/ymh-mcr400/site_008.swf" quality="high" bgcolor="#ffffff" width="990" height="650" name="site_008" align="top" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" /></object><div style="display:block; background-color: transparent; "></div>';

        var close_modal = document.createElement('a');
        close_modal.id = 'close_modal';
        close_modal.href = 'javascript:void(0)';
        close_modal.innerHTML = 'Закрыть';
        close_modal.style.backgroundColor = 'transparent';
        close_modal.style.fontFamily = 'arial, sans-serif';
        close_modal.style.fontSize = '12px';
        close_modal.style.color = '#000';
        close_modal.style.lineHeight = '15px';
        close_modal.style.styleFloat = 'right';
        close_modal.style.padding = '0px 10px 5px 10px';

        $(close_modal).click(function(){
            $(modal).hide();
            $(modal_window).hide();
        });

        modal_window.appendChild(close_modal);

        _body.appendChild(modal_window);
    }

    $.fn.ymhMini.defaults = {
        position_horizontal: 'right',
        position_vertical: 'top'
    };
})(jQuery);
