/*window.onload = index_do;
function index_do(){
 flowplayer("player", "/assets/video/flowplayer-3.1.5.swf",{
 clip: {
 autoPlay: false,
 onStart: function(clip){stop_music();}
 }});
}
*/
// check if browser have cookies enabled
if(!navigator.cookieEnabled)
{
  alert('Your browser has cookies disabled, please activate it to achieve the best performans of site!');
}
 

function hideRightMenu(){

    /*$("#dailynews").children().hide();
    $("#dailynews_block").hide();
    $("#specialoffers").children().hide();
    $("#specialoffers_block").hide();
    $("#fordproject").children().hide();
    $("#fordproject_block").hide();
    $("#fordservices").children().hide();
    $("#fordservices_block").hide();
    $("#partexchenge").children().hide();
    $("#partexchenge_block").hide();
    $("#financecalc").children().hide();
    $("#financecalc_block").hide();*/
    $("#search").children().hide();
    $("#search_block").hide();
    $("#compare").children().hide();
    $("#compare_block").hide();
}

function viewTab(name, id){
    hideRightMenu();
    $("#"+name+"_block").show();
    $("#"+name).children().show();
    $.cookie("rb_menu", id, { path: '/' });
}


$(function() {

  $("#car_gallery").jCarouselLite({
    btnNext: "#right_arrow",
    btnPrev: "#left_arrow",
    visible: 9
  });

  $("#fos_gallery").jCarouselLite({
    btnNext: ".fos_right",
    btnPrev: ".fos_left",
    visible: 4
  });

  $('.car_img_big').lightBox({fixedNavigation:true});

  $('.fos_gallery').lightBox({fixedNavigation:true}); 

// init table sorter

  $("#grid_service").tablesorter({widgets: ['zebra'], headers: {            
            0: { sorter: false }, 
            1: { sorter: false },            
            2: { sorter: false }
          }             
        } );

  $("#grid").tablesorter({widgets: ['zebra']} );  

  $("#grid_service tbody").hide();

  $("#grid_service thead").click(function(){
    if($(this).parent().children('tbody').hasClass('hidden')){
      $(this).parent().children('tbody').slideDown();
      $(this).parent().children('tbody').removeClass('hidden');
      $(this).parent().children('tbody').addClass('show');
    }else {
      $(this).parent().children('tbody').slideUp();
      $(this).parent().children('tbody').removeClass('show');
      $(this).parent().children('tbody').addClass('hidden');
    }
  });

  $("#viewservices").click(function(){
     if($("#grid_service tbody").hasClass('hidden')){
      $("#grid_service tbody").slideDown();
      $("#grid_service tbody").removeClass('hidden');
      $("#grid_service tbody").addClass('show');
    }else {
      $("#grid_service tbody").slideUp();
      $("#grid_service tbody").removeClass('show');
      $("#grid_service tbody").addClass('hidden');
    }
  });

// vehicles hover show nav buttons

$("div[class ^= 'car_carusel_']").each(function(){
    $(this).hover(
        function(){
          $(this).children(".next_car").show();
          $(this).children(".prev_car").show();
        },
        function(){
          $(this).children(".next_car").hide();
          $(this).children(".prev_car").hide();
        });

     $(this).carousel( { direction: "vertical" , nextBtn: "<div style='display:none;' class='next_car'></div>", prevBtn: "<div style='display:none;' class='prev_car'></div>" } );
});

// Right block menu 

switch($.cookie("rb_menu")){
  case "1": {$("#compare_block").show();$("#compare").children().show();break;}
  case "2": {$("#search").children().show();$("#search_block").show();break;}
  /*case "3": {$("#financecalc").children().show();$("#financecalc_block").show();break;}
  case "4": {$("#partexchenge").children().show();$("#partexchenge_block").show();break;}
  case "5": {$("#fordservices").children().show();$("#fordservices_block").show();break;}
  case "6": {$("#fordproject").children().show();$("#fordproject_block").show();break;}
  case "7": {$("#specialoffers").children().show();$("#specialoffers_block").show();break;}
  case "8": {$("#dailynews").children().show();$("#dailynews_block").show();break;}*/
  default: {$("#compare_block").show();$("#compare").children().show();break;}
}

/*
$("#dailynews").click(function(){
    viewTab('dailynews', 8);
});

$("#specialoffers").click(function(){
    viewTab('specialoffers', 7);
});

$("#fordproject").click(function(){
    viewTab('fordproject', 6);
});

$("#fordservices").click(function(){
    viewTab('fordservices', 5);
});

$("#partexchenge").click(function(){
    viewTab('partexchenge', 4);
});

$("#financecalc").click(function(){
    viewTab('financecalc', 3);  
});
*/

$("#search").click(function(){
    viewTab('search', 2);   
});

$("#compare").click(function(){
    viewTab('compare', 1);
});


});


/////////////////////////////////////////////////////////////////////////////
//                                                                         //
//  Compare feature functions                                              //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

$(function(){

  $(".compare_list").each(function(){
    $(this).click(function(){       
      var id = $(this).parent().parent().parent().parent().attr('id');
      var model = $(this).parent().parent().parent().parent().children(".table_element_title").text();
      var img = $(".car_carusel_"+id+" ul li img").attr('src');
      var desc = $(this).parent().parent().parent().parent().children(".table_element_description").text();
      var reg = $(this).parent().parent().parent().parent().children(".car_description").children(".car_description_text").children(".car_description_text_cat_sec_1").children(".regspan").children().text();

      addToCompareList(id, model, img, desc, reg);
      viewTab('compare', 1);// display comapre tab
      return false;
    });    
  });
  
  appendItemsToCompare();
});

