// Event Handlers
Core.event.updateTransportBegin = function (sender, addressId) {
	$('#make_an_order').removeClass('orange').addClass('disabled').removeAttr("onclick").unbind('click');
	$('.total, .step4 .title, .step4 .stepcontent').remove();
	$('.step2, .step3').removeClass("active").removeClass("complete").addClass("inactive");
	$('.step2 .stepcontent, .step3 .stepcontent').html('');
	
	if (addressId != ''){
		if (addressId == 0) {
			$('#addresslist div.collapsible.collapsed#address_block_new').each( function () {
				Core.toggleFieldset(this);
			});
		}
		else {
			$('#addresslist div.collapsible:not(.collapsed)').each( function () {
				Core.toggleFieldset(this);
			});
		}
	}
};

Core.event.updateTransportEnd = function (sender,data,addressId) {
	if (addressId != 0) {
		$('.step1').addClass("complete").removeClass("active").removeClass("inactive");
		$('.step2').addClass("active").removeClass("inactive").removeClass("complete");
	}
	else {
		$('.step1').addClass("active").removeClass("inactive").removeClass("complete");
	}
	$('.step2 .stepcontent').html(data).ready(function() {
		Core.attachBehaviors(this);
        $('.step2 .title').html($('.step2 #step2name').html());
	});
	
	$("#carriers-list").show();

	setActivStep("checkout_step2_title");
	document.getElementById('checkout_step2_tag').scrollIntoView(true);
};

Core.event.updateCheckoutProductsBegin = function (sender, deliveryId) {
	$('#make_an_order').removeClass('orange').addClass('disabled').removeAttr("onclick").unbind('click');
	$('.total, .step4 .title, .step4 .stepcontent').remove();
	$('.step2, .step3').removeClass("active").removeClass("complete").addClass("inactive");
	$('.step2 .stepcontent, .step3 .stepcontent').html('');
	
	if (deliveryId != ''){
		if (deliveryId == 0) {
			$('#addresslist div.collapsible.collapsed#address_block_new').each( function () {
				Core.toggleFieldset(this);
			});
		}
		else {
			$('#addresslist div.collapsible:not(.collapsed)').each( function () {
				Core.toggleFieldset(this);
			});
		}
	}
};

Core.event.updateCheckoutProductsEnd = function (sender,data,deliveryId) {
	if (deliveryId != 0) {
		$('.step1').addClass("complete").removeClass("active").removeClass("inactive");
		$('.step2').addClass("active").removeClass("inactive").removeClass("complete");
	}
	else {
		$('.step1').addClass("active").removeClass("inactive").removeClass("complete");
	}
	$('.step2 .stepcontent').html(data).ready(function() {
		Core.attachBehaviors(this);
        $('.step2 .title').html($('.step2 #step2name').html());
	});
	document.getElementById('checkout_step2_tag').scrollIntoView(true);
};


Core.event.setCarrierBegin = function (sender,stockId, carrierId) {
	$('.step2 div.collapsible:not(.collapsed)').each( function () {
		Core.toggleFieldset(this);
	});
};

Core.event.setCarrierEnd = function (sender,data) {
	$('.step2 .stepcontent').html(data).ready(function() {
		Core.attachBehaviors(this);
		if (!$('.store:not(.deliveryset)').length) {
			UpdatePayment (this);
		}
	});	
};

Core.event.updatePaymentBegin = function (sender, data) {
	//$('#make_an_order').removeClass('orange').addClass('disabled').removeAttr("onclick").unbind('click');
	$('.total, .step4 .title, .step4 .stepcontent').remove();
	$('.step3').removeClass("active").addClass("inactive");
	$('.step3 .stepcontent').html('');
};

Core.event.updatePaymentEnd = function (sender,data) {
	$('.step2').removeClass("active").removeClass("inactive").addClass("complete");
	$('.step3').removeClass("inactive").removeClass("complete").addClass("active");
	$('.step3 .stepcontent').html(data).ready(function() {
		Core.attachBehaviors(this);
	});
	
	$("#payments-list").show();
	setActivStep("checkout_step3_title");
	document.getElementById('checkout_step3_tag').scrollIntoView(true);
	//$('div.title').css('color', '#CCCCC');
	//$('div#checkout_step3_title').css('color', '#E45304');
	
};

