/********************************************************************/
/********************************************************************/
/* START: PageToolsShareLayer Class                                 */
var T__S;
document.observe('dom:loaded', function() {
	if( $('tools-share') || $('footer-tools-share') ) {
		new Insertion.Before( $('footer-position-placeholder'), '<div id="tools-layer-share" class="toolbar-layer"><div class="toolbar-content clearfix" style="padding:0pt;"><div class="close"><a href="javascript:void(0);"><span class="access">Close Contact Layer</span></a></div><div id="tools-layer-share-content"><br /><img src="'+ROOT_PATH+'framework/pub/img/layer/ajax-loader.gif" alt="Loading" /></div><div></div>');
		$('tools-layer-share').setStyle({ width:'276px' });
		T__S = new PageToolsShareLayer($('tools-layer-share'), $('tools-share') );
	}
});

var __toolsShareFile = ROOT_PATH + (PAGE_LANG == 'en' ? 'apps/share/tools-share.php' : 'apps/share/tools-share-de.php');

var PageToolsShareLayer = Class.create();
PageToolsShareLayer.prototype = Object.extend(new Layer, {
    initialize: function(node, trigger) {
		//case A: top pagetools do exist
		if(trigger != null) {
			this.initSuper(node, trigger);
			if($('footer-tools-share') ) {
				$('footer-tools-share').href = 'javascript:void(0);';
				$('footer-tools-share').observe("click", function(e) {this.toggle(e);  Event.stop(e); }.bindAsEventListener(this) );
			}
		//case B: top pagetools do not exist
		} else {
			this.initSuper(node, $('footer-tools-share'));
			$('footer-tools-share').href = 'javascript:void(0);';
		}

        var closeButton = Helper.getCloseButton(this.node);
        closeButton.observe("click", function(){this.close();}.bindAsEventListener(this));
        
        Event.observe(window, 'resize', function() {this.correctPosition() }.bindAsEventListener(this) );
        
        this.isFooter		= false;
        this.content 		= '';
        this.contentReady 	= false;
        this.contentLoading = false;
    },
    afterClose: function(newLayer) {
        if (!Layer.toggle) { HeaderAnimation.augment(); }
    },
    afterOpen: function() {
        HeaderAnimation.diminish();
        this.correctPosition();
    },
    beforeClose: function() {
        HeaderAnimation.unregisterLayer();
        return true;
    },
    beforeOpen: function() {
        HeaderAnimation.registerLayer(this);
        if( this.contentLoading == false && this.contentReady == false ) {
            this.contentLoading = true;
            this.getPageToolsShareContent(__toolsShareFile);
        }
        return true;
    },
    correctIframe: function() {
        this.iframeLining.correction.left = -1;
        this.iframeLining.correction.top = -1;
    },
    correctPosition: function() {
    	if(this.isFooter) {
        	var footerpos 	= $('footer-pagetools-zone').cumulativeOffset();
        	var iconpos		= $('footer-tools-share').cumulativeOffset();
        	$('tools-layer-share').setStyle({ 	top:  ( footerpos.top - $('tools-layer-share').getHeight() + 11 ) +'px',
        										left: ( iconpos.left - $('tools-layer-share').getWidth() - 4 )+'px' });
        	if(Info.browser.isIEpre7) { this.iframeLining.refresh(); }
        }
    },
    hide: function() {
        $("tools-layer-share").removeClassName("active-layer");
		//fix IE6 <select> Bug
		if(Info.browser.isIEpre7) {	$$('select').each( function(elm) { elm.show(); } );	}
    },
    getPageToolsShareContent: function(url) {
        if( this.contentReady == false ) {
            new Ajax.Request(url,
                  {
                    method:'get',
                    onComplete: this.onresult.bind(this),
                    onFailure: function(oXHR) {
                        alert("Error: " + oXHR.status);
                    }  
            });
        }
    },
    onresult: function(xhr) {
        var tmpStr  = xhr.responseText.substring( xhr.responseText.indexOf('<div id="social-bookmarking">') );
        tmpStr      = tmpStr.substring ( 0, tmpStr.indexOf('<!-- end social bookmarks -->')  );		
        $('tools-layer-share-content').update(tmpStr);
        this.contentReady = true;
        this.contentLoading = false;
        	
		//fix IE6 <select> Bug
		if(Info.browser.isIEpre7) {	$$('select').each( function(elm) { elm.hide(); } );	}
		this.afterOpen();
    },
    checkSiteIdStatus: function () {
        return this.contentReady;
    },
    show: function(e) {
    	var _parent 	= Event.findElement(e,'div');
    	if( _parent.id == 'pagetools-zone') {
    		var pos 		= $('pagetools-zone').cumulativeOffset();
    		this.isFooter	= false;
    		$('tools-layer-share').setStyle({ top: (pos.top + $('pagetools-zone').getHeight() + 5) +'px', bottom:'', left: (pos.left -1)+'px' });
    	} else {
    		var footerpos 	= $('footer-pagetools-zone').cumulativeOffset();
    		var iconpos		= $('footer-tools-share').cumulativeOffset();
    		this.isFooter	= true;
    		$('tools-layer-share').setStyle({ 	top: (footerpos.top + 10 ) +'px',
    											left: (iconpos.left - $('tools-layer-share').getWidth() - 6 )+'px' });
    	}
		
        $("tools-layer-share").addClassName("active-layer");
        this.trigger.addClassName("clicked"); // avoids hover effect (only for the first time)
        this.trigger.observe("mouseout",
            function(e) {
                this.trigger.removeClassName("clicked");
                this.trigger.stopObserving("mouseout");
            }.bindAsEventListener(this)
        );
    }
});

