/** 
 *  czyszczenie herbaty (PIERWSZY KROK)
 *  
 *  @param int id numer hebraty
 *  @return string
 */

function clearTea(id)
{
	var text = "<div class='container' ><div class='plate'><img src='image/configurator/small_plate.png' border='0' />	</div><div class='name'></div></div><input type='hidden' id='teaPrice' value='0' />";
	jQuery('#teaTo').html(text);

	nextPrev();
}
/** 
 *  czyszczenie smaku (DRUGI KROK)
 *  
 *  @param int id numer smaku
 *  @return string
 */
function clearTaste(id)
{
	jQuery.post(
		urlTasteRemove,
		{
			"id":id
		},
		function(data)
		{
			
			if(data[1]=="false")
			{
				jQuery(".tasteTo").html(data[0]);
				
				alert(noMoreTaste);
				
				
				return false;
			}
			else
			{
				jQuery(".tasteTo").html(data[0]);
				
				setPrice();
			}
			
		},
		'json'
		
	)
}
/** 
 *  czyszczenie dodatku (TRZECI KROK)
 *  
 *  @param int id numer dodatku
 *  @return bool
 */
function clearBonus(id)
{
	jQuery.post(
		urlBonusRemove,
		{
			"id":id
		},
		function(data)
		{
			if(data[1]=="false")
			{
				jQuery(".bonusTo").html(data[0]);
				
				alert(noMoreBonus);
				
				
				return false;
			}
			else
			{
				jQuery(".bonusTo").html(data[0]);
				
				setPrice();
			}
			
		},
		'json'
		
	)
}
/** 
 *  ustawienie ceny
 *  
 *  @param int quantity wartość wpisana w pole input
 *  @return string
 */
function setPrice(quantity)
{
	var teaPrice = parseFloat(jQuery('#teaPrice').val());
	
	var tastePrice1 = parseFloat(jQuery('#tastePrice0').val());
	var tastePrice2 = parseFloat(jQuery('#tastePrice1').val());
	
	var bonusPrice0 = parseFloat(jQuery('#bonusPrice0').val());
	var bonusPrice1 = parseFloat(jQuery('#bonusPrice1').val());
	var bonusPrice2 = parseFloat(jQuery('#bonusPrice2').val());
	
	var packetPrice = parseFloat(jQuery("input[name='box_conf']:checked").val());

	var quantity = jQuery("#quantity").val();
	
	var price = teaPrice + tastePrice1 + tastePrice2 + bonusPrice0 + bonusPrice1 + bonusPrice2 + packetPrice;
	
	if(quantity!=null)
	{
		price = price * quantity;
	}
	
	price = price.toFixed(2);
	
	jQuery("#price").val(price);
	
	jQuery('#priceDiv').html(price + ' zł');
}


/** 
 *  nastepny poprzeni krok
 *  
 *  @param int quantity wartość wpisana w pole input
 *  @return string
 */
function nextPrev(elem)
{
	var teaPrice = parseInt(jQuery("#teaPrice").val());
	
	var next = jQuery("#next");
	var prev = jQuery("#prev");

	var imgNext = jQuery("#img_next");
	var imgPrev = jQuery("#img_prev");

	var step_1_off = jQuery("#step_1_off");
	var step_1_on = jQuery("#step_1_on");
	
	var step = parseInt(jQuery("#step").val());
	
	if(elem=="next")
	{
		step = step + 1;
		jQuery("#step").val(step);
		
		show_hide(step);
		
	}
	else if(elem == "prev")
	{
		step = step - 1;
		jQuery("#step").val(step);
		
		show_hide(step);
	}
	
	if(step==1)
	{
		imgPrev.css("display","block");

                step_1_off.css("display","block");
                step_1_on.css("display","none");

	}
	else if(step==3)
	{
		imgNext.css("display","block");
		next.css("display","none");
		
		imgPrev.css("display","none");
		prev.css("display","block");
	}
	else
	{
		imgPrev.css("display","none");
		prev.css("display","block");
		
		imgNext.css("display","none");
		next.css("display","block");
	}
	
	
	if(teaPrice>0)
	{
		if(step==1)
		{
			imgPrev.css("display","block");
			prev.css("display","none");



		}
		imgNext.css("display","none");
		next.css("display","block");
		if(step>=4)
		{
			imgNext.css("display","none");
			next.css("display","none");
			imgPrev.css("display","none");
			prev.css("display","none");
		}
		
	}
	else
	{

                step_1_off.css("display","none");
                step_1_on.css("display","block");
		if(step<4)
		{
			imgNext.css("display","block");
			next.css("display","none");
		}
		
		show_hide(1);
	}
	
}



