
// I'm comming back to this.
// var Payment = {
//   
//   init: function(card_type) {
//     // if(card_type)
//       this.showCards(card_type);
//   },
//   
//   showCards: function() {
// 
//     // $$('#supported_cardtypes ul').invoke('hide');
//     $('supported_cardtypes').show();
//     // $(card).show();    
//   },
//   
//   listen: function(){
//     $('account_payment_gateway_type').observe()
//   }
//   
// }

// Payment.SupportedCards = {
//   'authorize_net' : ['visa', 'master', 'american_express', 'discover'],
//   'braintree' : ['visa', 'master', 'american_express', 'discover']  
// }

// GLOBAL VARS //
	var fieldPending = false
//

// Creates, inserts, and hides flash messages
var Messenger = {
  autohide_error: null,
  autohide_notice: null,
  
  notice: function(message) {
    $$('.ajaxNoticeValid')[0].innerHTML = "<p>" + message + "</p>";           
    new Effect.Appear('flashWrapper', {duration: 0.3}); 
    if (this.autohide_notice != null) {clearTimeout(this.autohide_notice);}
    this.autohide_notice = setTimeout(Messenger.fadeNotice.bind(this), 5000);
  },
  
  fadeNotice: function() {
    new Effect.Fade('flashWrapper', {duration: 0.4});
    this.autohide_notice = null;
  }
};


// Toggle values

function toggleValue(value){
	var url = '/admin/account/update_toggle?value=' + value
	new Ajax.Request(url, {
	  method: 'put'	
	});

}


function toggleDBValue(setting, on){
	var url = '/admin/account/update_toggle_setting?v=' + on + '&f=' + setting
	new Ajax.Request(url, {
	  method: 'put'	
	});
}

// Simple AJAX indicator; spinner follows cursor.

var Indicator = Class.create();
Indicator.prototype = {
  initialize: function(offsetX, offsetY) {
    this.offsetX = offsetX || 8;
    this.offsetY = offsetY || 5;
    
		var body = $$('body')[0];
    
		// HTML snippet. Source to spinner: /images/spinner.gif
    new Insertion.Bottom(
      body, 
      '<div id="indicator" style="z-index: 1000; position:absolute; width:20px; height:20px; padding:6px; display: none; "><img src="/images/spinner.gif" border="0"></div>' 
    );
    
    this.indicator = $('indicator');
        
    Ajax.Responders.register({
      onLoading:  this.indicate.bind(this, true),
      onComplete: this.indicate.bind(this, false) 
    });  
    
    Event.observe(body, "mousemove", this.onMouseMove.bind(this));
  },
  
  onMouseMove: function(event) {
    try {
    this.indicator.style.top    = Event.pointerY(event) + this.offsetY + "px";
    this.indicator.style.left   = Event.pointerX(event) + this.offsetX + "px"; 
    } catch(e) {}
  },
      
  indicate: function(visible) {
    if(visible) { this.indicator.show(); }
    else        { this.indicator.hide(); }
  }
};

Event.observe(window, "load", function(){ new Indicator(); });


// Cookies: crete, read and delete
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




function contractsBoxDisplay() {
	var box = $('manageContractsBox');
	box.toggle();
	if(box.visible()) {
		$('pendingChanges').update('Hide');
	} else {
		$('pendingChanges').update('Manage Contracts');		
	}
}

var ToggleBox = {
	
  toggle : function(off, on) {
    off.show();
    on.hide();
  }
}

var Help = {

  toggleHelp: function(image, flip) {
    var helperLayer = $('help_layer_' + image.id);
    if(helperLayer.visible()){
      helperLayer.hide();
      image.src = '/images/v2/questionPassive.png';
      image.style.zIndex = 998;
    } else {
      image.addClassName('active');
      var layerTopPos = image.cumulativeOffset()[1] + image.getHeight() -1;
      var layerLeftPos = image.cumulativeOffset()[0];
			helperLayer.style.zIndex = 1000;
      helperLayer.show();
      if(flip){
        helperLayer.style.top = layerTopPos - helperLayer.getHeight() - image.getHeight() + 2 + 'px';
        helperLayer.style.left = layerLeftPos - helperLayer.getWidth() + image.getWidth() + 1 + 'px';
        image.src = '/images/v2/questionActiveFlipped.gif'
      } else {
        helperLayer.style.top = layerTopPos + 'px';
        helperLayer.style.left = layerLeftPos + 'px';
        image.src = '/images/v2/questionClose.png';
      }
      image.style.zIndex = 1001;
    }
  }
}