/* END: PageToolsShareLayer                                         */
/********************************************************************/

/********************************************************************/
/* START: Fix for Footer-PageTools Fontsize                         
 * Important: To take effect this .js has to be inlcuded
 *            AFTER VI's script.js and/or module.fontsize.js
*/

function init_fontsize() {
	if ($('pagetools-fontsize') || $('footer-tools-fontsize-decrease') ) {
		var font = new FontSize;
		var title = new Cookie('text-size').read(font.getDefaultStyleSheet());
		font.setActiveStyleSheet(title);
	}
}

FontSize.prototype.setActiveStyleSheetDefault = FontSize.prototype.setActiveStyleSheet;
FontSize.prototype.initializeDefault = FontSize.prototype.initialize;

FontSize.prototype.initialize = function() {
	//this.initializeDefault();
	if ($('pagetools-zone')) {
		$("tools-fontsize-decrease").observe("click", function() {
			this.fontSizeDown();
		}.bindAsEventListener(this));
		$("tools-fontsize-enlarge").observe("click", function() {
			this.fontSizeUp();
		}.bindAsEventListener(this));
	}
	
	if($("footer-pagetools-zone")) {
		$("footer-tools-fontsize-decrease").observe("click", function() {this.fontSizeDown();}.bindAsEventListener(this));
		$("footer-tools-fontsize-enlarge").observe("click", function() {this.fontSizeUp();}.bindAsEventListener(this));
	}
};

FontSize.prototype.setActiveStyleSheet = function(title) {
	//this.setActiveStyleSheetDefault(title);
	
	var i, a;
	for(i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (/\bstylesheet\b/.test(a.rel) && a.title) {
			a.disabled = true; // always set true first for IE7 
			if (a.title == title) {
				a.disabled = false;
			}
		}
	}

	if($("pagetools-zone")) {
		switch(title) {
			case 'A+':
				$('tools-fontsize-enlarge').removeClassName('deactivated');
				$('tools-fontsize-decrease').removeClassName('deactivated');
				break;
			case 'A++':
				$('tools-fontsize-enlarge').addClassName('deactivated');
				break;
			default:
				$('tools-fontsize-decrease').addClassName('deactivated');
				break;
		}
	}
	
	if($("footer-pagetools-zone")) {
		switch(title) {
			case 'A+':
				$('footer-tools-fontsize-enlarge').removeClassName('deactivated');
				$('footer-tools-fontsize-decrease').removeClassName('deactivated');
				break;
			case 'A++':
				$('footer-tools-fontsize-enlarge').addClassName('deactivated');
				break;
			default:
				$('footer-tools-fontsize-decrease').addClassName('deactivated');
				break;
		}
	}
	
	new Cookie('text-size').write(title, 365 * 24 * 3600);
};
/* END: Fix for Footer-PageTools Fontsize                           */
/********************************************************************/