// www.potentus.com
 
var URL_HOME = 'http://www.zidle-nabytek.cz/';
//var URL_HOME = 'http://tst.zidle-nabytek.cz/';
//var URL_HOME = 'http://server.idn/zidle-nabytek.cz/';

function toggle_menu(menu_id){
	if(Element.hasClassName('cat'+menu_id, 'closed')==true){
		var remove_class = 'closed';
		var add_class = 'opened';
	}
	else{
		var remove_class = 'opened';
		var add_class = 'closed';		
	}
	Element.removeClassName('cat'+menu_id, remove_class);
	$('cat'+menu_id).addClassName(add_class);
	Effect.toggle('submenu'+menu_id, 'slide', {duration: 0.3});
}

function add_to_basket(prod_link, form_id){
	//alert(prod_link);
	if(form_id=='add_one') var quantity = 1;
	else{
		var form = $(form_id);
		var input = form['amount'];
		var quantity = Form.Element.getValue(input);
	}
	show_worker();
	//new Ajax.Updater('basket_content', 'http://server.idn/zidle-nabytek.cz/cart.php?link='+prod_link+'&quantity='+quantity, {asynchronous:true, onLoading:function(request){Effect.Appear('basket_loader', {from: 0, to: 0.7, duration: 0.3})}, onComplete:function(request){Element.hide('basket_loader', {queue: 'front'}),new Effect.Highlight('basket', {queue: 'end', startcolor:'#6edcfe', endcolor:'#fdfcec'}),new Effect.ScrollTo('basket', {offset: -24})}});
	new Ajax.Updater('basket_content', 
	                   URL_HOME+'cart.php?link='+prod_link+'&quantity='+quantity, 
	                   {asynchronous:true, 
	                   onLoading:function(request){
	                           Effect.Appear('basket_loader', {from: 0, to: 0.7, duration: 0.3})
	                   }, 
	                   onComplete:function(request){
	                           Element.hide('basket_loader', {queue: 'front'}),
	                           Element.update('worker', '<b>Produkt byl vložen do košíku</b>') ,
	                           new Effect.Highlight('basket', {queue: 'end', startcolor:'#6edcfe', endcolor:'#fdfcec'})
	                   }});
	setTimeout("hide_worker()",3000);
	//new Ajax.Updater('basket_content', 'http://server.idn/zidle-nabytek.cz/_basket_add.php?link='+prod_link, {asynchronous:true, onComplete:function(request){after_insertion()}});
	//Effect.Appear('basket_loader', {from: 0.7, to: 0, duration: 0.3, queue: 'front'})
}
function add_config(prod_link){
    var check_bad_combination = $F('bad_combination');
    //alert(check_bad_combination);
    if(check_bad_combination==1){
        alert('Zvolena nepovolená kombinace potahů!');
        return false;
    }
    var serialized_form = Form.serialize('go-config');
    var serialized_quantity = Form.serialize('left_form');
    serialized_form = serialized_form+'&'+serialized_quantity;
    //alert(serialized_quantity);
    show_worker();
	//new Ajax.Updater('basket_content', 'http://server.idn/zidle-nabytek.cz/cart.php?link='+prod_link+'&quantity=1', {asynchronous:true, onLoading:function(request){Effect.Appear('basket_loader', {from: 0, to: 0.7, duration: 0.3})}, onComplete:function(request){Element.hide('basket_loader', {queue: 'front'}),new Effect.Highlight('basket', {queue: 'end', startcolor:'#6edcfe', endcolor:'#fdfcec'}),new Effect.ScrollTo('basket', {offset: -24, queue: 'front'}), Element.update('worker', '<b>Konfigurace byla vložena do košíku</b>');}, postBody: serialized_form});
	new Ajax.Updater('basket_content', 
	                   URL_HOME+'cart.php?link='+prod_link+'&config=1', 
	                   {asynchronous:true, 
	                   onLoading:function(request){
	                       Effect.Appear('basket_loader', {from: 0, to: 0.7, duration: 0.3})
	                   }, 
	                   onComplete:function(request){
	                       Element.hide('basket_loader', {queue: 'front'}),
	                       new Effect.Highlight('basket', {queue: 'end', startcolor:'#6edcfe', endcolor:'#fdfcec'}),
	                       Element.update('worker', '<b>Konfigurace byla vložena do košíku</b>');
	                   }, 
	                   postBody: serialized_form
	                   });
	setTimeout("hide_worker()",3000);
}
function after_insertion(){
	Element.setStyle('last_list', {display:'block'} );
	new Effect.Highlight('basket', {queue: 'front', startcolor:'#6edcfe', endcolor:'#fdfcec'});
	new Ajax.Updater('last_list', 
	                   URL_HOME+'_last_3.php?move=true', 
	                   {asynchronous:false, 
	                   onComplete:function(request){
	                       new Effect.Move('list_mover', {y: 16, queue: 'end'})
	                   }});
	
}