/** 
 *  idź do zakupów
 *  
 *  @return string
 */
function goToShopping()
{
	var tekst = jQuery("#teaName").val();
	var quantity = jQuery("#quantity").val();
	
	if(tekst=="" || quantity<=0)//ktoś nie wybrał nazwy w konfiguratorze
	{
   


            openprompt();

	}
	else
        {
            document.confgiForm.submit();
        }
	
}


function packetCost(cost,id,name)
{
	jQuery("#packetId").val(id);
	jQuery("#packetName").val(name);
	
	setPrice();
}


function openprompt(){

        var temp = {
                state0: {
                        html:textNoName,
                        buttons:[{title: 'Tak Chcę',value:true},{title: 'Nie Chcę'}],
                        focus: 1,
                        show : 'show',
                        submit:function(v,m,f){
                                if(!v)
                                {
                                    document.confgiForm.submit();
                                    return false;
                                }
                                else jQuery.prompt.goToState('state1','show');//go forward
                                return false;
                        }
                },
                state1: {
                        html:'Podaj nazwę: <input type="text" class="width11" id="tmpName" name="tmpName" />',
                        buttons: [{title: 'Akceptuj', value: true}],
                        show : 'show',
                        submit:function(v,m,f){

                                    if(jQuery("#tmpName").val()!="")
                                    {
                                        jQuery("#teaName").val(jQuery("#tmpName").val());
                                    }


                                    document.confgiForm.submit();
                                    return false;
                                
                        }
                }
        }

        jQuery.prompt(temp, {opacity: 0.7});

}

function addTea(id)
{var loader = "<img src='/image/gallery/ajax-loader.gif' style='margin-left:60px;margin-top:10px' />";
    jQuery(".teaTo").html(loader);
    //dodanie herbaty
    jQuery.post(
            urlTea,
            {
                    "id":id
            },
            function(data){
                    jQuery(".teaTo").html(data[0]);

                    setPrice();
                    nextPrev();

//jQuery(".teaFrom teaCont_" + id).html('<img border="0" src="/image/configurator/plate.png">');
            },
            'json'
    )
}


function addTaste(id)
{var loader = "<img src='/image/gallery/ajax-loader.gif' style='margin-left:60px;margin-top:10px' />";

    jQuery(".tasteTo").html(loader);
    //dodanie herbaty
    jQuery.post(
            urlTaste,
            {
                    "id":id
            },
            function(data){

                    if(data[1]=="false")
                    {
                            if(data[2]=="exists")
                            {
                                    jQuery(".tasteTo").html(data[0]);

                                    setPrice();
                                    alert(configTasteError);
                                    return false;
                            }
                            else
                            {
                                    jQuery(".tasteTo").html(data[0]);

                                    alert(noMoreTaste);
                                    return false;
                            }
                    }
                    else
                    {
                            jQuery(".tasteTo").html(data[0]);

                            setPrice();
                            return false;
                    }

            },
            'json'

    )

}


function addBonus(id)
{var loader = "<img src='/image/gallery/ajax-loader.gif' style='margin-left:60px;margin-top:10px' />";
    jQuery(".bonusTo").html(loader);
    //dodanie herbaty
    jQuery.post(
            urlBonus,
            {
                    "id":id
            },
            function(data){

                    if(data[1]=="false")
                    {
                            if(data[2]=="exists")
                            {
                                    jQuery(".bonusTo").html(data[0]);

                                    setPrice();
                                    alert(configBonusError);
                                    return false;
                            }
                            else
                            {
                                    jQuery(".bonusTo").html(data[0]);

                                    alert(noMoreBonus);
                                    return false;
                            }

                    }
                    else
                    {
                            jQuery(".bonusTo").html(data[0]);

                            setPrice();
                    }

            },
            'json'

    )
}


