var shareOverlay;
var shareWindow;
var shareTween;
var isDisplayed;

function initShare() {
	shareOverlay = $('shareOverlay');
	shareWindow = $('shareWindow');
	isDisplayed = false;

	shareTween = new Fx.Tween(
			'shareOverlay',
			{
				property: 'opacity',
				duration: 500, 
				transition: Fx.Transitions.Quart.easeOut
			}
	);
	
	window.addEvent('resize',function(e){
		shareResizeOverlay();
	});
	shareResizeOverlay();
	
	if (window.location.hash.substr(0, 7) == '#share_') {
		showShare(window.location.hash.substr(7));
	}
	else if (window.location.hash.substr(0, 6) == '#share') {
		if ($('sharekey')) {
			showShare($('sharekey').value);
		}
	}
}

function displayShareWindow() {
	shareWindow.setStyle('display','block');
	new Fx.Scroll(window, { wait: false, duration: 1000, offset: {'x': -200, 'y': -50}, transition: Fx.Transitions.Quad.easeInOut }).toElement(shareWindow);
}

function shareGetScrollBarWidth () {
	var inner = document.createElement('p');
	inner.style.width = '100%';
	inner.style.height = '200px';
	
	var outer = document.createElement('div');
	outer.style.position = 'absolute';
	outer.style.top = '0px';
	outer.style.left = '0px';
	outer.style.visibility = 'hidden';
	outer.style.width = '200px';
	outer.style.height = '150px';
	outer.style.overflow = 'hidden';
	outer.appendChild (inner);
	
	document.body.appendChild (outer);
	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';
	var w2 = inner.offsetWidth;
	if (w1 == w2) {
		w2 = outer.clientWidth;
	}
	
	document.body.removeChild (outer);

	return (w1 - w2);
}

/*
 * shareGetPageSize()
 * Returns array with page width, height and window width, height
 * Core code from - quirksmode.org
 * Edit for Firefox by pHaez
 * Some more edits to get the desired result by AUVICA GmbH
 */
function shareGetPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else {
		xScroll = Math.max(document.body.scrollWidth, document.body.offsetWidth);
		yScroll = Math.max(document.body.scrollHeight, document.body.offsetHeight);
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	pageHeight = Math.max(windowHeight, yScroll);
	pageWidth = Math.max(windowWidth, xScroll);
	
	// This detects Gecko-based browsers or Safari, which unfortunately count the scrollbar as part of the page...how considerate of them
	if (!window.ActiveXObject && ((document.getBoxObjectFor != undefined) || (!navigator.taintEnabled))) {
		var scrollbarWidth = shareGetScrollBarWidth();
		var hasScrollbar = new Array(windowWidth < pageWidth, windowHeight < pageHeight);
		
		if (hasScrollbar[1]) {
			pageWidth -= scrollbarWidth;
		}
		if (hasScrollbar[0]) {
			pageHeight -= scrollbarWidth;
		}
	}
	
	return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
}
	
function shareResizeOverlay() {
	shareOverlay.setStyle('display','none'); // hide overlay before resizing
	var pageSize = shareGetPageSize();
	if (isDisplayed) {
		shareOverlay.setStyle('display','block');
	}
	var width;
	var height;
	
	width = parseInt(pageSize[0]) + 'px';
	height = parseInt(pageSize[1]) + 'px';
	
	shareOverlay.setStyle('width',width);
	shareOverlay.setStyle('height',height);
}

/** fill the share window and show it
 * @param key media key
 */
function showShare(key) {
	if (key) {
		shareOverlay.setStyle('display','block');
		shareTween.start(0,.8);
		displayShareWindow.delay(500);

		new Request(
		{
			url: "ajax_com.php?action=share",
			method:'post',
			onSuccess: function(e){updateField('shareWindow', this.xhr); initAccordion();},
			onFailure: function(e){showError(this.xhr)},
			data: 'key='+encodeURIComponent(key)
		}
		).send();
		
		isDisplayed = true;
	}
}

function hideShare() {
	shareWindow.setStyle('display','none');
	shareOverlay.setStyle('display','none');
	shareOverlay.setStyle('opacity','0');
	
	isDisplayed = false;
}

function initAccordion() {
	// Anpassung IE6
	if(window.ie6) var heightValue='100%';
	else var heightValue='';

	// Selektoren der Container für Schalter und Inhalt
	var togglerName='dt.accordion_toggler_';
	var contentName='dd.accordion_content_';


	// Selektoren setzen
	var counter=1;   
	var toggler=$$(togglerName+counter);
	var content=$$(contentName+counter);

	while(toggler.length>1)
	{
		// Accordion anwenden
		new Accordion(toggler, content, {
			opacity: false,
			display: 0,
			alwaysHide: true,
			onComplete: function() { 
			var element=$(this.elements[this.previous]);
			if(element && element.offsetHeight>0) element.setStyle('height', heightValue);         
		},
		onActive: function(toggler, content) {
			toggler.addClass('open');
		},
		onBackground: function(toggler, content) {
			toggler.removeClass('open');
		}
		});

		// Selektoren für nächstes Level setzen
		counter++;
		toggler=$$(togglerName+counter);
		content=$$(contentName+counter);
	}
}
