/* SSS Price Promise JS */

function random_string(len) {
	var set = "-_0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var out = "";
	for (var i=0; i<len; i++) {
		var num = Math.floor(Math.random() * set.length);
		out += set.substring(num,num+1);
	}
	return out;
}

function gotourl(w){
  window.location = w;
}

var datasafe = '<img src="./media/catalog/product/2/_/2.jpg" /><h3>Digital Lock</h3>A digital lock safe is much the same as a keylock safe, except you dont need a to keep with you, just your code. The safe comes with an emergency override key aswell';
var keysafe = '<h3>Key Lock</h3>A lock is the standard safe lock, it is double barreled to prevent drilling and comes with 2 keys.';

function showAlert(m,b,c){
  var div = $(b.id);
  var bb = div.up();
  var bit = "<div class='"+ c + "' style='display:none;' id='box' onclick=\"hideAlert('box');\"><span style='display:block;'>click this box to hide it</span>" + m + "</div><br />";
  bb.insert ({'after' : bit });
  Effect.Appear('box');
}

function hideAlert(b){
  var box = $(b);
  Effect.Fade(b, {
    afterFinish: function() { box.remove() }
  });
}

// this function gives span's,div's and li's hrefs to make them links.
function spanhref(){
  $$('span,div,li').each(function(s){
    if(s.readAttribute('href')){
      Event.observe(s, 'click', function() { gotourl(s.readAttribute('href')); });
      s.setStyle("{cursor: pointer;}");
    }
  });
}

function jumptoadd(){
  if(!productAddToCartForm.submit()){
    $('product-options-wrapper').scrollTo();
  }
}

function updateInside(e,t){
  var a = e.innerHTML;
  return a + t;
}

function give_id(ele){
  $(ele).id = random_string(23);
}

// FAQ's unobtrusive load script.
var auto_load_faqs = 1;

function faqs(){
if(auto_load_faqs){
  // get all answers and hide them + add a "hide_answer" link
  $$('ol.faqs ul').each(function(s){
    s.hide();
    ele = s.descendants()[0];
    give_id(s);
    ele.update("<div style='width: 100%; text-align: right; color: #01468b;'><a href='javascript: void(0);' style='color: #01468b;'>click here to hide</a></div>" + ele.innerHTML);
    Event.observe(s,'click', function() { $(s.id).hide(); });
  });
  
  // set it to 'show answer' on click.
  $$('ol.faqs li').each(function(s){
    s.setStyle({ cursor: 'pointer'});
    ele = s.next();
    if(ele != null){
      Event.observe(s,'click', function() { this.next().show(); });
    }
  });
}
}

var on_show = 0;

function faqs_show_all_answers(){
  $$('ol ul').each(function(s){
    if(!s.visible()){
      s.show();
    }
    on_show = 1;
  });
}

function faqs_hide_all_answers(){
 $$('ol ul').each(function(s){
    if(s.visible()){
      s.hide();
    }
    on_show = 0;
  });
}

// end faqs

function showOptions(idx){
  new Effect.SlideDown('option'+idx);
}

function innerAlert(m){
  $('analertbox').show();
  $('analertbox').innerHTML = m;
}

function is_dirty(id){
  if(id.value !== id.defaultValue){ return true; } else { return false; }
}

function is_empty(id){
  if(id.empty()){ return true; } else { return false }
}

function do_blanking(ele){
}

function build_quickshop_url(form){
}

function highlight_me(div){
  //var div = $(div);
  //div.ScrollTo();
  Effect.ScrollTo(div);
  //$(div).morph('background:#ccc');
}

function update_bit(s){
  $('where-are-we').innerHTML = s;
}

function email_someone(a,b){
  var who = prompt('Please enter the name of person you would like to email...');
  var address = prompt("what's their email address?");
  var you = prompt('And what is your name?');
  var body = "Hello "+who+",\n\n"+you+" has seen a "+a+" on chubb-safe.co.uk and would like you to take look. here is the url, "+b+"\n\nThanks!\nThe Chubb Safe Team";
  if(who && address && you){ $$('body')[0].update($$('body')[0].innerHTML + "<iframe style='display: none;' src='mailto:"+address+"?subject="+a+"&body="+body+"'></iframe>"); }
  else { alert("Sorry, there was a problem"); }
}

function slide_big_cart(){
  var ele = $('expanded-cart').show();
  Event.observe('body','click', function(){ hide_big_cart(); });
}

function hide_big_cart(){
  var ele = $('expanded-cart').hide();
}

function change_image(file){
  $('view-image-box').innerHTML = "<img src='"+file+"' width='265' height='265' />";
}

// register all events on window load.
Event.observe(window, 'load', function() { spanhref(); faqs();});

