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

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function ping_item_parsing( param_hash ) {
  var postbody = Object.toQueryString(param_hash);
  var request = new Ajax.Request('/item/parsed_update', {asynchronous:true, evalScripts:true,  method:'post', postBody:postbody});
}

function edit_price_threshold(id) {
  Element.toggle('edit_price_threshold_' + id);
  Element.toggle('edit_price_threshold_form_' + id);
  if (Element.visible('edit_price_threshold_form_' + id)) {
    Field.activate('item_' + id + '_formatted_price_threshold');
  }
}

function saving_price_threshold(id) {
  Element.update('edit_price_threshold_price_' + id, 'Saving...');
  Element.update('edit_price_threshold_link_' + id, '');
  edit_price_threshold(id);
}



function edit_note(id) {
  Element.toggle('edit_note_' + id);
  Element.toggle('edit_note_form_' + id);
  if (Element.visible('edit_note_form_' + id)) {
    Field.activate('item_' + id + '_note');
  }
}

function saving_note(id) {
  Element.update('edit_note_note_' + id, 'Saving...');
  Element.update('edit_note_link_' + id, '');
  edit_note(id);
}



function edit_tags(id) {
  Element.toggle('edit_tags_' + id);
  Element.toggle('edit_tags_form_' + id);
  if (Element.visible('edit_tags_form_' + id)) {
    Field.activate('amazon_item_' + id + '_tags');
  }
}

function saving_tags(id) {
  Element.update('edit_tags_note_' + id, 'Saving...');
  Element.update('edit_tags_link_' + id, '');
  edit_tags(id);
}


function toggle_cover(id, url){
  image = document.getElementById('cover_image_' + id);
  image.src = url;
  cover_container = document.getElementById('cover_container_' + id);
  var covers = YAHOO.util.Dom.getElementsByClassName("large_cover_image");
  for (var i = 0; i < covers.length; i++) {
    cover = covers[i]
    if(cover.style.display != "none" & cover.id != "cover_container_" + id) {
      cover.style.display = "none";
    }
  }

  Element.toggle(cover_container);
  if(YAHOO.util.Dom.getStyle(cover_container, 'opacity') != 0) {
    var anim = new YAHOO.util.Anim(cover_container, {opacity : { to: 0 } }, 0.25);
    anim.animate(); 
  } else {
    var anim = new YAHOO.util.Anim(cover_container, {opacity : { to: 1.0 } }, 0.25);
    anim.animate();   
  }

}


// for shelf view
function toggle_info_block(id){
  info_block = document.getElementById('info_block_' + id);
  var blocks = YAHOO.util.Dom.getElementsByClassName("info_block");
  for (var i = 0; i < blocks.length; i++) {
    block = blocks[i]
    if(block.style.display != "none" & block.id != "info_block_" + id) {
      block.style.display = "none";
    }
  }
  Effect.toggle(info_block, 'appear', {duration:0.5});  
}


function show_login () {
  document.getElementById('flashcontent').innerHTML = '<img src="/images/wishradar-intro.gif" height="400" width="800" id="wishradar-intro-png" border="0"/>';
  login_dialog_var.render();
  login_dialog_var.show();  
}

function show_signup () {
  document.getElementById('flashcontent').innerHTML = '<img src="/images/wishradar-intro.gif" height="400" width="800" id="wishradar-intro-png" border="0"/>';
  signup_dialog_var.render();
  signup_dialog_var.show();  
}

  
function init_homepage() {
  login_dialog_var = new YAHOO.widget.Dialog("login_dialog", {
    postmethod:"form",
    modal:true, 
    visible:false,
    width:"314px", 
    fixedcenter:true,
    constraintoviewport:true, 
    close:true,
    underlay:'none',
    effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .25},
    draggable:false });

  signup_dialog_var = new YAHOO.widget.Dialog("signup_dialog", { 
    postmethod:"form",
    modal:true, 
    visible:false,
    width:"314px", 
    fixedcenter:true,
    constraintoviewport:true, 
    close:true,
    underlay:'none',
    effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .25},
    draggable:false });
  
  
}

function start_movie() {
 var so = new SWFObject("/images/wishradar-intro.swf", "wishradar-intro", "800", "400", "6", "#2e2e2e");  	  
 so.write("flashcontent"); 
}




function toggle_sidebar() {
	var margin = control_panel ? 50 : 275;
	var titles = YAHOO.util.Dom.getElementsByClassName('amazon_item_title');
  var bodies = YAHOO.util.Dom.getElementsByClassName('amazon_item');

  notice = document.getElementById('notice');
  giftcert = document.getElementById('giftcert');

  all = titles.concat(bodies).concat([notice]).concat([giftcert])
  for (var i = 0; i < all.length; i++) {
	  YAHOO.util.Dom.setStyle(all[i], 'margin-right',  margin + 'px');
	}
  
  if(control_panel) {
	  YAHOO.util.Dom.setStyle('sidebar',    'display', 'none'  );
	  YAHOO.util.Dom.setStyle('sidebarOff', 'display', 'block' );
	} else {
	  YAHOO.util.Dom.setStyle('sidebar',    'display', 'block' );
	  YAHOO.util.Dom.setStyle('sidebarOff', 'display', 'none'  );
	}
	
	control_panel = !control_panel;
	set_cookie('control_panel', control_panel.toString());
}




function deleting(node) {
  if (!node) return false;
  if (typeof node == 'string') node = document.getElementById(node);
  if (node) node.style.opacity = "0.5"
}


function adding(id) {
  document.getElementById('item_status_' + id).innerHTML = "Adding...";
}


function set_cookie( name, value, expires, path, domain, secure )	{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
  	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
			
function get_cookie( name ) {	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
			
function delete_cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


