//
//  application.js
//  Created July 6, 2006
//

Event.observe(window, "load", function () {
	var searchButton = $("search_button");
	if (!searchButton) return;

	var preloadImage = new Image();
	preloadImage.src = "/images/search_button_hover.png";

	searchButton.onmouseover = function () {
		this.src = "/images/search_button_hover.png"; }
	searchButton.onmouseout = function () {
		this.src = "/images/search_button.png"; }
});

var currentPhoto = 0;

Event.observe(window, 'load', function () {
  if ($('billboard')) {
    var photos = $('billboard').getElementsByTagName('img');
    currentPhoto = photos.length - 1;
    
    setInterval(function () {
      var billboard = $('billboard');
      var photos = billboard.getElementsByTagName('img');
      
      if (currentPhoto > 0) {
        Element.show(photos[currentPhoto - 1]);
        Effect.Fade(photos[currentPhoto].getAttribute('id'),
          { duration: 1.5 });
      } else
        Effect.Appear(photos[photos.length - 1].getAttribute('id'),
          { duration: 1.5 });
      
      currentPhoto = currentPhoto - 1;
      if (currentPhoto < 0)
        currentPhoto = photos.length - 1;        
    }, 5000);
  }
});

Event.observe(window, 'unload', function () {
  if ($('billboard')) {
    var photos = $('billboard').getElementsByTagName('img');
    for (var index = 0; index < photos.length; index ++) {
      photos[index].setOpacity(1.0);
      photos[index].show();
    }
  }
});

Event.observe(window, 'load', function () {
  var paymentType = $('shop_form_shop_payment_type');
  if (paymentType) {
    Event.observe(paymentType, 'change', function () {
      var type = this.value;
      var checkForm = $('check_form');
      var creditForm = $('credit_form');
      var pendingForm = $('pending_form');
      var trialForm = $('trial_form');
      
      checkForm.hide();
      creditForm.hide();
      pendingForm.hide();
      trialForm.hide();
      
      if (type == "check") checkForm.show();
      if (type == "credit") creditForm.show();
      if (type == "pending") pendingForm.show();
      if (type == "trial") trialForm.show();
    });
  }
});

Event.observe(window, 'load', function () {
  var links = document.getElementsByTagName('a');
  for (var index = 0; index < links.length; index ++) {
    var theLink = links[index];
    if (theLink.href.indexOf('#') + 1 && 
        theLink.href.split('#')[1] != '' &&
        theLink.href.split('#')[1].indexOf("popup") == -1) {
      theLink.oldHref = theLink.href;
      theLink.href = 'javascript:void();';
      theLink.onclick = function () {
        new Effect.ScrollTo(this.oldHref.split('#')[1], { duration: 0.5 });
      }
    }
  }
});

Event.observe(window, 'load', function () {
	var labels = document.getElementsByTagName('label');
	for (var index = 0; index < labels.length; index ++) {
		Event.observe(labels[index], 'click', function () {
			var fieldId = this.getAttribute('for').
				replace('[', '_').replace(']', '');
			var field = document.getElementById(fieldId);
			
			field.focus();
			field.click();
		});
	}
});

Event.observe(window, 'load', function () {
	if (navigator.userAgent.indexOf('WebKit') + 1) {
		var buttons = document.getElementsByClassName('submit');
		for (var index = 0; index < buttons.length; index ++) {
			buttons[index].style.width = 
				(parseInt(buttons[index].offsetWidth) + 38) + "px";
			buttons[index].style.fontSize = "16px";
		}
	}
});

Event.observe(window, 'load', function () {
	if (navigator.userAgent.indexOf('WebKit') + 1) {
		var selects = document.getElementsByTagName('select');
		for (var index = 0; index < selects.length; index ++)
			selects[index].style.fontSize = "16px";
	}
});