//******************************************************************************
function addToCompareList(id, model, img, desc, reg){
  var maxItems = 3;
  var item = [maxItems];
  
/*   $.cookie("cItemNr", 0, { path: '/' });   
    $.cookie("compareItem1", "", { path: '/' });
$.cookie("compareItem2", "", { path: '/' });
$.cookie("compareItem3", "", { path: '/' });
*/
  if(!$.cookie("cItemNr") || parseInt($.cookie("cItemNr")) == 0){
    $.cookie("cItemNr", 1, { path: '/' });   
    $.cookie("compareItem1", id+"|"+model+"|"+img+"|"+desc+"|"+reg, { path: '/' });
    appendItemsToCompare();
  }else{
    
    if(maxItems > $.cookie("cItemNr")){
      var next = parseInt($.cookie("cItemNr")) + 1;
      var splited1 = $.cookie("compareItem1").split("|");
      // check if no identic vehicles added to compare list
      if(splited1[0] == id) {alert("Can not add same item twice to compare list!");return false;}
      if(next == 3) {
        var splited2 = $.cookie("compareItem2").split("|");
        if(splited2[0] == id) {alert("Can not add same item twice to compare list!");return false;}
      }      

      $.cookie("cItemNr", next, { path: '/' });
      $.cookie("compareItem"+next, id+"|"+model+"|"+img+"|"+desc+"|"+reg, { path: '/' });

      appendItemsToCompare();
    }else {
      alert("Can not add more then "+maxItems+" items, please remove some items from compare list to add another.");
    }
  }

//alert("Items: "+$.cookie('cItemNr')+" \n Body1="+$.cookie("compareItem1")+" \n Body2="+$.cookie("compareItem2")+" \n Body3="+$.cookie("compareItem3"));
}

// ******************* append compare list to tab block **************************************
function appendItemsToCompare(){
    var compare_cont = "";

    if($.cookie("compareItem1")){
      var splited1 = $.cookie("compareItem1").split("|");
      compare_cont = "<table class='compare_grid'><tr><th>"+splited1[1]+"</th></tr><tr><td><img src='"+splited1[2]+"' width='120' height='90' />Registration: <b>"+splited1[4]+"</b><br />"+splited1[3]+"</td></tr><tr><td><a class='rm' onclick='rmItem(1)'>X remove item</a></td></td></table>";
    }
    if($.cookie("compareItem2")){
      var splited2 = $.cookie("compareItem2").split("|");
      compare_cont = compare_cont +"<table class='compare_grid'><tr><th>"+splited2[1]+"</th></tr><tr><td><img src='"+splited2[2]+"' width='120' height='90' />Registration: <b>"+splited2[4]+"</b><br />"+splited2[3]+"</td></tr><tr><td><a class='rm' onclick='rmItem(2)'>X remove item</a></td></td></table>";
    }
    if($.cookie("compareItem3")){ 
      var splited3 = $.cookie("compareItem3").split("|");
      compare_cont = compare_cont + "<table class='compare_grid'><tr><th>"+splited3[1]+"</th></tr><tr><td><img src='"+splited3[2]+"' width='120' height='90' />Registration: <b>"+splited3[4]+"</b><br />"+splited3[3]+"</td></tr><tr><td><a class='rm' onclick='rmItem(3)'>X remove item</a></td></td></table>";
    }

  if(compare_cont == "") compare_cont = "<p align='center' style='font: bold 15px arial;color: #2B1A0D;'>Choose vehicles to compare them.</p>";
  else compare_cont = "<table><tr><td>" + compare_cont + "</td></tr><tr><td align='right'><a style='color:#000;cursor:pointer;font:bold 13px Arial;margin-right: 10px;' onclick='removeCompareItems()'>Clear list</a></td></tr></table>";
  $("#compare_block_2").html(compare_cont);
}

//************** remove item from compare list ******************************************
function removeCompareItems(){
  
  $.cookie("cItemNr", null); 
  $.cookie("compareItem1", null);
  $.cookie("compareItem2", null);
  $.cookie("compareItem3", null);
 
  appendItemsToCompare();
  return false;
}

function rmItem(id){


  var nr = parseInt($.cookie("cItemNr")) - 1;
  $.cookie("cItemNr", nr);
  var item1 = $.cookie("compareItem1");
  var item2 = $.cookie("compareItem2");
  var item3 = $.cookie("compareItem3");
  
  switch(id){
    case 1:{
            $.cookie("compareItem1", item2);
            $.cookie("compareItem2", item3);
            $.cookie("compareItem3", null);
            break;}
    case 2:{
            $.cookie("compareItem2", item3);
            $.cookie("compareItem3", null);
            break;}
    case 3:{            
            $.cookie("compareItem3", null);
            break;}
  }



  appendItemsToCompare();
  return false;
}

/* append a random username */
function saveUsername(){
  var numRand = Math.floor(Math.random()*9999999);
  $("input[name='username']").val("user_"+numRand);
  return true;
}

function regBox(){
  if($.cookie("regbox") != 1){
    $.cookie("regbox", "1", { expires: 999, path: '/' });
    $( "#regbox" ).dialog({               
		height: 140,
		modal: true
    });
  }        
}
$(function() {
// registration dialog
//regBox();
});




