/******************************************
* Slider Bar Form Element Script
* 
* Original program copyright David Harrison
*   d_s_h2@hotmail.com
* Version 2 copyright Eric C. Davis
*   eric@10mar2001.com
* 
* Visit http://www.dynamicdrive.com for
*   loads of other scripts.
* 
* This notice MUST stay intact for use.
*
* Modified by Tom Westcott
* http://www.cyberdummy.co.uk
******************************************/

var x, lgap,
	pel = null;

function newValue(value, tag) {
	el = document.getElementById(tag);
	var i = (Number(value));
	el2 = document.getElementById(tag + "out");
	width = (el2.style.width.replace(/\D/g,"") - 11);
	if(i > width) i = width;
	if(i < 0) i = 0;
	el.style.marginLeft = i + "px";
	el.inputElement.value = i;
}

function mousetracker (event) {
	if (!event) {
		event = window.event;
	}
	x = event.clientX;
	if (pel != null) {
		var width = pel.sliderWidth;
		pel.style.marginLeft = ( ( (lgap + x) > width) ? width : ( ( (lgap + x) < 0) ? 0 : lgap + x ) ) + "px"; //>
		pel.inputElement.value = pel.style.marginLeft.replace(/\D/g,"");
		pel.inputElement.onDrag();
	}
}

function track () {
	pel = this;
	// added this so it mouse strays from the drag tab mouse up still releases it
	if (document.addEventListener) {
                document.addEventListener('mouseup', stop, false);
        } else {
		document.onmouseup = stop;		
        }
	pel.inputElement.onDragStart();
	lgap = parseInt(pel.style.marginLeft.replace(/\D/g,""));
	if (isNaN(lgap)) {
		lgap = 0;
	}
	lgap -= x;
}

function stop() {
	pel.inputElement.onDragStop();
	pel = null;

	if (document.removeEventListener) {
                document.removeEventListener('mouseup', stop, false);
        } else {
                document.onmouseup = null;
        }
}

function form_slider (el, width) {
	var wid 		= parseInt(width);
	var newEl 		= document.createElement("input");
	newEl.type 		= "hidden";
	var outDiv 		= document.createElement("div");
	outDiv.className	= "move";
	outDiv.style.width 	= (wid+11) + "px";
	outDiv.id		= el.id + "out";
	var inDiv 		= document.createElement("div");
	inDiv.className 	= "move2";
	inDiv.id 		= el.id + "in";
	var slider 		= document.createElement("div");
	slider.className 	= el.className;
	slider.id 		= el.id;
	// convert original form element to new form element
	for (key in el) {
		try {
			newEl[key] = el[key];
		} catch (er) {
			// whoops! Can't assign that property.
		}
	}
	newEl.type 		= "hidden";
	newEl.className 	= "";
	newEl.name 		= el.name;
	newEl.id 		= el.id + "hidden";
	// assign persistent properties to slider div
	slider.inputElement	= newEl;
	slider.sliderWidth 	= wid;
	// assign events
	if (slider.addEventListener) {
		slider.addEventListener('mousedown', track, false);
		slider.addEventListener('mouseup', stop, false);
	} else {
		slider.onmousedown = track;
		slider.onmouseup = stop;
	}
	// put the new elements in the document
	outDiv.appendChild(inDiv);
	outDiv.appendChild(slider);
	value = el.value;
	id = el.id;
	el.parentNode.insertBefore(outDiv, el);
	// remove the old element before inserting the new element
	el.parentNode.removeChild(el);
	outDiv.parentNode.insertBefore(newEl, outDiv);
	if(value > 0) {
		newValue(value, id);
	}
}

if (window.addEventListener) {
	document.addEventListener('mousemove', mousetracker, false);
} else if (window.attachEvent) {
	document.attachEvent('onmousemove', mousetracker);
} else {
	document.onmousemove = mousetracker;
}



