var operaA = false;
var startX;
var stopX;
var moveX;
var mouseDown;
var speed;
var tabLeft;
var direction = 0;
var old;
var tobasketinaction = true;
var _X = 0;
var _Y = 0;
var emailactive = false, opinionactive=false;
var polz, tab, wrap;

var retoldTimer, retoldmessageTimer, addopinionTimer;

var delay = 5000;

var corners = new Array (new Image(),new Image());
corners[0].src="/imgs/card/topdecor.png";
corners[1].src="/imgs/card/bottomdecor.png";

function mouseXY(e) {
	var x = 0, y = 0;

	if (!e) e = window.event;

	if (e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}
	return {"x": x, "y": y};
}

function initi() {

	polzWidth ();

	if ($('costdown1')) {
		costownspans = $('costdown1').getElementsByTagName('span');
		for (i = 0; i < costownspans.length; i++) {
			costownspans[i].onmouseover = function () {
				for (j = 0; j < costownspans.length; j++) {
					costownspans[j].style.backgroundImage = "url('/imgs/blank.gif')";
				}
			}
			costownspans[i].onmouseout = function () {
				for (j = 0; j < costownspans.length; j++) {
					costownspans[j].style.backgroundImage = "url('/imgs/dash2.gif')";
				}
			}
		}

		if (polz = $('polz')) {
			polz.style.left = 25 + "px";
		}
		if (tab = $('tab')) {
			tab.style.left = 0 + "px";
			tab.style.width = tab.offsetWidth + 'px';
			if (wrap = $('wrap')) {
				wrap.style.height = tab.offsetHeight + 10 + 'px';
				if (Prototype.Browser.IE) {
					wrap.style.height = parseInt(wrap.style.height) +10 + 'px';
				}
			}
		}

		$('costdown1').onclick = function () {
			$('costdown1').style.visibility = "hidden";
			//$('costdown2').style.visibility = "hidden";

			$('message').style.left = '-5px';
			$('message').style.top = '0px';
			$('topMessage').style.backgroundColor = grabColor(this.parentNode.parentNode.parentNode.className);
			$('midMessage').style.backgroundColor = grabColor(this.parentNode.parentNode.parentNode.className);
			$('bottom1').style.display = "none";
			$('bottom2').style.display = "none";
			$('bottom3').style.display = "none";
			$('bottom4').style.display = "none";
			$('message').style.display = 'block';

			clearTimeout(retoldmessageTimer);
			retoldmessageTimer = setTimeout("retoldmessage()",delay);

			return false;
		}

		if ($('polz')) {
			Event.observe($('polz'), 'mousedown', start, false);
			Event.observe($('polz'), 'mouseup', stop1, false);
			Event.observe($('polz'), 'mousemove', move, false);
			Event.observe($('polz'), 'mouseover', stopOb, false);
			Event.observe($('polz'), 'mouseout', stop2, false);
		}
		if ($('bar')) {
			Event.observe($('bar'), 'click', step, false);
		}
		if ($('lbt')) {
			Event.observe($('lbt'), 'click', leftbt, false);
			Event.observe($('lbt'), 'mouseover', stopOb, false);
			Event.observe($('lbt'), 'mouseout', startOb, false);
		}
		if ($('rbt')) {
			Event.observe($('rbt'), 'click', rightbt, false);
			Event.observe($('rbt'), 'mouseover', stopOb, false);
			Event.observe($('rbt'), 'mouseout', startOb, false);
		}
		if ($('email')) {
			Event.observe($('email'), 'focus', emailisactive);
			Event.observe($('email'), 'blur', emailnoactive);
		}
		Event.observe(window, 'resize', polzWidth);
		Event.observe(window, 'load', polzWidth, false);
		Event.observe(document, 'mousemove', _XY);
		//Event.observe(document, 'click', retoldall);
	}
	
	Event.observe(document, 'click', retoldall);
	/*$('costdown2').onclick = function () {
		$('costdown1').style.visibility = "hidden";
		$('costdown2').style.visibility = "hidden";
	
		$('message').style.left = '-5px';
		$('message').style.top = '0px';
		$('topMessage').style.backgroundColor = grabColor(this.parentNode.parentNode.parentNode.className);
		$('midMessage').style.backgroundColor = grabColor(this.parentNode.parentNode.parentNode.className);
		$('bottom1').style.display = "none";
		$('bottom2').style.display = "none";
		$('bottom3').style.display = "none";
		$('bottom4').style.display = "none";
		$('message').style.display = 'block';

		setTimeout("retoldmessage()", delay);

		return false;
	}*/

	if ($('opinion_name')) {
		Event.observe($('opinion_name'),'focus',opinionisactive);
		Event.observe($('opinion_name'),'blur',opinionnoactive);

		Event.observe($('opinion_text'),'focus',opinionisactive);
		Event.observe($('opinion_text'),'blur',opinionnoactive);

		Event.observe($('opinion_prot'),'focus',opinionisactive);
		Event.observe($('opinion_prot'),'blur',opinionnoactive);
	}

	tobaskets = document.getElementsByClassName('tobasket');
	for (i = 0; i < tobaskets.length; i++) {
		Event.observe(tobaskets[i], 'click', _XY);
		if (Prototype.Browser.Opera) {
			Event.observe(tobaskets[i].getElementsByTagName('a')[0], 'click', operaFix);
		}
		tobaskets[i].onclick = function (e) {
			if (tobasketinaction) {
				tobasketinaction = false;
				retold();
			}
			el = this;
			//el.style.visibility = "hidden";

			if (operaA == true) {
				_X -= 19;
				_Y -= 6;
			}

			$('card').style.left = _X + 9 + 'px';
			$('card').style.top = _Y - 1 + 'px';


			operaA = false;
			$('top').style.backgroundColor = grabColor(this.parentNode.className);
			$('mid').style.backgroundColor = grabColor(this.parentNode.className);
			if ($('bottom5')) $('bottom5').style.display = "none";
			if ($('bottom6')) $('bottom6').style.display = "none";
			if ($('bottom7')) $('bottom7').style.display = "none";
			if ($('bottom8')) $('bottom8').style.display = "none";
			$('card').style.display = "block";
			clearTimeout(retoldTimer);
			retoldTimer = setTimeout("retold()", delay);
			if (Prototype.Browser.IE) this.getElementsByTagName('div').item(0).getElementsByTagName('a').item(0).click();
			return false;
		}
	}
	
	if ($('addopinion')) {
		$('addopinion').onclick = function () {
			//$('topOpinion').style.backgroundColor=grabColor(this.className,2);
			//$('midOpinion').style.backgroundColor=grabColor(this.className,2);
			//$('opunionbutton').style.display = "none";
			$('opinion').style.display = "block";
		
			clearTimeout (addopinionTimer);
			addopinionTimer = setTimeout ("hideOpinion()", delay);
			return false;
		}
	}
}/*--init--*/

