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')) {
		if($(fieldset).parents('#addresslist').length){
			$('#addresslist div.collapsible:not(.collapsed)').each( function () {
				Core.toggleFieldset(this);
			});
		}
    // 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).slideDown('fast', function() {
    	$(this.parentNode).removeClass('collapsed');
    	this.parentNode.animating = false;
    	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.deleteWrap = function (context) {
	$('div.collapsible > div.collapse-title:not(.collapse-processed) .deleteAddressButton', context).each(function() {
		$(this).click(function(){
			return false;
		});
	});
};

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) {
			className = className.replace("-over", "");
           	$(this).removeClass(className).addClass(className + '-over');
       }
	});
	$('input[type=submit]').live("mouseout", function(){
       var className = $(this).attr('class');
       $(this).removeClass(className).addClass(className.replace("-over", ""));
	});

        //if ($(".free-delivery-notification-cart").css('display')=='none') $(".free_delivery_block").css('display','block');
	
});

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 + '?noHelpers=true', $("#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);
}

function _updateFreeDeliveryNotification(jsonFreeDeliveryNotification){
        console.log(jsonFreeDeliveryNotification);
	if (jsonFreeDeliveryNotification['Style'].search('none') != -1 && jsonFreeDeliveryNotification['CartDiscount'] == 1){
		$(".free-delivery-notification").css('display','none');
		$(".free-delivery-notification-cart").css('display','none');
		$(".free_delivery_block").css('display','block');
	}
	else if (jsonFreeDeliveryNotification['CartDiscount'] == 2) {
		$(".free_delivery_block").css('display','none');
		$(".free-delivery-notification").css('display','block');
		$(".free-delivery-notification-cart").css('display','block');
		$(".cart-amount", ".free-delivery-notification").text(jsonFreeDeliveryNotification.CartAmount);
		$(".request-amount", ".free-delivery-notification").text(jsonFreeDeliveryNotification.RequestedAmount);
	}
        else if (jsonFreeDeliveryNotification['CartDiscount'] == 0) {
                $(".free_delivery_block").css('display','none');
                $(".free-delivery-notification").css('display','none');
		$(".free-delivery-notification-cart").css('display','none');
        }
}
