hs.graphicsDir = '/public/image/highslide/';
hs.showCredits = false;

$(document).ready(function() {
  $('h3').hover(
    function (event) {
      $('#' + $(this).attr("id") + '_bubble').fadeIn(250);
    },
    function (event) {
      $('#' + $(this).attr("id") + '_bubble').fadeOut(250);
    }
    );
  $('html').click(function(event) {
    //console.debug($(event.target).attr("class"));
    $('.bubble').fadeOut("slow");
  });
  
  
  $('body').click(function() {
    $("#header .arrow").animate({
      left:"393px"
    }, {
      duration: 300, 
      easing: 'easeOutQuad'
    });
    closeSubmenu();
  });

  $(".blank").focus(function () {
    if ($(this).val() == $(this).attr("title")) {
      $(this).val("");
      $(this).removeClass("blank");
    }
  });
  $(".blank").blur(function () {
    if ($(this).val() == "") {
      $(this).val($(this).attr("title"));
      $(this).addClass("blank");
    }
  });

  $(".partner").hover(
    function() {
      $(this).children(".color").animate({
        opacity:1
      }, 300);
      $(this).children(".grey").animate({
        opacity:0
      }, 300);
    },
    function() {
      $(this).children(".grey").animate({
        opacity:1
      }, 300);
      $(this).children(".color").animate({
        opacity:0
      }, 300);
    }
    );

  $('#header #menu').mouseout(function(event) {
    //alert(event.relatedTarget.nodeName);
    var target = $(event.relatedTarget);
    //alert("id:" + target.attr("id") + "class:" + target.attr("class"));
    if (target.is('span.lbl') || (!target.is('.arrow') && !target.is('.submenu') && !target.is('#menu a') && !target.is('ul') && !target.is('li') && !target.is('span') && !target.is('img.point') && !target.is('img.color') && !target.is('img.grey') && !target.is('a.submenu_lnk') && !target.is('a.close'))) {
      gotoSelected();
    }
  });

  $('#header #menu #menu_3 a.menu_lnk').hover(
    // $('#header #menu #menu_home a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"63px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      closeSubmenu();
    },
    function() {
    }
    );
  $('#header #menu #menu_5 a.menu_lnk').hover(
    // $('#header #menu #menu_personalcars a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"193px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      closeSubmenu();
    },
    function() {
    }
    );
  $('#header #menu #menu_7 a.menu_lnk').hover(
    // $('#header #menu #menu_lorries a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"393px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      closeSubmenu();
    },
    function() {
    }
    );
  $('#header #menu #menu_8 a.menu_lnk').hover(
    // $('#header #menu #menu_services a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"537px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      //if ($(this).parent().children("ul").css("display") != "block") {
      closeSubmenu();
      $(this).parent().children("ul").fadeIn(300);
      $(this).parent().children(".close").fadeIn(300);
    //}
    },
    function() {
    }
    );
  $('#header #menu #menu_10 a.menu_lnk').hover(
    // $('#header #menu #menu_financing a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"655px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      //if ($(this).parent().children("ul").css("display") != "block") {
      closeSubmenu();
      $(this).parent().children("ul").fadeIn(300);
      $(this).parent().children(".close").fadeIn(300);
    //}
    },
    function() {
    }
    );
  $('#header #menu #menu_22 a.menu_lnk').hover(
    //$('#header #menu #menu_about a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"780px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      //if ($(this).parent().children("ul").css("display") != "block") {
      closeSubmenu();
      $(this).parent().children("ul").fadeIn(300);
      $(this).parent().children(".close").fadeIn(300);
    //}
    },
    function() {
    }
    );
  $('#header #menu #menu_9 a.menu_lnk').hover(
    //$('#header #menu #menu_contact a.menu_lnk').hover(
    function() {
      $("#header .arrow").animate({
        left:"890px"
      }, {
        duration: 300, 
        easing: 'easeOutQuad'
      });
      closeSubmenu();
    },
    function() {
    }
    );

  $("#menu li ul li a").hover(
    function() {
      $(this).addClass("blue");
      $(this).children(".imgbox").children(".grey").addClass("invisible")
      $(this).children(".imgbox").children(".color").removeClass("invisible")
    },
    function() {
      $(this).removeClass("blue");
      $(this).children(".imgbox").children(".color").addClass("invisible")
      $(this).children(".imgbox").children(".grey").removeClass("invisible")
    }
    );

  $("#header #menu li .menu_lnk").click(function() {
    //return false;
    });

  $("#header #menu li .submenu").click(function(event) {
    event.stopPropagation();
  });

  $("#header .logo").mouseover(function(event) {
    closeSubmenu();
  });
  $("#header .stemp").mouseover(function(event) {
    closeSubmenu();
  });
  $("#header .rightcol").mouseover(function(event) {
    closeSubmenu();
  });

  $("#header #menu li .submenu").mouseout(function(event) {
    var target = $(event.relatedTarget);
    //alert("id:" + target.attr("id") + "class:" + target.attr("class"));
    if (target.is('span.lbl') || (!target.is('ul') && !target.is('li') && !target.is('span') && !target.is('img.point') && !target.is('img.color') && !target.is('img.grey') && !target.is('a.submenu_lnk') && !target.is('a.close'))) {
      $(this).fadeOut(300);
      $(".close").fadeOut(300);
    }
  });

  $("#header #menu li .close").click(function() {
    closeSubmenu();
  });

  /*$('#banner').jqFancyTransitions({
    width:990, 
    height:351, 
    effect:'zipper', 
    navigation: true, 
    links : true
  });*/
  
  $('.dlgbtn').click(function() {
    $('.dlg').removeClass('invisible');
    $('#overlay').removeClass('invisible');
    
    return false;
  });
  
  $('.dlg .btnclose').click(function() {
    $('.dlg').addClass('invisible');
    $('#overlay').addClass('invisible');
    
    return false;
  });
  
  $('#overlay').click(function() {
    $('.dlg').addClass('invisible');
    $(this).addClass('invisible');
  });

});


