// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*
document.body.onresize = function (){
 alert("Thanks for resizing!");
}
*/

function resized() {
	var info_pos = $('page-information').viewportOffset();
	var extra_posx = info_pos[0] + 143;
	//alert( info_pos );
	$('page-extras').style.left = extra_posx+'px';
	$('page-extras').style.top = info_pos[1]-2+'px';
	//$('page-extras').style.offsetLeft = info_pos;
	//$('page-extras').style.pixelLeft = info_pos;
}

function adjustLinkBlocks( ) {
	spans = $$('.top-text');
	//alert(spans.length);
	
	spans.each( function(s) {
		//alert(s.getHeight());
		top = s.getStyle('top');
		adjust = (s.getHeight() - 13) / 2; 
		new_top = parseInt(top)-adjust;
		//alert(new_top);
		$(s).setStyle( { top: new_top+'px' } );
		//alert(s.getStyle('top'));
		
	});
	
}
 
function product_rollover(id) {
	//alert('bo');
	//Effect.Appear(id+'_text', { duration: 0.5 });
	
  //$(id).addClassName('over');
  $(id + '_text').addClassName('yellow');
	
	/*
	if ($(id + '_text').hasClassName('transparent')) {
  	new Effect.Opacity(id, {
  		from: 1,
  		to: 0.7,
  		duration: 0.3
  	});
  	new Effect.Opacity(id + '_text', {
  		from: 0.7,
  		to: 1,
  		duration: 0.3
  	});
  	$(id).addClassName('over');
  	$(id + '_text').removeClassName('transparent');
  } //else alert( 'not');
  */
	/* $(id).style = "filter:alpha(opacity=50);-moz-opacity:0.5;opacity: 0.5"; */
  /* $(id).setStyle({filter:alpha(opacity=50),-moz-opacity:0.5,opacity: 0.5}); */	
	
	
  return false;
	
}

function product_rollout(id) {
	//Effect.Fade(id+'_text', { duration: 0.5 });

  //$(id).removeClassName('over');
  $(id + '_text').removeClassName('yellow');
	
	/*
	if (!$(id + '_text').hasClassName('transparent')) {
  
  	new Effect.Opacity(id, {
  		from: 0.7,
  		to: 1,
  		duration: 0.3
  	});
  	new Effect.Opacity(id + '_text', {
  		from: 1,
  		to: 0.7,
  		duration: 0.3
  	});
  	
  	$(id).removeClassName('over');
  	$(id + '_text').addClassName('transparent');
  }
  */
	/*
  $(id).style = "filter:alpha(opacity=100);-moz-opacity:1.0;opacity: 1.0";
  */
  /* $(id).setStyle({filter:alpha(opacity=100),-moz-opacity:1.0,opacity: 1.0}); */ 
  return false;

}

var inplace_editor_edit_hint = 'Click to edit...';

function createInplaceEditor(field, update_path, highlightcolor,
                             highlightendcolor, width)
{
   fillIfEmpty($(field));
  
   new Ajax.InPlaceEditor(field, update_path, { 
     highlightcolor: highlightcolor, 
     highlightendcolor: highlightendcolor,
     okButton: false,
     cancelLink: false,
     submitOnBlur: true,     
     cols: width,
     callback: function(form) {
        input_field = form.elements[0];
        
        // This is to ensure we don't save the edit hint if
        // the user accidentally clicked an empty field
        if(input_field.value == inplace_editor_edit_hint)
          input_field.value = '';
        
        return Form.serialize(form);                   
      },
     onComplete: function(transport, element)
     { 
       fillIfEmpty(element);
       
       if(transport.statusText != "Internal Server Error")
           onEditorSuccess(); // cb
       
       new Effect.Highlight(element, {
           startcolor: this.options.highlightcolor,
           endcolor: this.options.highlightendcolor
       });
     },
     onFailure: function(element, transport) {
       onEditorFailure(transport.responseText); // cb
     }
   });
}

function fillIfEmpty(element)
{
  if(element.innerHTML == '')
    element.innerHTML = inplace_editor_edit_hint;  
}


  function onEditorSuccess()
  {
    $('error').innerHTML = '';
    $('notice').innerHTML = 'Model was successfully updated.';
  }
  
  function onEditorFailure(response)
  {
    response = response.evalJSON();
    
    var lines = new Array();
   
    for(var i = 0; i < response.length; i++)
      lines.push(response[i][0] + ' ' + response[i][1]);
            
    $('notice').innerHTML = '';
    $('error').innerHTML = 'Error: ' + lines.join('. ')
  }
	
function set_attachment_element(element) {
	if( $('image_attachment_element_id') )
	 $('image_attachment_element_id').value = element.id 
	if( $('file_attachment_element_id') ) 
	$('file_attachment_element_id').value = element.id
}


//---- FOR INLINE EDIT - SHOW HTML

Object.extend(Ajax.InPlaceEditor.prototype, {
    onLoadedExternalText: function(transport) {
			  //alert('got external text');
        Element.removeClassName(this.form,
        this.options.loadingClassName);
        this.editField.disabled = false;
        this.editField.value = transport.responseText;
        Field.scrollFreeActivate(this.editField);
				
    }

});

Object.extend(Ajax.InPlaceEditor.prototype, {
    getText: function() {
			//alert('gettext');
      return this.element.innerHTML;
		 
	  }

}); 

// The right way to add load events. By Simon Willison
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
