var omessage = '';
var oemail = '';
var oname = '';
var otheme = '';
var ocode = '';
formText='none';


	$.extend({

  		getUrlVars: function(){
    		var vars = [], hash;
    		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    		for(var i = 0; i < hashes.length; i++)
    		{
			 hash = hashes[i].split('=');
			 vars.push(hash[0]);
			 vars[hash[0]] = hash[1];
    		}
    		return vars;

  	},

  	getUrlVar: function(name){

    		return $.getUrlVars()[name];

  	}
	});

    
    

        $(document).ready(
		function () {
		var idp = $.getUrlVar('pId');
		
			 jQuery.post("/jqmail/mailform.php", {idp:idp},
   				function(data){
     				formText =data;
   			});

			}		
	);

        var diagnose = function(boxy) {
            alert("Position: " + boxy.getPosition() +
                  "\nSize: " + boxy.getSize() +
                  "\nContent size: " + boxy.getContentSize() +
                  "\nCenter: " + boxy.getCenter());
        };

	function sendMail(r){
		if(r==1){
		oprod_cat = $('#prod_cat').val();
		oprod_name = $('#prod_name').val();
		oprice = $('#price').val();
		omount = $('#mount').val();
		
		
		omessage = $('#message').val();
		ophone = $('#phone').val();
		oname = $('#name').val();
		otheme = $('#theme').val();		
		ocode = $('#code').val();
		$.post("/jqmail/mail.php",{prod_cat:oprod_cat,prod_name:oprod_name,price:oprice,mount:omount,message:omessage,phone:ophone,name:oname,theme:otheme,code:ocode},
   				function(data){
     					sendResult(data);
   			});

	}
	};



	function sendResult(data){
		var res=data.split('{&}');
		if(res[0]==1) Boxy.alert(res[1]); else {
			Boxy.ask(formText+"<hr><span style='color:red;'>"+res[1]+"</span><br>", {"1":"Отправить", "0":"Отмена"}, function(r) {sendMail(r); });
		$('#message').val(omessage);
		$('#phone').val(ophone);
		$('#name').val(oname);
		$('#theme').val(otheme);
		$('#capcha').attr('src','/jqmail/capcha.php?'+Math.random());
		}
	}
        $(function() {
          
          Boxy.DEFAULTS.title = '<img src="/jqmail/images/mail.png" style="float:left;" >&nbsp;&nbsp;&nbsp; Заказ товара';
          
          //
          // Diagnostics
          
          $('#diagnostics').click(function() {
              new Boxy("<div><a href='#' onclick='diagnose(Boxy.get(this));'>Diagnose</a></div>");
              return false;
          });
        
          //
          // Set content
          
          var setContent = null;
          $('#set-content-open').click(function() {
              setContent = new Boxy(
                "<div style='background-color:red'>This is content</div>", {
                  behaviours: function(c) {
                    c.hover(function() {
                      $(this).css('backgroundColor', 'green');
                    }, function() {
                      $(this).css('backgroundColor', 'pink');
                    });
                  }
                }
              );
              return false;
          });
          $('#set-content').click(function() {
              setContent.setContent("<div style='background-color:blue'>This is new content</div>");
              return false;
          });
          
          //
          // Callbacks
          
          $('#after-hide').click(function() {
              new Boxy("<div>Test content</div>", {
                afterHide: function() {
                  alert('after hide called');
                }
              });
              return false;
          });
          
          $('#before-unload').click(function() {
              new Boxy("<div>Test content</div>", {
                beforeUnload: function() {
                  alert('before unload called');
                },
                unloadOnHide: true
              });
              return false;
          });
          
          $('#before-unload-no-auto-unload').click(function() {
              new Boxy("<div>Test content</div>", {
                beforeUnload: function() {
                  alert('should not see this');
                },
                unloadOnHide: false
              });
              return false;
          });
          
          $('#after-drop').click(function() {
              new Boxy("<div>Test content</div>", {
                afterDrop: function() {
                  alert('after drop: ' + this.getPosition());
                },
                draggable: true
              });
              return false;
          });
          
          $('#after-show').click(function() {
              new Boxy("<div>Test content</div>", {
                afterShow: function() {
                  alert('after show: ' + this.getPosition());
                }
              });
              return false;
          });
          
          //
          // Z-index
          
          var zIndex = null;
          $('#z-index').click(function() {
              zIndex = new Boxy(
                "<div>Test content</div>", { clickToFront: true }
              );
              return false;
          });
          
          $('#z-index-latest').click(function() {
              zIndex.toTop();
              return false;
          });
          
          //
          // Modals
          
          function newModal() {
              new Boxy("<div><a href='#'>Open a stacked modal</a> | <a href='#' onclick='alert(Boxy.isModalVisible()); return false;'>test for modal dialog</a></div>", {
                modal: true, behaviours: function(c) {
                  c.find('a:first').click(function() {
                    newModal();
                  });
                }
              });
          };
          
          $('#modal').click(newModal);
          
          //
          // No-show
          
          var noShow;
          $('#no-show').click(function() {
              noShow = new Boxy("<div>content</div>", {show: false});
              return false;
          });
          
          $('#no-show-now').click(function() {
              noShow.show();
              return false;
          });
          
          // Actuator
          
          $('#actuator').click(function() {
              var ele = $('#actuator-toggle')[0];
              new Boxy("<div>test content</div>", {actuator: ele, show: false});
              return false;
          });
          $('#actuator-toggle').click(function() {
              Boxy.linkedTo(this).toggle();
              return false;
          });
          
        });
