
var galleryselected = Array();

function galleryswitchto(gid,iid)
{
	if(iid == galleryselected[gid])
		return;
		
	// change preview selected
	$('#smg'+gid+'_'+galleryselected[gid]).removeClass('sel');
	$('#smg'+gid+'_'+iid).addClass('sel');
	
	$('#larg'+gid+'_'+galleryselected[gid]).hide(500);
	$('#larg'+gid+'_'+iid).show(500);
	
	galleryselected[gid] = iid;
}

var timeoutToMove = 1000;
	var maxHeightToMove = 3220; //px
	var currentPosition = 0; // px

// moving clients	
function moveclients()
{
	posunutie = Math.random()*100;
	posunutie = Math.round(posunutie);
	currentPosition = posunutie * 70;
	
	$('.clientsWindow').fadeTo(400,0.01,function(){
		$(this).css('background-position','0px -'+currentPosition+'px');										 
	}).fadeTo(400,1,function(){
		setTimeout("moveclients()",timeoutToMove);																					   
	});
	
	if(currentPosition < -10000)
	{
		currentPosition = 0;
	}
	
}

$(document).ready(function(){
						   
	// OTVARANIE a zatvaranie menu
	$('li a.open').click(function(){
		openn = this;
		id = $(this).attr('id');
		
		// skontrolujeme ci je polozka otvorena
		opened = $('#mc'+id).hasClass('opened');
		
		if(!opened)
		{
			$('#mc'+id).addClass('opened openedbyjs');
			coords = $('#mc'+id+' .menu_x_icon').css('background-position');
			coords = coords.split(' ');			
			$('#mc'+id+' .menu_x_icon').css('background-position',coords[0]+' 21px');
			$(openn).html('-');
			$('#mc'+id+' ul').delay(100).slideDown(500,function(){					
					
				});
		}
		else
		{
			$('#mc'+id+' ul').slideUp(1000,function(){
					coords = $('#mc'+id+' .menu_x_icon').css('background-position');
					coords = coords.split(' ');					
					$('#mc'+id+' .menu_x_icon').css('background-position',coords[0]+' 0px');
					$('#mc'+id).removeClass('opened openedbyjs');
					$(openn).html('+');
				});
		}
		
	});
						   
    $(document).pngFix(); 
		
	// setting timeout for moving clients
	setTimeout("moveclients()",timeoutToMove);
	
	// create spam protection of mail	
	$('.contacts').attr('href','mailto:'+m);						   
	
	//main manu hoveration
	$(".headermenu li").hover(
	  function () {
		$(this).addClass('headermenuover');
	  }, 
	  function () {
		$(this).removeClass('headermenuover');
	  }
	);
	
	//search input 	
	$('[name=query]').attr('autocomplete','off');
	
	$('[name=query]').focus(function() {
			if($(this).val() == websearchdefault)
				$(this).val('');
		});
	
	$('[name=query]').blur(function() {
			if($(this).val() == '')
				$(this).val(websearchdefault);
		});
	
	// formulere povinne polia
	
	var requiredfocused = Array;
	
	$('.required').focus(function(){		
		if(requiredfocused[$(this).attr('name')] == undefined)
		{				
			requiredfocused[$(this).attr('name')] = $(this).attr('value');
		}
	});
	
	$('.required').blur(function(){
		value = $(this).attr('value');
		value = trim(value);
		
		if(value == '')
			$(this).attr('value',textinputfocused[$(this).attr('name')]);
		
		value = $(this).attr('value');
		value = trim(value);
		
		if(value == '' || value == requiredfocused[$(this).attr('name')])
		{
			$(this).addClass('errored');
		}
		else
			$(this).removeClass('errored');		
	});
	
	// formulare zmena farby texty po vyplneni a zmazanie texty pri vybrani policka;
	
	var textinputfocused = Array;
	
	$('.textinput').focus(function(){						
		if(textinputfocused[$(this).attr('name')] == undefined)
		{			
			textinputfocused[$(this).attr('name')] = $(this).attr('value');
		}
		
		value = $(this).attr('value');
		value = trim(value);
		
		if(value == textinputfocused[$(this).attr('name')])
			$(this).attr('value','');
		
		$(this).addClass('filled');
	});
	
	$('.textinput').blur(function(){
		
		value = $(this).attr('value');
		value = trim(value);
		
		if(value == '')
			$(this).attr('value',textinputfocused[$(this).attr('name')]);
		
		value = $(this).attr('value');
		value = trim(value);
				
		if(value != '' && value != textinputfocused[$(this).attr('name')])
			$(this).addClass('filled');
		else
			$(this).removeClass('filled');
				
	});
	
	var doruceniehidden = true;
	$('[name=dodaciaadresarovnaka]').change(function(){
		if(!$('[name=dodaciaadresarovnaka]').attr('checked'))
		{
			if(doruceniehidden)
			{
				// show dorucenie
				$('#dodaciaadresarovnaka').slideDown('slow',function(){ doruceniehidden = false;  })
			}			
		}
		else
		{
			if(!doruceniehidden)
			{
				// show dorucenie
				$('#dodaciaadresarovnaka').slideUp('slow',function(){ doruceniehidden = true;  })
			}	
		}
	});
	
	Shadowbox.init();
	
	$('.topoffermover').mouseover(function(){		
		clearInterval(topofferinterval);
	});
	
	$('.topoffermover').mouseout(function(){		
		topofferinterval = setInterval('topofferswitchtonext()',4000);
	});
	
	$('.image').mouseover(function(){		
		clearInterval(topofferinterval);
	});
	
	$('.image').mouseout(function(){		
		topofferinterval = setInterval('topofferswitchtonext()',4000);
	});
	
});

