document.observe("dom:loaded", function() { 
  if ($$("body.editing").size() > 0) {
    Lightbox.init();
  }
});
/*
	tinyMCE.init({
		mode : "textareas",
		theme : "simple"
	});*/
	
	

var LangTextTabs = {
  cur_lang: '',
  
	init: function(lang) {
		this.cur_lang = lang;
	},  
  
  open: function(lang) {
    this.cur_lang = $('cur_lang').innerHTML;
    if (lang != this.cur_lang) {
      $('lang_tab_link_'+lang).addClassName('current');
      //alert('lang_tab_link_'+lang);
      $('lang_tab_link_'+this.cur_lang).removeClassName('current');
      $('lang_tab_'+lang).show();
      $('lang_tab_'+this.cur_lang).hide();     
      $('cur_lang').innerHTML = lang;
    }
  }
  
}

var Elements = {
  rename: function(link) {
  	var container = $(link).up('.actions').toggle().next().toggle();
  	if (!container.innerHTML) {
  	  new Ajax.Updater(container, link.href, {asynchronous:true, evalScripts:true});
  	} 
    return false;
  },
  
  remove: function(el) {
    if (confirm(are_you_sure_page_saved)){
      new Ajax.Request(el.href, {asynchronous:true, evalScripts:true}); 
      $(el).up('li').hide();
      
    }
    return false;
  },
  
  add: function(link) {
  	$(link).next('div.new_el_by_url').toggle();
    return false;
  },
  
  create: function(button) {
  	//console.log(button);
  	if (confirm(are_you_sure_page_saved)) {
    	var url = $(button).up('div').previous('a').href;
      new Ajax.Request(url, {asynchronous:true, evalScripts:true, parameters: {url_name: $(button).previous().value}}); 
     	$(button).previous().value = ''; 
     	$(button).up().hide();
    }
    return false;
  },
  
  update: function(button) {
  	//console.log(button);
  	if (confirm(are_you_sure_page_saved)) {
    	var value = $(button).previous().value;
      new Ajax.Request(button.className, {asynchronous:true, evalScripts:true, parameters: {url_name: value}}); 
     	var link = $(button).up('.edit').hide().previous().show().down('a');
     	link.href = link.href.gsub(/\/[^\/]*$/, '/'+value);
    }
    return false;
  }
};