jQuery(document).ready(function(){


jQuery("img.tooltip_trigger").tooltip({position: "bottom center"});





       

	
	jQuery("#packetShopping").click(function(){
		goToShopping();
	})
	
	
	jQuery("#packetReturn").click(function(){
		nextPrev('prev');
	})
	
	
	
	
	jQuery("#step").val(1);
	
	var step = jQuery("#step").val();
	
		
	jQuery("#next").click(function(){
		//nextPrev('next');
	})
	
	jQuery("#prev").click(function(){
		nextPrev('prev');
	})
		
	/*
	*
	* akcja zmiany pomiedzy herbatą smakiem i dodatkiem
	*
	*/
	jQuery(".steps").find("a").each(function(index,i){
		jQuery(this).click(function(){
			show_hide(index+1);
			jQuery("#step").val(index+1);
			nextPrev();
		})
	})
	
	
	
	/** 
	 *  zmiana wartości ceny po zmianie wartości inputa
	 *  
	 *  @return bool
	 */
	jQuery("#quantity").keyup(function(){
		var quantity = jQuery(this).val();
		setPrice(quantity);
	})
		
	
	var loader = "<img src='/image/gallery/ajax-loader.gif' style='margin-left:60px;margin-top:10px' />";
			
			
	/**
	*
	* herbata
	*
	**/
	jQuery(".teaFrom img").draggable({
		helper:'clone',
		connectToSortable:'.mainConf0',
        scroll: true        
	});
	
            
	jQuery(".mainConf0").sortable({
	
		update : function(event, ui){
			jQuery(".teaTo").html(loader);
			
			var id = ui.item.attr('id');
			id = id.split("tea_")[1];
			jQuery(".mainConf0 #tea_" + id).css('display','none');
	        //jQuery("." + ui.item.attr('id')).html("");
	        
			//dodanie herbaty
			jQuery.post(
				urlTea,
				{
					"id":id
				},
				function(data){
					jQuery(".teaTo").html(data[0]);
					
					setPrice();
					nextPrev();
                    
                    //jQuery(".teaFrom teaCont_" + id).html('<img border="0" src="/image/configurator/plate.png">');
                    
				},
				'json'
				
			)
	      }
	});
		
	/**
	*
	* herbata - koniec
	*
	**/
	
	
		
	/**
	*
	* smak
	*
	**/
	
	jQuery(".tasteFrom img").draggable({
		helper:'clone',
		connectToSortable:'.mainConf1'
	});
	
	jQuery(".mainConf1").sortable({
	
		update : function(event, ui){
			
			
			var id = ui.item.attr('id');
			id = id.split("taste_")[1];
			jQuery(".mainConf1 #taste_" + id).css('display','none');
	        //jQuery("." + ui.item.attr('id')).html("");
	        
            if(id)
            {
                
			     jQuery(".tasteTo").html(loader);
    	        //dodanie herbaty
    			jQuery.post(
    				urlTaste,
    				{
    					"id":id
    				},
    				function(data){
    					
    					if(data[1]=="false")
    					{
    						if(data[2]=="exists")
    						{
    							jQuery(".tasteTo").html(data[0]);
    							
    							setPrice();
    							alert(configTasteError);
    							return false;
    						}
    						else
    						{
    							jQuery(".tasteTo").html(data[0]);
    							
    							alert(noMoreTaste);
    							return false;
    						}
    					}
    					else
    					{
    						jQuery(".tasteTo").html(data[0]);
    						
    						setPrice();
    						return false;
    					}
    					
    				},
    				'json'
    				
    			)
			}
			
			
	      }
	});	
	/**
	*
	* smak - koniec
	*
	**/
	
	
	
	/**
	*
	* dodatek
	*
	**/
	
	jQuery(".bonusFrom img").draggable({
		helper:'clone',
		connectToSortable:'.mainConf2'
	});
	
	jQuery(".mainConf2").sortable({
	
		update : function(event, ui){
			
			
			var id = ui.item.attr('id');
			id = id.split("bonus_")[1];
			jQuery(".mainConf2 #bonus_" + id).css('display','none');
	        //jQuery("." + ui.item.attr('id')).html("");
	        if(id)
            {
                
                jQuery(".bonusTo").html(loader);
                
    	        //dodanie herbaty
    			jQuery.post(
    				urlBonus,
    				{
    					"id":id
    				},
    				function(data){
    					
    					if(data[1]=="false")
    					{
    						if(data[2]=="exists")
    						{
    							jQuery(".bonusTo").html(data[0]);
    							
    							setPrice();
    							alert(configBonusError);
    							return false;
    						}
    						else
    						{
    							jQuery(".bonusTo").html(data[0]);
    							
    							alert(noMoreBonus);
    							return false;
    						}
    						
    					}
    					else
    					{
    						jQuery(".bonusTo").html(data[0]);
    						
    						setPrice();
    					}
    					
    				},
    				'json'
    				
    			)
			}
    			
			
	      }
	});	
	/**
	*
	* dodatek - koniec
	*
	**/



});