/**
 * FusionCharts: Flash Player detection and Chart embed 
 * 
 * Morphed from SWFObject (http://blog.deconcept.com/swfobject/) under MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
if(typeof infosoftglobal == "undefined") var infosoftglobal = new Object();
if(typeof infosoftglobal.FusionChartsUtil == "undefined") infosoftglobal.FusionChartsUtil = new Object();
infosoftglobal.FusionCharts = function(swf, id, w, h, debugMode, registerWithJS, c, scaleMode, lang){
	if (!document.getElementById) { return; }
	
	//Flag to see whether data has been set initially
	this.initialDataSet = false;
	
	//Create container objects
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	
	//Set attributes for the SWF
	if(swf) { this.setAttribute('swf', swf); }
	if(id) { this.setAttribute('id', id); }
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	
	//Set background color
	if(c) { this.addParam('bgcolor', c); }
	
	//Set Quality	
	this.addParam('quality', 'high');
	
	//Add scripting access parameter
	this.addParam('allowScriptAccess', 'always');
	
	//Pass width and height to be appended as chartWidth and chartHeight
	this.addVariable('chartWidth', w);
	this.addVariable('chartHeight', h);

	//Whether in debug mode
	debugMode = debugMode ? debugMode : 0;
	this.addVariable('debugMode', debugMode);
	//Pass DOM ID to Chart
	this.addVariable('DOMId', id);
	//Whether to registed with JavaScript
	registerWithJS = registerWithJS ? registerWithJS : 0;
	this.addVariable('registerWithJS', registerWithJS);
	
	//Scale Mode of chart
	scaleMode = scaleMode ? scaleMode : 'noScale';
	this.addVariable('scaleMode', scaleMode);
	//Application Message Language
	lang = lang ? lang : 'EN';
	this.addVariable('lang', lang);
}

infosoftglobal.FusionCharts.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	getVariablePairs: function(){
		var variablePairs = new Array();
		var key;
		var variables = this.getVariables();
		for(key in variables){
			variablePairs.push(key +"="+ variables[key]);
		}
		return variablePairs;
	},
	getSWFHTML: function() {
		var swfNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { 
			// netscape plugin architecture			
			swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"  ';
			swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
			var params = this.getParams();
			 for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
			var pairs = this.getVariablePairs().join("&");
			 if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
			swfNode += '/>';
		} else { // PC IE			
			swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
			swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
			var params = this.getParams();
			for(var key in params) {
			 swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			var pairs = this.getVariablePairs().join("&");			
			if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
			swfNode += "</object>";
		}
		return swfNode;
	},
	setDataURL: function(strDataURL){
		//This method sets the data URL for the chart.
		//If being set initially
		if (this.initialDataSet==false){
			this.addVariable('dataURL',strDataURL);
			//Update flag
			this.initialDataSet = true;
		}else{
			//Else, we update the chart data using External Interface
			//Get reference to chart object
			var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
			chartObj.setDataURL(strDataURL);
		}
	},
	setDataXML: function(strDataXML){
		//If being set initially
		if (this.initialDataSet==false){
			//This method sets the data XML for the chart INITIALLY.
			this.addVariable('dataXML',strDataXML);
			//Update flag
			this.initialDataSet = true;
		}else{
			//Else, we update the chart data using External Interface
			//Get reference to chart object
			var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
			chartObj.setDataXML(strDataXML);
		}
	},
	render: function(elementId){
		var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
		n.innerHTML = this.getSWFHTML();
		return true;		
	}
}

// ------------ Fix for Out of Memory Bug in IE in FP9 ---------------//
/* Fix for video streaming bug */
infosoftglobal.FusionChartsUtil.cleanupSWFs = function() {
	if (window.opera || !document.all) return;
	var objects = document.getElementsByTagName("OBJECT");
	for (var i=0; i < objects.length; i++) {
		objects[i].style.display = 'none';
		for (var x in objects[i]) {
			if (typeof objects[i][x] == 'function') {
				objects[i][x] = function(){};
			}
		}
	}
}
// Fixes bug in fp9
infosoftglobal.FusionChartsUtil.prepUnload = function() {
	__flash_unloadHandler = function(){};
	__flash_savedUnloadHandler = function(){};
	if (typeof window.onunload == 'function') {
		var oldUnload = window.onunload;
		window.onunload = function() {
			infosoftglobal.FusionChartsUtil.cleanupSWFs();
			oldUnload();
		}
	} else {
		window.onunload = infosoftglobal.FusionChartsUtil.cleanupSWFs;
	}
}
if (typeof window.onbeforeunload == 'function') {
	var oldBeforeUnload = window.onbeforeunload;
	window.onbeforeunload = function() {
		infosoftglobal.FusionChartsUtil.prepUnload();
		oldBeforeUnload();
	}
} else {
	window.onbeforeunload = infosoftglobal.FusionChartsUtil.prepUnload;
}

/* Add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* Function to return Flash Object from ID */
infosoftglobal.FusionChartsUtil.getChartObject = function(id)
{
  if (window.document[id]) {
      return window.document[id];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    if (document.embeds && document.embeds[id])
      return document.embeds[id]; 
  } else {
    return document.getElementById(id);
  }
}
/* Aliases for easy usage */
var getChartFromId = infosoftglobal.FusionChartsUtil.getChartObject;
var FusionCharts = infosoftglobal.FusionCharts;


//
// ELNINO
//


