// Popin
// p_popin = jQuery object of the popin
// p_width = width of the popin
// p_links = jQuery object of links that open this popin
// p_openAtStart = open this popin on page load
// p_bgPopin = Background layer of the popin

var leafPopin = function (p_popin, p_width, p_links, p_openAtStart, p_bgPopin) {
	this.popin = p_popin;
	this.width = p_width;
	this.toBottomWidth = 100;
	this.links = p_links;
	this.openAtStart = p_openAtStart;
	this.wrapper = p_popin.parent();
	this.bgPopin = p_bgPopin;
	

	this.centerPopin = function(forceCenter){
		if (jQuery(window).height() > this.popin.height() || forceCenter) {
			this.popin.css('left', jQuery(window).width() / 2 - this.popin.width() / 2 + jQuery(window).scrollLeft());
			this.popin.css('top', jQuery(window).height() / 2 - this.popin.height() / 2 + jQuery(window).scrollTop());
		}
	}
	this.centerPopinHandler = function(e){
		e.data.leafPopinObject.centerPopin(false);
	}
	
	this.closePopin = function () {
		this.bgPopin.addClass('hidden');
		this.popin.addClass('hidden');

		if (jQuery.browser.msie && jQuery.browser.version <= 6) {
			jQuery('#A_body select').css('visibility', 'visible');
		}
	};
	this.closePopinHandler = function (e) {
		e.preventDefault();
		e.data.leafPopinObject.closePopin();
		
		jQuery(".bgPopin").unbind();
	};
	
	this.openPopin = function () {
		this.bgPopin.removeClass('hidden');
		this.popin.removeClass('hidden');
		this.centerPopin(true);
		
		if (jQuery.browser.msie && jQuery.browser.version <= 6) {
			jQuery('#A_body select').css('visibility', 'hidden');
		}
		
		jQuery(".bgPopin").bind("click", {leafPopinObject : this}, this.closePopinHandler);
	};
	this.openPopinHandler = function (e) {
		e.preventDefault();
		e.data.leafPopinObject.openPopin();
	};
	
	
	this.init = function () {
		if (this.popin !== null && this.popin.size() > 0) {
			
			// Fix IE
			if (jQuery.browser.msie && jQuery.browser.version <= 7) {
				this.bgPopin.css('opacity', 0.7);
			}
			if (jQuery.browser.msie && jQuery.browser.version <= 6) {
				this.bgPopin.height(this.wrapper.height());
			}
			
			
			var popinContent = this.popin.find('.popin-content');
			
			this.toBottomWidth = this.width + parseInt(popinContent.css('padding-left')) + parseInt(popinContent.css('padding-right'));
			
			popinContent.width(this.width);
			this.popin.find('.t').width(this.toBottomWidth);
			this.popin.find('.b').width(this.toBottomWidth);
			
			if(this.links != null) {
				this.links.bind('click', {leafPopinObject : this}, this.openPopinHandler);
			}
			
			this.popin.find('.jsClosePopin').bind('click', {leafPopinObject : this}, this.closePopinHandler);
			
			jQuery(window).bind('scroll', {leafPopinObject : this}, this.centerPopinHandler);
			jQuery(window).bind('resize', {leafPopinObject : this}, this.centerPopinHandler);
			
			if(this.openAtStart) {
				this.openPopin();
			}
		}
	};
	
	this.init();
};



