
var IE= null;
var ua = navigator.appName.toLowerCase();
if(ua.indexOf('explorer')> -1 ) {IE=true;}

var dragobj;
var rozdil;
var position;
var pmin;
var pmax;
var krok;

function m_down(event, object) {
	dragobj = object;

	document.getElementById('priceMax').style.zIndex = '10';
	document.getElementById('priceMin').style.zIndex = '10';

	dragobj.style.zIndex = "11";
	if(!dragobj.style.left) { dragobj.style.left = "-15px"; }

	dragobj.clickX = event.clientX;
	dragobj.clickY = event.clientY;

	dragobj.left = parseInt(dragobj.style.left);
	dragobj.top = parseInt(dragobj.style.top);

	if(IE) {
		document.attachEvent('onmousemove', m_move);
		document.attachEvent('onmouseup', m_up);
	} else {
		document.addEventListener('mousemove', m_move, true);
		document.addEventListener('mouseup', m_up, true);
	}

	rozdil = dragobj.clickX - dragobj.offsetLeft;
	return stopEvent(event);
}


function m_move(event) {
	var xmin1 = document.getElementById('priceMinMax').offsetLeft + -15 + rozdil;
	var xmax2 = document.getElementById('priceMinMax').offsetLeft + 130 + rozdil;

	if (dragobj == document.getElementById('priceMin'))
	{
		var xmax = document.getElementById('priceMax').offsetLeft - 1 + rozdil;
		if (event.clientX < xmax) xmax = event.clientX;
		if (xmax < xmin1) xmax = xmin1;
	} else if (dragobj == document.getElementById('priceMax')) {
		var xmax = document.getElementById('priceMin').offsetLeft + 1 + rozdil;
		if (event.clientX > xmax) xmax = event.clientX;
		if (xmax > xmax2) xmax = xmax2;
	}

	dragobj.ondragdrop(xmax-dragobj.clickX,event.clientY-dragobj.clickY, event);

	if (dragobj == document.getElementById('priceMin')) {
		position = dragobj.offsetLeft - document.getElementById('priceMinMax').offsetLeft;
		cena = (pmin + ((position + 15) * krok));
		tempcena = cena / 10;
		if (Math.round(tempcena) * 10 < pmin) {
			cena = pmin;
		} else {
			cena = Math.round(tempcena) * 10;
		}
		if (position == -15) { cena = pmin; }
		if (cena > pmax) { cena = pmax; }
		if (cena < pmin) { cena = pmin; }
		document.getElementById('priceMin').innerHTML = cena;
		document.getElementById('cena_od').value = cena;
	} else if (dragobj == document.getElementById('priceMax')) {
		position = dragobj.offsetLeft - document.getElementById('priceMinMax').offsetLeft;
		cena = (pmax - ((130 - position) * krok));
		tempcena = cena / 10;
		if (Math.round(tempcena) * 10 > pmax) {
			cena = pmax;
		} else {
			cena = Math.round(tempcena) * 10;
		}
		if (position == 130) { cena = pmax; }
		if (cena > pmax) { cena = pmax; }
		if (cena < pmin) { cena = pmin; }
		document.getElementById('priceMax').innerHTML = cena;
		document.getElementById('cena_do').value = cena;
	}

	// document.getElementById('obj_pos').innerHTML = position;

	return stopEvent(event);
}

function m_up(event) {
	// Ukonci sledovani mysi
	if(IE) {
		document.detachEvent('onmousemove', m_move);
		document.detachEvent('onmouseup', m_up);
		if(dragobj.ondragend) { dragobj.ondragend(event); }
	} else {
		document.removeEventListener('mousemove', m_move, true);
		document.removeEventListener('mouseup', m_up, true);
		if(dragobj.ondragend) { dragobj.ondragend(event); }
	}

	stopEvent(event);
}


// Prirad objektu moznost presunu
function startDrag(what) {
	what.onmousedown = function(e) {if(e) {event=e;} m_down(event,what);}
	what.drag = 1;
}

// Odeber objektu moznost presunu
function stopDrag(what) {
	what.onmousedown = null;
	what.drag = 0;
}

// Zastav sireni udalosti
function stopEvent(event) {
	if(IE) {
		event.cancelBubble = true;
		return false;
	} else {
		event.stopPropagation();
		event.preventDefault();
		return false;
	}
}