// Gregory Tomlinsons Array Chunk.
Array.prototype.chunk = function( chunkSize ) {
	var base = [], i;
	for(i=0; i<this.length; i+=chunkSize ) { base.push( this.slice( i, i+chunkSize ) ); }	
	return base;
}

function random_string(len) {
	var set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var str = "";
	for (var i = 0; i < len; i++) {
		var num = Math.floor(Math.random() * set.length);
		str += set.substring(num,num+1);
	}
  return str;
}

var shatter = {
  // shatter's settings.
  limit: 4,
  prev_text: 'Previous',
  next_text: 'Next',
  ele: 'shatter',
  tag: 'LI',
  children: { temp: [], real: [] },
  chunks: [],
  current: [],
  bound: 1,
  effect_out: 'fade',
  effect_in: 'appear',
  //effect_in: false,
  //effect_out: false,
  external_controls: 'controls',
  controls_class: { prev: 'left-control', next: 'right-control' },
  effect_time: 0.4,
  auto: 0,
  timeout: 5000,
  
  pass_element: function(){
    shatter.ele = $(shatter.ele);
  },
  
  // gives elements without one an id.
  force_id: function(s){
    if(!s.id){ s.id = random_string(); }
  },
  
  first_hide_all_children: function(ar){
    ar.each(function(s){ s.hide(); });
  },
  
  hide_all_children: function(ar){
    if(shatter.effect_out){
    
      if(shatter.effect_out == 'fade'){
        ar.each(function(s){ shatter.force_id(s); new Effect.Fade(s.id, { queue: 'end', duration: shatter.effect_time} ); });
      } else if (shatter.effect_out == 'slide'){
        ar.each(function(s){ shatter.force_id(s); new Effect.SlideUp(s.id, { queue: 'end', duration: shatter.effect_time}); });
      } else { ar.each(function(s){ s.hide(); }); }
      
    } else {
      ar.each(function(s){ s.hide(); });
    }
  },
  
  show_all_children: function(ar){
     if(shatter.effect_in){ 
        if(shatter.effect_in == 'appear'){
          ar.each(function(s){ shatter.force_id(s); new Effect.Appear(s.id, { queue: 'end', duration: shatter.effect_time}); });
        } else if (shatter.effect_in == 'slide'){
          ar.each(function(s){ shatter.force_id(s); new Effect.SlideDown(s.id, { queue: 'end', duration: shatter.effect_time}); });
        } else { ar.each(function(s){ s.show(); }); }
      } else { 
        ar.each(function(s){ s.show(); });
      }
  },
  
  make_chunks: function(){
    // force a current reset
    shatter.current = [];
    // VERY VERY convoluted, but couldn't each descendants :s?
    shatter.children.temp = shatter.ele.descendants();
    shatter.children.temp.each(function(s){ if(s.tagName == shatter.tag){ shatter.children.real.push(s); } });
    shatter.chunks = shatter.children.real.chunk(shatter.limit);
    
    if(shatter.chunks.size() > 1){
      var i = 0;
      shatter.chunks.each(function(s){
        i++;
        if(i > 1){ shatter.first_hide_all_children(s); }
      });
      shatter.buttons();
    }
    
    shatter.current = 0;
    shatter.bound = shatter.chunks.size();
  },
  
  buttons: function(){
    var next = new Element('span');
    var prev = new Element('span');
    
    next.update("<a class='"+shatter.controls_class.next+"' href='javascript:void(0)' onclick='shatter.next()'>"+shatter.next_text+"</a>");
    prev.update("<a class='"+shatter.controls_class.prev+"' href='javascript:void(0)' onclick='shatter.prev()'>"+shatter.prev_text+"</a>");
    if(shatter.external_controls){
      $(shatter.external_controls).insert({'before': prev });
      $(shatter.external_controls).insert({'after': next });
    } else {
      shatter.ele.insert({'before' : prev}); 
      shatter.ele.insert({'after' : next}); 
    }
  },
  
  next: function(){
    var n = shatter.current + 1;
    if(n >= shatter.bound){ n = 0; }
    shatter.hide_all_children(shatter.chunks[shatter.current]);
    shatter.show_all_children(shatter.chunks[n]);
    shatter.current = n;
  },
  
  prev: function(){
    var n = shatter.current - 1;
    if(n < 0){ n = shatter.bound - 1; }
    shatter.hide_all_children(shatter.chunks[shatter.current]);
    shatter.show_all_children(shatter.chunks[n]);
    shatter.current = n;
  },
  
  iint: function(){
    if(Prototype){
      if(Effect){
        shatter.pass_element();
        shatter.make_chunks();
        if(shatter.auto){
          setInterval("shatter.next()", shatter.timeout);
        }
      } else { throw "Sorry, Script.aclo.us was not found."; }
    } else { throw "Sorry, Prototype was not found."; }
  }
}
