/* enable console.log where theres no firebug */
if(!console){
   function consoler(){}
   consoler.prototype.log = function(txt)   {
      alert(txt);
   }   
   var console = new consoler();   
}

/* Popovers START */
var P = false; //global popover object

function Popover(a){	
	this.template  = a.tpl       ? a.tpl       : false;    //string
	this.close     = a.onclose   ? a.onclose   : false;    //function
	this.buttons   = a.buttons   ? a.buttons   : false;    //array of arrays
	this.separator = a.separator ? a.separator : '&nbsp;'  //string
	this.move      = a.dontmove  ? false       : true;     //bool
	this.noblock   = a.noblock   ? true        : false;    //bool
	this.basetpl   = a.basetpl   ? a.basetpl   : 'Base-Popover-Container'; //string
	this.params    = a.params    ? a.params    : {};       //array - a.tpl params for Smarty
	this.title     = a.title     ? a.title     : false;    //string
	this.but_align = a.but_align ? a.but_align : 'center'; //string: left,right,center
	this.width     = a.width     ? a.width     : false;    //integer
	this.height    = a.height    ? a.height    : false;    //integer
	this.mainclass = a.mainclass ? a.mainclass : 'PopoverContainer'; //string 
	
	this.r = '';	
	var instance  = this;
	
	if(!P){
   	if(!this.noblock){
   		// append background div
   		if(!$('#PopoverBlocker').length){
   			this.BlockerEnable();
   		}
   	}   		
   	instance.PrepareView();
   	$('input#loginpop').keydown(Login2Fixer);
   	$('input#haslopop').keydown(Pass2Fixer);
   	$('input.but_anuluj').keydown(Anuluj2Fixer);
	}
}

function Login2Fixer(e){
   if(e.keyCode == 9)      {
      e.preventDefault();
      $('input#haslopop').val('');
      $('input#haslopop').focus();
   }
}

function Pass2Fixer(e){
   if(e.keyCode == 9)      {
      e.preventDefault();
      $('input.but_ok').focus();
   }
}

function Anuluj2Fixer(e){
   if(e.keyCode == 9)      {
      e.preventDefault();
      $('input#loginpop').focus();
   }
}

Popover.prototype.BlockerEnable = function(){
	var w  = '100%'
	var dh = parseInt($(document).height(),10);
	var wh = parseInt($(window).height(),10);
	var h  = 2000+'px';
	if(dh>wh){h = dh+'px';}else{h = wh+'px';}
	var cl = 'PopoverBlocker';
	if($.browser.msie){cl = 'PopoverBlocker4IE';}
	$('<div id="PopoverBlocker" style="height:'+h+';width:100%;">&nbsp;</div>').addClass(cl).appendTo("body");
	if($.browser.msie){$('#PopoverBlocker').css('opacity', .5);}
}

Popover.prototype.PrepareView = function(){
	if(this.template){
	   var ContentTpl = new Smarty({
	      tpl:    this.template,
	      params: this.params
	   });
	   
	   var ContentData = ContentTpl.Get();
	   delete ContentTpl;

	   var ConntentButtons = '';
	   if(this.buttons){
	   	for(var i in this.buttons){
	   		if(this.buttons[i].value){	   		
	   			ConntentButtons += '<input type="button" class="button_'+i+(this.buttons[i].cl ? ' '+this.buttons[i].cl : '')+'" value="'+this.buttons[i].value+'"/>';
	   		}
	   		if(this.buttons[i+1]){
	   			ConntentButtons += this.separator;	
	   		}
	   	}
	   	
	   	if(this.but_align){
	   		var b  = '<div class="PopButtons" style="text-align:'+this.but_align+'">';
	   			    b += ConntentButtons;
	   			 b += '</div>';	   	
	   			 ConntentButtons = b;
	   	}
	   }
	   
	   var ContentTitle = '';
	   if(this.title){
	   	ContentTitle += '<div class="PopTitle">';
	   		ContentTitle += '<h3>'+this.title+'</h3>';
	   	ContentTitle += '</div>';
	   }	   
	   
	   if(this.basetpl){
		   var BasicTpl = new Smarty({
		      tpl:    this.basetpl,
		      params: {
		   		content: ContentData,
		   		buttons: ConntentButtons,
		   		title:   ContentTitle
		   	}
		   });
		   
		   this.r = BasicTpl.Get();
		   delete BasicTpl,ContentData,ConntentButtons;
		   
		   var instance = this;
		   instance.Render();
	   }
	}
}

Popover.prototype.Render = function(){
	var p = {
			'z-index' : 101,
			'position': 'absolute',
			'top'     : 0
	};
	
	if(this.width){p['width']  = this.width+'px'}
	if(this.width){p['height'] = this.height+'px'}
	
	$(this.r).css(p).addClass('PopoverBaseClass').addClass(this.mainclass).appendTo('body');
	var bh = parseInt($('.PopoverBaseClass').height(),10);
	var wh = parseInt($(window).height(),10);
	var bt = 0;
	if(wh>bh){bt = parseInt((wh-bh)/2)}
	
	var bw = parseInt($('.PopoverBaseClass').width(),10);
	var ww = parseInt($(window).width(),10);
	var bl = 0;
	if(ww>bw){bl = parseInt((ww-bw)/2,10)}
	
	var st = parseInt($(window).scrollTop(),10);	
	$('.PopoverBaseClass').css({top: (bt+st)+'px', left: bl+'px'});
	if(this.buttons){$('.PopButtons').css({width: parseInt($('.PopoverBaseClass').width(),10)+'px'});}	
	
	var instance = this;
	instance.EnableEvents();
}

Popover.prototype.EnableEvents = function(){	
	var instance  = this;
	
	if(this.move){
		$('.PopoverBaseClass').draggable({ handle: 'div.close','containment': 'document' });
	}
	
	$('.PopoverBaseClass > .close > img').click(function(){
		instance.Close();
		if(instance.close){
			instance.close();
		}
	});
	
   if(this.buttons){
   	for(var i in instance.buttons){
   		if(instance.buttons[i].func){
   			$('.PopButtons > .button_'+i).bind('click',instance.buttons[i].func);
   		}
   	}
   }
   
   P = this;
}

Popover.prototype.Close = function(){
	$('.PopoverBaseClass, #PopoverBlocker').remove();
	P = false;
}

Popover.prototype.Fade = function(after){
	$('.PopoverBaseClass').fadeOut(700,function(){
		$('.PopoverBaseClass').remove();
		P = false;
		if(after){after();}		
	});
}

/* Popovers END */
