function toggle_box_display(box_id){
  var elem = document.getElementById("box_content_" + box_id);
  if (elem.style.display == 'none') {
    document.getElementById("box_display_btn_" + box_id).src = "/images/graphics/box_control_collapse.gif";
    document.getElementById("box_display_btn_" + box_id).alt = "Minimize";
    document.getElementById("box_display_btn_" + box_id).title = "Minimize";
    elem.style.display = '';
  } else {
    document.getElementById("box_display_btn_" + box_id).src = "/images/graphics/box_control_expand.gif";
    document.getElementById("box_display_btn_" + box_id).alt = "Maximize";
    document.getElementById("box_display_btn_" + box_id).title = "Maximize";
    elem.style.display = 'none';
  }
} 

function toggleDiv(divid){
  var elem = document.getElementById(divid);
  if(elem.style.display == 'none'){
    elem.style.display = 'block';
  }else{
    elem.style.display = 'none';
  }
}    
       
function ajaxLoader(dom_id, text, blocked) {
  if(blocked != false){
    blocked = true;
  }
  
  var elem = document.getElementById(dom_id);
  
  if(blocked){
    elem.innerHTML = '<div class="box"><div class="box_content"><span class="ajax_loader">' + text + '&nbsp;</span></div></div>';
  }
  else {
    elem.innerHTML = '<span class="ajax_loader">' + text + '&nbsp;</span>';
  }
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

function clearInput(e, style) {  
  e.className = style || 'input_text';
  e.value = '';  
}

function restoreInput(e, value, style) {  
  if (trim(e.value) == '') {        
    e.value = value;
    e.className = style || 'input_text input_blank';    
  }
}

function previewBackground() {
  $$('.wallpaper').each(function(elem) {
    elem.setStyle({backgroundColor: '#' + $('color').value});
    elem.setStyle({backgroundRepeat: $('repeat').value});
  });
}
  
function initialize_menu(menuElement) {
  menuElement.childElements().each(function(node){
    // if there is a submenu
    var submenu = $A(node.getElementsByTagName("ul")).first();
    if(submenu != null){
      // make sub-menu invisible
      //Element.extend(submenu).setStyle({display: 'none'});
      // toggle the visibility of the submenu
      node.onmouseover = node.onmouseout = function(){
        Element.toggle(submenu);
        if(!node.hasClassName("active")){
          node.addClassName("active");
        }
        else {
          node.removeClassName("active");
        }
      };
    }
  });
}

function close_popup() {
  close_colorpicker();
  new Effect.Fade('facebox', {duration: .1});
}

function close_colorpicker() {
  if(Control != undefined && Control.colorPickers){
    Control.colorPickers.each(function(colorPicker) {      
      colorPicker.options.onClose = undefined;
      colorPicker.close();
    });
  }
}

function show_box_menu(box_id) {
  $$('div.box_menu').each(function(elem) {
    
    if(elem != $(box_id)){
      elem.hide();
    }
  });
  
  Element.toggle(box_id);
}

function show_customizer(){
  if (!Element.visible('customizer')) { 
    new Effect.SlideDown('customizer', { duration: 0.25 });
  }
}

function hide_customizer(){
  if (Element.visible('customizer')) { 
    new Effect.SlideUp('customizer', { duration: 0.25 });
  }
}

function set_theme (body_bg_color, body_bg_image_url, sprite_url, box_sprite_url, box_border_color, box_bg_color_1, box_bg_color_2, box_bg_color_3, box_head_bg_color, box_title_font, box_title_color, text_color_1, text_color_2, link_color) {
  $$('.body_left').each(function(elem) {
    elem.setStyle({backgroundColor: body_bg_color});
    if(body_bg_image_url){
      elem.setStyle({backgroundImage: 'url(' + body_bg_image_url.toString() + ')'});
    }
    else{
      elem.setStyle({backgroundImage: 'none'});
    }
  });
  
  $$('.box a').each(function(elem) {
    elem.setStyle({color: link_color});
  });  

  $$('.box').each(function(elem) { 
    elem.setStyle({backgroundColor: box_bg_color_1});
    if(box_sprite_url){
      elem.setStyle({backgroundImage: 'url(' + box_sprite_url.toString() + ')'});
    }
    else {
      elem.setStyle({backgroundImage: 'none'});
    }
    elem.setStyle({color: text_color_1});
    if(!box_border_color || box_border_color == ''){
      elem.setStyle({border: 'none'});
    }else{
      elem.setStyle({borderWidth: '1px'});
      elem.setStyle({borderStyle: 'solid'});
      elem.setStyle({borderColor: box_border_color});
    }
  });
  
  $$('.box.panel').each(function(elem) { 
    elem.setStyle({backgroundColor: box_bg_color_1});
    elem.setStyle({backgroundImage: 'none'});
    if(!box_border_color || box_border_color == ''){
      elem.setStyle({border: 'none'});
    }else{
      elem.setStyle({borderWidth: '1px'});
      elem.setStyle({borderStyle: 'solid'});
      elem.setStyle({borderColor: box_border_color});
    }
  });
  
  $$('.box .box_head').each(function(elem) {
    elem.setStyle({backgroundColor: box_head_bg_color});
    if(sprite_url){
      elem.setStyle({backgroundImage: 'url(' + sprite_url.toString() + ')'});
    }
    else {
      elem.setStyle({backgroundImage: 'none'});
    }
  });
  
  $$('.box_title a, .box_title a:visited, a.box_title_link, a.box_title_link:visited').each(function(elem) {
    elem.setStyle({color: box_title_color});
  });

  $$('.box .box_footer').each(function(elem) {
    if(sprite_url){
      elem.setStyle({backgroundImage: 'url(' + sprite_url.toString() + ')'});
    }
    else {
      elem.setStyle({backgroundImage: 'none'});
    }
  });
  
  $$('.box .image_text').each(function(elem) {
    elem.setStyle({borderColor: box_bg_color_2});
  });  
  
  $$('.box .dimmed').each(function(elem) {
    elem.setStyle({color: text_color_2});
  });
  
  $$('.box .feed_date').each(function(elem) {
    elem.setStyle({borderColor: box_bg_color_3});
  });
  
  $$('.box .alternate_row').each(function(elem) {
    elem.setStyle({backgroundColor: box_bg_color_2});
  });
  
  $$('img.thumbnail').each(function(elem) {
    elem.setStyle({backgroundColor: box_bg_color_2});
    elem.setStyle({borderColor: box_bg_color_2});
  }); 

  $$('.box a.popup_link').each(function(elem) {
    elem.setStyle({color: link_color});
  });    
  
  $$('.box .tabs').each(function(elem) {
    elem.setStyle({borderColor: text_color_1});
  });   
  
  $$('.box .toggle_tabs li a').each(function(elem) {
    elem.setStyle({backgroundColor: box_bg_color_2});
    elem.setStyle({borderColor: box_bg_color_2});
    elem.setStyle({color: text_color_1});
  });    
  
  $$('.box .toggle_tabs li a.active').each(function(elem) {
    elem.setStyle({backgroundColor: text_color_1});
    elem.setStyle({borderColor: text_color_1});
    elem.setStyle({color: box_bg_color_1});
  });   
  
  $$('.box input[type=submit]').each(function(elem) {
    elem.setStyle({backgroundColor: link_color});
    elem.setStyle({color: box_bg_color_1});
  }); 
  
  $$('.box input[type=button]').each(function(elem) {
    elem.setStyle({backgroundColor: box_bg_color_2});
    elem.setStyle({color: text_color_1});
  });   

}

function set_background(image_url, background_color, repeat) {
  $$('.wallpaper').each(function(elem) {
    elem.setStyle({backgroundColor: background_color.toString()});
    elem.setStyle({backgroundImage: 'url(' + image_url.toString() + ')'});
    elem.setStyle({backgroundRepeat: repeat.toString()});
  });  
  
}

function preview_customization(background_image, background_repeat, body_bg, body_bg_image, box_head_bg, box_head_bg_image, box_body_bg, link_color) {
  $$('.box_head').each(function(elem) {
    elem.setStyle({backgroundImage: 'url(' + box_head_bg_image.toString() + ')'});
  });
  
  $$('.box_body').each(function(elem) {
    elem.setStyle({backgroundColor: box_body_bg.toString()});
  });
  
  var links = $('profile_body').getElementsBySelector ('a'); 
  links.each(function(elem) {
    elem_classes = $w(elem.className);
    if(elem_classes.size() == 0) {
      elem.setStyle({color: link_color.toString()});
    }
  });

  $$('.body_left').each(function(elem) {
    elem.setStyle({backgroundColor: body_bg.toString()});
    //elem.setStyle({backgroundImage: 'url(' + body_bg_image.toString() + ')'});
  });
  
  $$('.wallpaper').each(function(elem) {
    if(background_image){
      elem.setStyle({backgroundImage: 'url(' + background_image.toString() + ')'});
    }
    else {
      elem.setStyle({backgroundImage: 'none'});
    }
    elem.setStyle({backgroundRepeat: background_repeat.toString()});
  });  
  
}

function toggle_section(elem, toggler) {
  Element.toggle(elem);
  if($(elem).visible()){
    $(toggler).addClassName('expanded');
  }
  else {
    $(toggler).removeClassName('expanded');
  }
}

function checkAll(frm) {
  // set the form to look at (your form is called form1)
  //var frm = document.form1
  // get the form elements
  var el = frm.elements;
  // a variation to store whether any boxes are checked
  var testit = false;
  // loop through the elements...
  for(i=0;i<el.length;i++) {
    // check if the element is a checkbox and whether it is checked
    if (el[i].type == "checkbox" && el[i].checked == true) {
      // we have found a checked box so remember this!
      testit = true;
    }
  }
  // loop through the elements again...
  for(i=0;i<el.length;i++) {
    // and check if it is a checkbox
    if(el[i].type == "checkbox" ) {
      // if it is a checkbox and we have found a checked box
      if(testit == true) {
        // tick the box
        el[i].checked = false; }
      else {
        // otherwise untick the box
        el[i].checked = true; }
    }
  }
}

function toggle_css(elem, css_class) {
  
  if(elem.hasClassName(css_class)){
    elem.removeClassName(css_class);
  }
  else {
    elem.addClassName(css_class);
  }
}

function toggle_avatar_selection(avatar_id){
  var avatar = $('avatar_' + avatar_id);
  var avatar_checkbox = $('avatar_checkbox_' + avatar_id);
  
  toggle_css(avatar, 'highlight');
  
  if(avatar_checkbox.checked){
    avatar_checkbox.checked = 0;
  }
  else {
    avatar_checkbox.checked = 1;
  }
}

function toggle_avatar_selection_all(value) {
  $$('.avatar_select input').each(function(e){ e.checked = value });
  $$('.avatar_select td').each(function(e){ value ? e.addClassName('highlight') : e.removeClassName('highlight'); });
}

function clearTabs(tabId, domPrefix, nrOfTabs, className) {

  for(var i=0; i < nrOfTabs; i++) {
    var obj = $(domPrefix+i);

    if(obj){
      obj.className = '';
    }
  }

  $(tabId).className  = className;
}

function displayMessage(domId, msg, cssClass){
  var elem = $(domId);
  var div = new Element('div');
  var span = new Element('span');
  var h4 = new Element('h4');

  while(elem.firstChild) {
    elem.removeChild(elem.firstChild);
  }
   
  elem.innerHTML = "";

  div.className = 'mini_message with_icon ' + cssClass;
  span.className = 'mini_message_body';
  h4.className = 'mini_message_title';
  h4.update(msg);

  span.appendChild(h4);
  div.appendChild(span);

  elem.appendChild(div);
}

function changeRating(star, rateableId){
  var title = 'Rate this!';
  for(var i = star + 1; i <= 5; i++){
    var elem = $('star_' + i + '_' + rateableId);
    elem.src = '/images/icons/star_empty.png';
    elem.title = title;
    elem.alt = title;
  }

  for(var j = star; j > 0; j--){
    var elem2 = $('star_' + j + '_' + rateableId);
    elem2.src = '/images/icons/star.png';
    elem2.title = title;
    elem2.alt = title;
  }
}

function resetRating(rateableId, rating){
  for(var i = 1; i <= Math.floor(rating); i++){
    var elem = $('star_' + i + '_' + rateableId);
    elem.src = '/images/icons/star.png';
    elem.title = rating;
    elem.alt = rating;
  }

  if(Math.ceil(rating) != Math.floor(rating)) {
    var elem2 = $('star_' + Math.ceil(rating) + '_' + rateableId);
    elem2.src = '/images/icons/star_half.png';
    elem2.title = rating;
    elem2.alt = rating;
    i++;
  }

  for(var j = i; j <= 5; j++){
    var elem3 = $('star_' + j + '_' + rateableId);
    elem3.src = '/images/icons/star_empty.png';
    elem3.title = rating;
    elem3.alt = rating;
  }

}