(function( $ ) {
  $.widget( "ui.combobox", {

    _create: function() {
      var self = this,
      select = this.element.hide(),
      /*selected = select.children( ":selected" ),*/
      selected = select.children( ".default" ),
      value = selected.val() ? selected.text() : "";
      var input = this.input = $( "<input>" )
      .insertAfter( select )
      .val( value )
      .autocomplete({
        delay: 0,
        minLength: 0,
        source: function( request, response ) {
          var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
          response( select.children( "option" ).map(function(data) {
            var text = $( this ).text();
            var valueId = $( this ).val();
            if ( this.value && ( !request.term || matcher.test(text) ) )
              return {
                label: text.replace(
                  new RegExp(
                    "(?![^&;]+;)(?!<[^<>]*)(" +
                    $.ui.autocomplete.escapeRegex(request.term) +
                    ")(?![^<>]*>)(?![^&;]+;)", "gi"
                    ), "<strong>$1</strong>" ),
                value: text,
                valueId: valueId,
                myClass: $( this ).attr('class'),
                option: this
              };
          }) );
        },
        select: function( event, ui ) {
          ui.item.option.selected = true;
          self._trigger( "selected", event, {
            item: ui.item.option
          });
          
          var regExp = /searchitem/gi;
          
          if (ui.item.myClass == 'searchitem search_filter_sign') { 
            $(".search_loading").removeClass('invisible');
            $("#search_filter_model").html('');
            //$("#search .formfieldbox .formfield input").val('');
            //select = this.element.hide()
            //console.log($(this).parent().parent().next().children('.formfield').children('input'));
            
            var elemParent = $(this);
            
            $("#search_filter_model").html('');
            var jqxhr = $.post("/modely?znacka=" + ui.item.valueId, function(data) {
              $("#search_filter_model").html(data);
              var text = $('#search_filter_model').children('.default').text();
              var elem = elemParent.parent().parent().next().children('.formfield').children('input');
              elem.val(text);
              $(".search_loading").addClass('invisible');
            })
            .success(function() { 
            })
            .error(function() { 
              $(".search_loading").addClass('invisible');
            })
            .complete(function(data) {
            });
          }
          else if (typeof(eval('ui.item.myClass')) != undefined || ui.item.myClass == undefined || ui.item.myClass.search(regExp) == -1) {
            if (ui.item.myClass != "searchitem" && ui.item.myClass != "searchitem search_filter_model" && ui.item.myClass != "searchitem search_filter_fuel" && ui.item.myClass != "searchitem search_filter_year") {
              $('#filter_loading').removeClass('invisible');
              $('#main_frm').submit();
            }
          }
        },
        change: function( event, ui ) {
          if ( !ui.item ) {
            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
            valid = false;
            select.children( "option" ).each(function() {
              if ( $( this ).text().match( matcher ) ) {
                this.selected = valid = true;
                return false;
              }
            });
            if ( !valid ) {
              // remove invalid value, as it didn't match anything
              $( this ).val( "" );
              select.val( "" );
              input.data( "autocomplete" ).term = "";
              return false;
            }
          }
        }
      })
      .addClass( "ui-widget ui-widget-content ui-corner-left" );

      input.data( "autocomplete" )._renderItem = function( ul, item ) {
        var isDefault = false;
        var isSearch = false;
        var classes = '';
        //console.log(ul);
        //console.log(item);
////////////////////////////////////////////    
        /*alert('item:' + item + ',item.option:' + item.option + ',item.option.classList:' + item.option.classList);
        alert('item:' + item + ',item.option:' + item.option + ',item.option.className:' + item.option.className);*/
        
        var classList = null;
        if (item.option.classList === undefined) {
          classList = item.option.className;
        }
        else {
          classList = item.option.classList;
        }
        
        //alert('classList:' + classList);
        
        var classListArr = new String(classList).split(' ');

        if (classListArr != null && classListArr != undefined) {
          for (var i=0; i < classListArr.length; i++) {
            if (classListArr[i] === 'default') {
              isDefault = true;
            }
            else if (classListArr[i] === 'searchitem') {
              isSearch = true;
            }
          }
        }
        
        /*
        $.each( classList, function(index, optionItem){
          if (optionItem === 'default') {
            isDefault = true;
          }
          else if (optionItem === 'searchitem') {
            isSearch = true;
          }
        });*/
        
        //alert('isDefault:' + isDefault);
        if (isDefault) {
          //alert('isSearch:' + isSearch);
          if (isSearch) {
            //alert('ff');
            $(ul).addClass('searchcombolist');
          }
          return null;
        }
        else {
          return $( "<li></li>" )
          .data( "item.autocomplete", item )
          .append( "<a>" + item.label + "</a>" )
          .appendTo( ul );
        }
      };

      this.button = $( "<button type='button'>&nbsp;</button>" )
      .attr( "tabIndex", -1 )
      .attr( "title", "" )
      //.attr( "title", "Show All Items" )
      .insertAfter( input )
      .button({
        icons: {
          primary: "ui-icon-triangle-1-s"
        },
        text: false
      })
      .removeClass( "ui-corner-all" )
      .addClass( "ui-corner-right ui-button-icon" )
      .click(function() {
        // close if already visible
        if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
          input.autocomplete( "close" );
          return;
        }
        
        // closing all the others because otherwise new one is positioned sometimes randomly
        $(".ui-autocomplete-input").each(function(index) {
          $(this).autocomplete( "close" );
        });

        // work around a bug (likely same cause as #5265)
        $( this ).blur();

        // pass empty string as value to search for, displaying all results
        input.autocomplete( "search", "" );
        input.focus();
      });
    },

    destroy: function() {
      this.input.remove();
      this.button.remove();
      this.element.show();
      $.Widget.prototype.destroy.call( this );
    }
  });
})( jQuery );

