var Core = Core || { 'behaviors': {}};
Core.event = new Object();

Core.jsEnabled = document.getElementsByTagName && document.createElement && document.createTextNode && document.documentElement && document.getElementById;


Core.attachBehaviors = function(context) {
  context = context || document;
  if (Core.jsEnabled) {
    // Execute all of them.
    jQuery.each(Core.behaviors, function() {
      this(context);
    });
  }
};


// Global Killswitch on the <html> element
if (Core.jsEnabled) {
  // Global Killswitch on the <html> element
  $(document.documentElement).addClass('js');
  // 'js enabled' cookie
  document.cookie = 'has_js=1; path=/';
  // Attach all behaviors.
  $(document).ready(function() {
    Core.attachBehaviors(this);
  });
}

Core.toggleFieldset = function(fieldset) {
  if ($(fieldset).is('.collapsed')) {
    // Action div containers are processed separately because of a IE bug
    // that alters the default submit button behavior.
    var content = $('> div.collapse-content:not(.action)', fieldset);
    
    $(".collapse-content").slideUp('fast');
	$("div.collapsible").addClass("collapsed");
	
    $(fieldset).removeClass('collapsed');
    content.hide();
    content.slideDown( {
      duration: 'fast',
      easing: 'linear',
      complete: function() {
        Core.collapseScrollIntoView(this.parentNode);
        this.parentNode.animating = false;
        $('div.action', fieldset).show();
      },
      step: function() {
        // Scroll the fieldset into view
        Core.collapseScrollIntoView(this.parentNode);
      }
    });
  }
  else {
    $('div.action', fieldset).hide();
    var content = $('> div.collapse-content:not(.action)', fieldset).slideUp('fast', function() {
      $(this.parentNode).addClass('collapsed');
      this.parentNode.animating = false;
    });
  }
};

/**
 * Scroll a given fieldset into view as much as possible.
 */
Core.collapseScrollIntoView = function (node) {
  var h = self.innerHeight || document.documentElement.clientHeight || $('body')[0].clientHeight || 0;
  var offset = self.pageYOffset || document.documentElement.scrollTop || $('body')[0].scrollTop || 0;
  var posY = $(node).offset().top;
  var fudge = 55;
  if (posY + node.offsetHeight + fudge > h + offset) {
    if (node.offsetHeight > h) {
      window.scrollTo(0, posY);
    } else {
      window.scrollTo(0, posY + node.offsetHeight - h + fudge);
    }
  }
};

Core.behaviors.collapse = function (context) {
  $('div.collapsible > div.collapse-title:not(.collapse-processed) .collapse-link', context).each(function() {
    var fieldset = $(this.parentNode.parentNode)[0];
    
    // Expand if there are errors inside
    if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
      fieldset.removeClass('collapsed');
    }

    // Turn the legend into a clickable link and wrap the contents of the fieldset
    // in a div for easier animation
    var text = this.innerHTML;
      $(this).empty().append($('<a class="toggle" onfocus="this.blur()" href="#">'+ text +'</a>').click(function() {
        var fieldset = $(this).parents('div.collapsible:first')[0];
        // Don't animate multiple times
        if (!fieldset.animating) {
          fieldset.animating = true;
          Core.toggleFieldset(fieldset);
        }
        return false;
      }));
  });
};

$(document).ready(function() {
	$('.button:not(.address, .editbutton)').live("mouseover", function(){
        var className = $(this).attr('class');
		className = className.replace("-over", "");
        $(this).removeClass(className).addClass(className + '-over');
	});
	$('.button:not(.address, .delete, .editbutton)').live("mouseout", function(){
        var className = $(this).attr('class');
        $(this).removeClass(className).addClass(className.replace("-over", ""));
	});

	$('input[type=submit]').live("mouseover", function(){
       var className = $(this).attr('class');
       if (className.indexOf('-wait') == -1) {
           	$(this).removeClass(className).addClass(className + '-over');
       }
	});
	$('input[type=submit]').live("mouseout", function(){
       var className = $(this).attr('class');
       $(this).removeClass(className).addClass(className.replace("-over", ""));
	});

});

function fixsize()
{
	if (window.outerHeight) {
		window.resizeTo(window.outerWidth - window.innerWidth + 620, window.outerHeight - window.innerHeight + document.body.clientHeight + 11);
	}
	else {
		var bodyH = document.body.clientHeight;
		window.resizeTo(screen.availWidth, screen.availHeight);
		var h = document.documentElement.clientHeight;
		window.resizeTo(652, screen.availHeight - h + bodyH);
	}
}

function UpdateCarriers() {
	if ((document.getElementById("count_item").value > 0) && (document.getElementById("count_item").value < 1000)) {
		var url = typeof(language)  == 'undefined' ? "/common/calculateshipping/getstocks" : language + "/common/calculateshipping/getstocks";
		$.post(url, $("#form").serialize(), function(data){
			$('#result').replaceWith(data).ready(function(){
				//fixsize();
			});
	    });
    }
}

function testKey(e)
{
	var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);

	if (e.ctrlKey || e.altKey || key < 32)
		return true;

	key = String.fromCharCode(key);
	//return /[\d\.]/.test(key);
	return /\d/.test(key);
}