$(document).ready(function(){

$.fn.formCenter = function() {
      //get margin left
      var marginLeft =  - $(this).width()/2 + 'px';
      //get margin top
      var marginTop =  - $(this).height()/2 + 'px';
      //return updated element
      return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
   };
	 
 $.fn.windowPopup = function(form_name){
     //detect whether popup is visible or not
     if($('#popup_wm').hasClass('hidden'))
     {
       //hidden - then display
       //when IE - fade immediately
       if($.browser.msie)
       {
         $('#opaco_wm').height($(document).height()).toggleClass('hidden')
                    .click(function(){$(this).windowPopup(form_name);});
       }
       else
       //in all the rest browsers - fade slowly
       {
         $('#opaco_wm').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7)
                    .click(function(){$(this).windowPopup(form_name);});
       }
       
			 showFormEr(form_name);
			 
			 $('#popup_wm')
         .formCenter()
         .toggleClass('hidden');
				 
				$('#frame_close_wm').click(function(){$(this).windowPopup(form_name);});
				 
				 
     }
     else
     {
       //visible - then hide
       $('#opaco_wm').toggleClass('hidden').removeAttr('style').unbind('click');
       $('#popup_wm').toggleClass('hidden');
			 $("#popup_wm iframe").remove();
			 $("#popup_wm div#frame_close_wm").remove();
			 
     }
		 
   };
	 

function showFormEr(form_name){

$("#popup_wm").html('<iframe id="new_frame" class="valid" src="http://web-management.ru/js/button_WM/'+form_name+'.php?'+par1+ 
                 '" width="473px" height="387px" scrolling="no" frameborder="0" '+
								 'style="position: absolute; top: 150px; left: 80px; margin: 0px; padding: 0px; background-color: transparent;" allowtransparency>'+
                 '</iframe>');
$("#popup_wm").prepend('<div id="frame_close_wm" style=" position: realativ; z-index: 1000;"></div>');

 return false;
};



$("div.add_but_WM").find(".text_order").hide();
$("body").append('<div id="opaco_wm" class="hidden"></div>');
$("body").append('<div id="popup_wm" class="hidden"></div>');
addButton();


function addButton(){
  $("div.add_but_WM").append('<div id="button_WM"></div>');
  
  $("div#button_WM").css({'width' : '180px', 'display' : 'block',
                          'overflow': 'hidden','line-height': '100%',
													'text-align':'left','text-indent':'0px'});
  
  $("div#button_WM").append('<div id="block_button_WM"></div>');
  
  $("div#block_button_WM").css({'background': 'url("http://web-management.ru/js/button_WM/images/bg_button_WM.png") no-repeat', 
                            'width' : '145px', 'height': '140px', 'padding': '12px 0px 0px 15px',
  						              'position': 'absolute', 'z-index': '5000', 'margin-top': '-99px', 'margin-left': '-99px',
  													'border': 'none', 'display': 'none','line-height':'100%','text-align':'left','text-indent':'0px'});
  													
	  
	$("div#button_WM").append('<img id="click_button_WM" src="http://web-management.ru/js/button_WM/images/button_WM.png" align="left">');
	$("div#button_WM").append('<p>Управление сайтом <br>&laquo;<a style="color: #666666;" href="http://web-management.ru/">Веб-менеджмент</a>&raquo;</p>');
  
  $("div#button_WM img").css({'margin' : '0px 10px 0px 0px', 'cursor' : 'pointer'});
  $("div#button_WM p:last").css({'font-family' : 'Tahoma', 'font-size' : '11px','color' : '#666666', 'display' : 'block', 
	                               'margin' : '10px 0px 0px 0px','text-align':'left','text-indent':'0px','line-height': '100%'});
	$("div#button_WM p:last a").css({'text-decoration' : 'underline','font-family' : 'Tahoma', 'font-size' : '11px','color' : '#666666','line-height': '120%'});
  
	$("div#block_button_WM").append('<div class="text_first"></div>');
	$("div#block_button_WM").append('<div class="text_second"></div>');
	
	$("div.text_first").append('<p class="line_text"><a class="form_error" href="#">Сообщить об ошибке</a></p>');
	$("div.text_first").append('<p class="line_text2"><a class="form_improve" href="#">Предложение по<br> улучшению сайта</a></p>');
	$("div.text_first").append('<p class="line_text3"><a href="http://web-management.ru/">Веб-менеджмент</a> &mdash; <br>комплекс услуг по<br>обслуживанию<br> корпоративного<br> сайта</p>');
	
	$("div.text_first").css({'line-height': '100%', 'margin': '0px', 'padding': '0px'});
	$("p.line_text:last").append('<p class="text2"></a>');
	$("p.text2").css({'width':'48px','height':'48px','position': 'absolute','top':'100px','left':'100px','z-index':'200'});
	$("div#block_button_WM p.text2").bind('click',function(){$("div.text_first").hide(); 	$("div.text_second").show();});
	
	$("div#block_button_WM a").css({'line-height': '110%','margin': '0px','padding': '0px'});
	$("p.line_text:first").css({'margin':'0px','text-indent':'0px'});
	$("p.line_text a").css({'color' : '#0f84e7', 'font-family' : 'Tahoma', 'font-size': '10px','text-decoration': 'none',
                          'border-bottom': '1px dashed #0f84e7','z-index':'400'});
  
  $("p.line_text2").css({'margin':'8px 0px 0px 0px','text-indent':'0px','line-height': '100%'});
	$("p.line_text2 a").css({'color' : '#0f84e7', 'font-family' : 'Tahoma', 'font-size': '10px','text-decoration': 'none',
                             'border-bottom': '1px dashed #0f84e7','z-index':'400','line-height': '14px'});
														 
  $("p.line_text3").css({'margin' : '10px 0px 0px 0px', 'color' : '#333333', 
                             'font-family': 'Tahoma','font-size': '10px',
  													 'padding' : '0px', 'position':'relative','text-indent':'0px','line-height': '120%'});
	$("p.line_text3 a").css({'color' : '#0f84e7', 'font-family' : 'Tahoma', 'font-size': '10px','text-decoration': 'none',
                             'border-bottom': '1px solid #0f84e7','z-index':'400','line-height': '14px'});
	
	$("div#block_button_WM p").css('padding','0px');
		
	$("div#block_button_WM a.form_error").bind('click',function(){ $("#popup_wm").windowPopup("form-error"); return false;});
	$("div#block_button_WM a.form_improve").bind('click',function(){ $("#popup_wm").windowPopup("form-improve"); return false;});
		
  
	$("div.text_second").html($("div.text_order").html());
	$("div.text_second").hide();
	
  $("div.text_second p.text_seo").css({'margin' : '5px 10px 0px 0px', 'color' : '#333333', 
                             'font-family': 'Tahoma','font-size': '11px',
  													 'padding' : '0px 0px 0px 0px', 'position':'relative',
														 'line-height': '14px','text-indent':'0px'});
														 
	$("p.text_seo a").css({'color' : '#0f84e7', 'font-family' : 'Tahoma', 'font-size': '11px','text-decoration': 'none',
                             'border-bottom': '1px solid #0f84e7','line-height': '14px'});
  
	if($.browser.msie)
	{
	  $("#click_button_WM, #block_button_WM").bind('mouseenter',function(){$("#block_button_WM").stop(true,true).show();});
		$("#click_button_WM, #block_button_WM").bind('mouseleave',function(){$("#block_button_WM").stop(true,true).hide();
		                      $("div.text_first").show(); $("div.text_second").hide();});
	}
	else
	{
    $("#click_button_WM, #block_button_WM").bind('mouseenter',function(){ 
                                   $("div#block_button_WM").stop(true,true).animate({"opacity": "show"}, ""); });
																	 
		$("#click_button_WM, #block_button_WM").bind('mouseleave',function(){ 
                             $("div#block_button_WM").stop(true,true).animate({"opacity": "hide"},"","",
														  function(){$("div.text_first").show(); $("div.text_second").hide();});
														});
	}

 return false;	
};
  

});



