/***********************************************************
* Copryght RHO software 2009 - wszewlkie prawa zastrzeżone *
*          www.rho.pl                                      *
* **********************************************************/

$(document).ready( function() {
  $( '.search_input' ).autocomplete('search.php',{width: 325,selectFirst: false});
  $( '#header' ).prependTo( '#page' );
  $( '.datepicker' ).datepicker( {dateFormat: 'dd-mm-yy'} ).css('width','70px');
  $( '.tabs' ).tabs();

  $( '#icoAccount, #icoForum, #icoCart, #icoOrder' ).click( function() {
                              var link = $(this).children(":last");
                              location.href = link.attr( 'href' );
                          });                       
  
  $( '#boxRand .roller' ).everyTime( 5000, function(i) {
    $( '#boxRand .roller' ).scrollTo( '.boxPrdImg:eq(1)', 1000, function() {
      var clone = $( '#boxRand .roller' ).children( '.boxPrdImg:first-child' ).clone();
      $( '#boxRand .roller .boxPrdImg:first-child' ).remove();
      clone.insertAfter( $( '#boxRand .roller' ).children( '.boxPrdImg:last-child' ) );
      $( '#boxRand .roller' ).scrollTo( '.boxPrdImg:eq(0)', 0 );
    });
  });
  
  // rozwijana lista produktów
  $( 'div.navtrail a.top' ).hover( function() {
    var _this    = this;
    var _display = $(_this).next('.list').css('display');
    
    if( _display != 'block' && !$(_this).parent().is(':first-child') ) {
      $( 'div.navtrail a.top' ).css( 'background','none' );
      $( 'div.navtrail a.top' ).css( 'color','#4D4D4D' );
      $( 'div.navtrail a.top' ).css( '-moz-box-shadow','' );
      $( 'div.navtrail a.top' ).css( '-webkit-box-shadow','' );
      $( 'ul.list' ).slideUp( 'show' ); // chowamy wszytkie listy
      
      if( !$(_this).next('ul').is('.list') ) {
        var temp = $(_this).attr('href').split( '_' );
        
        if( temp.length==3 ) {
          category = temp[1].substring(0,temp[1]);
          product = temp[2].substring(0,temp[2].length-1);
        }
        else {
          category = temp[1].substring(0,temp[1].length-1);
          product = 0;
        }
        
        if( parseInt(category) ) {
          // jezeli ostani element to pobieramy produkty
          var _file   = 'get_products.php';
          var _params = new Object();
          _params['product_id'] = product;
          _params['category_id'] = category;
          if( !$(_this).parent().is(':last-child') ) {
            _file = 'get_categories.php';
            _params['parent_id'] = category;
          }
          
          $.post( _file, _params, function(data) {  
            if( data != '' ) {  
              $( _this ).after( data );
              $( _this ).css( 'background', '#DF0000');
              $( _this ).css( 'color', '#FFFFFF');
              $( _this ).css( '-moz-box-shadow', '0px 0px 10px #4F4F4F' );
              $( _this ).css( '-webkit-box-shadow', '0px 0px 10px #4F4F4F' );
            
              $(_this).parent().mouseleave( function() {
              $(_this).next().slideUp('slow', function(){
                  $( _this ).css( 'background', 'none' );
                  $( _this ).css( 'color', '#4D4D4D' );
                  $( _this ).css( '-moz-box-shadow', '' );
                  $( _this ).css( '-webkit-box-shadow', '' );
                });
              });
            
              $( _this ).next('ul').slideDown( 'slow' );
            }
          });
        }
      }
      else {
        $( _this ).css( 'background', '#DF0000' );
        $( _this ).css( 'color', '#FFFFFF' );
        $( _this ).css( '-moz-box-shadow', '0px 0px 10px #4F4F4F' );
        $( _this ).css( '-webkit-box-shadow', '0px 0px 10px #4F4F4F' );
        $( _this ).next('ul').slideDown( 'slow' );
      }
    }
  });
});

/*
* Funkcja pokazuje lub ukrywa zawartość boxa i zapamiętuje akcje w pliku cookie
*/
function rho_show_box_content( _id_boxContent ) {
  if(  $( '#'+_id_boxContent ).css('display')=='none' ) {
    $( '#'+_id_boxContent ).slideDown();
  }
  else {
    $( '#'+_id_boxContent ).slideUp();
  }
}

/*
* Funkcja zwarca wartosc bez jednostki
*/
function rho_get_size_px( _size ) {
  var index = size.indexOf( 'p' );
  return parseInt( _size.substr(0,index) );
}

/*
* Funkcja zaznacza element _area jezeli spelniony jest warunek
*/
function rho_select_option( _id_select, _area, _value ) {
  var val = $( '#'+_id_select ).val();
  if( val != _value ) {
    $( '#'+_area ).css( {background: '#9bf192'} );
  }
  else {
    $( '#'+_area ).css( {background: 'none'} );
  }
}

/*
*
*/
function rho_create_select( _id, _options, _value, _name ) {
  var select = '<select id="'+_id+'" name="'+_id+'">';
  
  if( _options ) {
    for( var i=0; i<_options.length; i++ ) {
      select += '<option value="'+_options[i][_value]+'">'+_options[i][_name]+'</option>';
    }
  } 
  
  return select+'</select>';
}

