$( function(){
  $( '#tartalom .preview' ).previewSlider( );
  
  $( '#login, #kosar' ).click( function(event){
    if($('#logindlg').length != 0){
      $( '#logindlg' ).toggle();
      $( '#logindlg a.login' ).attr('href',$(this).attr('href'));
      event.preventDefault();
      event.stopPropagation();
      return false;
    }
    return true;
  });
  
  $('.menu>div, .menu>div>form').click(function(event){
    if(event.target.nodeName != 'INPUT' ){
      $(this).children('form').submit();
    }
  });
  
  $( 'html' ).click( function(){
    $( '#logindlg' ).hide();
    if($('#magyarazat').length != 0)$( '#magyarazat').hide();
    if($('#sendmaildlg').length != 0)$( '#sendmaildlg').hide();
    if($('#feliratkozas').length != 0)$( '#feliratkozas').hide();
  });

  $( '#logindlg' ).click( function(event){
    event.stopPropagation();
  });

  $( '#logindlg .dlgclose' ).click( function(event){
    $( '#logindlg' ).hide();
  });
  
  $( '#logindlg input').keypress(function(e){
      if(e.which == 13){
        $('#logindlg a.login').trigger('click');
      }
  });

  $( '#logindlg a.login' ).click( function(event){
    $.ajax({
      type:     'POST',
      url:      'logincheck.php',
      data:     $( '#logindlg input' ).serialize(),
      async:    false,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success: function( text ){        
        if(text.length){
          $( '#logindlg .status' ).html(text);
        }else{
          var href = $('#logindlg a.login').attr('href');
          if(href.length){
            window.location=href;
          }else{
            window.location.reload();
          }
        }
      }
    });
    return false;
  });

  $( '#logindlg a.elfelejtett' ).click( function(event){
    $.ajax({
      type:     'POST',
      url:      'logincheck.php',
      data:     $( '#logindlg input' ).serialize()+'&elfelejtett=1',
      async:    false,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success: function( text ){
        if(text.length){
          $( '#logindlg .status' ).html(text);
        }else{
          window.location.reload();
        }
      }
    });
    return false;
  });
  
  
  $( '#oldal_fiok a.button' ).click( function(event){
    $( '#oldal_fiok .status' ).html('Adatok küldése');
    $.ajax({
      type:     'POST',
      url:      'fiokom.php',
      data:     $( '#adatlap' ).serialize(),
      async:    false,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success: function( text ){        
        $('#oldal_fiok .err').html('');
        if(!text.length){
          $( '#oldal_fiok .status' ).html('Adatok elmentve');
        }else{
         var err = text.split('\n');
         for(i=0;i<err.length;i++){
           var fi = err[i].split('@');
           if(!fi[0].length){
             $( '#oldal_fiok .status' ).html(fi[1]);           
           }else{
             $('#oldal_fiok .err_'+fi[0]).html(fi[1]);
           }
         }
        }
      }
    });
    return false;
  });
  
  $( '#oldal_reg a.button' ).click( function(event){
    event.preventDefault();
    $.ajax({
      type:     'POST',
      url:      'regisztracio.php',
      data:     $( '#adatlap' ).serialize(),
      async:    false,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success: function( text ){
        $('#oldal_reg .err').html('');
        if(!text.length){
          window.location = 'regisztracio.php?oldal=reg';
        }else{
         var err = text.split('\n');
         for(var i=0;i<err.length;i++){
           var fi = err[i].split('@');
           $('#oldal_reg .err_'+fi[0]).html(fi[1]);
         }
        }
      }
    });
    return false;
  });
    
  
  $( '#oldal_termek .ajanlo' ).previewAjanlatok();
  $( '#oldal_termek a.email' ).click(function(event){
    event.preventDefault();
    event.stopPropagation();    
    $('#sendmaildlg').show();
  });
  
  $( '#sendmaildlg' ).click( function(event){
    event.stopPropagation();
  });

  $( '#sendmaildlg .dlgclose' ).click( function(event){
    $( '#sendmaildlg' ).hide();
  });
  
  $( '#sendmaildlg input').keypress(function(e){
      if(e.which == 13){
        $('#sendmaildlg a.kuldes').trigger('click');
      }
  });
  $( '#sendmaildlg a.kuldes' ).click(function(event){
    event.preventDefault();
    event.stopPropagation();    
    $.ajax({
      async:       false,
      data:        $('#sendmailform').serialize(),
      type:        'POST',
      url:         'megosztom.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
        $('#sendmaildlg .err').html('');
        if(data.length==0){
          $('#sendmaildlg').hide();
        }else{
          var err = data.split('\n');
          for(i=0;i<err.length;i++){
            var fi = err[i].split('@');
            $('#sendmaildlg .err_'+fi[0]).html(fi[1]);
          }
        }
      }
    });  
  });


  $( '#footer a.email' ).click(function(event){
    event.preventDefault();
    event.stopPropagation();    
    $('#feliratkozas').show();
  });
  
  $( '#feliratkozas' ).click( function(event){
    event.stopPropagation();
  });

  $( '#feliratkozas .dlgclose' ).click( function(event){
    $( '#feliratkozas' ).hide();
  });
  
  $( '#feliratkozas input').keypress(function(e){
      if(e.which == 13){
        $('#feliratkozas a.kuldes').trigger('click');
      }
  });
  $( '#feliratkozas a.kuldes' ).click(function(event){
    event.preventDefault();
    event.stopPropagation();    
    $.ajax({
      async:       false,
      data:        $('#feliratkozasform').serialize(),
      type:        'POST',
      url:         'feliratkozas.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
        $('#feliratkozas .err').html('');
        if(data.length==0){
          $('#feliratkozas').hide();
        }else{
          $('#feliratkozas .err').html(data);
        }
      }
    });  
  });
  
  
  
  $( '#oldal_termeklista .filter input').click(function(event){
    window.location = $(this).parent().attr('href');
  });
  
  $( '#oldal_termeklista .kivalaszto').click(function(event){
    var id = $(this).data('id');
    var sel = ($(this).prop('checked'))?1:0;
    var csoport = $(this).data('csoport');
    
    var param = 'termek='+id+'&sel='+sel+'&csoport='+csoport;
    $.ajax({
      async:       false,
      data:        param,
      type:        'POST',
      url:         'kivalaszt.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
        var elements = data.split('\n').length - 2;
        if(elements > 1){
          $('#oldal_termeklista .osszelink').addClass('aktiv');
        }else{
          $('#oldal_termeklista .osszelink').removeClass('aktiv');        
        }
      }
    });
  });
  
  $( '#oldal_hasonlitas .kivalaszto').click(function(event){
    var id = $(this).data('id');
    var csoport = $(this).data('csoport');
    
    var param = 'termek='+id+'&sel=0&csoport='+csoport;
    $.ajax({
      async:       false,
      data:        param,
      type:        'POST',
      url:         'kivalaszt.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
        window.location.reload();
      }
    });
  });  
  
  $( '#oldal_termek .cikkselect' ).change(function(){
    var cikk    = $( '#oldal_termek .cikkselect>option:selected' ).data( 'cikk' );
    var regiar  = $( '#oldal_termek .cikkselect>option:selected' ).data( 'regiar' );
    var ar      = $( '#oldal_termek .cikkselect>option:selected' ).data( 'ar' );
    var keszlet = $( '#oldal_termek .cikkselect>option:selected' ).data( 'keszlet' );
    var hazhoz  = $( '#oldal_termek .cikkselect>option:selected' ).data( 'hazhoz' );
    var kiskep  = $( '#oldal_termek .cikkselect>option:selected' ).data( 'kiskep' );
    var nagykep = $( '#oldal_termek .cikkselect>option:selected' ).data( 'nagykep' );
    $( '#oldal_termek .param' ).hide();
    $( '#param_' + cikk ).show();
    $( '#oldal_termek .ar' ).text( ar);
    $( '#oldal_termek .ar.strike').text( regiar);
    if( keszlet ){
      $( '#oldal_termek .keszleten' ).removeClass( 'grey' ).addClass( 'blue' );
      $( '#oldal_termek .rendelheto' ).removeClass( 'blue' ).addClass( 'grey' );
    }else if( cikk > 0 ){
      $( '#oldal_termek .rendelheto' ).removeClass( 'grey' ).addClass( 'blue' );
      $( '#oldal_termek .keszleten' ).removeClass( 'blue' ).addClass( 'grey' );
    }else{
      $( '#oldal_termek .rendelheto' ).removeClass( 'blue' ).addClass( 'grey' );
      $( '#oldal_termek .keszleten' ).removeClass( 'blue' ).addClass( 'grey' );
    }
    if( hazhoz ){
      $( '#oldal_termek .hazhoz' ).show();
    }else{
      $( '#oldal_termek .hazhoz' ).hide();    
    }
    if(kiskep.length){
      $( '#zoomnagy' ).attr( 'href' ,nagykep );
      $( '#zoomkicsi' ).attr( 'src' ,kiskep );    
      $( '#zoomnagy' ).CloudZoom();
    }
  });
  
  
  $( '#oldal_termek a.altimg' ).click( function( event ) {
    var nagy  = $( this ).attr( 'href' );
    var kicsi = $( this ).attr( 'data' );
    $( '#zoomnagy' ).attr( 'href' ,nagy );
    $( '#zoomkicsi' ).attr( 'src' ,kicsi );    
    $( '#zoomnagy' ).CloudZoom();
    return false;
  });
  
  $( '#oldal_termek a.kosarba' ).click( function( event) {
    event.preventDefault();
    var ref =  $(this).attr( 'href' );
    if( ref.length < 5 ){
      var egycikk = $( '#oldal_termek .egycikk' ).attr( 'value' );
      var cikk    = $( '#oldal_termek .cikkselect>option:selected' ).data( 'cikk' );
      if( egycikk > 0 ) cikk = egycikk;
      if( cikk <= 0 ){
        alert( 'Válasszon az elérhető változatok közül!' );
        return false;
      }else{
        ref = 'add=' + cikk;
      }
    }
    $.ajax({
      async:       false,
      data:        ref,
      type:        'POST',
      url:         'kosarinfo.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
         $( '.kosar' ).html( data );
      }
    });
    return false;
  });

  $( '#oldal_termek a.hasznalt' ).click( function( event) {
    event.preventDefault();
    var egycikk = $( '#oldal_termek .egycikk' ).attr( 'value' );
    var cikk    = $( '#oldal_termek .cikkselect>option:selected' ).data( 'cikk' );
    var login    = $(this).data( 'login' );
    if( egycikk > 0 ) cikk = egycikk;
    if( cikk <= 0 ){
      alert( 'Válasszon az elérhető változatok közül!' );
      return false;
    }
    if(login.length<1){
      alert('Kérjük lépjen be fiókjába vagy regisztráljon');
      return false;
    }
    
    $.ajax({
      async:       false,
      data:        'cikk='+cikk+'&login='+login,
      type:        'POST',
      url:         'hasznaltemail.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
        alert('Köszönjük érdeklődését!\nKollegáink a termékkel kapcsolatban hamarosan felveszik Önnel a kapcsolatot.');
      }
    });
    return false;    
  });
  
  
 $( '#oldal_termek a.kulcsszo' ).click( function( event) {
    var ref =  $(this).attr( 'href' );
    var off = $(this).offset();
    off.left += $(this).width(); 
    ref = ref.split('?')[1];
    event.preventDefault();
    event.stopPropagation();

    $.ajax({
      async:       false,
      data:        ref,
      type:        'POST',
      url:         'magyarazat.php',
      timeout:     20000,
      contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-2',
      success:     function( data ) {
         $('#magyarazat').html( data );
         $('#magyarazat').show();
         $('#magyarazat').offset(off);
      }
    });
    return false;    
  });  
  
  $( '#oldal_kosar .menny' ).change( function( event ) {
    var id    = event.target.id;
    var tetel = id.split('_')[1];
    var menny = $( '#' + id + '>option:selected' ).attr( 'value' );
    window.location = 'kosar.php?modtetel=' + tetel + '&menny=' + menny;
  });  

  $( '#szallmod,#fizmod' ).change( function( event ) {
    $( '#oldal_kosar form:first' ).attr('action','');
    $( '#oldal_kosar form:first' ).trigger('submit');
    return false;
  });

  
  $( '#oldal_kosar a.mentes' ).click( function( event ){
    $( '#oldal_kosar form:first' ).trigger('submit');
    return false;
  });    
  
  
  