Core.event.setPaymentEnd = function (sender,data) {
	$('.step3 .stepcontent').html(data.block).ready(function() {
		Core.attachBehaviors(this);
	});  
	if (data.not_cash){ // checking is it beznal ru
		UpdateBankDetails(sender);
	}else {
		UpdateTotal(sender);
	}
	setActivStep("none");
	//setActivStep("checkout_step2_title");
	document.getElementById('make_an_order').scrollIntoView(false);
	scrollBy(0,50);
};

Core.event.updateBankDetailsBegin = function(sender, data) {
	$('#make_an_order').removeClass('orange').addClass('disabled').removeAttr("onclick").unbind('click');
	$('.total, .step4 .title, .step4 .stepcontent').remove();
	$('.step4').removeClass("active").addClass("inactive");
	setActivStep("checkout_step4_title");
};

Core.event.updateBankDetailsEnd = function(sender, data) {
	$('.step3').removeClass("active").removeClass("inactive").addClass("complete");
	$('.step4').removeClass("inactive").removeClass("complete").addClass("active");
	$('.step4').replaceWith(data.step4);
	$('.bottom-buttons').replaceWith(data.total_buttons);	
	$('.step4').removeClass("active").removeClass("inactive").addClass("complete");
	$('.step4 .stepcontent').ready(function() {
		Core.attachBehaviors(this);
	});
	setActivStep("none");
	document.getElementById('make_an_order').scrollIntoView(false);
};

Core.event.updateTotalBegin = function (sender,data) {
	$('#make_an_order').removeClass('orange').addClass('disabled').removeAttr("onclick").unbind('click');
	$('.step4').removeClass("active").addClass("inactive");
	$('.total, .step4 .title, .step4 .stepcontent').remove();
};

Core.event.updateTotalEnd = function (sender,data) {
	$('.step3').removeClass("active").removeClass("inactive").addClass("complete");
	$('.bottom-buttons').replaceWith(data).ready(function() {
		Core.attachBehaviors(this);
	});
};

Core.event.makeAnOrderBegin = function (button, data) {
	$(button).removeAttr('onclick').removeClass('orange').addClass('wait');
};

function onSetCarrierClick (stockId, carrierId){
	if (carrierId != null){
		SetCarrier(this, stockId, carrierId);
	}	
}

function onSetPaymentClick (paymentId){
	if (paymentId != null){
		$('.step3 div.collapsible:not(.collapsed)').each( function () {
			Core.toggleFieldset(this);
		});
		SetPayment(this,paymentId);
	}
}

function setActivStep(stepId) {
	$("#checkout_step1_title").css('color', '#000000');
	$("#checkout_step2_title").css('color', '#000000');
	$("#checkout_step3_title").css('color', '#000000');
	$("#checkout_step4_title").css('color', '#E45304');
	if(stepId != "none") {
		$("#"+stepId).css('color', '#E45304');
	}	
}

Core.behaviors.selectors = function (context) {
	
	$('#addresses .addresslist .collapse-title', context).unbind('click').click(function(){
		$(".addresslist .collapse-title").css("fontWeight", "normal");
		$(this).css("fontWeight", "bold");		
		$('h3').css('color', '#000000');
		updateTransport($("input",this).attr('checked', true).val());
	});
	
	$('#selfdelivery .addresslist .collapse-title', context).unbind('click').click(function(){
		$(".addresslist .collapse-title").css("fontWeight", "normal");
		$(this).css("fontWeight", "bold");
		$('h3').css('color', '#000000');
		updateCheckoutProducts($("input",this).attr('checked', true).val());
	});
	
	$('.carriers tr', context).unbind('click').click(function(){
		$("input",this).attr('checked', true);
	});
	
	$('.carriers tr', context).mouseout(function(){
		$(this).css("background-color", "#EFEFEF");
	});
	
	$('.carriers tr', context).mouseover(function(){
		$(this).css("background-color", "#ffffcc");
	});
	
	$('.carriers-rows').mouseout(function(){
		$('.carriers-button-wrapper', this).hide();
	});
	
	$('.carriers-rows').mouseover(function(){
		$('.carriers-button-wrapper', this).show();
	});
		
	$('div.address').click(function(){
		var checked_address = $("input[@name=address]:checked").val();
		if(checked_address){
			$("#carriers-list").show();
		}
	});
	
	/*$('input.address-radio').click(function(){
		$('h3').css('color', '#000000');
		//$('div.title').css('color', '#CCCCC');
		//$('div#checkout_step2_title').css('color', '#E45304');
	});*/
};