function leftOffset(what)
{
	var totaloffset = what.offsetLeft;
	var parentEl = what.offsetParent;
	while (parentEl != null)
	{
		totaloffset = totaloffset + parentEl.offsetLeft;
		parentEl = parentEl.offsetParent;
	}
	return totaloffset;
}

function switchTab()
{	
	var tab = this.id.split('List');
	
	var subTabs = $$('#' + tab[0] + ' .elTabSub');
	subTabs.each(function(s) { s.hide(); });	

	var listTabs = $$('#' + tab[0] + ' .elTabList li');
	listTabs.each(function(s) { s.removeClassName('active'); });
	
	this.addClassName('active');
	
	$(tab[0] + 'Sub' + tab[1]).show();
}

function setTabs()
{
	var tabs = $$('.elTab');
	for(var i=0; i<tabs.length; ++i)
	{
		var subTabs = $$('#' + tabs[i].id + ' .elTabSub');
		subTabs.each(function(s) { if(!s.hasClassName('display')) s.hide(); });
		
		var listTabs = $$('#' + tabs[i].id + ' .elTabList li');
		listTabs.each(function(s) { s.onclick = switchTab; s.onmouseover = function() { this.addClassName('h'); }; s.onmouseout = function() { this.removeClassName('h'); }; });
	}
}

function updateCharts(newFund)
{
	var chart = $$('.fundsChart');
	for(var i=0; i<chart.length; ++i)
	{
		var chi = chart[i].id.substr(8);
		var di = chi.split('_');
		var ch = getChartFromId('Chart_' + chi);
		
		var xx = getChartFromId('Chart_' + chi);

		di[0] = di[0] + '-' + newFund;
		
		xx.setDataURL("/finanse/fundusze/xml/funds_" + di[0] + "/date_" + di[1] + "/");

		



		
	}
}
//
//	var chart = $$('.fundsChart');
//	for(var i=0; i<chart.length; ++i)
//	{
//		var chi = chart[i].id.substr(8);
//		var di = chi.split('_');
//		var ch = getChartFromId('Chart_' + chi);
//		alert(di);
//		ch.setDataURL("/finanse/fundusze/xml/funds_" + di[0] + "-" + newFund + "/date_" + di[1] + "/");
//		ch.render(chart[i].id);
//	}

function convert(fID)
{
	if(fID == 0)
	{
		var cnv = $('ord_ffun_id');
		cnv.options[0].text = '-- najpierw wybierz fundusz do konwersji --';
		cnv.disabled = true;
		cnv.addClassName('disabled');
	}
	else
	{
		$('ord_ffun_id').options[0].text = '-- wybierz --';
		
		var cur = fID.split('#');
	
		var opts = $$('#ord_ffun_id option');
		
		for(var i=0; i<opts.length; ++i)
		{
			var opt = opts[i].value.split('#');
			opts[i].style.display = (~~cur[1] == ~~opt[1] && ~~opt[1] > 0 && ~~cur[0] != ~~opt[0] ? '' : 'none');
		}
		
		var cnv = $('ord_ffun_id');
		
		cnv.disabled = false;
		cnv.removeClassName('disabled');
	}
}



function elForm()
{
	$$('a.blank').each( function(s) { s.target='_blank'; } );
	
	var elForms = $$('form.elForm');
	
	for(var i=0; i<elForms.length; ++i)
	{
		elForms[i].onsubmit = function() {
			
			this.request({
				parameters: { ajax:'1' },
				onCreate: function()
				{
					this.disable();
				}.bind(this),
				onComplete: function(transport, json) 
				{ 
					eval("var res = " + transport.responseText);
					if(res['oki'])
						$(res['formId']).innerHTML = '<div class="oki">' + res['oki'] + '</div>';
					else
					{
						$$('#' + res['formId'] + ' .err').each( function(s) { s.innerHTML = ''; s.hide(); } )			
						for(var i=0; i<res['err'].length; ++i)
						{
							var errDiv = $(res['err'][i]['inputId'] + 'Err');
							errDiv.innerHTML = res['err'][i]['err'];
							errDiv.show();
						}
					}
					this.enable();
					var elms = this.getElements();
					elms.each( function(s) { if(s.hasClassName('disabled')) s.disable(); } );
					
				}.bind(this)
			});

			return false;		
			
		};	
		
		if(i == 0)
			elForms[i].focusFirstElement();
	}
}

function pleaseWait()
{

		document.getElementById("pleasewaitScreen").style.pixelTop = (document.body.scrollTop + 450);
		document.getElementById("pleasewaitScreen").style.visibility="visible";
		//window.setTimeout('do_totals2()',1);
}	

function hideWait()
{
		//document.all.pleasewaitScreen.style.visibility="hidden";

}



Event.observe(window, 'load', setTabs);
Event.observe(window, 'load', elForm);
