$(function() {
	
	$("div.article-expander a, div.article-collapser a").click(function() {
		var expandedClass = "expanded";
		if ($("div.pagetype-webshop").hasClass(expandedClass)) {
			$("div.pagetype-webshop").removeClass(expandedClass);
		} else {
			$("div.pagetype-webshop").addClass(expandedClass);
		}
		return false;
	});



	$("div.login div.step1 a.login_step").click(function() {
		$("div.eshop div.login div.step").hide();
		$("div.eshop div.login div.step2").show();
		return false;
	});
	
	/* Christian removed this, no longer in use.   
	$("div.login div.step2 div.white_button_small a").livequery('click', function() {
		$("div.eshop div.login div.step").hide();
		$("div.eshop div.login div.step3").show();
		return false;
	});
	*/
	
	$("div.delivery input.deliverytype").livequery('click', function() {
		$("div.delivery div.delivery_options").removeClass("options_expanded");
		$("div.delivery div.deliveryrow").removeClass("active_row");
		var parent = $(this).parents("div.deliveryrow:eq(0)");
		parent.addClass("active_row").find("div.delivery_options").addClass("options_expanded");
		
		//Används för "Apotek eller apoteksombud"
		var get0 = parent.find(':input').not(this).get(0);
		var get1 = parent.find(':input').not(this).get(1);
		if (get0) {
			get0.checked = true;
		}
		if (get1) {
			get1.focus();
		}
		else {
			try {
				this.blur();
				this.focus();
			}catch(e){}
		}
	});
	$("div.payment input.paymenttype").click(function() {
		$("div.payment div.payment_options").removeClass("options_expanded");
		$("div.payment div.paymentrow").removeClass("active_row");
		$(this).parents("div.paymentrow:eq(0)").addClass('active_row').find("div.payment_options").addClass("options_expanded");
	});
	
	$("div.banks").show();
	$("div.cards").show();
	
	$("div.payment select.bank_select").change(function() {
		$('div.payment div.banks div.bank').removeClass("bank_active").filter('.'+$(this).find("option:selected").attr("class")).addClass('bank_active');
		this.blur();
		this.focus();
	});
	$("div.payment select.card_select").change(function() {
		var cardtype = $(this).attr("value");
		$('div.payment div.cards div.card').removeClass('card_active').filter('.'+cardtype).addClass('card_active');
		$('div.payment a.cvvcode').hide().filter('.'+cardtype).show();
		this.blur();
		this.focus();
	});
	
	$("div.cart input.focus_input").click(function() {
		if (this.checked) {
			var input = $(this).siblings('input[type=text]').get(0);
			setTimeout(function() { //Timing issue with validation if no setTimeout
				input.focus();
			}, 0);
		}
	});
	
	var usermessage = $("div.user_message");
	if (usermessage.size() > 0 && usermessage.val() == '' && $("div.address input.userinput_message:checked").size() == 0) { //usermessage exists, value is empty and associated triggering-checkbox is NOT checked
		usermessage.hide();
	}
	$("div.address input.userinput_message").click(function() {
		$("div.address div.user_message").toggle();
	});
	
	$('div.productlist div.prodpic img').click(function() {
		$(this).parents('div.product').find('h3 a').click();
	});
	
	$('a.ajax-link').livequery('click', function() {
		if(this.href) $.get(this.href);
		return false;
	});
	
	$('table.agreement td.product div.important_note h4').click(function() {
		$(this).next('p').toggleClass('closed');
	});
	/* end filtrera .productlist */


    /** One phone number has to be entered if the customer wants Rådgivning */
    if ($("span.advicetype input").length > 0) {
        var adviceRadioButtons = $("span.advicetype input");
        adviceRadioButtons.change(function() {
            if ($("span.advicetype input:first").is(":checked")) {
                $('#validator-Address div.group div:eq(2)').addClass('exclude'); 
            } else {
                $('#validator-Address div.group div:eq(2)').removeClass('exclude'); 
            }
        });
    }
	// document ready end
});
