document.observe("dom:loaded", function() {
  
	var elements = $$(".add_to_basket");   
	elements.each(function(e) {         
	    	Event.observe(e, "click", submitAddToBasketForm);
	  }
	);
	
	var elements = $$(".increase_amount");   
	elements.each(function(e) {         
	    	Event.observe(e, "click", increaseAmount);
	  }
	);
	
	var elements = $$(".decrease_amount");   
	elements.each(function(e) {         
	    	Event.observe(e, "click", decreaseAmount);
	  }
	);
	
	var fyzicka = $('fyzicka');
	var pravnicka = $('pravnicka');
	if (fyzicka && pravnicka) {
		Event.observe(fyzicka, "click", switchRegistrationForm);  
		Event.observe(pravnicka, "click", switchRegistrationForm);
	}	
  
	if (typeof(registrationJS)!="undefined") {
		initRegistration();
	}	
	
	if(typeof(orderJS)!="undefined"){
		initOrder();
	}
	
	var zobrazine = $('zobrazine');	
	if (zobrazine) {
		Event.observe(zobrazine, "click", zobrazIneFunction);		
	}
	
	if ($('videoDiv')!=null) {
		flowplayer("a.flowPlayer", "/scripts/flowplayer/flowplayer-3.1.3.swf",
			{
			    clip:  {
			        autoPlay: false,
			        autoBuffering: false
			    },
			    plugins: {
				    controls: null
				}
			}
		);
	}
	
	if($('eshop-manager')!=null){
		var cnt = "<ul><li>poradenstvo k produktom</li><li>navigácia k e-shopu</li><li>telefonické objednávky</li><li>riešenie problémov</li></ul>";
		new Tip('eshop-manager', cnt, {
		  title: 'BEZPLATNÁ LINKA',
		  className: 'silver',
		  effect: 'appear'
		  //hook: { target: 'topRight', tip: 'bottomLeft'}
		});
	}
	if($('objednat_info')!=null){
		var cnt = "<ul><li>poradenstvo k produktom</li><li>navigácia k e-shopu</li><li>telefonické objednávky</li><li>riešenie problémov</li></ul>";
		new Tip('objednat_info', cnt, {
		  title: 'BEZPLATNÁ LINKA',
		  className: 'silver',
		  effect: 'appear'
		  //hook: { target: 'topRight', tip: 'bottomLeft'}
		});
	}
});

function submitAddToBasketForm(e) {
	var span = e.target.identify();
	var id = span.substr(7);	
	if (parseInt($('amount_'+id).value)==0) {
		alert('Musíte zadať množstvo tovaru');
		return false;
	}
	$('addToBasketForm_'+id).submit();
}

function increaseAmount(e) {
	var img = e.target.identify();
	var id = img.substr(5);	
	$('amount_'+id).value = parseInt($('amount_'+id).value) + 1;
}

function decreaseAmount(e) {
	var img = e.target.identify();
	var id = img.substr(6);	
	$('amount_'+id).value = Math.max(parseInt($('amount_'+id).value) - 1,0);
}

function switchRegistrationForm(e) {	
	if (e.target==$('fyzicka')) {
		$('companyRegistrationForm').hide();
		$('registrationForm').show();
	} else {
		$('companyRegistrationForm').show();
		$('registrationForm').hide();
	}	
}

function switchAdress(e){	
	if($('typeadress').value==-1){
		$('fieldset-adressGroup').show();
	}else{
		$('fieldset-adressGroup').hide();
	}
}

function zobrazIneFunction(){	
	$('othercategories').toggle();
	return false;
	/*$('footer').setStyle({
  		width: '995px',
		position: 'absolute',
		left: 0,
		bottom: 0,	
		background: 'rgb(225, 225, 225) url(../images/layout/bg_foot.jpg) no-repeat',	
		lineHeight: '50px',
		height: '50px',
		overflow: 'hidden'
	});*/			
}

function submitEnter(myfield,e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
	 	keycode = e.which;
	} else {
		return true;
	}
	
	if (keycode == 13) {
	   myfield.form.submit();
	   return false;
	}
	else {
	   return true;
	}
}

function getCategories(category1,category2) {
	category2 = category2 || null;
	var url = "/category/ajax-categories/category/"+category1;
	level = 2;
	if (category2!=null) {
		url+="-"+category2;
		level = 3;
	}
	var params = "";
	var ajax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {populateCategories(transport, level)},
			onFailure: function(request) {alert('Chyba získania kategórií!')}
		}
	);
}

function populateCategories(response, level) {	
	var element = $('category'+level);
	var categories = (response.responseText.length > 0 ? response.responseText.evalJSON() : null);
	var ids = Object.keys(categories);	
	var val = Object.values(categories);
	element.options.length = 0;	
	for(var i=0;i<ids.length;i++) {
		element.options[i] = new Option(val[i],ids[i]);		
	}
	element.options[ids.length] = new Option('Žiadne obmedzenie', 0);
	if (level==2) {
		getCategories($('category1').value,$('category2').value);
	}
}

function showTooltip(){
	new Tip('eshop-manager', 'This tooltip uses the &quot;blind&quot; effect.', {
	  title: 'more effects',
	  className: 'darktip',
	  effect: 'blind',
	  hook: { target: 'topRight', tip: 'bottomLeft'}
	});

}