var Pages = {
	sections: [],
	
	init: function() {
		this.sections = [];
	},
	
	set_hover_size: function(el_name) {
		elem = $(el_name);
		xy = Element.getDimensions(el_name);
		//alert(xy.width+'x'+xy.height);
		elem.setStyle({height: xy.height+'px'});
		$(el_name+'_hover').setStyle({height: xy.height+'px'});
	},
	
	add_section: function(section, element) {
		if (!this.sections[section]) {
			this.sections[section] = [];
		}	
		if (!this.sections[section]['elements']) {
			this.sections[section]['elements'] = [];
		}
		if (element > 0) {
			this.sections[section]['element'] = element;
			this.set_hover_size(section+'_direct_'+element);
		} else {
			this.sections[section]['element'] = false;
		}


	},
	
	make_pagination_droppable: function(pages, current, section_name, lang) {
		for (i=1; i<=pages; i++) {
			if (i != current) {
				url = '/'+lang+'/elements/'+section_name+'/move_to_page/'+i;
				Droppables.add(section_name+'_page_'+i, {containment:'section_'+section_name+'_sortable', hoverclass:'testing', onDrop:function(element){new Ajax.Request(url, {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});

			}
		}
	},
	
	add_elements: function(section, elements) {
		this.sections[section] = [];
		this.sections[section]['elements'] = elements;
		for (i=0; i<elements.length; i++) {
			this.set_hover_size(section+'_'+elements[i]);
		}		
	},

	add_element: function(section, element) {
		this.sections[section]['elements'].push(element);
		this.set_hover_size(section+'_'+element);
	},	
	
	element_hover: function(section, element) {
		if (el = $(section+'_direct_'+element+'_hover')) {
			el.addClassName('element_hover');
			Element.show(section+'_direct_'+element+'_menu');
		} else {
			el = $(section+'_'+element+'_hover');
			el.addClassName('element_hover');
			Element.show(section+'_'+element+'_menu');
		}
	},
	
	element_out: function(section, element) {
		if (!(el = $(section+'_direct_'+element+'_hover'))) {
			el = $(section+'_'+element+'_hover');
			Element.hide(section+'_'+element+'_menu');
		} else {
			Element.hide(section+'_direct_'+element+'_menu');
		}
		el.removeClassName('element_hover');
	},	
	
	section_hover: function(section) {
		for (i=0; i<this.sections[section]['elements'].length; i++) {
			element = this.sections[section]['elements'][i];
			el = $(section+'_'+element+'_hover');
			if (el) {
				el.addClassName('section_hover');
			}
		}
	},
	
	section_out: function(section) {
		for (i=0; i<this.sections[section]['elements'].length; i++) {
			el = $(section+'_'+this.sections[section]['elements'][i]+'_hover');
			if (el) {
				el.removeClassName('section_hover');
			}
		}
	},
	
  remove_attachment_locally: function(img) {
    $(img).up('.attachment').remove();
  },
  
  mark_attachment_for_destroy: function(link, id) {
    $('attachments_to_destroy').value = $F('attachments_to_destroy') ? $F('attachments_to_destroy')+','+id : id;
    //$(img).next('.should_destroy').value = 1;
    $(link).up('.attachment').hide(); 
  },

/*
function elements_highlight(container_id) {
  var container = $('container_'+container_id);
  var all = container.getElementsByTagName('div');
  for (var e = 0; e < all.length; e++) {
    if (all[e].className == 'element_highlighted highlighted_'+container_id) {
      cur_element = "element"+all[e].id.slice(11);
      item_xy = Element.getDimensions(cur_element);
      Element.setStyle(all[e], {width: (item_xy.width-2)+'px', height: (item_xy.height-2)+'px'});
      Element.setOpacity(all[e], 0.3);       
    }
  }  
}

function elements_highlight_off(container_id) {
  var container = $('container_'+container_id);
  var all = container.getElementsByTagName('div');
  for (var e = 0; e < all.length; e++) {
    if (all[e].className == 'element_highlighted highlighted_'+container_id) {
      Element.setOpacity(all[e], 0);   
    }
  }  
}

function for_edit_show(container, element) {
  element_parent = $('element_'+container+'_'+element);
  item_xy = Element.getDimensions(element_parent);
  Element.setStyle('element_highlight_'+container+'_'+element, {width: (item_xy.width-2)+'px', height: (item_xy.height-2)+'px'});
  Element.setOpacity('element_highlight_'+container+'_'+element, 0.4);
  Element.show('element_menu_'+container+'_'+element);
}

function for_edit_hide(container, element) {
  //Element.hide('highlighted_'+container+'_'+element);
  Element.hide('element_menu_'+container+'_'+element);
  Element.setOpacity('element_highlight_'+container+'_'+element, 0);
  

}*/	

  new_link: function(url, msg) {
    this.box_loading_msg(msg);
    Lightbox.showBoxByAJAX(url, 770, 700);
  },
  
  edit_link: function(url,  msg) {
    //new Ajax.Updater('settings_actions', url, {asynchronous:true, evalScripts:true, onComplete:function(request){Element.hide(name+'_indicator_'+id)}, onLoading:function(request){Element.show(name+'_indicator_'+id)}}); 
    /*myLightWindow.activateWindow({
    	href: url, 
    	title: 'Waiting for the show to start in Las Vegas'
    });*/
    this.box_loading_msg(msg);
    Lightbox.showBoxByAJAX(url, 770, 700);
  },
  
  box_loading_msg: function(msg) {
    $('boxContents').innerHTML = '<img src="/images/admin/loading.gif"> '+msg;
  },

  action_link: function(url, indicator) {
      new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete:function(request){Element.hide(indicator)}, onLoading:function(request){Element.show(indicator)}}); 
  }, 

  list_link: function(url, name) {
    new Ajax.Updater('settings_content', url, {asynchronous:true, evalScripts:true, onComplete:function(request){Element.hide('tab_indicator')}, onLoading:function(request){Element.show('tab_indicator')}}); 
    $('tab_'+this.cur_tab).removeClassName('current');
    $('tab_'+name).addClassName('current');
    this.cur_tab = name;
  }
}