/**
*
* Flashback JS.
* code is generally organized as: one pseudo-class per page.
*
*/

function redirect(url){
	if(url.length>1){
		location.href = url;
	}
}

function showFlashMessage(msg)
{
	var flash = document.getElementById("flash");
	flash.innerHTML = msg;
	flash.style.display = "block";
}


var popup_form = {
	
	width: '450',
	height: '300',
	
  show: function(url) {
		this.set_size(this.width,this.height);
    var req = new Ajax.Request(url, {
      method: 'get',
      onComplete: function(resp) {
        $('popup').update(resp.responseText);
        popup_form.show_div();
      }
    });
    return false
  },
  
	show_iframe: function(url, width, height) {
		$('popup').innerHTML = '<iframe frameborder="0" width="' + (width-10) + '" height="' + (height-10) + '" src="" id="media_frame"></iframe>';
		$('media_frame').src=url;
		this.set_size(width,height);
    this.show_div();	
	},
	
	set_size: function(width,height) {
		$('popup').style.width = width + 'px';
		$('popup').style.height = height + 'px';
		$('popup').style.marginTop = '-' + height/2 + 'px';
		$('popup').style.marginLeft = '-' + width/2 + 'px';		
	},
	
	show_div: function() {
		if (!$('popup_back')) {
			var overlay = document.createElement('div');
			overlay.setAttribute('id', 'popup_back');
			overlay.style.display = 'block';
			document.body.appendChild(overlay);
		}
		$('popup').style.display = 'block';
	},

  hide: function() {
		document.body.removeChild($('popup_back'));
    $('popup').hide()
    return false
  }
	
}


var profile = {
  left_column: function(select_column_id, calling_el) {
	    var column_ids = ['my_friends', 'my_groups']
	    column_ids.each(function(column_id, i) {
	      $(column_id).style.display = (column_id == select_column_id) ? 'block' : 'none'
	                        $(column_id+'_butt').toggleClassName('over')
	    })
	    calling_el.blur() // because of FF's dumbass gray boxes. LAME. move this somewhere more general!
	    return false
	  },
  
  switch_display_image: function(thumb_el, img_base_url) {
    $('display_image').src = img_base_url
    $$('div.display_images a').each(function(el) {
      el.removeClassName('primary')
    })
    thumb_el.toggleClassName('primary')
    thumb_el.blur()
    return false
  }
}


var play_page = {
  base_url: "",
  
  init: function(base_url) {
    this.base_url = base_url
    
    // dig ID out of anchor & select piece if possible. must be numeric!
    var id_matches = location.href.match(/#(\d+)/)
    if (id_matches && id_matches.length) this.edit_deed(id_matches[1])
  },
  
  edit_deed: function(completion_id) {
    $$('ul.pendingMissions li').each(function(el) {
      if (el.id == play_page.completion_el_id(completion_id)) el.addClassName('on')
      else el.removeClassName('on')
    })
    
    //$('completion_box').update('')
    var req = new Ajax.Request(this.base_url+completion_id, {
      method: 'get',
      onComplete: function(resp) {
        $('completion_box').update(resp.responseText);
      }
    })
    
    return false
  },
  
  completion_el_id: function(completion_id) {
    return "c" + completion_id
  },
	
	add_collabs: function(form,url) {
		var req = new Ajax.Request(url, {
      method: 'post',
			parameters: Form.serialize(form),
      onComplete: function(resp) {
        $('collab_list').update('Currently collaborating with: ' + resp.responseText);
				popup_form.hide();
      }
    })
	}
}

var jabber = {
	open_message: function (el, url) {
		popup_form.show(url);
		el.parentNode.parentNode.className = 'Read';
	}
}

var image_uploader = {
  num_inputs: 0,
  
  init: function(num_inputs) {
    this.num_inputs = num_inputs
  },
  /**
  * When user selects an image, dupe the file upload input so the user can select another.
  * The backend is ready for any # of images.
  */
  add_image_input: function() {
    this.num_inputs++
    
    var new_input = $('image1').cloneNode(false)
    new_input.id = new_input.value = ''
    new_input.name = "image" + this.num_inputs
    
    new Insertion.Bottom('imageUploads', new_input)
  }
}

function deleteImage(name){
	if (confirm('Do you really want to delete this image?')) {
		var pars = 'name='+name;
		var url = "/image/delete";
		var myAjax = new Ajax.Request( url, {
			method: 'POST', 
			parameters: pars,
			onComplete: function(resp) {
				id = resp.responseText;
				if ($(id)) $(id).style.display = 'none';
			}
		});
		return true;
	} else {
		return false;
	}
}


function openMediaLib(target_name)
{
	popup_form.show_iframe('/media/?m=editor#' + target_name, 620, 420);
	//window.open('{/literal}{$baseUrl}{literal}/media/?m=editor#' + target_name, 'IMAGE_BROWSER', 'left=20,top=20,width=600,height=400,toolbar=0,resizable=0,status=0');
}



/**
* to get rid of FireFox's dumbass gray boxes
* doesn't work.... just embed this in above funcs
*
Event.observe(window, 'load', function(e) {
  $$('a').each(function(el) {
    // el.attachEvent('onclick', function(e) {
    //   this.blur()
    // })
    // el.onclick = function(e) {
    //   this.blur()
    // }
  })
  // Event.observe($$('a'), 'click', function(e) {
  //     this.blur()
  //   }, false);
}, false);
*/