// sets cookie and hides/shows home page graphic.
function switchSplash() {
	if($$('.mini_splurge').first().visible()) {
		eraseCookie('hideSplash');
		$$('.mini_splurge').first().hide();
		$$('.large_splurge').first().show();
	} else {
	  	createCookie('hideSplash', 'true', 100);		
		$$('.mini_splurge').first().show();
		$$('.large_splurge').first().hide();
	}
}

function checkRule(form) {
	
  var min = parseInt($F('pricing_rule_min'));
  var maxEl = $$('input[name="pricing_rule[max]"]').first()
  var max = parseInt(maxEl.value)
	var submit = true
	
	if (min < 1) {
		alert('Your minimum value must be greater than zero.')
		submit = false;
	}
  if (min > max) {
    maxEl.focus();
    alert('Your minimum value can\'t be greater than your maxium value.');
		submit = false
  }
  if (submit)
    form.submit();
}


function toggleContractSummary() {
  if ($('contractsPreviewHolder').visible()) {
    $('contractsPreviewHolder').hide();
    $('contracts_panel').show();
    $('toggle_manage').innerHTML = 'Preview Contracts >>'
  } else {
    $('contractsPreviewHolder').show();
    $('contracts_panel').hide();
    $('toggle_manage').innerHTML = '<< Back to Control Panel'
  }
};

function slideSummary(){
  if ($('contracts_panel').visible()) {
    $('contracts_panel').hide();
    $$('.slideSummary').first().hide();
    $('summarySwitch').down(0).src = '/images/contracts_view.gif';
	
  }
  else {
    $('contracts_panel').show();
    $$('.slideSummary').first().show();
    $('summarySwitch').down(0).src = '/images/contracts_hide.gif';
  }
};



function configurePanel() {
  var panel = $('contracts_panel');
  if(panel.visible())
    Effect.SlideUp(panel)
  else
    Effect.SlideDown(panel)

}


function hideBubble(anchor){
	
  box = $(anchor).previous();
  Effect.Fade(box, {
    duration:0.3
  })
}


function showBubble(anchor){
  box = $(anchor).previous();
  box.style.position = 'absolute';
  box.style.bottom = 60 + 'px';
  box.style.zIndex = 300;

  Effect.Appear(box, {
    duration: 0.3
  });
}

function setPosition(box, link){
  box.style.left = (link.cumulativeOffset()[0])  +  "px";
  box.style.top = (link.up(0).up(0).cumulativeOffset()[1])  + "px";
 	
  box.style.position = 'absolute';
  box.style.zIndex = 99;
  box.setStyle({
    borderColor:'red',
    borderWidth:'1px'
  })
  link.setStyle({
    background:'lemonchiffon',
    borderColor:'red'
  })
	
}

var Displayer = {
  display: function(e, x) {
    $('limits_box_' + x).toggle();
    Event.stop(e);
  }
};


var bubble = Class.create({

  initialize: function(){
    this.setObservers();
  },
	
  setObservers: function(){

  }
	
});
	
  var x = 0;



Event.observe(window, 'load', function(){
	 $$('iframe').each(function(frame){
	   Event.observe(frame, 'mouseover', respondToClick, false);
		}
);
	 
function respondToClick(event) {
		fieldPending = true;
	}
})

if (window.hljs) {
  hljs.initHighlightingOnLoad();
}

// This is used as callback by open flash chart, to open links in the same window.
function openUrl(url) {
  window.location = url;
}

function openUrlInto(id, url) {
  new Ajax.Updater(id, url, {evalScripts: true});
}
