/* 
 * Java-Script library for Editor of Slideshow2-Plugin
 * 
 * requires Mootools 1.2
 *
 */
function showPreviewImage(path, img_id){
	var prev = $('prev_'+img_id).hasChild($('prev_img_'+img_id));
	if (prev) {
		$('prev_img_'+img_id).destroy();
	} 
	var prev_img = new Element('img', {
                    'id' : 'prev_img_'+img_id,
                    'src'   : path,
                    'alt' : 'Vorschau Bild '+img_id,
                    'height' : '80px;'
   });
   $(prev_img).inject('prev_'+img_id);
}

function setFocus(form_name) {
	$(form_name).focus();
}

//change values of existing image form ids 
function chImgID(new_id, old_id) {
	if ($('img_'+new_id) != null) {
		var old_src = $(old_id).getProperty('value');
		var new_src = $(new_id).getProperty('value');
		$(old_id).setProperty('value', new_src);
		$(new_id).setProperty('value', old_src);
		if ($('prev_'+old_id).hasChild($('prev_img_'+old_id))) {	
			var old_thumb = $('prev_img_'+old_id).getProperty('src');
			var new_thumb = $('prev_img_'+new_id).getProperty('src');
			$('prev_img_'+old_id).setProperty('src', new_thumb);
			$('prev_img_'+new_id).setProperty('src', old_thumb);
		}
		var old_alt = $('alt_'+old_id).getProperty('value');
		var new_alt = $('alt_'+new_id).getProperty('value');
		$('alt_'+old_id).setProperty('value', new_alt);
		$('alt_'+new_id).setProperty('value', old_alt);
	}	
}

function posupImg(img_id) {
	var new_id = parseInt(img_id)-1;
	new_id += '';
	chImgID(new_id, img_id);
}

function posdownImg(img_id) {
	var new_id = parseInt(img_id)+1;
	new_id += '';
	chImgID(new_id, img_id);
}
		
function removeImg(del_img) {
  	if ($('img_'+del_img) != null) {
  		var n_img = parseInt(del_img)+1;
		// update all img_numbers higher than the deleted img number 		
  		while ($('img_'+n_img) != null){
			n_img += '';
			del_img += '';
			chImgID(del_img, n_img);
			n_img++;
			del_img++;
		}
		$('img_'+del_img).destroy();
		var img_number = parseInt(del_img)-1;
		//update select field for total img_number
		$('option_'+del_img).removeProperty('selected');
		$('option_'+img_number).setProperty('selected', 'selected');
  	}		
}

function showImgForms(img_number){
	for (i=2;i<=img_number;i++) { //create new input-form-table for each new image
		if ($('img_'+i) == null){		
			var img_table = new Element('table', {
								'id': 'img_'+i,
								'class': 'img_form'
			});
		
			//1st row img-src		
			var img_table_1st_tr = new Element('tr');
			img_table_1st_tr.inject(img_table, 'top'); 
			
			var img_table_1st_trtd = new Element('td', {
								'class': 'column1',
								'html': '<label id="srclabel_'+i+'" for="src_'+i+'">Bild '+i+':</label>'		
			});
			img_table_1st_trtd.inject(img_table_1st_tr);
			
			var img_table_1st_trtd02 = new Element('td', {
								'colspan': '2',
								'html': '<table border="0" cellspacing="0" cellpadding="0"><tr><td><input name="src_'+i+'" type="text" id="'+i+'" value="" style="width:150px; margin-right:8px;" onBlur="showPreviewImage(this.value, this.id);" /></td><td class="open_manager" id="srcbrowsercontainer"><a id="browserlink_'+i+'" onclick="tinyBrowserPopUp(\'type\',\''+i+'\'); setFocus(\''+i+'\');" href="#"><img src="'+ed_theme_path+'/img/actions/get_img.gif" alt="Datei-Manager &ouml;ffnen" title="Datei-Manager &ouml;ffnen" /></a></td></tr></table>'		
			});
			img_table_1st_trtd02.inject(img_table_1st_tr);
			
			var img_table_1st_trtd03 = new Element('td', {
								'rowspan': '3',
								'class': 'img_prev_container',
								'id': 'prev_'+i
			});
			img_table_1st_trtd03.inject(img_table_1st_tr);
			
			//2nd row img-alt		
			var img_table_2nd_tr = new Element('tr');
			img_table_2nd_tr.inject(img_table);
			
			var img_table_2nd_trtd = new Element('td', {
								'class': 'column1',
								'html': '<label id="altlabel_'+i+'" for="alt_'+i+'">Beschreibung:</label>'		
			});
			img_table_2nd_trtd.inject(img_table_2nd_tr);
			
			var img_table_2nd_trtd02 = new Element('td', {
								'colspan': '2',
								'html': '<input id="alt_'+i+'" name="alt_'+i+'" type="text" value="" />'		
			});
			img_table_2nd_trtd02.inject(img_table_2nd_tr);
		
			//3rd row img_tile		
			var img_table_3rd_tr = new Element('tr');
			img_table_3rd_tr.inject(img_table);
			
			var img_table_3rd_trtd = new Element('td', {
								'class': 'column1',
								'colspan' : '3',
								"html": "<a href='#' id='img_remove_"+i+"' onclick=\"removeImg('"+i+"'); return false;\"><img src='"+ed_theme_path+"/img/actions/delete_small.png' alt='Bild entfernen' title='Bild entfernen' /></a><a href='#' id='img_up_"+i+"' onclick=\"posupImg('"+i+"'); return false;\"><img src='"+ed_theme_path+"/img/actions/up_small.png' alt='Bild nach oben verschieben' title='Bild nach oben verschieben' /></a><a href='#' id='img_down_"+i+"' onclick=\"posdownImg('"+i+"'); return false;\"><img src='"+ed_theme_path+"/img/actions/down_small.png' alt='Bild nach unten verschieben' title='Bild nach unten verschieben' /></a>"		
			});
			img_table_3rd_trtd.inject(img_table_3rd_tr);
			
			//inject whole table at last, due to correct rowspan interpretation
			img_table.inject($('basic_show_settings')); 
		}
	}
	// remove all Img Forms that are not needed	
	lastplus = parseInt(img_number)+1;	
	while ($('img_'+lastplus) != null){
		$('img_'+lastplus).destroy();
		lastplus++;
	}
}

//switch between view of basic and advanced settings in slideshow editor window
function showSettings(setting_nav_id) {
	 $$('a.active_setting').each(function(el){
	 	el.removeClass('active_setting');
	 });
	 $(setting_nav_id).addClass('active_setting');
	 $$('div.active_container').each(function(el){
	 	el.removeClass('active_container');
	 });
	 $(setting_nav_id+'_show_settings').addClass('active_container');
}