$(function() {
  $( ".combobox" ).combobox();
});

function closeSubmenu() {
  $(".submenu").fadeOut(300);
  $(".close").fadeOut(300);
}

function gotoSelected() {
  var selectedId = $("#menu a.selected").parent().attr("id");
  var left = 0;
  if (selectedId == "menu_home" || selectedId == "menu_3") {
    left = 63;
  }
  else if (selectedId == "menu_personalcars" || selectedId == "menu_5") {
    left = 193;
  }
  else if (selectedId == "menu_personalcars" || selectedId == "menu_7") {
    left = 193;
  }
  else if (selectedId == "menu_lorries" || selectedId == "menu_8") {
    left = 393;
  }
  else if (selectedId == "menu_services" || selectedId == "menu_10") {
    left = 537;
  }
  else if (selectedId == "menu_about" || selectedId == "menu_22") {
    left = 780;
  }
  else if (selectedId == "menu_contact" || selectedId == "menu_9") {
    left = 890;
  }

  $("#header .arrow").animate({
    left:left + "px"
  }, {
    duration: 300, 
    easing: 'easeOutQuad'
  });
}

function loadDetailImage(obj, target) { 
  $("#" + target).attr("src", $(obj).parent().attr("href"));
  /*$("#cardetailbox").children().removeClass("highslide-active-anchor");
  $('img[src="' + $(obj).parent().attr("href") + '"]').addClass("highslide-active-anchor");*/
  //console.log(s);
  
            
  return false;
}
