var basket_view = 'hide';


fn_ECOM_sortby_select = function(){

  sort = $('sort_by');
  query_action = $('form_action_val').value;
  destination = query_action + "&sort_by=" + sort.options[sort.selectedIndex].value;
  if(destination) location.href = destination;
}

fn_ECOM_initialise_basket = function(){
  $('basket_items').style.display = 'none';
  var basket_view = 'hide';
}
fn_ECOM_remove_basket_item = function(prod_id){

  new Ajax.Updater
  (
  'module_dynamic_basket',
  path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_basket_functions.php',
  {
    onComplete:function(request)
    {
      if(basket_view == 'show'){
        $('basket_items').style.display = 'block';
      }
      fn_ECOM_basket_status(basket_view);
      var removeBtns = $$('.cartRemove');
      removeBtns.each(
      function(btn){
        Event.observe(btn, 'click', function(event){Event.stop(event)}, false);
      }
      )
    },
    parameters:"function=ECOM_basket_remove_item&product_id="+prod_id,
    evalScripts:true,
    asynchronous:true
  }
  );
}
fn_ECOM_add_basket_item = function(prod_id){

  var quantity;
  if($('quantity')){
    quantity_obj = $('quantity');
    quantity = quantity_obj.options[quantity_obj.selectedIndex].value;
  }

  if($('ProductOptions')){
    //alert('options available');
    fnConfigProductOptions($('ProductOptions'),quantity);
  }

  new Ajax.Updater
  (
  'module_dynamic_basket',
  path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_basket_functions.php',
  {
    onComplete:function(request)
    {
      if(basket_view == 'hide'){
        //
        // BASKET IS CURRENTLY CLOSED OPEN/CLOSE
        fn_ECOM_showHide_basket();

      }else{
        //
        //BASKET IS OPEN - LEAVE OPEN
        $('basket_items').style.display = 'block';
        fn_ECOM_basket_status(basket_view);
      }

    },
    parameters:"function=ECOM_basket_add_item&product_id="+prod_id+"&quantity="+quantity,
    evalScripts:true,
    asynchronous:true
  }
  );
}
fn_ECOM_basket_status = function(status){

  if(status == 'hide'){

    $('cartControl').onclick = function(){
      fn_ECOM_show_basket(); return false;
    }
    $('cartControl').className = 'expandCart';
    $('cartControl').innerHTML = 'Open';

  }else{

    $('cartControl').onclick = function(){
      fn_ECOM_hide_basket(); return false;
    }
    $('cartControl').className = 'closeCart';
    $('cartControl').innerHTML = 'Close';
  }
  SkimButtons();
}
fn_ECOM_show_basket = function(){

  fn_hide_selects();
  if(basket_view != 'show'){
    new Effect.BlindDown('basket_items',{queue:'end',duration:1.0});
  }
  basket_view = 'show';
  fn_ECOM_basket_status(basket_view);

}
fn_ECOM_hide_basket = function(){

  fn_hide_selects();
  if(basket_view != 'hide'){
    new Effect.BlindUp('basket_items',{queue:'end',duration:1.0});
  }
  basket_view = 'hide';
  fn_ECOM_basket_status(basket_view);

}
/* AUSTIQUE SETUP */
fn_ECOM_basket_status_austique = function(status){

  if(status == 'hide'){

    $('cartControl').onclick = function(){
      fn_ECOM_show_basket_austique(); return false;
    }
    $('cartControl').className = 'expandCart';
    $('cartControl').innerHTML = 'Open Basket';

  }else{

    $('cartControl').onclick = function(){
      fn_ECOM_hide_basket_austique(); return false;
    }
    $('cartControl').className = 'closeCart';
    $('cartControl').innerHTML = 'Close Basket';
  }
  SkimButtons();
}
fn_ECOM_show_basket_austique = function(){

  fn_hide_selects();
  if(basket_view != 'show'){
    new Effect.BlindDown('basket_items',{queue:'end',duration:1.0});
  }
  basket_view = 'show';
  fn_ECOM_basket_status_austique(basket_view);

}
fn_ECOM_hide_basket_austique = function(){

  fn_hide_selects();
  if(basket_view != 'hide'){
    new Effect.BlindUp('basket_items',{queue:'end',duration:1.0});
  }
  basket_view = 'hide';
  fn_ECOM_basket_status_austique(basket_view);

}

var c=0
var t
var tobegin=0;
function timedCount()
{
  tobegin=1;
  $('debug').innerHTML = c;
  c=c+1;
  t=setTimeout("timedCount()",1000);
  if(c > 3){
    if(bs != 0){
      setTimeout("fn_ECOM_hide_basket();",610);
      c=0;
      tobegin=0;
      clearTimeout(t);
    }
    bs = 0;
  }
}
var bs = 0;
fn_ECOM_slideBasket = function(){
  if(bs == 0){
    fn_ECOM_show_basket();
    bs = 1;
  }else{
    /*
    if(tobegin != 1){
    timedCount();
    }
    */
  }
}

fn_ECOM_showHide_basket = function(){
  new Effect.BlindDown('basket_items',
  {
    onComplete:function()
    {
      //alert('here');
      //new Effect.BlindUp('basket_items',{duration:0.6});
    },
    duration:0.6
  }
  );
  setTimeout("new Effect.BlindUp('basket_items',{queue:'end',duration:0.6});",610);

}

