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


function handleResponse(documentObject,stringToEvaluate) {
window.eval(stringToEvaluate);
if (documentObject.location){ // Should be usefull for IE only .. but I cannot test it
if(documentObject.location != "") documentObject.location.replace('about:blank');
}
}

function set_order_color(color_id, model_id) {

 //clear highlighting from other color blocks
 $$('.color_block').each(function(s) { s.setStyle({ 'border': '0px solid rgb(256,234,0)' })})  
  
 //Highlight correct element
 element = "order_color_"+color_id; 
 $(element).setStyle({ 'border': '1px solid rgb(0,0,0)' });
 
 //Put the id in the hidden form field
 $("order_form[color_id]").value = (color_id);
 
 //Change Large Model Image
 $("model_layer").src = "/wordansfiles/models/"+model_id+"/"+color_id+"-front.jpg";
  
}

function set_order_model(model_id, default_color_id,offset_x,offset_y,local_name) {

 //clear highlighting from other color blocks
 $$('.model_block').each(function(s) { s.setStyle({ 'border': '0px solid rgb(256,234,0)' })})
 
 //Highlight correct element
 element = "order_model_"+model_id; 
 $(element).setStyle({ 'border': '1px solid rgb(0,0,0)' });
 
 //Change Large Model Image
 $("model_layer").src = "/wordansfiles/models/"+model_id+"/"+default_color_id+"-front.jpg";
     
 //Update the colors
 $("color_links").innerHTML = colors_available[model_id];
 
 //Update the price
 $("order_price").innerHTML = model_prices[model_id]

 //Update price if needed
 var price = $("order_price").innerHTML.split('$')[1].split(' ')[0];
 var size = $('order_form_size_id').options[$('order_form_size_id').selectedIndex].value;
 var new_price = parseInt(price) + parseInt(size_prices[size-1]);
 $('order_price').innerHTML = $("order_price").innerHTML.replace(price, new_price);

 //Update model id in onchange event
 $("order_form_size_id").onchange = "";
 $('order_form_size_id').stopObserving('change');
 $("order_form_size_id").observe("change", function(){
     new Ajax.Request("/display/update_price/"+model_id+"/"+$('order_form_size_id').options[$('order_form_size_id').selectedIndex].value, {asynchronous:true, evalScripts:true});
 });
     
     
 //Put the model and default color ids in the hidden form fields
 $("order_form[model_id]").value = (model_id);
 $("order_form[color_id]").value = (default_color_id);

 $("model_desc").innerHTML = local_name;
 
  set_design_position(offset_x,offset_y);
  
}

function set_design_position(offset_x, offset_y) {
   var design_layer_width = $("design_layer").width;
   var model_layer_width = $("model_layer").width;
   var model_layer_offset_x = $("model_layer").getStyle('left').gsub('px', ''); 
   model_layer_offset_x = parseInt(model_layer_offset_x);

   design_layer_offset_y = 125+parseInt(offset_y);
   design_layer_offset_x = ((model_layer_width/2) - (design_layer_width/2) + model_layer_offset_x) + offset_x;
   $("design_layer").setStyle({left: design_layer_offset_x+"px"});
   $("design_layer").setStyle({top: design_layer_offset_y+"px"});
}

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {

        Element.show('spinner');

        //document.getElementById("spinner").style.display = 'block';
        new Ajax.Request(el.href, { method: 'get', onSuccess: function (transport) { Element.hide('spinner');} })
        e.stop()
        //document.getElementById("spinner").style.display = 'none';
      }
    })
  }
})