function _XY (e) {
	_X = mouseXY(e).x - parseInt(e.offsetX || e.layerX) - 3;
	_Y = mouseXY(e).y - parseInt(e.offsetY || e.layerY) - 3;
}
function operaFix() {
	operaA = true;
}
function retold(item) {
	//el.style.visibility = "visible";
	$('card').style.display = "none";
	tobasketinaction = true;
	if ($('bottom1')) $('bottom1').style.display = "none";
	if ($('bottom2')) $('bottom2').style.display = "none";
	if ($('bottom3')) $('bottom3').style.display = "none";
	if ($('bottom4')) $('bottom4').style.display = "none";
}

function retoldmessage() {
	if (emailactive == false) {
		$('costdown1').style.visibility = "visible";
		//$('costdown2').style.visibility = "visible";

		$('message').style.display = "none";
		$('bottom5').style.display = "none";
		$('bottom6').style.display = "none";
		$('bottom7').style.display = "none";
		$('bottom8').style.display = "none";
	} else {
		clearTimeout(retoldmessageTimer);
		retoldmessageTimer = setTimeout("retoldmessage()", delay);
	}
}

function retoldall(e) {
	//body.style.backgroudColor="yellow";
	//setTimeout("body.style.backgroudColor=\"#fff\";", 500);
	try {
		if (_event(e).parentNode.parentNode.id != "costdown1" && _event(e).parentNode.parentNode.id != "costdown2" && _event(e).parentNode.className != "tobasket" && _event(e).parentNode.parentNode.className != "tobasket") {

		try {
			retold();
		}
		catch (err) {
		}
		try {
			retoldmessage();
		}
		catch (err) {
		}
		}
	}
	catch (err) {
		//alert (err);
	}
}