function produkt_cennik_rekalkulate(id,min_odber,cenaza,ceny,taby,balenie,zlava)
{
	//ceny = 'cena1,cena2,cena3,cena4;cena1,cena2,cena3,cena4';
	//taby = '5000,10000,15000,20000';
	
	taby = taby.split(',');
	ceny = ceny.split(';');
	balenie = parseInt(balenie);	
	for(var i in ceny)
	{
		txt = new String(ceny[i]);
		ceny[i] = txt.split(',');
	}
	
	cenatyp = $('#cenatyp'+id).attr('value');
	kusy = $('#kusy'+id).attr('value');
	
	if(kusy < min_odber)
		kusy = min_odber;
	
	if(kusy % balenie != 0)
	{
		zvysok = kusy % balenie;
		div = (kusy - (kusy % balenie)) / balenie;
		//alert('zvysok:'+zvysok+'div:'+div+'balenie:'+balenie);
		
		if(zvysok >= balenie/2)
		{
			kusy = div*balenie + balenie;
		}
		else
		{
			kusy = div*balenie;
		}		
	}
	
	vypocet = kusy/cenaza;
	
	cena = 0;
	// zistime ktoru cenu pouzije
	for(var i=0;i<4;i++)
	{		
		kusy = parseInt(kusy);
		taby[i] = parseInt(taby[i]);
		
		if(kusy >= taby[i] && taby[i] > 0)
		{
			cena = ceny[cenatyp-1][i];
		}
	}	
	vypocet = vypocet * cena;
	
	if(zlava > 0)
	{
		vypocet = vypocet - ((vypocet / 100) * zlava);
	}
	vypocet = Math.round(vypocet*100)/100;
	vypocet = vypocet.toFixed(2);
	vypocet = addCommas(vypocet);
	//vypocet = vypocet.replace('.',',');
	
	$('#cena'+id).html(vypocet);	
}

