//:mode=javascript:encoding=utf8:folding=explicit:collapseFolds=1:

document.documentElement.id = 'js';

function formError() {
	var msg = $('req-fields-info');
	if (msg.style.fontWeight == 'bold') {
		new Effect.Shake(msg, {
			duration: .2,
			pulses: 2,
			distance: 3
		})
	} else {
		$('req-fields-info').setStyle({'fontWeight': 'bold'});
	}
}

//{{{ glider
var glider = {

	currPos: 0,

	init: function() {
		var wrap = $('car-box-in2');
		if (wrap) {
			glider.cars = $$('#car-box .car');
			wrap.style.width = ($('car-box').offsetWidth * glider.cars.length) + 'px'
			$$('#car-box .car-box-button-js-only').each(function(button) {
				button.onclick = function() {
					glider.clicked(this);
					return false;
				};
				button.ondblclick = function() {
					return !Boolean(window.opera);
				}
			});
		}
	},

	clicked: function(button) {
		var target;

		if ($(button).hasClassName('next')) {

			target = (glider.cars.length - 1 == glider.currPos)
						? 0
						: glider.currPos + 1;
		} else {
			target = (glider.currPos == 0)
						? glider.cars.length - 1
						: glider.currPos - 1;

		}
		glider.currPos = target;
		glider.switchTo(target);
	},

	switchTo: function(pos) {
		target = glider.cars[pos];
		new Effect.InnerHorizontalScroll($('car-box-in'), $(target), {duration: .3});
		glider.onSwitch(pos);
	},

	onSwitch: function(pos) {
		// hook
	}

} //}}}

//{{{ gallery
var gallery = {

	wrap: null,
	active: null,
	duration: .5,

	init: function() {
		gallery.wrap = $$('.gallery')[0];
		if (gallery.wrap) {
			$$('.gallery p a').each(function(link, i) {
				if (i == 0) {
					$(link).addClassName('active');
				}
				link.onclick = function() {
					gallery.activate(this);
					return false;
				}
			});
		}
	},

	activate: function(link) {
		if (gallery.active) {
			gallery.active.removeClassName('active');
		}
		var img = gallery.wrap.getElementsByTagName('img')[0];
		gallery.active = link;
		gallery.active.addClassName('active');
		new Effect.Fade(img, {
			duration: gallery.duration,
			afterFinish: function() {
				img.onload = function() {
					new Effect.Appear(img, {duration: gallery.duration});
				}
				img.src = link.href;
			}
		});
	}

} //}}}

//{{{ testRide
var testRide = {
	init: function() {
		var select = $('car-model');
		if (select) {
			glider.switchTo(select.selectedIndex);
			select.onchange = function() {
				glider.switchTo(this.selectedIndex);
			}
			glider.onSwitch = function(pos) {
				select.selectedIndex = pos;
			}
			$('date').onfocus = function() {
				if (!testRide.dateInited) {
					testRide.dateInited = true;
					new scal('datepicker', 'date');
				}
				$('datepicker').show();
			}
			$('trial-race-form').onsubmit = testRide.validateForm;
		}
	},

	validateForm: function() {
		if (!$F('date') || !$F('kontakt')) {
			formError();
			return false;
		}
	}
} //}}}

//{{{ xlightbox
var xlightbox = {

	images: [],
	box: null,
	content: null,
	close: null,

	init: function() {
		$$('a[rel ~= xlightbox]').each(function(a) {
			a.onclick = function() {
				xlightbox.action(a);
				return false;
			}
		});
	},

	action: function(a) {
		xlightbox.iniOverlay();

		if (/jpe?g|png|gif/i.test(a.href)) {
			xlightbox.initUi();
			img = new Image();
			img.src = src;
			xlightbox.images[src] = $(img);
			xlightbox.content.innerHTML = '<img src="' + a.href + '" alt="" />';
			xlightbox.show();
		} else {
			new Ajax.Request(a.href + '?back=' + window.location.href, {
				onSuccess: function (transport) {
					xlightbox.initUi();
					xlightbox.content.innerHTML = transport.responseText;
					xlightbox.show();
					var form = xlightbox.content.down('form');
					if (form) {
						formSender.init(form);
					}
				},
				onFaluire: function() {
					window.location = a.href;
				}
			});
		}
	},


	iniOverlay: function() {
		xlightbox.overlay = $(document.createElement('div'));
		xlightbox.overlay.setOpacity(.7);
		xlightbox.overlay.setAttribute('id', 'xlightbox-overlay');
		var size = getPageSize();
		xlightbox.overlay.style.width = size[0] + 'px';
		xlightbox.overlay.style.height = size[1] + 'px';
		xlightbox.overlay.show();
		document.body.appendChild(xlightbox.overlay);
	},


	initUi: function() {

		xlightbox.box = $(document.createElement('div'));
		xlightbox.box.setAttribute('id', 'xlightbox')

		xlightbox.content = $(xlightbox.box.appendChild(document.createElement('div')));
		xlightbox.content.setAttribute('id', 'xlightbox-content');

		xlightbox.close = $(xlightbox.box.appendChild(document.createElement('div')));
		xlightbox.close.setAttribute('id', 'xlightbox-close');
		xlightbox.close.innerHTML = 'x<span></span>';
		xlightbox.close.onclick = function() {
			xlightbox.box.remove();
			xlightbox.overlay.remove();
		}


		document.body.appendChild(xlightbox.box)
	},

	show: function() {
		var size = getPageSize();

		var scrolled = getPageScrolled();

		xlightbox.box.style.left = (((size[2] / 2) - (xlightbox.box.offsetWidth / 2)) + scrolled.x) + 'px';
		xlightbox.box.style.top = (((size[3] / 2) - (xlightbox.box.offsetHeight / 2)) + scrolled.y) + 'px';

		xlightbox.box.show()
		xlightbox.box.style.visibility = 'visible';
	}

} //}}}

//{{{ formSender()
var formSender = {

	init: function(form) {
		form.onsubmit = function() {
			return formSender.send($(this));
		}
	},

	send: function(form) {
		if (!validateContactForm()) {
			return false;
		}
		var loader = document.createElement('img');
		new Ajax.Request(form.action, {
			parameters: form.serialize(),
			onFailure: function() {
				form.submit();
			},
			onSuccess: function() {
				form.down('fieldset').innerHTML = '<h4 class="form-message">E-mail byl úspěšně odeslán.</h4><p class="form-message">Některý z našich prodejců vás bude obratem kontaktovat.</p>';
			}
		});
		return false;
	}

} //}}}

//{{{ getPageSize()

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}
//}}}

//{{{ getPageScrolled()
function getPageScrolled() {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return {x: x, y: y};
} //}}}


function validateContactForm() {
	if (!$F('jmeno') || !$F('email') || !$F('text')) {
		formError();
		return false;
	}
	return true;
}

Effect.InnerHorizontalScroll = function(scrollingElement, targetElement, options) {
        var options = options || { },
        from = $(scrollingElement).scrollLeft,
        to = $(targetElement).cumulativeOffset().left  - $(scrollingElement).cumulativeOffset().left;
        if (options.offset) {
			to += options.offset;
        }
        return new Effect.Tween(null, from, to, options,
			function(to) {
					$(scrollingElement).scrollLeft = to.round();
			}
        );
};


glider.init();
gallery.init();
testRide.init();
xlightbox.init();

// tipcars
function nOkno(dvT){ 
  window.open('http://www.TipCars.com/cz/daimlerchrysler-1435/?Lang=cz&dvT='+dvT,'WinTip','scrolling=yes,resizable=yes,status=yes,top=0,left=0');
}