function start(e) {
	startX = parseInt(e.offsetX || e.layerX);
	mouseDown = true;
	Event.stopObserving($('bar'), 'click', step);
}
function stopOb(e) {
	Event.stopObserving($('bar'), 'click', step);
}
function startOb(e) {
	Event.observe($('bar'), 'click', step, false);
}
function stop1(e) {
	mouseDown = false;
}
function stop2(e) {
	mouseDown = false;
	Event.observe($('bar'), 'click', step, false);
}

function move(e) {
	moveX = parseInt(e.offsetX || e.layerX);
	if (mouseDown) {
		$('polz').style.left = parseInt($('polz').style.left) + moveX - startX + 'px';
	}
	tabzz()
}

function polzWidth() {
	if (!$('polz')) return;
	//alert (parseInt($('wrap').getWidth()));
	//alert ( (parseInt($('tab').getWidth() < 1 )) );
	if ( (parseInt($('tab').getWidth()) / parseInt($('wrap').getWidth())) > 1 ) {
		$('polz').style.width = (parseInt($('wrap').getWidth()) - 50) / (parseInt($('tab').getWidth()) / parseInt($('wrap').getWidth())) + 'px';
		$('bar').style.display = "block";
	}
	else {
		$('bar').style.display = "none";
	}
}

function step(e) {
	var delta;
	var now = parseInt(e.offsetX || e.layerX);
	delta = now - parseInt($('polz').getStyle('left'));
	if (delta > 100) {
		tabLeft = parseInt($('tab').getStyle('left')) - parseInt($('wrap').getWidth()) + 150;
		speed = parseInt($('wrap').getWidth()) / 8;
		direction = 2;
		slow();
	}
	if (delta < 0) {
		tabLeft = parseInt($('tab').getStyle('left')) + parseInt($('wrap').getWidth()) - 150;
		speed = parseInt($('wrap').getWidth()) / 8;
		direction = 1;
		slow();
	}
}

function leftbt() {
	tabLeft = parseInt($('tab').getStyle('left')) + parseInt($('wrap').getWidth()) - 150;
	speed = parseInt($('wrap').getWidth()) / 8;
	direction = 1;
	slow();
}
function rightbt() {
	tabLeft = parseInt($('tab').getStyle('left')) - parseInt($('wrap').getWidth()) + 150;
	speed = parseInt($('wrap').getWidth()) / 8;
	direction = 2;
	slow();
}

function polzzz () {
	$('polz').style.left = -(parseInt($('bar').getWidth()) - parseInt($('polz').getWidth()) - 50) * parseInt($('tab').getStyle('left')) / (parseInt($('tab').getWidth()) - parseInt($('wrap').getWidth())) + 25 + 'px';
	norm();
}

function tabzz () {
	$('tab').style.left = -(parseInt($('tab').getWidth()) - parseInt($('wrap').getWidth())) * (parseInt($('polz').getStyle('left')) - 25) / (parseInt($('bar').getWidth()) - parseInt($('polz').getWidth()) - 50) + 'px';
	norm();
}

function norm () {
	if (parseInt($('polz').getStyle('left')) < 25) {
		$('polz').style.left = '25px';
		direction = 0;
	}
	if (parseInt($('polz').getStyle('left')) - 25 > ((parseInt($('bar').getWidth()) - parseInt($('polz').getWidth()) - 50))) {
		$('polz').style.left = (parseInt($('bar').getWidth()) - parseInt($('polz').getWidth()) - 50) + 25 + 'px';
		direction = 0;
	}
	if (direction == 0)
	$('tab').style.left = -(parseInt($('tab').getWidth()) - parseInt($('wrap').getWidth())) * (parseInt($('polz').getStyle('left')) - 25) / (parseInt($('bar').getWidth()) - parseInt($('polz').getWidth()) - 50) + 'px';
}