/*  $( '#hirek_oldal a' ).click( function(event){
    event.preventDefault();
    var id = $(this).attr('href');
    var h = $('.'+id).height();
    var small = 100;
    if(h > small){
      $('.'+id).height(small);
    }else{
      $('.'+id).height('auto');
    }
    return false;
  });*/
  
  $('#gallery a').lightBox({fixedNavigation:true});  
});

$.widget( 'ui.previewAjanlatok', {
  _create:function(){
    var same   = this;
    this.rows  = $( this.element ).find( '.termekek>div' );
    $( this.element ).find( '.fl>a' ).click( function(){
      var active = same.rows.not( '.hidden' );
      if( active.prev( '.hidden' ).length ){
        active
          .addClass( 'hidden' )
          .prev( '.hidden' )
          .removeClass( 'hidden' );
      }
      return false;
    });
    $( this.element ).find( '.fr>a' ).click( function(){
      var active = same.rows.not( '.hidden' );
      if( active.next( '.hidden' ).length ){
        active
          .addClass( 'hidden' )
          .next( '.hidden' )
          .removeClass( 'hidden' );
      }
      return false;
    });
  },
});


$.widget( "ui.previewSlider", {
   options:{
     timeOutShort:  3000,
     timeOutLong : 15000
   },
   _create:function(){
     var same = this;
     this.images = this.element.children('div');
     var i,s;
     for( s= '<div class="bulletcontainer" style="z-index:9999">', i = 0; i < this.images.length; i++ ){
       s += '<div class="bullet' + ( i == this.images.length-1 ? ' active' : '' )+ '"></div>';
     }
     s += '</div>';
     
     this.element.append( s );
     this.bullets = this.element.find( '.bulletcontainer>div' );
     this.bullets.click( function(){
       var index = same.images.length - same.bullets.index( this ) - 1;
       same.setImage( index, true );    
     }).disableTextSelect();
     
     this.element.append( '<div class="nav prev"></div><div class="nav next"></div>' );
     this.element.find( '.prev' ).click(function(){
       same.prevImage( true );
     }).disableTextSelect()
     this.element.find( '.next' ).click(function(){
       same.nextImage( true );
     }).disableTextSelect()
     
     this.active = 0;
     this.timeOut = setTimeout( function(){
       same.nextImage( false )
     }, this.options.timeOutShort );
     this.doTimeOut = true;
   },
   nextImage:function( cancelTimeOut ){
     var index = this.active + 1;
     if( index >= this.images.length ) index = 0;
     this.setImage( index, cancelTimeOut );
   },
   prevImage:function( cancelTimeOut ){
     var index = this.active - 1;
     if( index < 0  ) index = this.images.length - 1;
     this.setImage( index, cancelTimeOut );
   },
   setImage:function( index, cancelTimeOut ){
     var same = this;
     if( cancelTimeOut ){
       this.doTimeOut = false;
     }
     clearTimeout( this.timeOut );
     var zIndex = 1000;
     this.images.stop();
     this.images.not( this.images.eq( this.active ) ).css( 'z-index',  zIndex - 100 );
     this.images.eq( this.active ).css( 'z-index',  zIndex);
     this.images.eq( index ).css( 'z-index',  zIndex + 100 );
     $('.bulletcontainer').css('z-index',zIndex +1000);
     this.images.not( this.images.eq( index ) ).fadeTo( 1000, 0,'easeInQuad' );
     this.images.eq( index ).fadeTo( 1000, 1, 'easeOutQuad', function(){
       same.timeOut = setTimeout( function(){
         same.nextImage( false )
       }, same.doTimeOut ? same.options.timeOutShort : same.options.timeOutLong );
       same.doTimeOut = true;
     });
     this.bullets.removeClass( 'active' );
     this.bullets.eq( this.images.length - index - 1).addClass( 'active' )
     this.active = index;
   }
 });
 
 
