// Version SVN: $Id: share.js.php 16791 2010-04-29 15:36:41Z sven $
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, 0.8);
		displayShareWindow.delay(500);
		
    new Request(
      {
        url: 'http://www.lifestyler.tv/media/ajax?action=share',
        method: 'post',
        onSuccess: function(e){updateShareWindow(this.xhr);},
        onFailure: function(e){showError(this.xhr)},
        data: 'key='+encodeURIComponent(key)
      }
    ).send();
		
		isDisplayed = true;
	}
}

function updateShareWindow(xhr) {
	var field = $('shareWindow');
	if (!field) {
		return;
	}
	
	field.set('html', xhr.responseText);
  initAccordion();
}

function hideShare() {
	shareWindow.setStyle('display','none');
	shareOverlay.setStyle('display','none');
	shareOverlay.setStyle('opacity','0');
	
	isDisplayed = false;
}

function initAccordion() {
	var tmpElements = [];
	$$('.accordion_content').each(function(el, i) {
		tmpElements[i] = new Element('div', {'styles': {'position': 'relative', 'overflow': 'hidden'}}).wraps(el);
	});
	var tmpTogglers = [];
	$$('.accordion_toggler').each(function(el, i) {
		tmpTogglers[i] = new Element('div', {'styles': {'position': 'relative'}}).wraps(el);
	});
	
	var shareAccordion = new Accordion(tmpTogglers, tmpElements, {
		opacity: false,
    show: 0,
    alwaysHide: true,
		onActive: function(toggler, element) { toggler.addClass('open'); },
		onBackground: function(toggler, element) { toggler.removeClass('open'); }
	});
}

function send2friend(key) {
	if (!$('send2friend_email') || !$('send2friend_message')) {
		return;
	}
	
	new Request(
	  {
	    url: 'http://www.lifestyler.tv/media/ajax?action=send2friend',
		  method: 'post',
		  onFailure: function(e){ showError(this.xhr) },
		  onComplete: function(e){ updateShareBox(this.xhr) },
		  data: 'key='+ encodeURIComponent(key) +'&email='+ encodeURIComponent($('send2friend_email').value) +'&message='+ encodeURIComponent($('send2friend_message').value)
	  }
	).send();
}

function updateShareBox(transport) {
	if (!transport.responseXML || !transport.responseXML.documentElement) {
		return;
	}
	
	var docElement = transport.responseXML.documentElement;
	
	var message = docElement.getElementsByTagName('message')[0].firstChild.data;
	var error = parseInt(docElement.getElementsByTagName('error')[0].firstChild.data);
	
	$('sharebox_status').set('html', message);
	$('sharebox_status').setStyle('display', 'block');

	if (!error) {
		$('send2friend_email_id').value = '';
		$('send2friend_message_id').value = '';
	}

	window.setTimeout('hideShareBoxStatus()', 3000);
}

function hideShareBoxStatus() {
	$('sharebox_status').set('html', '');
	$('sharebox_status').setStyle('display', 'none');
}

function showError(r){
  alert("Error: " + r.status + " - " + r.statusText);
}