function changePage(my_lock) {
	var speed = 600;
	if (stop) return;
	if (my_lock != lock) {
		return;
	}

	var delay = 500;
	var activePageContent = $("#bestseller-body > div:not(.switch-off)");
	activePageContent.delay(delay).fadeOut('slow', function() {if (stop) $("#bestseller-body div").css("display", "block");});
	$("#bestseller-body > div").addClass("switch-off");
	
	if( activePageContent.next().length != 0) {
		activePageContent.next().first().delay(delay).fadeIn('slow', function() {if (stop) $("#bestseller-body div").css("display", "block");});
		activePageContent.next().first().removeClass("switch-off");
	} else {
		$("#bestseller-body div").first().delay(delay).fadeIn('slow', function() {if (stop) $("#bestseller-body div").css("display", "block");});
		$("#bestseller-body div").first().removeClass("switch-off");
	}
	if (stop) {
		$("#bestseller-body div").css("display", "block");
	} else {
		setTimeout('changePage(' + my_lock + ')', 5000);
	}
}
stop = false;
lock = 1;
$(document).ready(function(){
	$("#bestseller-body div.switch-off").css("display", "none");
	$("#bestseller-body > div").css("position", "absolute");
	$("#bestseller-body").css("height", "80px");
	if ($("#bestseller-body > div").length > 1) {
		setTimeout('changePage(' + lock + ')', 2000);

		$("#down-line").removeClass("invisible");

		$("#down-line").click(function(){
			if (!$("#down-line").hasClass("open")) {
				$("#bestseller-body > div").css("position", "relative");
				$("#bestseller-body div").css("display", "block");
				$("#bestseller-body").css("height", "");
				$("#down-line").addClass("open");
				stop = true;
			} else {
				$("#bestseller-body > div").css("position", "absolute");
				$("#bestseller-body  div.switch-off").css("display", "none");
				$("#bestseller-body").css("height", "80px");
				$("#down-line").removeClass("open");
				stop = false;

				setTimeout('changePage(' + (++lock) + ')', 2000);
			}
		});
	}
	
	$(".filter div h2 img, #dle-zarizeni h2 img, .help-window").hover(
		function(){
			var helpWindow = $(this).parent().parent().children().children(".help-window").get(0);
			//helpWindow.style.display = "inline";
			$(helpWindow).stop();
			$(helpWindow).css("opacity", "1.0");

			$(helpWindow).fadeIn(300);
		},
		function(){
			var helpWindow = $(this).parent().parent().children().children(".help-window").get(0);
			$(helpWindow).stop();
			$(helpWindow).fadeOut(300);
		}
	);

	$("#dalsi-parametry #open-close-parametry").click(function() {
		if ($(this).parent().children("#dalsi-parametry .parametr").hasClass("invisible")) {
			$(this).parent().children("#dalsi-parametry .parametr").removeClass("invisible");
			$("#open-close-parametry img").attr('src', '/img/arrow-down.png');
		} else {
			$(this).parent().children("#dalsi-parametry .parametr").addClass("invisible");
			$("#open-close-parametry img").attr('src', '/img/arrow-right.png');
		}
	});

	$("#brand input").change(function() {
		if ($(this).attr('checked')) {
			$("#filters form>div").append('<input type="hidden" name="' + $(this).attr("name") + '" value="' + $(this).val() +'"/>');
		} else {
			$('#filters [name^="vyrobce"][value="' + $(this).val() + '"]').remove();
		}
	});

	$("#filters input, #filters select").change(function() {
			$('#brand #fi [name="' + $(this).attr('name') + '"]').remove();

		if ($(this).attr('type') != 'checkbox' || $(this).attr('checked') == true) {
			$("#brand #fi").append('<input type="hidden" value="' + $(this).val() +'" name="' + $(this).attr("name") + '" />');
		}
	});

	/*
	$(".parametr").click(function() {
		if ($(this).children(".parametr-data").hasClass("invisible")) {
			$(this).children(".parametr-data").removeClass("invisible");
		} else {
			$(this).children(".parametr-data").addClass("invisible");
		}
	});
		*/

	$("#filter-form input, #filter-form select, #brand input").change(function() {
		var url = "/vypis/recount?" + $("#filter-form").serialize();
		$.getJSON(url,
		function(data){
			if (data != null) {
				$("#filter-form input[type=submit]").val("VYBRAT (" + data + ")");
				$("#brand input[type=submit]").val("VYBRAT (" + data + ")");
			}
		});
	});
});

