/**
 * Galerieverwaltung
 *
 * @author aw
 * @package Autrado
 */


var Galerie = function()
{
	console.log("Galerie::construct");

	// höhe einer area
	this.m_areah = 260;
	// höhe der control unter der gallery in einer array
	this.m_areacontrolh = 50;
	// verfügbare areas
	this.m_areas = [ ];
	// variablen für die quell-relationen (json-assoc array)
	this.m_sourcevars = null;
	// variablen für die ziel-relationen (json-assoc array)
	this.m_destvars = null;
	// variable für quell-flags (online, schmuckbild etc.)
	this.m_sourceflags = 0;
	// variable für ziel-flags (online, schmuckbild etc.)
	this.m_destflags = 0;
	// zusatzinformation die übergeben werden können
	this.m_extra = null;
	// array, der das aktive tab in der view speichert
	this.m_activetab = {};

	console.log(this);
}

Galerie.marked = [];

Galerie.set_source_vars = function(p_params)
{
	console.log("Galerie::set_source_vars: gsr = " + p_params);

	this.m_sourcevars = p_params;
}

Galerie.set_dest_vars = function(p_params)
{
	this.m_destvars = p_params;
}

Galerie.set_source_flags = function(p_params)
{
	this.m_sourceflags = p_params;
}

Galerie.set_dest_flags = function(p_params)
{
	this.m_destflags = p_params;
}

Galerie.set_extra = function(p_extra)
{
	this.m_extra = p_extra;
}

Galerie.get_active_tab = function(p_view)
{
	return this.m_activetab[p_view];
}

Galerie.open = function(p_title, p_params)
{
	console.log(
		"Galerie::open: " +
		p_params.gsr + " -- " +
		p_params.gsf + " -- " +
		p_params.gdr + " -- " +
		p_params.gdf + " -- " +
		p_params.ext
	);

	var l_url = "../../galerie.php?";

	if(typeof p_params.gsr != 'undefined') {
		for(l_key in p_params.gsr) {
			l_url += "&gsr["+encodeURI(l_key)+"]="+encodeURI(p_params.gsr[l_key]);
		}
	}

	if(typeof p_params.gdr != 'undefined') {
		for(l_key in p_params.gdr) {
			l_url += "&gdr["+encodeURI(l_key)+"]="+encodeURI(p_params.gdr[l_key]);
		}
	}

	if(typeof p_params.gsf != 'undefined') {
		l_url += "&gsf=" + p_params.gsf;
	}

	if(typeof p_params.gdf != 'undefined') {
		l_url += "&gdf=" + p_params.gdf;
	}

	if(typeof p_params.ext != 'undefined' && p_params.ext) {
		l_url += "&ext=" + p_params.ext;
	}

	GB_showFullScreen(p_title, l_url);
}

Galerie.ajax_push = function(p_params)
{
	var l_url = "galerie.php?ajax=dao";

	for(l_key in p_params) {
		l_url += "&" + encodeURI(l_key) + "=" + encodeURI(p_params[l_key]);
	}

	for(l_key in this.m_sourcevars) {
		l_url += "&gsr["+encodeURI(l_key)+"]="+encodeURI(this.m_sourcevars[l_key]);
	}

	for(l_key in this.m_destvars) {
		l_url += "&gdr["+encodeURI(l_key)+"]="+encodeURI(this.m_destvars[l_key]);
	}

	l_url += "&gsf=" + this.m_sourceflags;
	l_url += "&gdf=" + this.m_destflags;

	if(typeof this.m_extra != 'undefined' && this.m_extra) {
		l_url += "&ext=" + this.m_extra;
	}

	new Ajax.Request(l_url, {method: 'get'});
}

Galerie.ajax_get = function(p_dest, p_type, p_params)
{
	if($(p_dest)) {
		var l_url = "galerie.php?ajax=" + p_type;

		for(l_key in p_params) {
			l_url += "&" + encodeURI(l_key) + "=" + encodeURI(p_params[l_key]);
		}

		for(l_key in this.m_sourcevars) {
			l_url += "&gsr["+encodeURI(l_key)+"]="+encodeURI(this.m_sourcevars[l_key]);
		}

		for(l_key in this.m_destvars) {
			l_url += "&gdr["+encodeURI(l_key)+"]="+encodeURI(this.m_destvars[l_key]);
		}

		l_url += "&gsf=" + this.m_sourceflags;
		l_url += "&gdf=" + this.m_destflags;

		if(typeof this.m_extra != 'undefined' && this.m_extra) {
			l_url += "&ext=" + this.m_extra;
		}

		new Ajax.Updater(p_dest, l_url, {
			method: 'get',
			evalScripts: true
		});
	} else {
		console.log('Galerie::ajax_get: destination element "' + p_dest + '" does not exist!');
	}
}

Galerie.ajax_request_tab = function(p_type, p_params)
{
	// Tab aktivieren
	var l_area = $(p_params.viewID + 'Area');

	console.log(p_params.viewID);
	var l_e = l_area.previous().down().down();
	do {
		if(l_e.className != 'toggler' && l_e.id != (p_params.viewID + 'TopControl')) {
			// Gültige LIs abfragen
			if(l_e.down().id != ('tab_' + p_params.viewID + '_' + p_params.tabID)) {
				l_e.down().className = '';
			} else {
				l_e.down().className = 'aktiv';
			}
		}

		// Durch li's iterieren
		l_e = l_e.next();
	} while(l_e != null);

	// Untere Kontrollen laden
	var l_pbottom = {controlType: 'bottom'};
	Object.extend(l_pbottom, p_params);
	Galerie.ajax_get(p_params.viewID + 'BottomControl', p_type, l_pbottom);

	// Oberen Kontrollen laden
	var l_ptop = {controlType: 'top'};
	Object.extend(l_ptop, p_params);
	Galerie.ajax_get(p_params.viewID + 'TopControl', p_type, l_ptop);

	if(typeof this.m_activetab == 'undefined') {
		this.m_activetab = {};
	}
	this.m_activetab[p_params.viewID] = p_params.tabID;

	// Galerieansicht laden
	Galerie.ajax_request_content(p_type, p_params, 'gallery');

}

