/*
 * jQuery extend for www.pivni-chlazeni.cz
 *
 * Copyright (c) 2008 Lukas Svoboda (lotofidea s.r.o.)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.lotofidea.com
 *
 */
isInteger = function(s){
    return !isNaN(parseInt(s));
}
/**
 * Funkce pro aktualizaci kosiku
 */

function updateBasket(){
    $(function(){
        $('#Basket').fadeOut('fast');
        $.get("/inc/eshop.inc.php?show=1", function(data){
            var i;
            $("#Basket").find("em").each(function(i){
                $(this).html(data);
            });
        });
        $('#Basket').fadeIn('fast');
    });
}

$(document).ready(function(){
    /**
     * eshop - pridat do kosiku
     *
     */
    $('.buy').click(function(){
        var pocet;
        id_produkt = $(this).attr('href');
        id_produkt = id_produkt.replace(/#/, '');
        // pridani do kosiku
        $.get("/inc/eshop.inc.php?add=" + id_produkt, function(data){
            $('#Basket').fadeOut('fast');
			$("#Basket").find("span").each(function(i){
                    $(this).html(data);
                });
            $.get("/inc/eshop.inc.php?show=1", function(data){
                var i;
                $("#Basket").find("em").each(function(i){
                    $(this).html(data);
                });
            });
            $('#Basket').fadeIn('fast');		
        });
        
    });
    /**
     * eshop - odstraneni polozky z kosiku
     */
    $('.remove').click(function(){
        var idproduct = $(this).parent().children(".product").attr('value');
        var radek = $(this).parents('tr');
        $.get("/inc/eshop.inc.php?remove=" + idproduct, function(data){
            if (data == true) {
                $('#Basket').fadeOut('fast');
                $.get("/inc/eshop.inc.php?show=1&nolook=yes", function(data){
                    var i;
                    var cena = parseInt(data);
                    $("#Basket").find("em").each(function(i){
                        $(this).html(cena+',00');
                    });
                    $("#OrderTable2").find(".cena-bezdph").each(function(i){
                        $(this).html(cena+',00');
                    });
                    $("#OrderTable2").find(".cena-dph").each(function(i){
                        $(this).html(Math.round((cena * 1.19)-cena)+',00');
                    });
                    $("#OrderTable2").find(".cena-celkem").each(function(i){
                        $(this).html(Math.round(cena * 1.19)+',00');
                    });
                });
                $('#Basket').fadeIn('fast');
                radek.fadeOut('fast');
            }
            else {
                alert(data);
            }
        });
        
    });
    
    /**
     * Zmeni pocet polozek
     */
    $('.count').keyup(function(){
        var id_product = $(this).parents('tr').find('.product').attr('value');
        var value = $(this).attr('value');
        if (isInteger(value) && (value > 0)) {
            $.get("/inc/eshop.inc.php?setcount=" + id_product + '&count=' + value, function(data){
                if (data == true) {
                    $('#Basket').fadeOut('fast');
                    $.get("/inc/eshop.inc.php?show=1&nolook=yes", function(data){
                        var i;
						var recyklacnipoplatek=1.50;
						var recyklacecena=recyklacnipoplatek*value;
						
                        var cena = parseInt(data);
                        $("#Basket").find("em").each(function(i){
                            $(this).html(cena+',00');
                        });
						
                        $("#OrderTable2").find(".cena-bezdph").each(function(i){
                            $(this).html(cena+',00');
                        });
                        $("#OrderTable2").find(".cena-dph").each(function(i){
                            $(this).html(Math.round((cena * 1.19)- cena)+',00');
                        });
                        $("#OrderTable2").find(".cena-celkem").each(function(i){
                            $(this).html(Math.round(cena * 1.19)+',00');
                        });
						
						
						$("#OrderTable1").find(".recyklace").each(function(i){
							
							var countcelkem=0;
							$("#OrderTable1").find(".count").each(function(i){
	                            countcelkem = parseInt(countcelkem) + parseInt($(this).attr('value'));
	                        });
							$("#OrderTable1").find(".pocet").each(function(i){
                           		$(this).html(countcelkem);
                        	});
                            $(this).html(Math.round(countcelkem*recyklacnipoplatek));
                        });
						
						
                    });
                    $('#Basket').fadeIn('fast');
                }
            });
        }
        else {
            //alert('Povolena jsou pouze cisla');
        }
        
    });
    
    // zobrazeni napovedy k registracnimu formulari, otaceni prvku pri dolnich polozkach
    /*
     $(':input').focus(function(){
     ide = this.id;
     var searchid;
     var max;
     var new_top;
     var top,top1;
     top = $('.helpBox').css('top');
     top1 = top.replace(/px/,'');
     top=parseInt(top1);
     
     $('.OrderForm :input').each(function(i, o){
     
     if(this.id == ide){
     searchid = i;
     new_top = (i*26);
     }
     max=i;
     });
     
     if(searchid>=(max-2)){
     new_top=new_top-(2*26);
     $(".helpBox > p").insertBefore(".helpBox > h2");
     }else{
     $(".helpBox > p").insertAfter(".helpBox > h2");
     }
     $('.helpBox').css("top",new_top+'px');
     $('.helpBox > p').html(this.title);
     });
     */
    // zalozky v detailu produktu	
    $(function(){
        //$('#ProductBox > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
        $('#ProductBox > ul').tabs();

    });
    // registrace
    $(function(){
        $('.LoginTypeLogin').click(function(){
            if ($('.login').css("display") == 'none') {
				$("#c_step2").attr("disabled",false);
				$("#c_step2").val("PŘIHLÁSIT");
                $('.login').fadeIn('fast');
            }
            $('.registration').fadeOut('fast');
        });
        $('.LoginTypeReg').click(function(){
            if ($('.registration').css("display") == 'none') {
				$("#c_step2").attr("disabled",true);
				$("#c_step2").val("REGISTROVAT");
                $('.registration').fadeIn('fast');
				
            }
            $('.login').fadeOut('fast');
        });
    });
    // registrace - kontrola logname
    $("#reg_logname").change(function(){
        $.get("./inc/registrace.check.inc.php?value=" + this.value, function(data){
            if (data == true) {
                $("#reg_logname").removeClass('readyNo');
                $("#reg_logname").addClass('readyOk');
                $("#send").attr("disabled", "disabled");
                
            }
            else {
                $("#reg_logname").removeClass('readyOk');
                $("#reg_logname").addClass('readyNo');
                $("#send").attr("disabled", "");
            }
        });
    });
    
	$("#agree").change(function(){
		if($("#send > .button").attr("disabled")==true){
			$("#send > .button").attr("disabled",false);
		}else{
			$("#send > .button").attr("disabled",true);
		}
	});
	
	
	 // registrace
    $(function(){
        $('#dodaci').click(function(){
            if ($('.dodaci').css("display") == 'none') {
                $('.dodaci').fadeIn('fast');
            }else{
				$('.dodaci').fadeOut('fast');
			}
        });
        $('#fakturacni').click(function(){
            if ($('.fakturacni').css("display") == 'none') {
				$('.fakturacni').fadeIn('fast');
			}else {
				$('.fakturacni').fadeOut('fast');
			}
        });
    });
	
	/**
	 * komentare
	 */
	$(function(){
		$('.CommentsButton').click(function(){
					
			if ($(this).val()=='REAGOVAT'){
				var prvek = $('#CommentsForm');
				var place = $(this).parent();	
				var title = place.parent().parent().children('h3').html();
				var idComment = place.parent().children('.id_comment').val();
				//alert(title + ' - ' + idComment);
				prvek.children('#commenttext').val("");	
				prvek.children().children('#headlinetext').val('Re: ' + title);
				
			}else{
				var prvek = $('#CommentsForm');
				prvek.children('#headlinetext').val("");
				prvek.children('#commenttext').val("");	
				var place = $(this).parent();	
				
			}
			//alert($(this).parent().parent());
			if(prvek.css("display") == 'none') {
	
				prvek.children().children('#idcomment').val(idComment);
				
				place.after(prvek);
				prvek.fadeIn('fast');
            }else{
				//$(this).parent('.commentContent').after(prvek);
				prvek.fadeOut('fast');
			}
			
		});
	});
	
	$('#SendComment').click(function(){
			
		var headline	=	$('#headlinetext').val();
		var commenttext	=	$('#commenttext').val();
		var idcomment 	= 	$('#idcomment').val();
		var idprodukt 	= 	$('#idproduct').val();
		var reguser 	= 	$('#iduser').val();
		
		if($("#showemail").attr("checked")==true){
			var showemail 	= 0;
		}else{
			var showemail 	= 1;
		}
		//alert(showemail);
		$.post("/inc/comments.inc.php",
		{headline: headline, commenttext:commenttext, produkt: idprodukt, id_reguser:reguser,idcomment:idcomment},
		   function(data){
		   	var message;
		   	switch(data){
				case 'produkt':
						message = "Není znám produkt, který bude komentován\n";
					break;
				case 'user':
						message = message + "Není znám uživatel, který bude komentován\n";
					break;
				case 'headline':
						message = message + "Není znám nadpis komentáře\n";
					break;
				case 'comment':
						message = message + "Není znám text komentáře\n";
					break;
			}
			 $("#CommentsForm").fadeOut("fast");
			 $("#CommentsForm").after('<div id="commentMessage"><h1>Úspěch</h1><p>Váš komentář byl přidán.</p></div>');
			 $("#commentMessage").fadeOut("slow");
			
			 $.post("/inc/comments.inc.php",
			 	{show_comment: 1,produkt: idprodukt},
			   function(data){
				 window.location.reload();
				 window.location.href=window.location.href + '#tab-comments';
				
			 });
			
			
			
		   }
		 );
	});
});