/*
*
*/
function create_address_selector(  _id, _address, _value ) {
  if( _address ) {
    var addresses = '<div id="'+_id+'"><fieldset>';
    
    for( var i=0; i<_address.length; i++ ) {
      addresses += '<table><tr><td style="width: 20px;"><input id="'+_id+'_i" type="radio" name="'+_id+'" value="'+_address[i]['address_book_id']+'"></td><td>';
      addresses += '<ul>';
      if( _address[i]['entry_company'] != '' ) {
        addresses += '<li>'+_address[i]['entry_company']+'</li>';
      }
      addresses += '<li>'+_address[i]['entry_city']+'</li>';
      addresses += '<li>'+_address[i]['entry_postcode']+'</li>';
      addresses += '<li>'+_address[i]['entry_street_address']+'</li>';      
      addresses += '</ul></td></tr></table>';
    }
      
    return addresses + '</fieldset></div>';
  }
  else {
    return '';
  }
}

/*
*
*/
function rho_orderForUserSetEvents( _select_address ) {
  $( '#cancel' ).click( function() {
    $( '#dialogWrapper' ).hide().remove();
  });
  
  $( '#confirm' ).click( function() {
    var status   = $( '#status' ).val();
    var customer = $( '#customer' ).val();
    var address  = $( "input[@name='address']:checked" ).val();
    
    if( address ) {
      $( 'body' ).css( 'cursor', 'progress' );
      
      $( '#checkout_confirmation' ).append( '<input type="hidden" name="customers_status_id" value="'+status+'">' );
      $( '#checkout_confirmation' ).append( '<input type="hidden" name="customers_id" value="'+customer+'">' );
      $( '#checkout_confirmation' ).append( '<input type="hidden" name="customers_address_id" value="'+address+'">' );
      
    
      var form = document.getElementById( 'checkout_confirmation' );
      form.submit();   
    }
    else {
      alert( _select_address );
    }
  });
  
  
  $( '#customer' ).change( function() {
    $( 'body' ).css( 'cursor', 'progress' );
      
    $.get( 'rho_get_addresses.php', {customer: $('#customer').val()}, function(data) {
      var json      = eval( "("+data+")" );
      var addresses = create_address_selector( 'address', json.addresses );
          
      if( $( '#address' ).length > 0 ) {
        $( '#address' ).replaceWith( addresses );
      }
      else {
        $( '#dialog .form li:last-child' ).append( addresses );
      }
          
      $( 'body' ).css( 'cursor', 'default' );
    });
  });
    
  $( '#status' ).change( function() {
    $( 'body' ).css( 'cursor', 'progress' );
      
    $.get( 'rho_get_customers.php', {status: $('#status').val()}, function(data) {
      var json      = eval( "("+data+")" );
      var customers = rho_create_select( 'customer', json.customers, 'id', 'name' );
      var addresses = create_address_selector( 'address', json.addresses );
          
      $( '#customer' ).replaceWith( customers );
          
      if( $( '#address' ).length > 0 ) {
        $( '#address' ).replaceWith( addresses );
      }
      else {
        $( '#dialog .form li:last-child' ).append( addresses );
      }
      
      $( '#customer' ).change( function() {
        $( 'body' ).css( 'cursor', 'progress' );
      
        $.get( 'rho_get_addresses.php', {customer: $('#customer').val()}, function(data) {
          var json      = eval( "("+data+")" );
          var addresses = create_address_selector( 'address', json.addresses );
          
          if( $( '#address' ).length > 0 ) {
            $( '#address' ).replaceWith( addresses );
          }
          else {
            $( '#dialog .form li:last-child' ).append( addresses );
          }
          
          $( 'body' ).css( 'cursor', 'default' );
        });
      });
          
      $( 'body' ).css( 'cursor', 'default' );
    });
  });
}

/*
* Okno wyboru klienta przy składaniu zamówienia
*/
function rho_orderForUser( _title, _group, _customer, _select_address, _cancel, _order_for_customer, _create_account ) {
  $( 'body' ).css( 'cursor', 'progress' );
  
  $.get( 'rho_get_customers_status.php', {}, function(data) {
    var json = eval( "("+data+")" );
    
    var cancel    = '<button id="cancel">'+_cancel+'</button>';
    var ok        = '<button id="confirm">'+_order_for_customer+'</button>';
    var groups    = rho_create_select( 'status', json.statuses, 'id', 'name' );
    var customers = rho_create_select( 'customer', json.customers, 'id', 'name' );
    var addresses = create_address_selector( 'address', json.addresses );
    
    $( 'body' ).append( '<div id="dialogWrapper"><table><tr><td><div id="dialog"><h3><span id="title">'+_title+'</span></h3></div></td></tr></table></div>' );
    $( '#dialog' ).append( '<div id="form" class="form"><p style="text-align: center; margin: 4px 0px;"><a onclick="target=\'new\'" href="admin/create_account.php"><button>'+_create_account+'</button></a></p><ul><li><label>'+_group+'</label>'+groups+'</li><li><label>'+_customer+'</label>'+customers+'</li><li>'+addresses+'</li></ul><div id="submit">'+cancel+ok+'</div></div></div>' );
    $( 'body' ).css( 'cursor', 'default' );
    
    $( '.tabs' ).tabs();
    
    rho_orderForUserSetEvents( _select_address );
  }); 
}