
var playerFiles = new Array();
var waitLoading = '<div class="alertbox1" style="width:400px;height;300px;">einen Augenblick bitte, die Daten wird gerade geladen</div>';
var mousehover  = false;

var emittentCount = 0;
var emittentPos   = 0;
var emittentList  = new Array();
var emittentPosField;
var emittentCountField;
var emittentDetailDiv;
var emittentPrintWindow;
var emittentInfoCard;
var emittentTermsheet = 'button_termsheet';
var emittentPrint = 'button_print';
var emittentKurslist = 'button_emittent';
var emittentButtonLeft = 'button_left';
var emittentButtonRight = 'button_right';
var emittentKuerzel = '';
var noTracking = false;

function setAbs(abs)
{
	absCorrect = abs;
}


/*
 * @param div - define the destination field where the data will be displayed
 * @param field - define the destination field where the data will be displayed
 * @param listid - id of the list obj the change the styles
 * @param destination - international, national...
 * @param partner - emittents from a partner
 * @param product - the emittent product
 * @param sort - the field witch will bi sorted
 * @param sortdir - the order type
 * @param style - style for the li list
 * @param sortdir - filter the basiswerts after the name
 */
function zertifikate_loadList(div, field, listid, destination, partner, product, sort, sortdir, style, filter)
{
	
	
	if($('listLoadInfo'))
	{
		var table1 = $('table_Partizipations-Produkte');
		var table2 = $('table_Renditeoptimierungs-Produkte');
		var table3 = $('table_Kapitalschutz-Produkte');
		var table4 = $('table_Diverse Produkte');
		var lastTable = table4 ? table4 : (table3 ? table3 : (table2 ? table2 : table1));
		$('ajaxLoader').style.display = 'block';
		$('listLoadInfo').style.width = lastTable ? lastTable.clientWidth + 'px' : '100%';
		$('listLoadInfo').style.height = lastTable ? (lastTable.clientHeight + lastTable.offsetTop) + 'px': '0px';
	
		new Effect.Appear('listLoadInfo', { duration: 0.5, from: 0.0, to: 0.2});
	}
	new Ajax.Request(
		absCorrect + 'de/zertifikate_list.ax.html',
		{
			method: 'get',
			parameters: 'objects.destination=' + destination + 
						'&objects.partner=' + partner + 
						'&objects.product=' + product + 
						'&objects.sort=' + sort + 
						'&objects.sortdir=' + sortdir +
						'&objects.filter=' + filter,
			onSuccess: function (t) {
					$(div).innerHTML = t.responseText;
					var table1 = $('table_Partizipations-Produkte');
					var table2 = $('table_Renditeoptimierungs-Produkte');
					var table3 = $('table_Kapitalschutz-Produkte');
					var table4 = $('table_Diverse Produkte');
					var lastTable = table4 ? table4 : (table3 ? table3 : (table2 ? table2 : table1));
					$('ajaxLoader').style.display = 'block';
					$('listLoadInfo').style.width = lastTable ? lastTable.clientWidth + 'px' : '100%';
					$('listLoadInfo').style.height = lastTable ? (lastTable.clientHeight + lastTable.offsetTop) + 'px': '0px';
					
					new Effect.Fade('listLoadInfo', { duration: 0.5, from: 0.2, to: 0.0, afterFinish: function() {$('ajaxLoader').style.display = 'none';}});
			},
			onFailure: function(e) {}
		}
	);
	if(field != null && listid != null)
	{
		var list = $(listid);
	    for (var i = 0; i < list.childNodes.length; i++)
	    {
	        if (list.childNodes[i].firstChild && list.childNodes[i].firstChild.nodeName == "A")
	        {
	            list.childNodes[i].firstChild.className = style;
	        }
	    }
		field.className = style + ' active';
	}
	if(noTracking == false)
	{
		wemfbox('zertifikate');
	}
}

/*
 * Load infolayer and display it in the layer div
 *
 * @param obj - the object to set position of the div
 * @param id  - id of the row in the db
 * @param div - layer where the dada will bi loaded
 *
 */
