$(document).ready(function(){

    $('input[type="button"]').addClass("button");
    $('input[type="submit"]').addClass("button");

    if(jQuery.trim($('#system_message').html()) != ''){
        $('#system_message').show();
    }
    if(jQuery.trim($('#error_message').html()) != ''){
        $('#error_message').show();
    }

	//* fix IE7 z-index bug
    var zIndexNumber = 300;
    $('.outerdiv div').not($("div[id^=fancybox]")).each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 1;
	});
    zIndexNumber = 300;
	$('.product_matrix div').not($("div[id^=fancybox]")).each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 1;
	});//*/
    $('.product_matrix div').css('zoom', 1);
	// reset correct z-index on some elements
    $('#footer').css('zoom', 1);
    $('#video').css('zIndex', 201);
    $('#videoCloseButton').css('zIndex', 203);
    $('.column_740').css('zIndex', 100);
    $('#videoPanel').css('zIndex', 202);
	$('#register').css('zIndex', 2002);
    $('#register_holder .close').css('zIndex', 2003);
    $('#forgot_password').css('zIndex', 2002);
    $('#forgot_password_holder .close').css('zIndex', 2003);

	$('#open_forgot_password').click(function(){
		$('#forgot_password').show();
		$('#forgot_password_holder .close').show();
	});
	$('#forgot_password_holder .close').click(function(){
		$('#forgot_password').hide();
		$('#forgot_password_holder .close').hide();
	});

	$('#open_register').click(function(){
		$('#register').show();
		$('#register_holder .close').show();
	});
	$('#register_holder .close').click(function(){
		$('#register').hide();
		$('#register_holder .close').hide();
	});

	$('.product_opener_closer').click(function(){
		var el = $(this).closest('.product_selector_container');
        toggle_drop(el);
	});
    $('span.selected_value').click(function(){
		var el = $(this).closest('.product_selector_container');
        toggle_drop(el);
	});
    $('.arrow').each(function() {
		$(this).click(function(e) {
            e.stopPropagation();
            var el = $(this).closest('.product_selector_container');
            toggle_drop(el);
		});
	});
	$('.product_drop_container a').each(function() {
		$(this).click(function(e) {
			e.stopPropagation();
			var el = $(this).closest('.product_selector_container');
			toggle_drop(el);

			var selval = $(this).html();
			var index = selval.toLowerCase().indexOf("<span>");
			if(index > 0)
				el.find(".selected_value").html(selval.substring(0,index));
			else
				el.find(".selected_value").html(selval);

			el.find(".selected_value").attr('name',$(this).attr('name'))
		});
	});
    function toggle_drop(container){
        if(!container.hasClass('lowered')){
            container.find(".product_drop").show();
            container.find(".product_drop_container").jScrollPane({
                scrollbarWidth : 8,
                scrollbarMargin : 5
            });
            $('.jScrollPaneContainer').css('zIndex', 2000);
            container.addClass("lowered");
        }
        else {
            container.find(".product_drop").hide();
            container.removeClass("lowered");
        }
    }

	$('.product_addtocartbutton button.enabled').click(function(){
		var product = $(this).closest('.product');
        if(product.length < 1)
            product = $(this).closest('.product_big');

		var model = product.find('.product_model_selector').find('.selected_value').attr('name');
        var quantity = product.find('input.selected_value').attr('value');

		product.find("#selected_model").val(model);
		product.find("#selected_quantity").val(quantity);

        if(model==undefined)
            alert("Specificera typ/modell!");
        else if(isNaN(quantity) || quantity<=0)
            alert('"Antal" kan bara vara ett positivt heltal!');
        else
            $(this).closest('FORM').submit();
        
		return false;
	});

	$("#button_approve_cart").bind("mouseenter",function(){
		$(this).addClass('button_140_41_green_hover');
		$(this).removeClass('button_140_41_green');
	}).bind("mouseleave",function(){
		$(this).addClass('button_140_41_green');
		$(this).removeClass('button_140_41_green_hover');
	});

    $('#change_dealer').css('zIndex', 2002);
    $('#change_dealer_holder .close').css('zIndex', 2003);

	$('#open_change_dealer').click(function(){
		$('#change_dealer').show();
		$('#change_dealer_holder .close').show();
	});
	$('#change_dealer_holder .close').click(function(){
		$('#change_dealer').hide();
		$('#change_dealer_holder .close').hide();
	});



});
