$(document).ready(function(){
   if ($("#datumOd").size()){
      $("#datumOd").datepicker({dateFormat: 'dd.mm.yy', showOn: 'button', buttonImage: '/tolteam/images/kalendar.jpg', buttonImageOnly: true, minDate: new Date()});

      $("#datumOd").change(function(){
         var date = $("#datumOd").val().split('.');
         $('#datumDo').datepicker('option', 'minDate', new Date(date[2], date[1] - 1, parseInt(date[0]) + 1));
      });
   }

   if ($("#datumDo").size()){
 	$("#datumDo").datepicker({dateFormat: 'dd.mm.yy', showOn: 'button', buttonImage: '/tolteam/images/kalendar.jpg', buttonImageOnly: true, minDate: new Date()});
   }


   //start lightbox
   if ($('.lightBox').size()){
      $('.lightBox').lightBox();
   }


   //set open group
   var hashLocation = window.location.hash.substr(1);
   var gId = "group_"+hashLocation;
   if (hashLocation != ''){
      $("#grupaVozila").val(hashLocation);
      $("#"+gId).addClass("active");
   }




   ///////////////////////
   //start load group data
      /*var urlHashLocation = window.location.hash.substr(1);
      var hashLocationSplited = urlHashLocation.split(';');

      if (urlHashLocation != ''){
         //change height
         if (document.getElementById('container_fleet')){
            var type = 1;
            var categoryList = 'charter_holder';
            var descriptionHolder = 'charter_holder_description';
            var height = document.getElementById('container_fleet').offsetHeight;
         }
         else {
            var type = 2;
            var categoryList = 'car_holder';
            var descriptionHolder = 'car_holder_description';
            var height = document.getElementById('container').offsetHeight;
         }


         $("#groupShadow").height(height);


         $("#groupShadow").show();
         $("#groupShadow").fadeTo("slow", 0.8);

         $(".categoryGroup").removeClass("active");

         $.ajax({
            type: "POST",
            url: "/dynamic/ajax/loadGroupCategoryData.php",
            cache: false,
            data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type,
            success: function(html){
                  $("#"+categoryList).html(html);
                  $("#groupShadow").fadeTo("slow", 0, function(){
                     $("#groupShadow").hide();
                  });

                  //start lightbox
                  if ($('.lightBox').size()){
                     $('.lightBox').lightBox();
                  }


                  $(".groupLink2").click(function(){
                     var urlHashLocation = window.location.hash.substr(1);
                     var hashLocation = $(this).attr("href").substr(1);
                     var hashLocationSplited = hashLocation.split(';');

                     //change height
                     $("#groupShadow").height(height);

                     //check current group
                     if (urlHashLocation == hashLocation){
                        return false;
                     }

                     $("#groupShadow").show();
                     $("#groupShadow").fadeTo("slow", 0.8);

                     $(".vozilo").removeClass("active");

                     $.ajax({
                        type: "POST",
                        url: "/dynamic/ajax/loadGroupData.php",
                        cache: false,
                        data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type+"&noSession=1",
                        success: function(html){
                              $("#"+descriptionHolder).html(html);
                              $("#groupShadow").fadeTo("slow", 0, function(){
                                 $("#groupShadow").hide();
                              });


                              //start lightbox
                              if ($('.lightBox').size()){
                                 $('.lightBox').lightBox();
                              }

                        //change height
                        $("#groupShadow").height(height);

                        $("#group_"+hashLocationSplited[1]).addClass("active");
                        $("#grupaVozila").val(hashLocationSplited[1]);
                        }
                     });
                  });

                  $(".vozilo").removeClass("active");

                  //change height
                  $("#groupShadow").height(height);

                  $("#groupCat_"+hashLocationSplited[0]).addClass("active");
                  $("#group_"+hashLocationSplited[1]).addClass("active");
                  $("#grupaVozila").val(hashLocationSplited[1]);
               }
            });

   }*/




/*
   $("a").click(function(){
      var urlHashLocation = $(this).attr("href").split("#");
      var hashLocationSplited = urlHashLocation[1].split(';');

      if (urlHashLocation != ''){
         //change height
         if (document.getElementById('container_fleet')){
            var type = 1;
            var categoryList = 'car_holder';
            var descriptionHolder = 'car_holder_description';
            var height = document.getElementById('container_fleet').offsetHeight;
         }
         else {
            var type = 2;
            var categoryList = 'charter_holder';
            var descriptionHolder = 'charter_holder_description';
            var height = document.getElementById('container').offsetHeight;
         }

         $("#groupShadow").height(height);



         $("#groupShadow").show();
         $("#groupShadow").fadeTo("slow", 0.8);

         $(".categoryGroup").removeClass("active");

         $.ajax({
            type: "POST",
            url: "/dynamic/ajax/loadGroupCategoryData.php",
            cache: false,
            data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type,
            success: function(html){
                  $("#"+categoryList).html(html);
                  $("#groupShadow").fadeTo("slow", 0, function(){
                     $("#groupShadow").hide();
                  });

                  //start lightbox
                  if ($('.lightBox').size()){
                     $('.lightBox').lightBox();
                  }


                  $(".groupLink2").click(function(){
                     var urlHashLocation = window.location.hash.substr(1);
                     var hashLocation = $(this).attr("href").substr(1);
                     var hashLocationSplited = hashLocation.split(';');

                     //change height
                     $("#groupShadow").height(height);

                     //check current group
                     if (urlHashLocation == hashLocation){
                        return false;
                     }

                     $("#groupShadow").show();
                     $("#groupShadow").fadeTo("slow", 0.8);

                     $(".vozilo").removeClass("active");

                     $.ajax({
                        type: "POST",
                        url: "/dynamic/ajax/loadGroupData.php",
                        cache: false,
                        data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type+"&noSession=1",
                        success: function(html){
                              $("#"+descriptionHolder).html(html);
                              $("#groupShadow").fadeTo("slow", 0, function(){
                                 $("#groupShadow").hide();
                              });

                              //start lightbox
                              if ($('.lightBox').size()){
                                 $('.lightBox').lightBox();
                              }


                        //change height
                        $("#groupShadow").height(height);

                        $("#group_"+hashLocationSplited[1]).addClass("active");
                        $("#grupaVozila").val(hashLocationSplited[1]);
                        }
                     });
                  });

                  $(".vozilo").removeClass("active");


                  //change height
                  if (document.getElementById('container_fleet')){
                     var height = document.getElementById('container_fleet').offsetHeight;
                  }
                  else {
                     var height = document.getElementById('container').offsetHeight;
                  }

                  $("#groupShadow").height(height);


                  $("#groupCat_"+hashLocationSplited[0]).addClass("active");
                  $("#group_"+hashLocationSplited[1]).addClass("active");
                  $("#grupaVozila").val(hashLocationSplited[1]);
               }
            });

   }
   });*/
   //end load group data
   /////////////////////




   $(".carRezervation").click(function(){
      var urlHashLocation = window.location.hash.substr(1);
      var hashLocation = $(this).attr("href").substr(1);
      var hashLocationSplited = hashLocation.split(';');

      //change height
      $("#groupShadow").height(document.getElementById('container').offsetHeight);

      //check current group
      if (urlHashLocation == hashLocation){
         return false;
      }

      $("#groupShadow").show();
      $("#groupShadow").fadeTo("slow", 0.8);

      $(".vozilo").removeClass("active");

      $.ajax({
         type: "POST",
         url: "/dynamic/ajax/loadCarRezervationData.php",
         cache: false,
         data: "groupName="+hashLocationSplited[1]+"&type=3",
         success: function(html){
               $("#car_holder_description").html(html);
               $("#groupShadow").fadeTo("slow", 0, function(){
               $("#groupShadow").hide();
            });

            //start lightbox
            if ($('.lightBox').size()){
               $('.lightBox').lightBox();
            }

         //change height
         $("#groupShadow").height(document.getElementById('container').offsetHeight);
         }
      });


      $("#group_"+hashLocation).addClass("active");
      $("#grupaVozila").val(hashLocation);
   });




   $(".groupLink").click(function(){
      var urlHashLocation = window.location.hash.substr(1);
      var hashLocation = $(this).attr("href").substr(1);
      var hashLocationSplited = hashLocation.split(';');

      //change height
      $("#groupShadow").height(document.getElementById('container').offsetHeight);

      //check current group
      if (urlHashLocation == hashLocation){
         return false;
      }

      $("#groupShadow").show();
      $("#groupShadow").fadeTo("slow", 0.8);

      $(".vozilo").removeClass("active");

      $.ajax({
         type: "POST",
         url: "/dynamic/ajax/loadGroupData.php",
         cache: false,
         data: "groupName="+hashLocationSplited[1]+"&type=3",
         success: function(html){
               $("#car_holder_description").html(html);
               $("#groupShadow").fadeTo("slow", 0, function(){
               $("#groupShadow").hide();
            });

            //start lightbox
            if ($('.lightBox').size()){
               $('.lightBox').lightBox();
            }

         //change height
         $("#groupShadow").height(document.getElementById('container').offsetHeight);
         }
      });


      $("#group_"+hashLocation).addClass("active");
      $("#grupaVozila").val(hashLocation);
   });





   $(".groupLink2").click(function(){
      var urlHashLocation = window.location.hash.substr(1);
      var hashLocation = $(this).attr("href").substr(1);
      var hashLocationSplited = hashLocation.split(';');

      //change height
      if (document.getElementById('container_fleet')){
         var type = 1;
         var categoryList = 'car_holder';
         var descriptionHolder = 'car_holder_description';
         var height = document.getElementById('container_fleet').offsetHeight;
      }
      else {
         var type = 2;
         var categoryList = 'charter_holder';
         var descriptionHolder = 'charter_holder_description';
         var height = document.getElementById('container').offsetHeight;
      }

      $("#groupShadow").height(height);

      //check current group
      if (urlHashLocation == hashLocation){
         return false;
      }

      $("#groupShadow").show();
      $("#groupShadow").fadeTo("slow", 0.8);

      $(".vozilo").removeClass("active");

      $.ajax({
         type: "POST",
         url: "/dynamic/ajax/loadGroupData.php",
         cache: false,
         data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type+"&noSession=1",
         success: function(html){
               $("#"+descriptionHolder).html(html);
               $("#groupShadow").fadeTo("slow", 0, function(){
                  $("#groupShadow").hide();
               });

               //start lightbox
               if ($('.lightBox').size()){
                  $('.lightBox').lightBox();
               }


         //change height
         $("#groupShadow").height(height);

         $("#group_"+hashLocationSplited[1]).addClass("active");
         $("#grupaVozila").val(hashLocationSplited[1]);
         }
      });
   });





   $(".categoryGroup").click(function(){
      var urlHashLocation = window.location.hash.substr(1);
      var hashLocation = $(this).attr("href").substr(1);
      var hashLocationSplited = hashLocation.split(';');

      //change height
      if (document.getElementById('container_fleet')){
         var type = 1;
         var categoryList = 'charter_holder';
         var descriptionHolder = 'charter_holder_description';
         var height = document.getElementById('container_fleet').offsetHeight;
      }
      else {
         var type = 2;
         var categoryList = 'car_holder';
         var descriptionHolder = 'car_holder_description';
         var height = document.getElementById('container').offsetHeight;
      }
      $("#groupShadow").height(height);

      //check current group
      if (urlHashLocation == hashLocation){
         return false;
      }

      $("#groupShadow").show();
      $("#groupShadow").fadeTo("slow", 0.8);

      $(".categoryGroup").removeClass("active");

      $.ajax({
         type: "POST",
         url: "/dynamic/ajax/loadGroupCategoryData.php",
         cache: false,
         data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type,
         success: function(html){
               $("#car_holder").html(html);
               $("#groupShadow").fadeTo("slow", 0, function(){
               $("#groupShadow").hide();
            });

            //start lightbox
            if ($('.lightBox').size()){
               $('.lightBox').lightBox();
            }


         //change height
         $("#groupShadow").height(height);




         $(".groupLink2").click(function(){
            var urlHashLocation = window.location.hash.substr(1);
            var hashLocation = $(this).attr("href").substr(1);
            var hashLocationSplited = hashLocation.split(';');
      
            //change height
            if (document.getElementById('container_fleet')){
               var type = 1;
               var categoryList = 'charter_holder';
               var descriptionHolder = 'charter_holder_description';
               var height = document.getElementById('container_fleet').offsetHeight;
            }
            else {
               var type = 2;
               var categoryList = 'car_holder';
               var descriptionHolder = 'car_holder_description';
               var height = document.getElementById('container').offsetHeight;
            }

            $("#groupShadow").height(height);
      
            //check current group
            if (urlHashLocation == hashLocation){
               return false;
            }
      
            $("#groupShadow").show();
            $("#groupShadow").fadeTo("slow", 0.8);
      
            $(".vozilo").removeClass("active");
      
            $.ajax({
               type: "POST",
               url: "/dynamic/ajax/loadGroupData.php",
               cache: false,
               data: "groupCatId="+hashLocationSplited[0]+"&groupName="+hashLocationSplited[1]+"&type="+type+"&noSession=1",
               success: function(html){
                     $("#car_holder_description").html(html);
                     $("#groupShadow").fadeTo("slow", 0, function(){
                     $("#groupShadow").hide();
                  });

                  //start lightbox
                  if ($('.lightBox').size()){
                     $('.lightBox').lightBox();
                  }

               //change height
               $("#groupShadow").height(height);
               }
            });


            $("#group_"+hashLocationSplited[1]).addClass("active");
            $("#grupaVozila").val(hashLocationSplited[1]);
         });
         }
      });


      $("#groupCat_"+hashLocationSplited[0]).addClass("active");
      $("#grupaVozila").val(hashLocationSplited[1]);
   });




   $(".extraOptions").click(function(){
      var optionId = $(this).attr("id").substr(4);
      $("#groupExtraOption").show();
      $("#groupExtraOption").fadeTo("slow", 0.8);
      //alert(document.getElementById('extraBox').offsetHeight);//$("#extraBox").height()
      //change height and width
      $("#groupExtraOption").height(document.getElementById('extraBox').offsetHeight);
      $("#groupExtraOption").width(document.getElementById('extraBox').offsetWidth);


      if (!$(this).is(":checked")){
         $.ajax({
            type: "POST",
            url: "/dynamic/ajax/unLoadOptionPrice.php",
            cache: false,
            dataType: "jsonp",
            data: "optionId="+optionId,
            success: function(html){
               if (html['success']){
                  $("#rac_"+optionId).remove();
                  $("#totalRightPrice").html(html['totalPrice']);
                  $("#mainTotalPrice").html(html['mainTotalPrice']);
               }
               else {
                  alert("Server error! Cannot remove option!");
               }

               $("#groupExtraOption").fadeTo("slow", 0, function(){
                  $("#groupExtraOption").hide();
               });

               //change height
               $("#groupExtraOption").height(document.getElementById('extraBox').offsetHeight);
            }
         });

         return true;
      }

      $.ajax({
         type: "POST",
         url: "/dynamic/ajax/loadOptionPrice.php",
         cache: false,
         dataType: "jsonp",
         data: "optionId="+optionId,
         success: function(html){
            $("#totalRight").before('<li id="rac_'+optionId+'"><label>'+html['naziv']+':</label>'+html['price']+'</li>');
            $("#totalRightPrice").html(html['totalPrice']);
            $("#mainTotalPrice").html(html['mainTotalPrice']);

            $("#groupExtraOption").fadeTo("slow", 0, function(){
               $("#groupExtraOption").hide();
            });

            //change height
            $("#groupExtraOption").height(document.getElementById('extraBox').offsetHeight);
         }
      });
   });
});