fn_ECOM_initialise = function(){
  Event.observe('module_dynamic_basket','mouseover',fn_ECOM_show_basket,false);
  //Event.observe(document,'mouseover',fn_ECOM_hide_basket);
}
fn_hide_selects = function(){

  BrowserDetect.init();
  //var el = $('quantity');
  var browser = BrowserDetect.browser;
  var version = BrowserDetect.version;

  //
  // Hide drop downs in IE 6
  if(browser == 'Explorer' && version == '6') {

    var tag = 'select';
    var fClass = '';

    var el = (tag == "*" && document.all) ? document.all : document.getElementsByTagName(tag);

    var o=new Array();

    for (var i=0;i<el.length;i++)	{

      el[i].style.visibility = (el[i].style.visibility == "hidden") ? "visible" : "hidden";

    }

  }

}
fnSwapProductVersion = function(url,tag){
  var sel = $(tag);
  var pid = sel.options[sel.selectedIndex].value;
  location.href = url+pid;
}

fnConfigProductOptions = function(theform,quantity){

  for (i=0; i<theform.elements.length; i++){

    if (theform.elements[i].type=="select-one"){


      new Ajax.Updater
      (
      'module_dynamic_basket',
      path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_basket_functions.php',
      {
        onComplete:function(request)
        {


        },
        parameters:"function=ECOM_basket_add_item&product_id="+theform.elements[i].value+"&quantity="+quantity,
        evalScripts:true,
        asynchronous:true
      }
      );

    }
  }

}
fnUpdatePrice = function(){

  //var sel = $(tag);
  //var pid = sel.options[sel.selectedIndex].value;
  //var costs = $('additionalCosts').innerHTML;
  var pidar = '';
  var theform = $('ProductOptions');

  //
  // Get the selected options productIDS
  for (i=0; i<theform.elements.length; i++){

    if (theform.elements[i].type=="select-one"){

      pidar = pidar + "@"+theform.elements[i].value

    }

  }


  new Ajax.Updater
  (
  'RunningTotal',
  path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_product_functions.php',
  {
    onComplete:function(request)
    {

    },
    parameters:"function=updatePrice&product_ids="+pidar,
    evalScripts:true,
    asynchronous:true
  }
  );


}
fnUpdatePriceOld = function(tag){

  var sel = $(tag);
  var pid = sel.options[sel.selectedIndex].value;
  var costs = $('additionalCosts').innerHTML;
  var pidar;


  for (i=0; i<theform.elements.length; i++){

    if (theform.elements[i].type=="select-one"){

      pidar = pidar + "@"+theform.elements[i].value;

    }

  }


  new Ajax.Updater
  (
  'additionalCosts',
  path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_product_functions.php',
  {
    onComplete:function(request)
    {

    },
    parameters:"function=updatePrice&product_id="+pid+"&costs="+costs+"&option="+sel.name,
    evalScripts:true,
    asynchronous:true
  }
  );


}

fn_triggerVariationEvent = function(theForm,sel)
{

  //$('attribute_options').innerHTML = "<img src=\""+path_to_root+"/media/gif/page_gif/ajax-loader.gif\">";
  //document.getElementById(il).options[document.getElementById(il).selectedIndex].value;
  //
  var num_elements = theForm.elements.length;
  for(i=0; i<num_elements; i++){

    if(theForm.elements[i].type == 'select-one'){
      theForm.elements[i].options[theForm.elements[i].selectedIndex].value = encodeURIComponent(theForm.elements[i].options[theForm.elements[i].selectedIndex].value);
    }else{
      theForm.elements[i].value = encodeURIComponent(theForm.elements[i].value);
    }
  }

  new Ajax.Updater
  (
  'module_dynamic_proddetail',
  path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_product_functions.php',
  {
    onComplete:function(request)
    {
      //fn_GetAttributes($('product_id').value);<br>
      $('jsOptions').style.display = 'block';
      initLightbox();		/* RE INITIALISE THE LIGHTBOX */
      if(typeof auto_insert_flash == 'function')
      auto_insert_flash();

      SkimButtons();
      fn_reset_coming_soon_form();
    },
    asynchronous:true,
    parameters:Form.serialize(theForm) + "&sel=" + sel.id
  }
  );


}

fn_GetAttributes = function(prod_id)
{
  new Ajax.Updater
  (
  'attribute_options',
  path_to_root + 'includes/logic/cms_scripts/php/ecom/ECOM_product_functions.php',
  {
    onComplete:function(request)
    {
    },
    parameters:"function=ajaxAttUpdate&product_id="+prod_id,
    evalScripts:true,
    asynchronous:true
  }
  );
}
//
// Disable links and buttons ensuring ajax takes over
function SkimButtons(){
  if($('cartControl')){
    Event.observe($('cartControl'), 'click', function(e){Event.stop(e)}, false);
  }
  // Basket remove buttons
  var removeBtns = $$('.cartRemove');
  removeBtns.each(
  function(btn){
    Event.observe(btn, 'click', function(event){Event.stop(event)}, false);
    //btn.onclick = function() {return false;};
  }
  )
  // Buy now button
  var buyBtn = $$('.product_buy_now')
  buyBtn.each(
  function(btn){
    Event.observe(btn.down(), 'click', function(event){Event.stop(event)}, false);
  }
  )
}

//Event.observe(window, 'load', fn_ECOM_initialise, false);
Event.observe(window, 'load', function(){
  SkimButtons();
},false);

// Reset form fields on change
function fn_reset_coming_soon_form () {

  // Clear any error message red text
  change_text = document.getElementById("enq_form_name");
  change_text.style.color = "#C6AFD3";

  change_text = document.getElementById("enq_form_email");
  change_text.style.color = "#C6AFD3";

  change_text = document.getElementById("enq_form_tel");
  change_text.style.color = "#C6AFD3";

  if ($('CustomerName').value){
    $('CustomerName').value = "";
  }
  if ($('CustomerEmail').value){
    $('CustomerEmail').value = "";
  }
  if ($('CustomerTel').value){
    $('CustomerTel').value = "";
  }
}
