
/**
 * [#UPLOAD_SAFARI_TRICK] 
 * make uploads not hang in safari. this is called when submitting upload 
 * /uploads/edit.php (click handler). this performes an ajax call to
 * share/ping/close.php which returns "connection: close" header, telling 
 * safari to close the active connection.
 */
function closeKeepAlive(url) {
	if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
		new Ajax.Request(url, { asynchronous:false });
	}
} // end closeKeepAlive()


// [#FORM_HANDLING] 
//if ( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', form_ff_adapt, false );

/**
 * [#FORM_HANDLING] does insert span tags in labels in order to prevent width in mozilla (Firefox) browsers
 * using the Mozilla-specific document.addEventListener() method to launch the script. 
 * This really is ideal, as it only runs for Mozilla (which is all we need) and 
 * it launches as soon as the DOM has been loaded.
 
function form_ff_adapt() {
	
	if (navigator.userAgent.match(/\bMozilla\b/)) {
	
		// Hide forms
		// $$('form').hide(); // latest prototype version and safari 2 does not recognize elements which are set display to hidden (as if they didn't exists)
		// with new $ version 1.2.1 and noconflict(), it can be used again
		
		//
		// with latest version of $ 1.2.1 li/label does not function properly, only label used here
		// $( 'form#editform' ).find( 'li/label' ).each( function( i ){
		//
		var f = $$('form label'); // get all label elements within form
		
		for(var i=0; i<f.length; i++){
			
			var labelContent = f[i].innerHTML;
			var labelWidth = f[i].getStyle('width');
			var labelSpan = document.createElement( 'span' );
				labelSpan.style.display = 'block';
				labelSpan.style.width = labelWidth;
				labelSpan.innerHTML = labelContent;
			f[i].style.display = '-moz-inline-box';
			f[i].innerHTML = ""; // statt this.innerHTML = null; fuer opera 9.0
			f[i].appendChild( labelSpan );
			
		}
	  
		// Show forms
		// $$('form').show();
	
	} // end if
} // end form_ff_adapt
*/

/*------------------------------------------------------------------------------
Function:       FigureHandler()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
Creation Date:  28 August 2007
Version:        0.1
Homepage:       http://code.google.com/p/easy-designs/wiki/FigureHandler
License:        MIT License (see homepage)
Note:           If you change or improve on this script, please let us know by
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------
function FigureHandler(g,h) {

	if(typeof(h)!=='object'){
		var h={'75-100':'full-col', '67-75':'three-quarters-col', '50-67':'two-thirds-col', '34-50':'half-col', '25-34':'third-col', '0-25':'quarter-col'}
	}
	var i='div.figure';
	if(typeof(g)=='string')i='#'+g+' '+i;
	
	function init(){
		$$(i).each(function(a){
			var b=a.getElementsByTagName('img')[0].width;
			var c=parseInt($(a.parentNode).getStyle('width'));
			var d=Math.ceil(b/c*100);
			var e,col_class;
			for(var f in h) { 		
				e=f.split('-');
				if(d>e[0]&&d<=e[1]){ 	
					col_class=h[f];
					break;
				}
			}
			//alert(col_class);
			a.addClassName(col_class);
			$A(a.getElementsByTagName('p')).each(function(p) { 	p.style.width=b+'px' })
		})
	}init()
}*/


function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

var resized = false;
var contentHeightStart; // footer being repositioned in content element (store original height)
function setViewport(onresize) {
	
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('scrollbar_content').offsetHeight;
			if (onresize==false) contentHeightStart = contentHeight;
			var viewportHeight = windowHeight-138;
			
			// set footer to bottom page if content < viewport
			if (contentHeightStart<viewportHeight) {
				contentHeight = contentHeightStart; // set original content height if repositioned footer moved
				$('footer').setStyle('padding-top: '+(viewportHeight - contentHeightStart - 2)+'px;');
			} // if
			
			if (!Prototype.Browser.MobileSafari) { // exclude iphone
				$('scrollbar_content').setStyle('height: '+viewportHeight+'px;');
				$('scrollbar_handle').setStyle('margin-top: 0;'); // show handle - handle is set to -...px margin-top in css to hide it when js turned off
				var scrollbar = new Control.ScrollBar('scrollbar_content','scrollbar_track');
				Effect.Pulsate('scrollbar_handle', { pulses: 1, duration: 0.5 });
				$('scrollbar_track').setStyle('height: '+viewportHeight+'px;');
			} else {
				$('scrollbar_track').setStyle('height: '+contentHeightStart+'px;'); // iphone stretch track entire content height
			} // if
			
			
			$('canvas-fill').setStyle('height: '+viewportHeight+'px;');
			resized = false;
			
			scrollbar.recalculateLayout();
		}
	}
} // setViewport()

window.onresize = function() {
	if (resized==false) setTimeout("setViewport(true);", 1000); // delay (better usability in firefox)
	resized = true;
}



var Slider = Class.create();
Slider.prototype = {
	
	// setup slider
	initialize: function(wrapperId,slidesId,delay,durationTransition) {

		if ($(wrapperId).id!=undefined) { // only if exists
		
			this.delay = delay;
			this.wrapper = $(wrapperId);
			this.slideslist = $(slidesId);
			this.slides = this.slideslist.getElementsByTagName('li');
			this.durationTransition = durationTransition/1000;
			
			for(i=0; i<this.slides.length; i++) {
				this.slides[i].style.position = 'absolute';
				if (i!=0) { // all but first one
					this.slides[i].style.display = 'none';
				} // if
			} // for
			
			this.startFrame = 0;
			this.endFrame = this.slides.length-1;
			this.currentFrame = this.startFrame;
			
			this.markup();
			setTimeout(this.cicle(this.delay).bind(this), this.delay);
			
		} //if
	}, // initialize
	
	// loop slides
	cicle: function(delay) {

		return (function() {
			this.transition(this.durationTransition);
			setTimeout(this.cicle(this.delay).bind(this), delay + (this.durationTransition*1000));
		})
		
	},
	
	// transition
	transition: function(durationTransition) {
		
		// fade out current slide
		Effect.Fade(this.slides[this.currentFrame], { duration: durationTransition });
		
		this.currentFrame++;
		if (this.currentFrame > this.endFrame) { // loop to start
			this.currentFrame = this.startFrame; 
		} // if
		Effect.Appear(this.slides[this.currentFrame], { duration: durationTransition, object: this, afterFinish: function(){ this.object.markup(); } });
		
		
	},
	
	markup: function() {
		var image = this.slides[this.currentFrame].getElementsByTagName('img')[0];
		this.wrapper.style.width = image.width+'px';
		//alert(image.height);
		this.slideslist.style.height = image.height+'px';
		$('slidebar').innerHTML = image.alt;
	}
	
} // Slider