function product_image_cycle_init() {
  var imgs = $("#product-main-images");

  if (imgs && imgs.size() > 0) {
    var initialSlide = 0;
  /*
    activ = jQuery("#product-image img.activeSlide");
    if (activ && activ.size() > 0) {
      initialSlide = activ.parent().children().index( activ[0] );
    }
    */

    $('#product-main-images').cycle({
      fx:     'fade',
      speed:  'fast',
//      timeout: 0,
      pager:  '#product-image-nav',
      startingSlide: initialSlide,
      // callback fn that creates a thumbnail to use as pager anchor
      pagerAnchorBuilder: function(idx, slide) {
				slide = $(slide).children('img')[0];
        var src = String(slide.src).replace('product_preview', 'product_thumb');
          return '<li><a href="#"><img src="' + src + '" title="'+slide.title+'" alt="'+slide.alt+'" width="50" height="50" /></a></li>';
      }
    });
  }
}

function product_update_image_height() {
  var max_h = 0;
  $("#product-main-images img").each(function() {
  var h = $(this).height();
  if (h > max_h) {
    max_h = h;
  }
  });
  if (max_h < 415 && max_h > 200) {
    max_h += 10;
    $("#product-main-images").css('height', max_h+'px');
  }
}

function on_radio_click(r) {
  var radio_id = $(r).attr('id');
  var id_arr = radio_id.split('-');
  var group_id = 'attribute-'+id_arr[2];

  $('.'+group_id +' label').removeClass('active');
	if ($.browser.msie){
		$('.form-radios label[htmlFor='+radio_id+']').addClass('active');
	} else {
  	$('.form-radios label[for='+radio_id+']').addClass('active');
	}

	checkKeyHooks(r);
  
	product_update_price(product_get_nid(), product_get_attrs());
}
function product_get_nid() {
  return Drupal.settings.product.nid;
}
function product_get_attrs() {
  var attr_ids = (Drupal.settings.product.attrs) ? Drupal.settings.product.attrs : 0;
  var combo = '';
  if (attr_ids) {
  for (aid in attr_ids) {
    if (combo!='') { combo+='|'; }
      val = $('.attribute-'+aid+' input').fieldValue();
    combo += aid+';'+val[0];
    }
  }
  return combo;
}

function product_cart_init() {
  $("label.option").each(function() {
    var checked = $('input', $(this)).attr('checked');
    $(this).removeClass('active');
    if (checked) {
      $(this).addClass('active');
    }
  });
}

function product_update_price(nid, combo) {
  var url = "/action/get-price/"+nid;
  var params = { 'combo': combo };

  $.getJSON(url, params, function(R) { product_update_price_callback(R, nid, params); });
}
function product_update_price_callback(R, nid, params) {
  if (R.status) {
    $("#form-product-price").html(R.data.price);
    $("#product-price-meta .price-retail .price").html(R.data.retail);
    $("#product-price-meta .price-sale .price").html(R.data.price);

    if (R.data.callbacks != '') {
      eval(R.data.callbacks);
    }
  }
}

function showArtworkUpload(bShow) {
  if (bShow) {
    $("#site-specific-add-artwork-upload-form").slideDown();
  }
  else {
    $("#site-specific-add-artwork-upload-form").slideUp('fast');
  }
}
function checkKeyHooks(r){
	if (!r)	r = '[name="attributes[3]"]';
	
	var radio_value = $(r).attr('value');
	$('#edit-attributes-2-10-wrapper').css('display',(radio_value==1)?'block':'none');
	$('#edit-attributes-2-9-wrapper').css('display',(radio_value==4)?'block':'none');
}


$(document).ready(function() {
  product_image_cycle_init();
  product_cart_init();
  product_update_price(product_get_nid(), product_get_attrs());
  product_update_image_height();
	checkKeyHooks('');
 
	var evt = $.browser.msie ? "click" : "change";
	var radios = ".attribute .form-radio";
	if ($.browser.msie){
		$(radios).css({'z-index':'1000','display':'block','float':'left','margin-left':'-20px'});
	}
	$(radios).bind(evt, function() {
		var radio_id = $(this).attr('id');
		var id_arr = radio_id.split('-');
		var group_id = 'attribute-'+id_arr[2];
	
		$('.'+group_id +' label').removeClass('active');
		if ($.browser.msie){
			$('.form-radios label[htmlFor='+radio_id+']').addClass('active');
		} else {
			$('.form-radios label[for='+radio_id+']').addClass('active');
		}
	
		checkKeyHooks(this);
		
		product_update_price(product_get_nid(), product_get_attrs());
	});
});