// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Methods for default expanding navigation
function setupNavigation( container_id ) {
  var container = $(container_id);
  
  if( ! container ) {
    return false;
  }
  
  $$('#' + container_id + ' > .nav_element a').each(function(arg) {
    if( arg.readAttribute('href') == '#' ) {
      arg.onclick = navigationElementClicked;
    }
  });
  
}

function navigationElementClicked( event ) {
  var target = Event.element(event);
  target.parentNode.toggleClassName('active');
  return false;
}

function buttonRemoteLoading( element ) {
  var button = $(element);
  button.writeAttribute('oldText', button.readAttribute('value') );
  button.value = "Please wait";
  button.disabled = true;
}

function buttonRemoteDone( element ) {
  var button = $(element);
  button.value = button.readAttribute('oldText');
  button.writeAttribute("oldText", '');
  button.disabled = false;
}

function adminCollapsibleToggle( ele, target ) {
  var ele = $(ele);
  var target = $(target);

  // if( target.style.display == 'none' ) {
  //   window.location.hash = ele.name;
  // }
  // 

  Effect.toggle(target, 'appear',{});
}

function ship_to_billing_clicked() {
  var disabled = document.information_form.ship_to_billing_address.checked;
  if( document.information_form['shipping_address_id'] ) {
    if( disabled ) {
      document.information_form['shipping_address_id'].value = "";
      document.information_form['shipping_address_id'].disabled = true;
    } else {
      document.information_form['shipping_address_id'].disabled = false;
    }
  }
	['first_name', 'last_name', 'line2', 'line3', 'city', 'province', 'postal_code', 'country_id'].each(function(field){
    if( disabled ) {
      document.information_form['shipping_address['+field+']'].value = document.information_form['billing_address['+field+']'].value;
    }
    document.information_form['shipping_address['+field+']'].disabled = disabled;
  });
}

function ship_to_billing_update(ele){
	to_update = $(ele.id.replace('billing','shipping'));
	to_update.value = ele.value;
}

function set_card_type(number){
  var logo = undefined;
  // remove spaces and hyphens
  cc_number = number.replace(/[ -]/g,"");
  // initially the card type is unknown
  cardtype = '';
  // define card names and their matching patterns
  ccArray = {"visa" : "^4[0-9]{12}(?:[0-9]{3})?$",
  "mastercard" : "^5[1-5][0-9]{14}$",
  "discover" : "^6011[0-9]{12}$",
  "amex" : "^3[47][0-9]{13}$"};
  
  // identify the card type
  for (key in ccArray) {
    regex = new RegExp(ccArray[key]);
    if (regex.test(cc_number)) {
      cardtype = key;
      break;
    }
  }

  //Now that we have the cardtype make it set type automatically
  logos=new Array();
  logos=document.getElementsByName("credit_card_img");
  for(i=0;i < logos.length;i++){    
    logos[i].style.opacity="1.0";
  }
  cardTypeId=document.getElementById("credit_card[credit_card_type_id]");
  cardTypeId.value=0; 
  switch(cardtype){
    case "visa":        
      for(i=0;i < logos.length;i++){
        if(logos[i].id == "1")logo=logos[i];
      }
      break;
    case "mastercard":
      for(i=0;i < logos.length;i++){
        if(logos[i].id == "2")logo=logos[i];
      }
      break;
    case "discover":
      for(i=0;i < logos.length;i++){
        if(logos[i].id == "3")logo=logos[i];
      }
      break;
    case "amex":
      for(i=0;i < logos.length;i++){
        if(logos[i].id == "4")logo=logos[i];
      }
      break;
    default:
      logo = undefined;
      break;
  }
  for(i=0;i < logos.length;i++){        
    if(logos[i] != logo){       
      logos[i].style.opacity="0.15";
    }else{
      logos[i].style.opacity="1.0"; 
      cardTypeId=document.getElementById("credit_card[credit_card_type_id]");
      cardTypeId.value=logo.id;           
    }
  }
}

function updateProductSkuDetailResult( sku, price, stock_message, image_path ) {
  if( $('product-sku') ) {
    Element.update('product-sku', sku );
  }
  
  if( $('product-price') ) {
    Element.update('product-price', price);
  }
  
  if( $('stock-message') ) {
    Element.update('stock-message', stock_message);
  }  
  updateDetailImage(image_path);
}

function updateDetailImage( image_path ) {
  if( $('detail-image') && image_path != '' ) {
    $('detail-image').src = image_path;
  }  
}

function updateZoomLink( image_path ) {
	if( $$('.zoom-link') && image_path != '' ) {
		$$('.zoom-link').each(function(e) {
			e.href = image_path;
		});
	}
}

/*** Product Search ***/
function autoCompleteSelected(ele){
	product_id = ele.id.split('-')[1];
	window.location = "/products/"+product_id;
}

function validateSearch(text) {
	text = text.toLowerCase();
  input = document.getElementById('terms').value.toLowerCase();
  if (input == "" || input == text) {
    alert("You must enter your search keywords first.");
    document.getElementById('terms').focus(); 
    return false; 
  }else{
    return true;
  }
}

/*** Scroll DIV position ***/
//TODO: Still needs checks for the y_axis
function slide(id, width, x_axis, y_axis){
	div = document.getElementById(id);
	left_position = parseInt(div.style.left.replace('px',''));
	if( (left_position + x_axis) > 0 ){
		new Effect.Move(id,{
			x: ( (width*-1) + x_axis ), y: y_axis, mode: 'relative',
			transition: Effect.Transitions.spring
		});
	}else if( (left_position + x_axis) <= (width*-1) ){
		new Effect.Move(id,{
			x: (width + x_axis), y: y_axis, mode: 'relative',
			transition: Effect.Transitions.spring
		});
	}else{
		new Effect.Move(id,{
			x: x_axis, y: y_axis, mode: 'relative',
			transition: Effect.Transitions.spring
		});
	}
}

/*** Scroll DIV position ***/
//TODO: Still needs checks for the y_axis
function linear_slide(id, width, x_axis, y_axis){
	div = document.getElementById(id);
	left_position = parseInt(div.style.left.replace('px',''));
	if( (left_position + x_axis) > 0 ){
		new Effect.Move(id,{
			x: ( (width*-1) + x_axis ), y: y_axis, mode: 'relative',
			transition: Effect.Transitions.linear
		});
	}else if( (left_position + x_axis) <= (width*-1) ){
		new Effect.Move(id,{
			x: (width + x_axis), y: y_axis, mode: 'relative',
			transition: Effect.Transitions.linear
		});
	}else{
		new Effect.Move(id,{
			x: x_axis, y: y_axis, mode: 'relative',
			transition: Effect.Transitions.linear
		});
	}
}


function decrement_character_count( src, dest ) {
    var dest = $(dest);
    var src = $(src);
    dest.innerHTML = src.maxLength - src.value.length;
}

