/**
 * jQuery jWin plugin
 * @name jwin.js
 * @author Maxim Antonov http://www.maxantonov.name/
 * @version 0.1
 * $Revision: 507 $
 * $Author: idler $
 * $Date: 2009-04-13 18:38:19 +0400 (Пнд, 13 Апр 2009) $
 * @category jQuery plugin
 * @copyright (c) 2008 Maxim Antonov (www.maxantonov.name)
 * @license GPL
 */

/**
 * USAGE:
 * var w = $.jWin.create(options);
 *
 * OPTIONS:
 * autoShow (bool): show window automaticaly after it created default false
 * modal (bool):    is window modal default false
 * overlayOpacity (float): opacity of overlay - can be used, only if window is modal default 0.7
 * overlayColor (string):  css-color of overlay layer default black
 * title (string): title
 * content (string): content
 * width (int): default 500
 * height (int): default 200
 * alwaysCentered (bool): default false
 */

(function($){

  $.jWin = function(options){
  var defaults = {
    autoShow: true,
    modal: true,
    zAxis: 500,
    overlayOpacity: 0.98,
    openDuration: 900,
    mode: 0
  };
  var options = $.extend(defaults, options);

  $.extend($.jWin, options);
  var ind=1;
  }
  /**
   * the Handle to keep concrete window pointer
   */
  $.jWin.handle = 0;
  $.jWin.zAxis = 50;
  $.jWin.handleDB = [];
  $.jWin.version = '0.1';

  /**
   * create a new window
   */
  $.jWin.create = function(options){
    return new jWindow(options||j);
  }

  /**
   * Window constructor
   * @param options config object
   */
  var jWindow = function(options){
    //console.dir(options);
    var defaults = {
       alwaysCentered:false,
       autoShow:false,
       modal:true,
       overlayOpacity: 0.7,
       overlayColor: '#000000',
       title: 'Ошибка',
       content: 'Ошибка при запросе',
       width: 500,
       handleEscape:false,
       mode:0
    };

    /// add options in defaults with overwriting and return changed defaults
    this.config = $.extend(defaults, options);

    this.init();
  }

  /**
   * initialize new window
   */
  jWindow.prototype.init = function(){
    //console.log('init');
    $.jWin.handle++;
    this.handle = $.jWin.handle;
    $.jWin.handleDB[this.handle] = this;

    this.create();

   // console.dir(this);
   // console.dir($.jWin.handleDB);
  }

  // Common properties
  jWindow.prototype.handle = 0;
  jWindow.prototype.dom = {};
  jWindow.prototype.title = {};
  jWindow.prototype.body = {};
  jWindow.prototype.overlay = false;
  jWindow.prototype.header = {};


  $.extend(jWindow.prototype,{

    /**
     * creating domElement of this window
     */
    create: function(){
        this.onBeforeCreate();
        this.overlay = this.createOverlay();

        this.dom = $('<div class="jw-wrap" id="jw'+this.handle+'">'+
            '<div class="jw-head" id="jwhead' + this.handle + '">'+
            '<div class="jw-closer"></div><div class="jw-title"></div>' +

            '</div>' +
            '<div class="jw-body">' +
                '<div class="jw-content"></div>'+
            '</div></div>'
        );
        this.dom.appendTo('body');
        $.jWin.zAxis+=10;
        this.calculatePosition();
        this.dom.css({
                        position: 'absolute',
                        visibility:'visible',
                        display:'none',
                        'z-index':$.jWin.zAxis
                      });




        this.setTitle(this.config.title);
        /* this.setContent(this.config.content); */

        this.calculatePosition();

        if(this.config.alwaysCentered){
            var slf = this;
            $(window).bind('resize',function(){slf.calculatePosition();});
            $(window).bind('scroll',function(){slf.calculatePosition();});
        }

      if(this.config.autoShow)  this.show();
    },
    setTitle: function(title){
       this.dom.find('.jw-title').html(title);
    },
    setContent: function(firm,art,descr,price){
    	
    	var letter_style = '';
    	var label_class = '';
    	if(this.config.mode==1){
    		letter_style = 'style="width:308px"'
    		label_class = 'label_1';
    	}
    	
		var content = '<div style="margin:5px 5px 7px;"></div>'+
						'<div id="forms" style="width:308px;float:left;"><div class="form"><form id="askform" method="post" action="/askprice/">'+
						'<input type="hidden" name="mode" value="'+this.config.mode+'">'+
						'<div '+letter_style+'>';
		if(this.config.mode==1){				
			content = content+ '<input type="hidden" name="model" value="'+firm+'">'+
								'<p>Полный каталог запчастей для '+firm+' готовится к публикации.<br/><br/>Для простого и быстрого заказа запчастей, оставьте свой номер телефона. Менеджер перезвонит Вам в кратчайшее время и поможет найти нужную запчасть или позвоните <b>(812) 642-2001</b></p><br/>';
		}else{
			content = content+ 
						'<input type="hidden" name="firm" value="'+firm+'">'+
						'<input type="hidden" name="art" value="'+art+'">'+
						'<input type="hidden" name="descr" value="'+descr+'">';
		}
						
		content=content+'<table '+letter_style+' class="letter letter_1">'+
						'<tr>'+
							'<td><div class="label '+label_class+'">Ваше имя</div></td>'+
							'<td><input type="text" class="error_mark '+label_class+'" name="name" id="f_name"></td>'+
						'</tr>'+
						'<tr>'+
							'<td><div class="label '+label_class+'">Номер телефона<span class="required">*</span></div></td>'+
							'<td><input type="text" class="error_mark '+label_class+'" name="phone" id="f_phone"></td>'+
						'</tr>';
		
		if(this.config.mode==1){
			content = content + '<tr>'+
							'<td><div class="label '+label_class+'">VIN-номер автомобиля</div></td>'+
							'<td><input type="text" class="error_mark '+label_class+'" name="vin" id="f_vin"></td>'+
							'</tr>';
		}
						
		content = content +	'<tr>'+
						'<tr>'+
							'<td align="center" id="submit_tr" style="padding-top:10px;" colspan="2"><input style="width:200px;margin:auto;" type="button" class="submit" value="Запросить"/></td>'+
						'</tr>'+
						'</table></form></div></div></div>';
		if(this.config.mode==1){
			content = content + '<div class="stmt-bg">&nbsp;</div>';
		}
    	$('.jw-content').html(content);
       	if(this.config.mode==1){
       		$('.jw-title').html('<b>Запчасти для '+firm+'</b>');
       	}else{
       		$('.jw-title').html('<b>Заказать '+art+' по цене '+price+' руб.</b>');
       	}
       	
       	var IE='\v'=='v';
		if(IE) {
			$('#submit_tr').css({
							'padding-left':'100px'
							});
		}
     },

    onBeforeCreate:   function(){},
    createOverlay:    function(){

      if(!this.config.modal) return false;
      var html = "<div class=\"overlay\"></div>";
      var z=$.jWin.zAxis -1;
      var h = $(document).height(); var w = $(document).width();
      var   o = $(html).appendTo('body');
      o.css({
             position:'absolute',
             top:0,left:0,
             opacity:this.config.overlayOpacity,
             backgroundColor:this.config.overlayColor,
             visibility:'visible',
             display:'none',
             'z-index':z,
             height:h,width:w
            });
                    $(window).bind('resize',function(){
                            var h = $(document).height();
                            var w = $(document).width();
                            o.css({height:h,width:w});
                    });
        return o;
    },

    show: function(){

        this.calculatePosition();

        if(this.overlay){
            var o = $(this.overlay);
            o.fadeIn(this.config.openDuration);
        }

        this.calculatePosition();
        this.dom.fadeIn(this.config.openDuration);
        var self = this;

        this.dom.find('.jw-closer').click( function(){self.close();});
        this.dom.find('.submit').click( function(){
        	if(checkForm()){
               	$.post('/order/', $("#askform").serialize());
        		$('.jw-content').html('<p style="padding-left:7px;">Ваша заявка принята. Оператор свяжется с Вами в ближайшее время.</p>');
        	}

        	return false;
        });
        
        var self=this;
        if(this.config.handleEscape){
          $(document).bind('keypress', {handle:self}, self.escapeClose);
        }

    },

    calculatePosition: function(){

       this.dom.css({width:this.config.width});
       var dh = this.dom.find('.jw-body').height();
       //if(dh>this.config.height){
            this.dom.find('.jw-content').css({height:this.config.height,overflow:'auto'}); // auto | hidden | scroll | visible
       //}

       var wnd = $(window);
       var doc = $(document);
       var pTop = doc.scrollTop();
       var pLeft = doc.scrollLeft(),
                minTop = pTop;
            pTop += (wnd.height() - this.dom.height()) / 2;
            pTop = Math.max(pTop, minTop);
            pLeft += (wnd.width() - this.dom.width()) / 2;
            this.dom.css({top: pTop, left: pLeft});

    },
    close: function(){
       var self = this;
       $(document).unbind('keypress', self.escapeClose);
       
      if(this.config.onBeforeClose){
        this.onBeforeClose = this.config.onBeforeClose;
      }
      this.onBeforeClose();
      var self = this;
     // console.log('closing',this.dom);
      var dom = this.dom;
      dom.fadeOut(300,function(){self.onAfterClose();dom.remove();} );
      var o = $(this.overlay);
      o.fadeOut(500,function(){o.remove();});

       //console.log('closed');
    },
    onBeforeClose: function(){},
    onAfterClose: function(){},
    escapeClose: function(e){
          if(e.keyCode == 27) e.data.handle.close();
    }
  });

})(jQuery);

function checkForm(name, phone){
		var phone = $('#f_phone').val();
		
		if (phone.length == 0){
			alert("Вы должны заполнить поле \"Телефон\"");
			$('f_phone').focus(); 
			return false;
		}
	
	return true;
}


/*
var i = {x:1,y:2,z:4};$.jWin.revision= (("$Revision: 507 $").replace(/^.*([0-9]*).*$/,'\$1'));
var s = {x:7,s:33,z:8};
var q = $.extend(i,s);
console.dir([i,s,q]);
*/

