// =============================================================================
//  Entry Point
// =============================================================================

var imgGal = null;
var param1 = null;
var param2 = null;

document.addEvent('domready', function(){

  var siteDesktop = new cfDesktop( 'site-desktop', {
    width: 850,    
    hAlign: 'center',      
    vAlign: 'top',     
    minWidth:850,         
    minHeight:650,         
    areas: {'top': {'id': 'site-top', 'height':100},
            'left': {'id': 'site-left', 'width':190},
            'center': {'id': 'site-center'}                         
           }               
  });     

  new cfShape( $('header'), {           
    width:850, 
    height:100,  
    begin: [0,0], 
    background: '#000',
    borderColor: '582828', 
    street: [      
      { 'drv':'lineTo', 'xy':[850,0] },
      { 'drv':'curveAt', 'xy':[850,45], 'r':10 },
      { 'drv':'curveAt', 'xy':[300,45], 'r':10 },
      { 'drv':'curveAt', 'xy':[300,100], 'r':10 },                  
      { 'drv':'curveAt', 'xy':[0,100], 'r':10 },
      { 'drv':'lineTo', 'xy':[0,0], 'r':10 }                  
    ]         
  });  
	
  loadCategorie();  
  RichiestaHome(); 
  SetTopMenu();  
});

// =============================================================================
//  Richiama l'elenco categorie
// =============================================================================

function loadCategorie() {
  $('collezioni').empty();
  
  cfRequestJSON( './bin/qdata.php?act=categorie', function( o ) { 
    if (o.answer.errnum == 0) {            
      var buf = '<ul>';            
      o.answer.ris.each( function(v) {
         buf += '<li><a href="#" onclick="onCategorieClick(\'' + v.id + '\');return false;">'+
                  v.Descrizione +
                '</a></li>';
      });   
      buf += '</ul>';
      $('collezioni').innerHTML = buf;        
    }
    else {  
      cfShowMessageEl( $('left-debug'), o.answer.errdesc );      
    }  
  }); 
}

function onCategorieClick( CodCat ) {
   
  $('site-center').empty();
  if (imgGal != null) imgGal = null;   

  cfRequestJSON( './bin/qdata.php?act=bagslist&cid=' + CodCat, function( o ) { 
     
    if (o.answer.errnum == 0) { 
    
      var iList = [];
      for (i = 0; i < o.answer.ris.length; i++) {
        iList[i] = {'img': o.answer.ris[i].codice + '.jpg', 'value': o.answer.ris[i] };      
      }   

      imgGal = new cfImageGallery( $('site-center'), {
        left:5, 
        top:10,                 
        width:630, 
        height:500,
        lheight:80,
        nrPics:5,
        imgsPath: '../bags',    
        imgsList: iList,
        phpThumbFile: 'libs/cf-image.php',              
        onImgChange: function( v ) { CompileInfo( v.el, v.ref ); }
      });            
    }
    else {  
      cfShowMessageEl( $('left-debug'), o.answer.errdesc );      
    }  
  });  
}
// =============================================================================
//  Richiama i menu Top
// =============================================================================

function SetTopMenu() {    
  
  cfRequestJSON( './bin/sessts.php', function(o) {
      if (o.answer.errnum == 1) {                   
        $('toptoolsmenu').innerHTML = "<li><a href='#' onclick='RichiestaCateg(); return false;'>Categorie</a></li>" +
                                      "<li><a href='#' onclick='RichiestaBorse(); return false;'>Borse</a></li>" + 
                                      "<li><a href='#' onclick='RichiestaNews(); return false;'>News</a></li>";
      }
      else {  
        $('toptoolsmenu').innerHTML = ''; 
      }  
  });  
}

// =============================================================================
//  Ricompila Info Immagine
// =============================================================================

function CompileInfo( el, ref ) {  
  //el.setStyles({'left': ref[0], 'top': ref[1] });
  //if (ref[2] > 0) el.setStyles({'width': ref[2]});
  
  el.innerHTML = "Codice: <b>" + ref.codice + "</b><br />"+
                 "<b>" + ref.descrizione + "</b><br />"+
                 "Materiale: <b>" + ref.materiale + "</b><br />"+
                 "Dimensione: <b>" + ref.dim + "</b><br />"+
                 "Disponibile: <b>" + ((ref.venduta!=0)?"No":"Si") + "</b>";                             
}

// =============================================================================
//  Form di invio mail
// =============================================================================

function RichiestaLogin() {
 cfRequestJSON( './bin/sessts.php', function(o) {
      if (o.answer.errnum == 1) {
        var r = confirm("Vuoi davvero abbandonare la sessione?");
        if ( r ) {      
          cfRequestJSON( './bin/sessts.php?logout=1', function(o) {                   
            $('toptoolsmenu').innerHTML = "";
            RichiestaHome();                
          });
        }
      }
      else {  
        $('site-center').empty();  
        $('site-center').load('./html/login.html'); 
      }  
  });
}

// =============================================================================
//  Pagine
// =============================================================================

function RichiestaHome() {
  $('site-center').empty();  
  $('site-center').load('./html/home.html');  	
}

function RichiestaInfo() {
  $('site-center').empty();  
  $('site-center').load('./html/info.html');
}

function RichiestaCateg() {
  $('site-center').empty();  
  $('site-center').load('./html/catlist.html');
}

function RichiestaNews() {
  $('site-center').empty();  
  $('site-center').load('./html/newslist.html');
}

function RichiestaBorse() {
  $('site-center').empty();  
  $('site-center').load('./html/bagslist.html');
}

function RichiestaDispo() {
  onCategorieClick( -1 );
}

function RichiestaCategAll() {
  $('site-center').empty();  
  $('site-center').load('./html/catall.html');
}