Galerie.ajax_request_content = function(p_type, p_params, p_contenttype)
{
	var l_pcontent = {contentType: p_contenttype};
	Object.extend(l_pcontent, p_params);
	Galerie.ajax_get(p_params.viewID + 'Gallery', p_type, l_pcontent);
}

Galerie.prototype._px = function(p_px)
{
	return p_px + 'px';
}

Galerie.prototype.area_init = function()
{
	console.log("Galerie::area_init");

	// durch alle areas iterieren und höhen festlegen
	this.m_areas.each(function(l_area) {
		var l_eh = $(l_area + 'Area');

		// höhe der area festsetzen
		l_eh.setStyle({height: this._px(this.m_areah)});

		// höhe der galerie in der area festsetzen
		$$('#' + l_eh.id + ' div.gallery').each(function(l_e) {
			l_e.style.height = this._px(this.m_areah - this.m_areacontrolh);
		}, this);
	}, this);
}

Galerie.prototype.area_exists = function(p_area)
{
	var l_obj = $(p_area + 'Area');

	if(l_obj) {
		return true;
	} else {
		return false;
	}
}

Galerie.prototype.area_open = function(p_area)
{
	return ($(p_area + 'Area').getStyle('display') != 'none');
}

Galerie.prototype.area_toggle = function(p_area)
{
	console.log("Galerie::area_toggle(" + p_area + ")");

	// area element lesen und prüfen
	var l_e = $(p_area + 'Area');
	if(!l_e) {
		console.log('Galerie::area_toggle: ' + p_area + 'Area not found.');
		return;
	}

	// area ein-/ausblenden
	toggledisplay(p_area + 'Area', p_area + 'Toggler');

	// neue höhe für noch offene areas berechnen und setzen
	var l_newh = this.get_dynamic_areasize();

	this.m_areas.each(function(l_area) {
		var l_eh = $(l_area + 'Area');
		if(l_eh.getStyle('display') != 'none') {
			// höhe der area festsetzen
			l_eh.setStyle({height: this._px(l_newh)});

			// höhe der galerie in der area festsetzen
			$$('#' + l_eh.id + ' div.gallery').each(function(l_e) {
				l_e.setStyle({height: this._px(l_newh - this.m_areacontrolh)});
			}, this);
		}
	}, this);
}

Galerie.prototype.get_dynamic_areasize = function()
{
	var l_open = 0;
	this.m_areas.each(function(l_area) {
		var l_eh = $(l_area + 'Area');
		if(l_eh.style.display != 'none') {
			l_open++;
		}
	}, this);

	var g_iframeHeight = parent.document.getElementById('GB_frame').offsetHeight;
	if(l_open == 0) {
		return g_iframeHeight - (this.m_areacontrolh + 80);
	} else {
		return (g_iframeHeight - (this.m_areacontrolh + 80)) / l_open;
	}
}

Galerie.ha_fill_hersteller = function(p_struktur, p_name, p_hid)
{
	var l_e = document.getElementById(p_name);
	l_e.options.length = 0;
	l_e.options[l_e.options.length] = new Option("Bitte wählen", -1, false, true);
	for(l_h in p_struktur.hersteller) {
		l_entry = new Option(p_struktur.hersteller[l_h], l_h, false, (p_hid == l_h));
		l_e.options[l_e.options.length] = l_entry;
	}
}

Galerie.ha_get_hersteller = function(p_name)
{
	var l_e = document.getElementById(p_name);
	if(l_e) {
		return l_e.options[l_e.options.selectedIndex].value;
	}

	return -1;
}

Galerie.ha_fill_aufbauten = function(p_struktur, p_name, p_hid, p_aid)
{
	var l_e = document.getElementById(p_name);
	l_e.options.length = 0;
	l_e.options[l_e.options.length] = new Option('Kein Aufbau', -1, false, false);
	for(l_a in p_struktur.aufbauten) {
		if(p_struktur.aufbauten[l_a].hersteller == p_hid) {
			l_entry = new Option(p_struktur.aufbauten[l_a].name, l_a, false, (p_aid == l_a));
			l_e.options[l_e.options.length] = l_entry;
		}
	}
}

Galerie.tinymce_handle = function(p_url)
{
	var l_ed = tinyMCEPopup.editor;
	var l_args = {};

	tinymce.extend(l_args, {
		src : p_url
	});

	l_ed.execCommand('mceInsertContent', false, '<img id="__mce_tmp" />', {skip_undo : 1});
	l_ed.dom.setAttribs('__mce_tmp', l_args);
	l_ed.dom.setAttrib('__mce_tmp', 'id', '');
	l_ed.undoManager.add();

	// Greybox schliessen
	parent.parent.GB_hide();
}

Galerie.ss_handle = function(p_id, p_link)
{
	parent.parent.document.getElementById('schmuckbild_img').src = p_link;
	parent.parent.document.getElementById('schmuckbild_relation').value = p_id;
	parent.parent.GB_hide();
}