function zertifikate_getPlayerEmittentById(obj, id, div)
{
	
	// set the id of the actual position, when the ajax request is it slow and the mouse is on the next position
	// it doesn't show the old layer
	mousehover = id;
	
	// data already loaded and stored
	if(playerFiles[id])
	{
		// set the data
		$(div).innerHTML = playerFiles[id];
		// if an object to position the layer is set
		if(obj != null)
		{
			var yPos = obj.offsetTop;
			tempEl = obj.offsetParent;
			// get the exact y position from top
			while (tempEl != null) {
				yPos += tempEl.offsetTop;
				tempEl = tempEl.offsetParent;
			}
			
			var scrollPos;
			if (typeof window.pageYOffset != 'undefined') {
			   scrollPos = window.pageYOffset;
			}
			else if (typeof document.compatMode != 'undefined' &&
			     document.compatMode != 'BackCompat') {
			   scrollPos = document.documentElement.scrollTop;
			}
			else if (typeof document.body != 'undefined') {
			   scrollPos = document.body.scrollTop;
			}
			
			var objClientHeight = obj.clientHeight > 0 ? obj.clientHeight : Math.max(obj.scrollHeight,obj.offsetHeight);
			if(yPos-scrollPos < (document.documentElement.clientHeight-obj.clientHeight)/2)
			{
				$(div).style.top =  (yPos + objClientHeight) + "px";
			}
			else
			{
				$(div).style.top =  (yPos - $(div).clientHeight) + "px";
			}
		}
		// the data is loaded, the layer at the right position, show it
		$(div).style.visibility = 'visible';
	}
	else
	{
		// start ajax request
		new Ajax.Request(
			absCorrect + 'de/zertifikate_neuemissionen_infokarte.html',
			{
				parameters: 'objects.detail_id=' + id,
				onSuccess: function (t) {
					// store the data
					playerFiles[id] = t.responseText;
					// check if the position is the same
					if(mousehover != id)
						return;
					// do the same as when the data already is loaded
					$(div).innerHTML = playerFiles[id];
					if(obj != null)
					{
						var yPos = obj.offsetTop;
						tempEl = obj.offsetParent;
						while (tempEl != null) {
							yPos += tempEl.offsetTop;
							tempEl = tempEl.offsetParent;
						}
						
						var scrollPos;
						if (typeof window.pageYOffset != 'undefined') {
						   scrollPos = window.pageYOffset;
						}
						else if (typeof document.compatMode != 'undefined' &&
						     document.compatMode != 'BackCompat') {
						   scrollPos = document.documentElement.scrollTop;
						}
						else if (typeof document.body != 'undefined') {
						   scrollPos = document.body.scrollTop;
						}
						
						var objClientHeight = obj.clientHeight > 0 ? obj.clientHeight : Math.max(obj.scrollHeight,obj.offsetHeight);
						if(yPos-scrollPos < (document.documentElement.clientHeight-obj.clientHeight)/2)
						{
							$(div).style.top =  (yPos + objClientHeight) + "px";
						}
						else
						{
							$(div).style.top =  (yPos - $(div).clientHeight) + "px";
						}
					}
					$(div).style.visibility = 'visible';
				},
				onFailure: function(t) {
					playerFiles[id] = null;
				}
			}
		);
		playerFiles[id] = "";
	}
}

/*
 * remove the layer
 * @param div - the layer to remove
 */
function zertifikate_removePlayer(div, obj)
{
	// when the ajax request is loading and the mouse go out. he should not display the layer
	mousehover=false;
	$(div).style.visibility = 'hidden';
	$(div).style.padding = '20px';
	$(div).innerHTML = '';
}


/*
 * open url in parameter in a new window
 * @param url
 */
function openPlayer(url)
{
	var tmp = window.open(url, "EmissionsMonitor", "width=510,height=600,left=" + (screen.width - 510) + ",top=0,menubar=no,status=no,location=no,resizable=no,status=no");
	tmp.focus();
}

/*
 * create url for loading player from valor
 * @param valor - valor number
 */
function openPlayerFromValor(valor, title)
{
	var t = '';
	var u = '';
	var type = 1;
	if(typeof title != 'undefined')
	{
		for(var i=0; i<valor.length; i++)
		{
			var v = valor[i];
			t += v + (i+1 >= valor.length ? '' : ', ');
			u += "&objects.valor[]=" + v;
		}
	}
	else
	{
		u = 'objects.valor[]=' + valor;
	t = 'Valor: ' + valor;
	}
	
	if(typeof title != 'undefined')
	{
		t = 'Alle Neuemissionen von ' + title;
		type = 0;
	}
	openPlayer(absCorrect + 'de/zertifikate_neuemissionen_player.html?' + u + '&objects.title=' + encodeURIComponent(t) + '&objects.type=' + type);
}

/*
 * create url for loading player from partner
 * @param partner - partner string
 */
function openPlayerFromPartner(id)
{
	openPlayer(absCorrect + 'de/zertifikate_neuemissionen_player.html?objects.partner='+id);
}

/*
 * create url for loading player from id
 * @param id - id number
 */