function cenajed_change(id,min_odber,cenaza,ceny,taby,balenie,zlava)
{
	//ceny = 'cena1,cena2,cena3,cena4;cena1,cena2,cena3,cena4';
	//taby = '5000,10000,15000,20000';
	
	taby = taby.split(',');
	ceny = ceny.split(';');
	balenie = parseInt(balenie);	
	for(var i in ceny)
	{
		txt = new String(ceny[i]);
		ceny[i] = txt.split(',');
	}
	
	cenatyp = $('#cenatyp'+id).attr('value');
	kusy = $('#kusy'+id).attr('value');
	
	if(kusy < min_odber)
		kusy = min_odber;
	
	if(kusy % balenie != 0)
	{
		zvysok = kusy % balenie;
		div = (kusy - (kusy % balenie)) / balenie;
		//alert('zvysok:'+zvysok+'div:'+div+'balenie:'+balenie);
		
		if(zvysok >= balenie/2)
		{
			kusy = div*balenie + balenie;
		}
		else
		{
			kusy = div*balenie;
		}		
	}
	
	vypocet = kusy/cenaza;
	
	cena = 0;
	// zistime ktoru cenu pouzije
	for(var i=0;i<4;i++)
	{		
		kusy = parseInt(kusy);
		taby[i] = parseInt(taby[i]);
		
		if(kusy >= taby[i])
		{
			cena = ceny[cenatyp-1][i];
		}
	}	
	cena = parseFloat(cena);
	cena = cena.toFixed(2);
	cena = addCommas(cena);		
	$('#cenajed'+id).html(cena);	
}

function produkt_check_min_odber(id,min_odber,balenie)
{
	kusy = $('#kusy'+id).attr('value');
	
	if(kusy % balenie != 0)
	{
		zvysok = kusy % balenie;
		div = (kusy - (kusy % balenie)) / balenie;
		//alert('zvysok:'+zvysok+'div:'+div+'balenie:'+balenie);
		
		if(zvysok >= balenie/2)
		{
			kusy = div*balenie + balenie;
		}
		else
		{
			kusy = div*balenie;
		}		
	}
	
	if(kusy < min_odber)
		kusy = min_odber;
	
	$('#kusy'+id).attr('value',kusy);
}

function price_type_set(type,id,min_odber,cenaza,ceny,taby,balenie,zlava)
{
	$('#cenatyp'+id).attr('value',type);
	produkt_cennik_rekalkulate(id,min_odber,cenaza,ceny,taby,balenie,zlava);
}

function price_type_amount_set(type,amount,id,min_odber,cenaza,ceny,taby,balenie,zlava)
{
	$('#cenatyp'+id).attr('value',type);
	$('#kusy'+id).attr('value',amount);
	produkt_cennik_rekalkulate(id,min_odber,cenaza,ceny,taby,balenie,zlava);
}

function topofferswitchtonext()
{
	if(topimgsel == 0)
		return;
		
	var next = topimgsel;
	// najdeme nasledujuce	
	for(var i in topofferss)
	{		
		i = parseInt(i);
		if(topofferss[i] == topimgsel)
		{			
			if(i == topofferss.length-1)
			{
				next = topofferss[0];
				break;
			}
			else			
			{
				x = i+1;				
				next = topofferss[x];
				break;
			}
			
		}
	}
	
	if(next == 0)
		next == topofferss[0];
	
	//alert(topimgsel+'|'+next+'|'+topofferss.length);
		
	topofferswitchto(next);
	
}

var animationends = true;

function topofferswitchto(to)
{
	if(!animationends)
		return;
		
	animationends = false;
	$('#tab_'+topimgsel).removeClass('opnd');
	$('#tab_'+to).addClass('opnd');
	
	$('#img'+topimgsel).fadeOut(500);
	$('#img'+to).fadeIn(500,function(){ animationends = true; });	
	
	topimgsel = to;
}

function switch_order_cenapopis(id,popis)
{
	popis = popis.split(';');
	cenatyp = $('#cenatyp'+id).attr('value');

	$('#cenapopis'+id).html(popis[cenatyp-1]);
}

function values_change()
{
	setTimeout('sumchange()',200);
}

function sumchange()
{	
	var summary = 0;
	
	$('.ordercena').each(function(){
		cena = $(this).html();
		cena = cena.replace("&nbsp;","");
		cena = parseFloat(cena);
		summary = summary + cena;
	});		
	
	summary = Math.round(summary*100)/100;
	summary = summary.toFixed(2);
	sum = summary;
	summary = addCommas(summary);
	$('#cena_summ_noVAT').html(summary);
	
	summary = sum;
	
	summary = Math.round(summary*100*1.19)/100;
	summary = summary.toFixed(2);
	summary = addCommas(summary);
	$('#cena_summ_VAT').html(summary);			
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '&nbsp;' + '$2');
	}
	return x1 + x2;
}

function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}