function empty_basket(){
	new Ajax.Updater('basket_content', 
	                   URL_HOME+'_basket_add.php?clear=true', 
	                   {asynchronous:false, 
	                   onComplete:function(request){
	                       new Effect.Highlight('basket', {queue: 'end', startcolor:'#6edcfe', endcolor:'#fdfcec'})
	                   }});
	Element.setStyle('last_list', {display:'none'} );
}

function add_porovnat(id, url){
	show_worker();
	new Ajax.Updater('worker', 
	                   url, 
	                   {asynchronous:true, 
	                   onComplete:function(request){
	                       recount_compared(), 
	                       Element.hide('porovnat_'+id), 
	                       Element.show('odebrat_'+id)
	                   }});
	recount_compared();
	setTimeout("hide_worker()",3000);
}
function remove_porovnat(id, url){
	show_worker();
	new Ajax.Updater('worker', 
	                   url, 
	                   {asynchronous:true, 
	                   onComplete:function(request){
	                       recount_compared(), 
	                       Element.hide(id+'_odebrat'), 
	                       Element.show(id+'_porovnat')
	                   }});
	setTimeout("hide_worker()",3000);
}
function hide_worker(){
	Effect.Appear('worker', { queue: 'front', duration: 0.3, from: 0.7, to:0 });
}
function show_worker(){
    $('worker').update('<img src="'+URL_HOME+'imgs/worker.gif" alt="working" />');
	Effect.Appear('worker', { queue: 'front', duration: 0.3, to:0.7 });
}
function recount_compared(){
	//alert(URL_HOME);
	new Ajax.Updater('compare_recount', URL_HOME+'_recount_compared.php', {asynchronous:true});
}
function empty_compare(){
	new Ajax.Updater('compare_recount', URL_HOME+'_recount_compared.php?empty=true', {asynchronous:true});
}

function check_string(obj){
	var val = $F(obj);
	new Ajax.Updater('worker', '_validate_reg.php?val='+val, {asynchronous:true});
}

function hide_params(){
	Element.hide('params_list');
	Element.hide('hide_params');
	Element.show('show_params');
	Element.hide('show_hide_pramas_txt');
	Element.show('show_show_pramas_txt');
}
function show_params(){
	Element.show('params_list');
	Element.hide('show_params');
	Element.show('hide_params');
	Element.show('show_hide_pramas_txt');
	Element.hide('show_show_pramas_txt');
}

function check_color(div_id, var_name, url_home){
	var reset_fields = $('go-config').getElementsBySelector('[label="color_'+var_name+'"]');
	for (var n = 0; n < reset_fields.length; n++){
		$(reset_fields[n]).update('');
		//alert(reset_fields[n]);
	}
	//alert(reset_fields);
	Element.update('color_'+var_name+'_'+div_id, '<img src="'+url_home+'imgs/ico-tick.gif" alt="ticked" />');
}

function go_config(base_price, pure_price){
	var serialized_form = Form.serialize('go-config');
	//alert(serialized_form);
	new Ajax.Updater('conf_list', 
	                   URL_HOME+'_config_change.php?base_price='+base_price+'&base_price_pure='+pure_price, 
	                   {asynchronous:true, 
	                   onComplete:function(request){
	                       Element.hide('config_loader')
	                   }, 
	                   onLoading:function(request){
	                       Element.show('config_loader')
	                   }, 
	                   postBody: serialized_form
	                   });
	new Ajax.Updater('detail_total_price', 
	                   URL_HOME+'_config_change.php?base_price='+base_price+'&just_total=true', 
	                   {asynchronous:true, 
	                   postBody: serialized_form
	                   });
	new Ajax.Updater('detail_total_price_small_left', 
            URL_HOME+'_config_change.php?base_price='+base_price+'&just_total_pure=true'+'&base_price_pure='+pure_price, 
            {asynchronous:true, 
            postBody: serialized_form
            });
	new Ajax.Updater('detail_add_price', 
	                   URL_HOME+'_config_change.php?base_price='+base_price+'&just_add=true', 
	                   {asynchronous:true, 
	                   postBody: serialized_form
	                   });
}