function openPlayerFromId(id)
{
	var title = '';
	openPlayer(absCorrect + 'de/zertifikate_neuemissionen_player.html?objects.detail_id='+id+'&objects.title=' + encodeURIComponent(title));
}


function openPlayerAll()
{
	var title = 'Alle Neuemissonen';
	openPlayer(absCorrect + 'de/zertifikate_neuemissionen_player.html'+'?objects.title=' + encodeURIComponent(title));
}


/*
 * parse the response Text and set the different vars
 * @param t - callback from ajax request
 */
function onSuccessGetEmittents(t)
{
	
	// the response is formatet in json, so the responseText has to get parsed
	var tmp = eval('(' + t.responseText + ')');
	// if the query not found some results
	if(tmp.title)
	{
		$('infocardtitle').innerHTML = tmp.title;
	}
	
	if(tmp.kurslist == 0)
	{
		$(emittentKurslist).disabled = true;
		$(emittentKurslist).addClassName('button_inactive');
	}
	else
	{
		$(emittentKurslist).disabled = false;
		$(emittentKurslist).removeClassName('button_inactive');
	}
	
	emittentKuerzel = tmp.kuerzel;
	
	if(tmp.count == 0)
	{
		$(emittentDetailDiv).innerHTML = '<div style="position: relative;left: 50%;top: 40%;text-align:center;width: 100%;margin-left: -50%; "><h1>Momentan sind der &laquo;Finanz und Wirtschaft&raquo; keine Neuemissionen dieses Emittenten bekannt.</h1></div>';
		emittentPos = 0;
		emittentCount = 0;
		emittentList = new Array();
		// set the navigation fields
		zertifikate_setEmittentStatusFields();
		return;
	}
	// set the vars for the navigation
	emittentPos  = 1;
	emittentCount = tmp.count;
	emittentList  = tmp.emittents;
	
	// set the navigation fields
	zertifikate_setEmittentStatusFields();
	// load info layer with the first id in array
	zertifikate_getPlayerEmittentById(null, emittentList[emittentPos-1].id, emittentDetailDiv);

}

/*
 * if the request fails set this error message for the customer
 * @param t - callback from ajax request
 */
function onFailureGetEmittents(t)
{
	$(emittentDetailDiv).innerHTML = 'Fehler aufgetreten, bitte nochmals versuchen';
}

/*
 * set ajax request to load the navigation, get all partner emittents
 */
function zertifikate_getNavAll()
{
	new Ajax.Request(
		absCorrect + 'de/zertifikate_player_nav.ax.html',
		{
			onSuccess: onSuccessGetEmittents,
			onFailure: onFailureGetEmittents
		}
	);
	checkButtons('Alle Partner-Infokarten');
	wemfbox('zertifikate');
}


function checkButtons(button) {
	var elements = document.getElementsByName('selectors');
	for(var i=0; i<elements.length; i++)
	{
		if(elements[i].nodeName == 'INPUT')
		{
			if(elements[i].value == button)
			{
				elements[i].style.backgroundColor = '#F7F7F7';
			}
			else
			{
				elements[i].style.backgroundColor = '#c9dff6';
			}
		}
	}
}

/*
 * set ajax request to load the navigation, get emittents from a partner
 * @param kuerzel - partner name
 */
function zertifikate_getNavByPartner(kuerzel)
{
	new Ajax.Request(
		absCorrect + 'de/zertifikate_player_nav.ax.html',
		{
			parameters: 'objects.partner=' + kuerzel,
			onSuccess: onSuccessGetEmittents,
			onFailure: onFailureGetEmittents
		}
	);
	checkButtons(kuerzel);
	wemfbox('zertifikate');
}

/*
 * set ajax request to load the navigation, get emittent by id
 * @param id - emittent id
 */
function zertifikate_getNavById(id)
{
	new Ajax.Request(
		absCorrect + 'de/zertifikate_player_nav.ax.html',
		{
			parameters: 'objects.detail_id=' + id,
			onSuccess: onSuccessGetEmittents,
			onFailure: onFailureGetEmittents
		}
	);
	wemfbox('zertifikate');
}

/*
 * set ajax request to load the navigation, get emittent by valor number
 * @param valor - emittent valor number
 */
function zertifikate_getNavByValor(valor, type)
{
	var params = '';
	for(var i=0; i<valor.length; i++)
	{
		params += 'objects.valor[]=' + valor[i] + '&';
	}
	params += 'objects.type=' + type;
	new Ajax.Request(
		absCorrect + 'de/zertifikate_player_nav.ax.html',
		{
			parameters: params,
			onSuccess: onSuccessGetEmittents,
			onFailure: onFailureGetEmittents
		}
	);
	
}