Event.observe(window, 'load', function () {
	if (navigator.userAgent.indexOf('WebKit') + 1) {
		var textAreas = document.getElementsByTagName('textarea');
		for (var index = 0; index < textAreas.length; index ++) {
			var textArea = textAreas[index];
			var whiteLines = new Array(4);
			
			textArea.parentNode.style.backgroundImage = 
				'url(\'/images/textarea.png\')';
			textArea.parentNode.style.backgroundRepeat = 'no-repeat';
			textArea.parentNode.style.backgroundPosition = '-1px 6px';
			
			for (var line = 0; line < 4; line ++) {
				whiteLines[line] = document.createElement('div');
				whiteLines[line].style.backgroundColor = 'white';
				whiteLines[line].style.position = 'absolute';
				
				whiteLines[line].style.width = '1px';
				whiteLines[line].style.height = '1px';
				textArea.parentNode.
					insertBefore(whiteLines[line], textArea);
			}
						
			whiteLines[0].style.width = textArea.offsetWidth + 'px';
			whiteLines[0].style.marginLeft = '2px';
			whiteLines[0].style.marginTop = '4px';
	
			whiteLines[1].style.height = textArea.offsetHeight + 'px';
			whiteLines[1].style.marginTop = '4px';
			whiteLines[1].style.marginLeft = '2px';
			
			whiteLines[2].style.width = textArea.offsetWidth + 'px';
			whiteLines[2].style.marginLeft = '2px';
			whiteLines[2].style.marginTop = 
				(textArea.offsetHeight + 3) + 'px';
				
			whiteLines[3].style.height = textArea.offsetHeight + 'px';
			whiteLines[3].style.marginTop = '4px';
			whiteLines[3].style.marginLeft = 
				(textArea.offsetWidth + 1) + 'px';
		}
	}
});

Event.observe(window, 'load', function () {
  if (navigator.userAgent.indexOf('MSIE') + 1) {
    var links = document.getElementsByTagName('a');
    for (var index = 0; index < links.length; index ++) {
      var theLink = links[index];
      var inputs = theLink.getElementsByTagName('input');
      
      for (var offset = 0; offset < inputs.length; offset ++) {
        var theInput = inputs[offset];
        theInput.linkUrl = theLink.href;
        theInput.onclick = function () {
          window.location = this.linkUrl;
        }
      }
    }
  }  
});

function alertFormChange () {
  var alerts = $('alerts');
  if (alerts) alerts.show();
}

Event.observe(window, 'load', function () {
  var allInputs = document.getElementsByTagName("input");
  for (var index = 0; index < allInputs.length; index ++) {
    var nextInput = allInputs[index];
    if (nextInput.className.indexOf("monitor") + 1) {
      Event.observe(nextInput, 'change', alertFormChange);
      Event.observe(nextInput, 'keydown', alertFormChange);
    }
  }
  
  var allSelects = document.getElementsByTagName("select");  
  for (var index = 0; index < allSelects.length; index ++) {
    var nextSelect = allSelects[index];
    if (nextSelect.className.indexOf("monitor") + 1)
      Event.observe(nextSelect, 'change', alertFormChange);
  }
});

Event.observe(window, 'load', function () {
	var links = document.getElementsByTagName("a");
	for (var index = 0; index < links.length; index ++) {
		var nextLink = links[index];
		var linkURL = nextLink.href;
		
		if (linkURL.indexOf("#popup-") + 1) {
			nextLink.oldhref = linkURL;
			nextLink.href = "javascript:void();";
			nextLink.onclick = popupLinkClicked;
		}
	}
});

function popupLinkClicked () {
	var linkURL = this.oldhref;
	var resourcePart = linkURL.split("#")[0];
	var popupPart = linkURL.split("#")[1];
			
	var width = parseInt(popupPart.split("-")[1]);
	var height = parseInt(popupPart.split("-")[2]);

	var top = ((screen.height / 2) - (height / 2)) * 0.5;
	var left = (screen.width / 2) - (width / 2);

	window.open(linkURL, 
		"Popup", "width=" + width + ",height=" + height + ",top=" + 
		top + ",left=" + left + ",toolbar=no,scrollbars=auto,resizable=no");
}
