

function center(e) {

	var me = $(e);
	var up = me.up();
	

	var myWidth = me.getWidth();
	var spaceWidth = up.getWidth()

	var offset = Math.round((spaceWidth - myWidth) / 2);

	// by padding left
	//	me.makePositioned();
	try {
		me.setStyle({ paddingLeft: offset + 'px'});
	} catch(ex) {
		alert(ex);
	}
}


function trans(e, p) {
	var me = $(e);
	var up = me.up();
        // up.relativize()
        
	var trans = new Element('div');
	trans.clonePosition(me);
        up.appendChild(trans);
        // trans.setStyle({ background: me.getStyle('background'), border: { '1px solid red'} });
	trans.update('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
	trans.setStyle({top: 0});
	me.setStyle({ background: 'none'});
        // trans.setStyle({ left: 0, top: 0});

	// trans.setOpacity(p || 0.7);


}

// safari workaround for niceform
function safariReplacements() {

	if (Prototype.Browser.WebKit) { // because on ff does work
		var texts = $$('form.niceform input[size]');
		var imagesPath = '/images/niceforms/';
		for(var q = 0; q < texts.length; q++) {
			texts[q].style.width = texts[q].size * 10 + 'px';
			txtLeft = document.createElement('img'); txtLeft.src = imagesPath + "input_left.gif"; txtLeft.className = "inputCorner";
			txtRight = document.createElement('img'); txtRight.src = imagesPath + "input_right.gif"; txtRight.className = "inputCorner";
			texts[q].parentNode.insertBefore(txtLeft, texts[q]);
			texts[q].parentNode.insertBefore(txtRight, texts[q].nextSibling);
			texts[q].className = "textinput";
			//create hovers
			texts[q].onfocus = function() {
				this.className = "textinputHovered";
				this.previousSibling.src = imagesPath + "input_left_xon.gif";
				this.nextSibling.src = imagesPath + "input_right_xon.gif";
			}
			texts[q].onblur = function() {
				this.className = "textinput";
				this.previousSibling.src = imagesPath + "input_left.gif";
				this.nextSibling.src = imagesPath + "input_right.gif";
			}
		}
		
		 
		var textareas = $$('form.niceform textarea');
		for(var q = 0; q < textareas.length; q++) {
			var where = textareas[q].parentNode;
			var where2 = textareas[q].previousSibling;
			textareas[q].style.width = textareas[q].cols * 10 + 'px';
			textareas[q].style.height = textareas[q].rows * 10 + 'px';
			//create divs
			var container = document.createElement('div');
			container.className = "txtarea";
			container.style.width = textareas[q].cols * 10 + 20 + 'px';
			container.style.height = textareas[q].rows * 10 + 20 + 'px';
			var topRight = document.createElement('div');
			topRight.className = "tr";
			var topLeft = document.createElement('img');
			topLeft.className = "txt_corner";
			topLeft.src = imagesPath + "txtarea_tl.gif";
			var centerRight = document.createElement('div');
			centerRight.className = "cntr";
			var centerLeft = document.createElement('div');
			centerLeft.className = "cntr_l";
			if(!this.ie) {centerLeft.style.height = textareas[q].rows * 10 + 10 + 'px';}
			else {centerLeft.style.height = textareas[q].rows * 10 + 12 + 'px';}
			var bottomRight = document.createElement('div');
			bottomRight.className = "br";
			var bottomLeft = document.createElement('img');
			bottomLeft.className = "txt_corner";
			bottomLeft.src = imagesPath + "txtarea_bl.gif";
			//assemble divs
			container.appendChild(topRight);
			topRight.appendChild(topLeft);
			container.appendChild(centerRight);
			centerRight.appendChild(centerLeft);
			centerRight.appendChild(textareas[q]);
			container.appendChild(bottomRight);
			bottomRight.appendChild(bottomLeft);
			//insert structure
			where.insertBefore(container, where2);
			//create hovers
			textareas[q].onfocus = function() {
				this.previousSibling.className = "cntr_l_xon";
				this.parentNode.className = "cntr_xon";
				this.parentNode.previousSibling.className = "tr_xon";
				this.parentNode.previousSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_tl_xon.gif";
				this.parentNode.nextSibling.className = "br_xon";
				this.parentNode.nextSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_bl_xon.gif";
			}
			textareas[q].onblur = function() {
				this.previousSibling.className = "cntr_l";
				this.parentNode.className = "cntr";
				this.parentNode.previousSibling.className = "tr";
				this.parentNode.previousSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_tl.gif";
				this.parentNode.nextSibling.className = "br";
				this.parentNode.nextSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_bl.gif";
			}
		}
			
			 
			  
	}
	   
}

	function ieCartHovers() {
		if (!/MSIE (5|6)/.test(navigator.userAgent)) return;
		var chosen = $$('.choose')[0];
		var form = $('expanded-buy-form');
		if (form && chosen) {
			chosen.observe('click', function(event) {
				var inTable = event.element().up('div#expanded-buy-form');
				var showed = form.getStyle('display') == 'block';
				form.setStyle({ display: (showed && !inTable ? 'none' : 'block')})
		    });
		}
	}
	 

var lastClicks = {}; 
var rotated = {};
var playing = false;
function rotate(id, f, player) {
	Event.observe(window, 'load', function() {
		var pe = new PeriodicalExecuter(function() {
			try {
				if (rotated[id] && (!player || !playing)) {
					f();
					 
				} else {
					if ((new Date()).getTime() - lastClicks[id] > 20000) {
						rotated[id] = true;
					}
				}
				 
			} catch(e) {
				// alert('sth wrong ' + e);
			}
	  
		}, 10);
		rotated[id] = true;
		 
		// stopping when clicked
		Event.observe(id, 'click', function() { 
			 rotated[id] = false;
			 playing = false; 
			 lastClicks[id] = (new Date()).getTime();
		});
	})
}


function start() {
	playing = true;
}
	 
function stop() {
	playing = false;
}


function open_drops(id) {
	var drop = $(id);
	drop.addClassName('show');
	drop.observe('mouseleave', function(event) {
		this.removeClassName('show');
	});
	return false;
}


function advancedSearchChecks() {
	
	var labels = $$('#advanced-search ul.checks label');
	
	for (var index = 0, len = labels.length; index < len; ++index) {
		labels[index].observe('click', advancedCheckboxClicked);
	}	
	
}

function advancedCheckboxClicked() {
	var id = $(this).readAttribute('for');
	var checked = $(id).checked;
	
	if (!checked) {
		$(this).addClassName('chosen');
	} else {
		$(this).removeClassName('chosen');
	}
}


var Loop = {
	soloCardOptions : function(hide) {
		$$("table.card-type a").each(function(el) {
			el.observe('click', function(event) {
				var card = this.id.gsub(/^button_/, '').gsub(/-radio$/, '');
				if (card == "solo" || card == "maestro") {
					$$(".solo-mastercard-options").each(function(el) { el.show(); });
				} else {
					$$(".solo-mastercard-options").each(function(el) { el.hide(); });
				}
				event.stop();
			})
		});
		if (hide) {
			$$(".solo-mastercard-options").each(function(el) { el.hide(); });
		}
	}
}


