/* settings */
// fixed iframe width
var egp_frame_w = 600;
// collapsed iframe height
var egp_frame_h = 64;
// expanded iframe height
// iframe document can try to update this value (see body onload in egp_frame_test.html example)
// but I'm affraid it will not work because the iframe document comes from a different domain
var egp_frame_h2 = 595;

var egp_frame_firstCall = true;

// ATTENTION: add 20px for IE6
// url to load in iframe
var egp_frame_url = "http://europeangreens.eu/widget/?frameurl="+escape(window.location.href);
//var egp_frame_url ="http://europeangreens.eu/fileadmin/js/egp_frame_content.html?frameurl="+escape(window.location.href);

// aditionally toggle animation parameters may be adjusted (see below)

function egp_frame_init() {
	var c = document.getElementById('egp_frame_container');
	if (! c)
		return;
	// var style = 'width:'+ egp_frame_w +'px; height:'+ egp_frame_h + 'px';
	// width set to maximum/auto
	var style = 'width: 100%; height:'+ egp_frame_h + 'px';
	var h = '<iframe id="egp_frame" name="egp_frame" src="'+ egp_frame_url +'" style="'+ style +'" scrolling="no" frameborder="0"></iframe>';
	c.innerHTML = h;
	if (typeof(console) != 'undefined') console.debug('EGP FRAME URL: '+egp_frame_url);

}

var egp_frame_expanded = false;
function egp_frame_toggle() {
	if (egp_frame_anim)
		return;
	if (! egp_frame_h2)
		return;
	var cur_h = egp_frame_expanded ? egp_frame_h2 : egp_frame_h;
	egp_frame_expanded = ! egp_frame_expanded;
	var next_h = egp_frame_expanded ? egp_frame_h2 : egp_frame_h;

	// animation parameters
	egp_frame_anim = {
		start: cur_h, // start value
		end: next_h, // end value
		step: 0, // current step, must be 0

		steps: 20, // number of animation steps
		duration: 500, // total animation duration
		animation: 'power3', // one of the function names from egp_frame_anim_funcs (see below)
		action : function(val) {
			var f = document.getElementById('egp_frame');
			f.style.height = val + 'px';
		}
	}
	egp_frame_animate();
}

var egp_frame_anim = null;


/* Animation transformation functions,
 * receive a float input parameter in range [0,1]
 * and must return a float also in range [0,1]
 */
var egp_frame_anim_funcs = {
	linear: function(input){
		return input
	},
	power2: function(input){
		return input * input;
	},
	power3: function(input){
		return input * input * input;
	},
	sqrt: function(input) {
		return Math.sqrt(input);
	}
}

function egp_frame_animate() {
	var a = egp_frame_anim;
	a.step++;
	if (a.step > a.steps) {
		egp_frame_anim = null;
		return;
	}

	// input is value in range [0,1]
	var input = a.step / a.steps;
	
	// calling transform function
	var factor = egp_frame_anim_funcs[a.animation](input);

	// interpolating with factor
	var interval = a.end - a.start;	
	var val = a.start + interval * factor; 
	a.action(val);
	
	var tm  = Math.round(a.duration / a.steps);
	setTimeout('egp_frame_animate()', tm);
}

// Re: http://www.tagneto.org/blogcode/xframe/ui.html
var lastId = "";

function egp_frame_change(newHeight) { // only called in expanded state
	if (egp_frame_anim)
		return;
	if (!egp_frame_expanded) // shuld only be called in expanded state!
		return;
	// animation parameters
	egp_frame_anim = {
		start: egp_frame_h2, // start value
		end: newHeight, // end value
		step: 0, // current step, must be 0
		steps: 20, // number of animation steps
		duration: 500, // total animation duration
		animation: 'power3', // one of the function names from egp_frame_anim_funcs (see below)
		action : function(val) {
			var f = document.getElementById('egp_frame');
			f.style.height = val + 'px';
      }
   }
   
	egp_frame_h2 = newHeight;
   egp_frame_animate();
}

function egp_height(newHeight) {
   var cur_h = egp_frame_expanded ? egp_frame_h2 : egp_frame_h;
   if (egp_frame_expanded) {
      egp_frame_change(newHeight);
   } else if (newHeight != cur_h) {
      egp_frame_h2 = newHeight;
      if (egp_frame_firstCall) {
			egp_frame_firstCall = false;
			return;
		}

		egp_frame_toggle();
   }
}

function checkForMessages(){
	if(window.location.hash != lastId) {		
		lastId = window.location.hash;
		// get height from message
		var newheight = parseInt(lastId.substring('#egpframe'.length,lastId.indexOf('frameegp')));

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

		 newheight = egp_frame_h2;      // FIXED HEIGHT!!!!!!!!!!

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if (!isNaN(newheight)) {
//			if (document.getElementById("output")) // debug help
//				document.getElementById("output").innerHTML = "Received length: " + newheight + document.getElementById("output").innerHTML;
			egp_height(newheight);
		}	
		window.location.hash = '#';
	}
}

egp_frame_init();
setInterval(checkForMessages, 250);

