﻿
//additional properties for jQuery object
$(document).ready(function(){
   //align element in the middle of the screen
   $.fn.alignCenter = 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});
   };

	fields = div = false;
   $.fn.togglePopup = function(data){
     //detect whether popup is visible or not
     if($('#popup').hasClass('hiddenPopup'))
     {
	 
       //hiddenPopup - then display
       //when IE - fade immediately
       if($.browser.msie)
       {
         $('#opaco').height($(document).height()).toggleClass('hiddenPopup')
                    .click(function(){$(this).togglePopup();});
       }
       else
       //in all the rest browsers - fade slowly
       {
         $('#opaco').height($(document).height()).toggleClass('hiddenPopup').fadeTo('slow', 0.7)
                    .click(function(){$(this).togglePopup();});
       }

	   $('#popup').css("background-color","white");
       $('#popup')
         .html($(this).html())
         .alignCenter()
         .toggleClass('hiddenPopup');
		 
 		if(fields) {
			if(div == "block") {
				$("form.requestForm").find("div.inputStyleHidden").css("display", div);
			}
			$.each(fields, function(i, field){
				if(i <= 6) {
					if(field.name == "data[date]") {
						$("form.requestForm").find(":input[value='"+field.value+"']").attr("checked", true).prev().addClass("activeReq");
					} else {
						$("form.requestForm").find(":input[name='"+field.name+"']").attr("value", field.value);
					}
				}
			});
		}
     }
     else
     {
	 if(data == "close") {
		fields = false;
		$("form.requestForm").find("div.inputStyleHidden").css("display", "none");
		$("form.requestForm").find("label.activeReq").removeClass("activeReq").parent().find(":input[value='Не имеет значения']").attr("checked", true).prev().addClass("activeReq");
	 } else {
		fields = $("form.requestForm").find(":input").serializeArray();
		div = $("form.requestForm").find("div.inputStyle").css("display");
	 }
	 $("form.requestForm").find("div.text1, div.text2").hide();
	 
       //visible - then hide
       $('#opaco').toggleClass('hiddenPopup').removeAttr('style').unbind('click');
       $('#popup').toggleClass('hiddenPopup');
     }
   };
});

