function view_image(img, width, height, link){
	$('the_image').src = img;
	if(width > height){
		$('the_image').className = "h_photo";
	}else{
		$('the_image').className = "v_photo";
	}
}

function preload_images(images){
	var arr = images.split("@image@");
	
	var pics = new Array();
	
	for(i=1;i<arr.length;i++){ // i=1 porque la imagen 0 ya está cargada
		pics[i] = new Image();
		pics[i].src = 'http://buenaventuravillas.com'+arr[i];
	}
}

// ********

function removeImg(id){
	
	new Effect.Fade(id, {duration:0.5, afterFinish:function(){ removeObj(id) }});
	
	refreshSortable();
}

function flashErrors(){
	var elems = $$('div.error');
	
	for(i=0;i<elems.length;i++){
		new Effect.Highlight(elems[i], {startcolor:"#FFE27F"});
	}
}

function flashSuccess(){
	var elems = $$('div.success');
	
	for(i=0;i<elems.length;i++){
		new Effect.Highlight(elems[i], {startcolor:"#59C31F"});
	}
}

function appear(id){
	new Effect.Appear(id, {duration:0.3});
}

function disappear(id){
	new Effect.Fade(id, {duration:0.3});
}

function appear_blind(id){
	new Effect.BlindDown(id, {duration:0.3});
}

function disappear_blind(id){
	new Effect.BlindUp(id, {duration:0.3});
}

function toggle(id){
	Effect.toggle(id+'_ctnr', 'blind', {duration: 0.3, afterFinish:function(){ toggle_img(id+'_img'); }});
}

function regular_toggle(id){
	Effect.toggle(id, 'blind', {duration: 0.3});
}

function set_dropdown_to(dropdown, value){
	$(dropdown).selectedIndex = value;
}

function toggle_img(id){
	if($(id).alt == "expand"){
		$(id).alt = "contract";
		$(id).src = "/images/manager/contract.gif";
	}else if($(id).alt == "contract"){
		$(id).alt = "expand";
		$(id).src = "/images/manager/expand.gif";
	}else{
		alert($(id).alt);
	}
}

function mksortable(id){
	Sortable.create(id, {overlap:'horizontal', constraint: false, onChange: function(){ refreshOrder(); }});
}

function removeObj(id){
	var d = document.getElementById('image_manager');
	var olddiv = document.getElementById(id);
	d.removeChild(olddiv);
	$('images').value = getOrder();
}

function getOrder(){
	var childs = $('image_manager').childElements();
	var str = '';
	for(i=0;i<childs.length;i++){
		str = str + childs[i].firstChild.innerHTML+"@image@";
	}
	return str;
}

function refreshSortable(){
	Sortable.destroy('image_manager');
	mksortable('image_manager');
}

function refreshOrder(){
	$('images').value = getOrder();
}

function descriptions_switch(lang){
	$('en_description_div').style.display = "none";
	$('es_description_div').style.display = "none";
	$('nl_description_div').style.display = "none";
	
	$('en_description_tab').className = "first";
	$('es_description_tab').className = "";
	$('nl_description_tab').className = "";
	
	$(lang+'_description_div').style.display = "block";
	$(lang+'_description_tab').className = "on";
	
	if(lang == 'en'){
		$(lang+'_description_tab').className = "first on";
	}
}

function status_changed(){
	if($('status').options[$('status').selectedIndex].value == 4){ // Sold
		if($('datereserved').style.display != "none"){
			new Effect.BlindUp('datereserved', { duration:0.3, afterFinish:function(){ appear_blind('datesold'); }});
		}else{
			appear_blind('datesold');
		}
	}else if($('status').options[$('status').selectedIndex].value == 3){ // Reserved
		if($('datesold').style.display != "none"){
			new Effect.BlindUp('datesold', { duration:0.3, afterFinish:function(){ appear_blind('datereserved'); }});
		}else{
			appear_blind('datereserved');
		}
	}else{
		disappear_blind('datereserved');
		disappear_blind('datesold');
	}
}

function price_more(num){
	var the_id = 'new_price_'+(num+1);
	if($(the_id).style.display == "none"){
		new Effect.BlindDown(the_id, {duration:0.3});
	}
}

function price_less(num){
	var the_id = 'new_price_'+num;
	$('price_'+num).value = "";
	$('prices_day_'+num).selectedIndex = 0;
	$('prices_month_'+num).selectedIndex = 0;
	$('prices_year_'+num).selectedIndex = 0;
	new Effect.BlindUp(the_id, {duration:0.3});
}

function event_more(num){
	var the_id = 'eventset_'+(num+1);
	if($(the_id).style.display == "none"){
		new Effect.BlindDown(the_id, {duration:0.3});
	}
	
	if(num == -1){
		new Effect.BlindUp('add_events', {duration:0.3});
	}
	
}

function count_events(){
	events = $('events_ctnr').immediateDescendants();
	
	count = 0;
	
	for(i=0;i<events.length;i++){
		if(events[i].style.display != "none"){
			count++;
		}
	}
	
	return count;
}

function event_less(num){
	
	var the_id = 'eventset_'+num;
	$('event_'+num).value = "";
	$('events_start_day_'+num).selectedIndex = 0;
	$('events_start_month_'+num).selectedIndex = 0;
	$('events_start_year_'+num).selectedIndex = 0;
	$('events_end_day_'+num).selectedIndex = 0;
	$('events_end_month_'+num).selectedIndex = 0;
	$('events_end_year_'+num).selectedIndex = 0;
	new Effect.BlindUp(the_id, {duration:0.3});
	
	ct = count_events()-1;
	if(ct == 0){
		new Effect.BlindDown('add_events', {duration:0.3});
	}
}