function init_config_new(base_price, pure_price){
    var serialized_form = Form.serialize('go-config');
	//alert(serialized_form);
	new Ajax.Updater('conf_list', 
	                   URL_HOME+'_config_change.php?base_price='+base_price+'&base_price_pure='+pure_price, 
	                   {asynchronous:true,
	                   postBody: serialized_form
	                   });
}

function scroll_observer(){	 
	 window.onscroll = function (e) {
	 	check_config_position();
	 }
}

function check_config_position(){
	 	var holder = $('config_top_border');
	 	var holder_bot = $('forum_label');
	 	var config = $('config');
	 	//alert(holder);
	 	var offsets = Position.cumulativeOffset(holder);
	 	var offsets_bot = Position.cumulativeOffset(holder_bot);
	 	var page = Position.page(config);
	 	var anc_offsets = Position.positionedOffset(config);
	 	var new_y_pos = (document.body.scrollTop-offsets[1]) + 10;
	 	if(((offsets_bot[1]-offsets[1])-400)>new_y_pos){
	 	if(new_y_pos<0) new_y_pos = 0;
	 	//alert(anc_offsets+new_y_pos);
	 	//alert(document.body.scrollTop);
	 	if(page[1]<10){
	 		$(config).setStyle({
			  top: new_y_pos+'px',
			  position: 'absolute'
			});
	 	}
	 		/*
	 		$(config).setStyle({
			  top: new_y_pos+'px',
			  position: 'absolute'
			});
			*/
	 		//new Effect.Move (config,{ y: new_y_pos, mode: 'absolute'});
	 	if(page[1]>10 && anc_offsets[1]>20 ){
	 		$(config).setStyle({
			  top: new_y_pos+'px',
			  position: 'absolute'
			});
	 	}
	 		/*
	 		$(config).setStyle({
			  top: new_y_pos+'px',
			  position: 'absolute'
			});
			*/
	 		//new Effect.Move (config,{ y: new_y_pos, mode: 'absolute'});
	 	}
}


function start_move_observer(title){
	show_thumb(title);
	//alert(Event.pointerX());
	var IE = document.all?true:false
	document.onmousemove = function (e){
	    //alert(event.clientX + document.body.scrollLeft);
	    if (IE) { // grab the x-y pos.s if browser is IE
            tempX = event.clientX + document.body.scrollLeft
            tempY = event.clientY + document.body.scrollTop
            move_thumb(tempX, tempY);
        } else {
		    move_thumb(Event.pointerX(e), Event.pointerY(e));
        }
	}
}
function stop_move_observer(){
	var thumb = $('thumb');
	Element.hide(thumb);
	onmousemove = null;
}
function show_thumb(title){
	// create an image object
	objImage = new Image();
    // set what happens once the image has loaded objImage.onLoad=imagesLoaded();
    objImage.onLoad=switch_thumb(title);
    // preload the image file
    objImage.src=title;
}
function move_thumb(x, y){
	var thumb = $('thumb');
	$(thumb).setStyle({
			  top: y+20+'px',
			  left: x+20+'px'
			});
}
function switch_thumb(src){
    var thumb = $('thumb');
    Element.update(thumb, '<img src="'+src+'" alt="náhled" />')
    Element.show(thumb);
}


function initUpScroll(){

    if(document.body.scrollTop>250 && !$('up_link').visible()){
        Effect.toggle('up_link', 'appear', { duration: 1.0 } );
        displayed = true;
    }
    if(document.body.scrollTop<250 && $('up_link').visible()){
        Effect.toggle('up_link', 'appear', { duration: 1.0 } );
        displayed = 1;
        //alert('hide');
    }
    
}
//Event.observe(window, 'scroll', initUpScroll, false);

function changeAmounts(){
	//alert(this.value);
	//alert($F('amountSide'));
	$('amount').value = $F('amountSide');
}
      
/*
function show_thumb(object){
	onmouseover = function (e){
		start_move_observer();
	}
	onmouseout = function (e){
		stop_move_observer();
	}
}
*/

/*
function refresh_promos(){
	new Ajax.PeriodicalUpdater('promotion', '_akce.php', {asynchronous:true, frequency:10, onLoading:function(request){Effect.Appear('promotion', {from: 1, to: 0, queue: 'front'})}, onSuccess:function(request){Effect.Appear('promotion', {from: 0, to: 1, queue: 'end'})}});
}
*/
