//var currentTab='auto';
function refreshProduct(el,tableName,id) { 
  $.ajax({
    type: "POST",
    url: "index.php",
    data: "ajax=1&class=prodotto&method=getById&params="+tableName+";"+id+";true",
    success: function(data) {
    //alert(data);
      data=data.split('|');
      $(el+" .container").css("background-image","url("+data[0]+")");
      $(el+" .container").html("<a href=\""+data[1]+"\"><img src=\"images/blank.gif\"></a>");
    }
  });
}
$(function(){ 

  $("#header a").each(function() {
    $(this).mouseover(function() {        
      $(this).css("background","#b00");  
    }); 
    $(this).mouseout(function() {   
      $(this).css("background","transparent");
    });  
  });

  $('#slider').nivoSlider({
        effect:'fold', // Specify sets like: 'fold,fade,sliceDown'
        slices:15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed:500, // Slide transition speed
        pauseTime:3000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:false, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav:false, // Use left & right arrows
        pauseOnHover:true, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });
  

  $("a[rel=gal]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
    'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
  $("a#contact").fancybox({
    'height' : '50%',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
    'scrolling'   : 'no'
	});




  $("#autotab").click(function() { currentTab=$(this).attr("rel");});
  $("#mototab").click(function() { currentTab=$(this).attr("rel");});
  $("#search").click(function() {
    var marca=$("#moto #marca").val();
    var modello=$("#moto #modello").val();
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=prodotto&method=showResults&params="+currentTab+";id_marca="+marca+";id_modello="+modello+";true",
      success: function(data) {         
        $("#main").html(data);
        $("#main-menu a.selected").removeClass("selected");
        $("#main-menu #"+currentTab+">li>a").addClass("selected");
      }
    });
  });
  

  $("ul.dropdown li").hover(function(){        
      $(this).addClass("hover");
      $('ul:first',this).css('visibility', 'visible');         
  }, function(){     
      $(this).removeClass("hover");
      $('ul:first',this).css('visibility', 'hidden');
  
  });       
  $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

  $("select").each(function() {
        $(this).selectmenu({style:"dropdown",maxHeight: 150});
  });
  $("button").each(function() {
        $(this).button();
  });
  //alert(currentTabId);
  if(currentTabId===undefined) { 
    var currentTabId=0;
  }
  $( "#filters" ).tabs({selected: currentTabId });     
  $( "#filters" ).css("visibility","visible"); 
  $( "#auto #marca" ).change(function() {
    var marca=$(this).val();
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=helper&method=table2option&params=modelli_auto;id;modello;id_marca;"+marca+";true;prodotti_auto;id_modello",
      success: function(data) {
        $("#auto #modello")
                    .html(data)
                    .selectmenu("destroy")
                    .selectmenu({style:"dropdown",maxHeight: 150})
                    .selectmenu("enable");
      }
    });   
  });
  
  $( "#moto #marca" ).change(function() {
    var marca=$(this).val();
    if(marca=='') return;
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=helper&method=table2option&params=modelli_moto;id;modello;id_marca;"+marca+";true;prodotti_moto;id_modello",
      success: function(data) {
        
        $("#moto #modello")
                    .html(data)
                    .selectmenu("destroy")
                    .selectmenu({style:"dropdown",maxHeight: 150})
                    .selectmenu("enable");
      }
    });  
    $( ".moto #id_marca" ).change(function() {
      var marca=$(this).val();
      if(marca=='') return;
      $.ajax({
        type: "POST",
        url: "index.php",
        data: "ajax=1&class=helper&method=table2option&params=modelli_moto;id;modello;id_marca;"+marca+";true;prodotti_moto;id_modello",
        success: function(data) {
          
          $(".moto #id_modello")
                      .html(data)
                      .selectmenu("destroy")
                      .selectmenu({style:"dropdown",maxHeight: 150})
                      .selectmenu("enable");
        }
      }); 
    }); 
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=prodotto&method=search&params=prodotti_moto;id_marca="+marca+";0;2;true",
      success: function(data) {  
        //alert(data);      
        var ids=data.split(";");
        for(var i in ids) {
          if(ids[i]=='') continue;
          refreshProduct('#search-result'+(parseInt(i)+1),'moto',ids[i]);
        }
      }
    });    
  });
  
  
  
  $( "#auto #marca" ).change(function() {
    var marca=$(this).val();
    if(marca=='') return;
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=helper&method=table2option&params=modelli_auto;id;modello;id_marca;"+marca+";true;prodotti_auto;id_modello",
      success: function(data) {
        
        $("#auto #modello")
                    .html(data)
                    .selectmenu("destroy")
                    .selectmenu({style:"dropdown",maxHeight: 150})
                    .selectmenu("enable");
      }
    });  
    $( ".auto #id_marca" ).change(function() {
      var marca=$(this).val();
      if(marca=='') return;
      $.ajax({
        type: "POST",
        url: "index.php",
        data: "ajax=1&class=helper&method=table2option&params=modelli_auto;id;modello;id_marca;"+marca+";true;prodotti_auto;id_modello",
        success: function(data) {
          
          $(".auto #id_modello")
                      .html(data)
                      .selectmenu("destroy")
                      .selectmenu({style:"dropdown",maxHeight: 150})
                      .selectmenu("enable");
        }
      }); 
    }); 
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=prodotto&method=search&params=prodotti_auto;id_marca="+marca+";0;2;true",
      success: function(data) {  
        //alert(data);      
        var ids=data.split(";");
        for(var i =0;i<=2;i++) {
          if(ids[i]=='') continue;
          refreshProduct('#search-result'+(parseInt(i)+1),'auto',ids[i]);
        }
      }
    }); 
    
    
  });
  
  
  
  
  
});


tinyMCE.init({
			mode : "exact",
			elements : "tinymce",
			theme : "advanced",
			plugins : "advimage,advlink,media,contextmenu,autosave",
			theme_advanced_buttons1_add : "fontsizeselect",
			theme_advanced_buttons2_add : "separator,forecolor,backcolor",
			theme_advanced_buttons3_add : "media",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			extended_valid_elements : "hr[class|width|size|noshade]",
			file_browser_callback : "ajaxfilemanager",
			paste_use_dialog : false,
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : true,
			apply_source_formatting : true,
			force_br_newlines : true,
			force_p_newlines : false,	
			relative_urls : true
		});

		function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = "../../../../js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";
			var view = 'detail';
			switch (type) {
				case "image":
				view = 'thumbnail';
					break;
				case "media":
					break;
				case "flash": 
					break;
				case "file":
					break;
				default:
					return false;
			}
            tinyMCE.activeEditor.windowManager.open({
                url: "../../../../js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php?view=" + view,
                width: 782,
                height: 440,
                inline : "yes",
                close_previous : "no"
            },{
                window : win,
                input : field_name
            });
            
/*            return false;			
			var fileBrowserWindow = new Array();
			fileBrowserWindow["file"] = ajaxfilemanagerurl;
			fileBrowserWindow["title"] = "Ajax File Manager";
			fileBrowserWindow["width"] = "782";
			fileBrowserWindow["height"] = "440";
			fileBrowserWindow["close_previous"] = "no";
			tinyMCE.openWindow(fileBrowserWindow, {
			  window : win,
			  input : field_name,
			  resizable : "yes",
			  inline : "yes",
			  editor_id : tinyMCE.getWindowArg("editor_id")
			});
			
			return false;*/
		}






















