var hexed = function(color) {

	var hex = function(N) {
	  if(N==null) return "00";
	  N = parseInt(N);
	  if(N==0 || isNaN(N)) return "00";
	  N = Math.max(0, N);
	  N = Math.min(N, 255);
	  N = Math.round(N);
	  return "0123456789ABCDEF".charAt((N - N%16) / 16) + "0123456789ABCDEF".charAt(N%16);
	};

  if(!color) { return false; }
  if(color.search('rgb') > -1) {
    color = color.substr(4,color.length-5).split(', ');
    color = hex(color[0]) + hex(color[1]) + hex(color[2]);
  }
  color = color.replace('#','');
  if(color.length < 6) {
    color = color.substr(0, 1) + color.substr(0, 1) + color.substr(1, 1) + color.substr(1, 1) + color.substr(2, 1) + color.substr(2, 1);
  }
  return color;
};
  
(function($) {
	var up = '<a href="#" class="up" alt=""><img src="/images/ico/harrow-up.png" /></a>',
	    down = '<a href="#" class="down" alt=""><img src="/images/ico/harrow-down.png" /></a>'; 

  function doInc(o, i, l)
  {
  	v = eval (o.val() + i);
    min = o.attr('min') ? o.attr('min') - 1 : -1;
    max = o.attr('max') ? o.attr('max') : 5;

  	if (v <= max && v > min)
  	{
  		o.val(v);
  	}
  }

  $.fn.cyclic = function () {
  	this.each( function () {
	  	o = $(this);
	  	o.attr('readonly','readonly');
	    	$(this).after($(down).bind('click', { o:o }, function(e){ e.preventDefault(); doInc(e.data.o, '-1') }));
	    	$(this).after($(up).bind('click', { o:o }, function(e){ e.preventDefault(); doInc(e.data.o, '+1') }));
  	});
  }
})(jQuery);

function checkOut()
{
	$(window).attr('location', env + '/cliente/endereco');
}

$(document).ready(function() {
  $('#cart-header').click(function(e){
  	e.preventDefault();
  	var $off = $(this).offset(),
  	    $t = $.browser.msie ? (parseInt($.browser.version) == 6 ? 21 : 23) : 20,
  	    $l = $.browser.msie ? 0 : 1;
  	
  	$off.top += $t;
  	$off.left += $l;
  	
  	if ($('#cart-main:visible').get(0))
    {
  		$('#cart-main').slideUp('fast');
  	}
    else
    {
    	src = $(window).attr('location').href.indexOf('https://') != -1 ? ssl_env : env;
//      $.get(src + '/cliente/cartIndex', $off);
      $.ajax({
      	url: src + '/cliente/cartindex',
      	method: 'GET',
      	data: $off,
      	global: false,
        beforeSend: function () { 
          $('#cartico').attr('src', "/images/indicator.gif");
        }
      });
    }
  }).corner("8px top");

  cbOptions = {
    comboboxContainerClass: "comboboxContainer",
    comboboxValueContentContainerClass: "comboboxValueContainer",
    comboboxValueContentClass: "comboboxValueContent",
    comboboxDropDownClass: "comboboxDropDownContainer",
    comboboxDropDownButtonClass: "comboboxDropDownButton",
    comboboxDropDownItemClass: "comboboxItem",
    comboboxDropDownItemHoverClass: "comboboxItemHover",
    comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader",
    comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer"
  };

  $('.box').corner('5px');
  $(document).pngFix();
});

$(document).bind("ajaxStop", function(){
	$('.box').corner('5px');
});
$().ajaxStart($.blockUI).ajaxStop($.unblockUI);
$.taconite.debug = false;
$.blockUI.defaults.message = '<h2><img src="/images/ajax-loader.gif" /></h2>';
$.blockUI.defaults.css = {
  padding:        0,
  margin:         0,
  width:          '30%', 
  top:            '40%', 
  left:           '35%', 
  textAlign:      'center', 
  color:          '#000', 
  backgroundColor:'transparent',
  cursor:         'wait'
};
$.blockUI.defaults.overlayCSS = {
	backgroundColor:'#fff', 
	opacity:        '0.3' 
};
$.blockUI.defaults.applyPlatformOpacityRules = false;
$.blockUI.defaults.fadeOut = 0;

var delayt;
function delayed(what, time)
{
	delayt = setTimeout(function() {
		if (typeof what == "string") {
			eval(what);
		} else {
			what();
		}
		clearTimeout(delayt);
	}, time);
}