function slow () {
	switch (direction) {
		case 1:
			$('tab').style.left = parseInt($('tab').getStyle('left')) + parseInt(speed) + 1 + 'px';
			if ((speed - 7) > 1) speed -= 7;
			else speed = 1;
			if (parseInt($('tab').getStyle('left')) >= tabLeft) {
				direction = 0;
			}
			else setTimeout("slow ()", 50);
		break;
		case 2:
			$('tab').style.left = parseInt($('tab').getStyle('left')) - parseInt(speed) - 1 + 'px';
			if ((speed - 7) > 1) speed -= 7;
			else speed = 1;
			if (parseInt($('tab').getStyle('left')) <= tabLeft) {
				direction = 0;
			}
			else setTimeout("slow ()", 50);
		break;
	};
	polzzz ();
}

function grabColor(parentItem) {
	//alert(parentItem);
	if (parentItem.indexOf("maroon") >= 0) {
		if ($('bottom1')) $('bottom1').style.display = "block";
		if ($('bottom5')) $('bottom5').style.display = "block";
		return "#c06";
	}

	if (parentItem.indexOf("green") >= 0) {
		if ($('bottom2')) $('bottom2').style.display = "block";
		if ($('bottom6')) $('bottom6').style.display = "block";
		return "#4db848";
	}

	if (parentItem.indexOf("blue") >= 0) {
		if ($('bottom3')) $('bottom3').style.display = "block";
		if ($('bottom7')) $('bottom7').style.display = "block";
		return "#09f";
	}

	if (parentItem.indexOf("orange") >= 0) {
		if ($('bottom4')) $('bottom4').style.display = "block";
		if ($('bottom8')) $('bottom8').style.display = "block";
		return "#f60";
	}

	/*default color*/
	return "black";
}

function emailisactive () {
	emailactive = true;
}
function emailnoactive () {
	emailactive = false;
}
function _event (eve) {
	try {
		ev = eve.target;
	} catch (er) {
		try {
			eve = event;
			ev = eve.srcElement;
		}
		catch (er) {};
	}
	return ev;
}

Event.observe(window, 'load', initi, false);

function submitSelection() {
	form = $('selectionform');
	
	form.submit();
	
	return false;
}

function submitUplink() {
	form = $('uplinkform');
	form.submit();
	return false;
}

function showSalones() {
	region = $('region').value;
	sub = $('subs').value;
	if (region != '' && sub != '') {
		if (region.include('_')) {
			arr = region.split('_');
			window.location.href = '/salones/' + arr[0] + '/?sub=' + sub + '&type=' + arr[1];
		} else {
			window.location.href = '/salones/' + region + '/?sub=' + sub;
		}
	}
	return false;
}

function searchGuid() {
	subs = $('subs');
	guid = $('subs2').value;
	region = $('region').value;
	
	if (subs) {
		if (subs.value == '' || guid == '' || region == '') return false;
	} else {
		if (guid == '' || region == '') return false;
	}
	
	if (region.include('_')) {
		arr = region.split('_');
		window.location.href = '/salones/' + arr[0] + '/?' + (subs ? 'sub=' + subs.value + '&' : '') + 'type=' + arr[1] + '&guid=' + guid;
	} else {
		window.location.href = '/salones/' + region + '/?' + (subs ? 'sub=' + subs.value + '&' : '') + 'guid=' + guid;
	}
	return false;
}

function emailisactive () {
	emailactive = true;
}
function emailnoactive () {
	emailactive = false;
}
function opinionisactive () {
	opinionactive=true;
}
function opinionnoactive () {
	opinionactive=false;
}

function hideOpinion() {
	if (opinionactive==false) {
		//$('opunionbutton').style.display = "block";
		$('opinion').style.display = "none";
	}
	else {
		clearTimeout (addopinionTimer);
		addopinionTimer = setTimeout ("hideOpinion()", delay);
	}
}

function isEmail(string) {	
	return string.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
}
