/*
	Function.js
	contains common functions and objects
*/


/*
	roller object
	constructor
*/
function roller( id, moveable ) {
	if ( !moveable ) return;

	var self = this;
	this.state = false;
	this.objname = id;
	this.lastY = 0;
	this.delta_y = 0;
	this.inDuration = false;
	this.RollInDuration = false;
	this.Effects = {
		"MovementEffect"		: null,
		"FoldEffect"			: null,
		"Fold2ndEffect"			: null
	};
	
	Event.observe( window, "load", function() { self.setup() } );
}

/*
	Setup function
	executed after body.onload event fired
*/
roller.prototype.setup = function() {
	var self = this;
	
	this.obj = $(this.objname);
	if ( this.obj.style.display != "none" ) {
		this.state = true;
	}

	
	// 
	// Effect für Scrollen des Actionpads
	//
	var effect = new Fx.Style(this.obj.parentNode, "margin-top", { 
					transition	: Fx.Transitions.cubicOut, 
					duration	: 500,
					onComplete  : function() {
						self.inDuration = false;
					},
					onStart 	: function() {
						self.inDuration = true;
					}
				});
	this.Effects.MovementEffect = effect;
	
	
	// Effect für das Klappen des Actionpads
	effect = new Fx.Style( this.obj, "height", {
		transition	: Fx.Transitions.cubicOut,
		duration	: 500,
		onComplete 	: function() {
			self.Effects.Fold2ndEffect.custom( 200, 0 );
		},
		onStart		: function() {
		}
	});
	this.Effects.FoldEffect = effect;
	
	
	// 2ter Anteil der Klappaktion
	effect = new Fx.Style( this.obj, "width", {
		transition	: Fx.Transitions.cubicOut,
		duration	: 500,
		onComplete	: function() {
		},
		onStart		: function() {
		}
	});
	this.Effects.Fold2ndEffect = effect;
	
	window.setInterval( this.scroll.bindAsEventListener(this), 100 );	
}


/*
	gibt den ScrollOffset der website zurueck
*/
roller.prototype.getScrollOffset = function() {
	var x,y;

	if (window.pageYOffset) // all except Explorer
	{
		x = window.pageXOffset;
		y = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return { x: x, y : y };
}

/*
	zeigt an ob sich das ActionPad bewegen muss
*/
roller.prototype.needMove = function() {
	var off = this.getScrollOffset();
	return ( parseInt(this.obj.parentNode.style.marginTop) != (off.y ) && ( !this.inDuration ) );
}

/*
	Scroll function
	executed every 100ms
	change topoffset for this.obj in relation to scrollbarposition
*/
roller.prototype.scroll = function() {
	if ( this.needMove() ) {
		var offset = this.getScrollOffset();
		this.Effects.MovementEffect.custom(parseInt(this.obj.parentNode.style.marginTop), offset.y);
	}
}



/*
	Roll function
	exectuted after click on the actionpad
	set session information
*/
roller.prototype.roll = function() {
	var self = this;
	
	if ( this.RollInDuration ) return;
	
	new Ajax.Request("includes/ajax_setsessiondata.php",{
		method 		: "post",
		parameters: {
						key : "user_actionpadstatus",
						keyvalue : v
					  }
	});
	
	
	self.RollInDuration = true;
	if ( this.state ) {
		new Effect.Fold( this.obj,{
			duration : 0.4,
			afterFinish 	: function() {
				setTimeout(function() { 
					self.RollInDuration = false;
					self.state = false; 
				}, 500 );
			}
		});
	} else {
		new Effect.Appear( this.obj,{
			duration : 0.2,
			afterFinish		: function() {
				setTimeout(function() { 
					self.RollInDuration = false;
					self.state = true; 
				}, 500 );
			}
		});
	}

	//this.state = !this.state;
	var v = "1";
	if ( this.state ) v = "1"; else v = "0";

	var c = $$("div#ap_left .ACTIONPAD_APSWITCH img");
	if ( c.length == 1 ) {
		c = c[0];

		if ( c.src.search(/_close/) > -1 ) {
			c.src = c.src.replace(/_close/,"_open");
		} else if ( c.src.search(/_open/) > -1 ) {
			c.src = c.src.replace(/_open/,"_close");
		}

	}
}




/*
	Category
	change position of category
*/
function toggle_display(cat_id) {
	var ele = document.getElementById("c_" + cat_id);


	var cat = ele.parentNode.getElementsByTagName("li");
	for ( i=1; i <= cat.length; i++ ) {
		if ( cat[i-1] && cat[i-1].childNodes[0] ) {
			cat[i-1].childNodes[0].style.marginTop = "0px";
			cat[i-1].childNodes[0].style.marginBottom = "2px";
		}
	}
	if ( ele.childNodes[0] ) {
		ele.childNodes[0].style.marginTop = "2px";
		ele.childNodes[0].style.marginBottom = "0px";
	}

	for (i=1; i <= cat.length; i++ ) {
		ele = document.getElementById("Top4Navi_color"+i);
		if ( ele ) {
			ele.style.display = "none";
		}
	}
	document.getElementById("Top4Navi_" + cat_id).style.display = "block";
	//new Effect.Appear( $("Top4Navi_" + cat_id) );

	//document.getElementById("placeholder").style.display = "none";

}
/*
	Mediagallery
*/
function display_media(winnum,medianum)
{
        var dest = document.getElementById('mediawindow' + winnum);
        var source = document.getElementById('media' + medianum);
        if (source) {
                if (dest.firstChild) {
                        dest.removeChild( dest.firstChild );
                }
                dest.appendChild( source.firstChild.cloneNode( 1 ) );
        }
}


/*
	function written vor href tag of a link
*/
function openPopup(URI, windowname, settings) {
	window.open(URI, windowname, settings);
}



/*
	Open the Popup containing the Gallery

	id = Post_ID

	Bruno
	17.04.2007

	now able to request media elements from a user profile also
	allen 1/5/2007
*/
function openGallery( type, postId ) {
	if (type == 'profile') {
		openPopup("/includes/limesurvey/includes/ajax_mgallery.php?userId=" + postId,"_blank","width=480,height=450, scrollbars=no, statusbar=no");
	} else if (type == 'survey') {
		openPopup("/includes/limesurvey/includes/ajax_mgallery.php?questionId=" + postId,"_blank","width=480,height=450, scrollbars=no, statusbar=no");
	} else {
		openPopup("/includes/limesurvey/includes/ajax_mgallery.php?postId=" + postId,"_blank","width=480,height=450, scrollbars=no, statusbar=no");
	}
	return false;
}
 


/*
	opens the Bookmark div
	
	Bruno
	30.04.2007
*/
function openBookmarks( where ) {
	var coords = Position.positionedOffset( $(where) );
	
	$("bookmarks").style.top = (coords[1] + $(where).getHeight()) + "px";
	$("bookmarks").style.left = coords[0] + "px";
	
	new Effect.toggle( $("bookmarks") );
	return false;
}