//skrypt wykorzystywany rowniez po stronie panelu
jQuery(document).ready(function(){
	
		
			jQuery('.show').click(function(){
				
				
				this.blur();
				
				var id = jQuery(this).attr('rel');
				
				if(jQuery('.teaElement').css('display') == "none")
				{
					jQuery('.show').html('ukryj szczegóły');
					jQuery('.teaElement').show();
					
				}
				else
				{
					jQuery('.show').html('pokaż szczegóły');
					jQuery('.teaElement').hide();	
					
				}
				
			})
});



//setTimeout(function(){

/*
//jQuery("#teaMainContainer");
var scrollingDiv =  document.getElementById('teaMainContainer');


if(scrollingDiv)
{
    var step = document.getElementById('step').value;
    var topMarker =  document.getElementById('minPosition');
    var positionOffsetTop = topMarker.offsetTop;
    var positionOffsetBottom = document.getElementById('maxPosition');



    var topPointer = positionOffsetTop + topMarker.offsetHeight;


    var bottom = positionOffsetBottom.offsetTop;


    var divHeight = document.getElementById('teaMainContainer').offsetTop;



    scroll_div_funct = function () {



    var step = document.getElementById('step').value;



    var bottompointer = positionOffsetBottom.offsetTop;

    scrollingDiv = document.getElementById('teaMainContainer');

   // var topPointer = scrollingDiv.data('top_pointer');
    //var bottompointer = scrollingDiv.data('bottom_pointer');
    //var divHeight = scrollingDiv.data('div_height');
    var scrollTop = document.viewport.getScrollOffsets()[1];


   //var step = jQuery("#step").val();

   var elem = document.getElementById('step_'+step);
   if(elem)
   {
        var bottompointertest = elem.offsetTop + elem.offsetHeight;


        bottompointer = bottompointertest;
   }

    var offset = bottompointer - topPointer - divHeight;

    var delta = bottompointer - topPointer;





    if(step<4)
    {
        //if (scrollTop > topPointer && scrollTop < delta - 100)
        if (scrollTop < delta)
        {
            if(scrollTop > topPointer )
            {
                var marginTop = scrollTop - topPointer;


                $('teaMainContainer').setStyle({marginTop:marginTop + 10 +'px'});
            }
            else if(scrollTop <  topPointer)
            {
                $('teaMainContainer').setStyle({marginTop: 0+'px'});
            }
        }
        else
        {
           if(offset>0)
           {
               $('teaMainContainer').setStyle({marginTop:bottompointer - topPointer - divHeight+'px'});

           }

        }
    }


    };
    Event.observe(window, 'scroll', scroll_div_funct);



}

    },1000)*/