/*
 * set the vars in the navigation
 */
function zertifikate_setEmittentStatusFields()
{
	if(emittentCount == 0)
	{
		$(emittentPrint).disabled = true;
		$(emittentPrint).addClassName('button_inactive');
		$(emittentTermsheet).disabled = true;
		$(emittentTermsheet).addClassName('button_inactive');
	}
	else
	{
		$(emittentPrint).disabled = false;
		$(emittentPrint).removeClassName('button_inactive');
		$(emittentTermsheet).disabled = false;
		$(emittentTermsheet).removeClassName('button_inactive');
	}
	if(emittentPos <= 1)
	{
		$(emittentButtonLeft).src = '../images/icons/ico_page_left_inactive.gif';
	}
	else
	{	
		$(emittentButtonLeft).src = '../images/icons/ico_page_left.gif';
	}
	
	if(emittentPos == emittentCount)
	{
		$(emittentButtonRight).src = '../images/icons/ico_page_right_inactive.gif';
	}
	else
	{	
		$(emittentButtonRight).src = '../images/icons/ico_page_right.gif';
	}
	
	if(emittentList.length && emittentList[emittentPos-1].kurslist == 0)
	{
		$(emittentKurslist).disabled = true;
		$(emittentKurslist).addClassName('button_inactive');
	}
	else
	{
		$(emittentKurslist).disabled = false;
		$(emittentKurslist).removeClassName('button_inactive');
	}
	$(emittentPosField).innerHTML = emittentPos;
	$(emittentCountField).innerHTML = emittentCount;
	
}


/*
 * set the vars in the navigation and load next info layer
 */
function zertifikate_getNextEmittent()
{
	if((emittentPos) >= (emittentCount))
	{
		return;
	}
	emittentPos++;
	emittentKuerzel = emittentList[emittentPos-1].kuerzel;
	zertifikate_setEmittentStatusFields();
	zertifikate_getPlayerEmittentById(null, emittentList[emittentPos-1].id, emittentDetailDiv);
	wemfbox('zertifikate');
}


/*
 * set the vars in the navigation and load previous info layer
 */
function zertifikate_getPreviousEmittent()
{
	if(emittentPos <= 1)
	{
		return;
	}
	emittentPos--;
	emittentKuerzel = emittentList[emittentPos-1].kuerzel;
	zertifikate_setEmittentStatusFields();
	
	zertifikate_getPlayerEmittentById(null, emittentList[emittentPos-1].id, emittentDetailDiv);
	wemfbox('zertifikate');
}

/*
 * init, to start the player
 * @param pos - start pos 1-x
 * @param count - how many info layer shult display
 * @param detail - the info layer div
 * @param partner - partner string, if it only shows partner emittents
 * @param detail_id - emittent id, if it only shows e specified emittent
 * @param valor - valor of an emittent
 */
function zertifikate_init(pos, count, detail, partner, detail_id, valor, name, type)
{
	emittentPosField = pos;
	emittentCountField = count;
	emittentDetailDiv = detail;
	if(partner.length)
	{
		zertifikate_getNavByPartner(partner);
	}
	else if(detail_id.length)
	{
		zertifikate_getNavById(detail_id);
	}
	else if(valor.length > 0 && valor[0].length > 0)
	{
		zertifikate_getNavByValor(valor, type);
	}
	else
	{
		zertifikate_getNavAll();
	}
}

/*
 * open window with the emittent pdf
 */
function zertifikate_getPDFById()
{
	var pdfbyid = opener.open(absCorrect + 'de/cmsimport_zertifikate_neuemission.html?objects.valor=' + emittentList[emittentPos-1].valor, 'pdf' +  emittentList[emittentPos-1].valor, '');
	pdfbyid.focus();
}

/*
 * open window width the partner kurslist
 */
function zertifikate_getKursliste()
{
	var kursliste = opener.open(absCorrect + 'de/cmsimport_zertifikate_neuemissionen_partner_kursliste.html?objects.emittent=' + emittentKuerzel, 'kurslist', '');
	kursliste.focus();
}

/*
 * open window with info layer without the navigation
 */
function zertifikate_playerPrint()
{
	window.print();
	
}

function wemfbox(tag) {
	if(tag == 'undefined')
	{
		return;
	}
	var img = new Image(1,1);
	img.src = 'http://fininfo.wemfbox.ch/cgi-bin/ivw/CP/' + tag + '?r='+escape(document.referrer)+'&d='+(Math.random()*100000);
	img.onload=function() { return; }
}
