// Product Lists JS

function showFilterUpdate(target) {
    /*  Grabs the next block section in the filter bar (which will be
        *  a hidden submit button and slides it down. If the next block
        *  is already visible don't slide, leave as is.
        */
    var next_block = $(target).closest('.block').next('.block');
	if(next_block.css('display')!='block') {
        $('.filter_control:visible').slideUp();
        $(next_block).slideDown();
    }
}

function switchImageTo(target_index) {
    $('.productlist-thumb').each(function() {
        var current_img = $(this);
        $.ajax({
            'url' : '/gallery/pickled/store_product/'+$(this).attr('id').replace(/currentimg-/g,'')+'/product_medium/'+target_index+'/',
            'dataType' : 'json',
                success : function(json) {
                current_img.attr('src', json);
            }
        });
    });
}

function handlePRchange(target_elem, is_link) {
    $('input.prbox').each(function() {
        $(this).attr('checked', false);
    });
    if(is_link) {
        var pr = $(target_elem).prev('input:checkbox');
    } else {
        var pr = $(target_elem);
    }
    if(pr.attr('checked', false)) {
        pr.attr('checked', true);
        showFilterUpdate(pr);
    }
    var ranges = pr.attr('id').replace(/pr-/g, '').split('_');
    $('#id_filter-price_range_lo').attr('value', ranges[0]);
    $('#id_filter-price_range_hi').attr('value', ranges[1]);
}

function setCurrentPR() {
    var lo = $('#id_filter-price_range_lo').attr('value');
    var hi = $('#id_filter-price_range_hi').attr('value');
    target_elem = '#pr-'+lo+'_'+hi;
    $(target_elem).attr('checked', true);
}

function handlePRtextchange() {
    $('input.prbox').each(function() {
        $(this).attr('checked', false);
    });
    setCurrentPR();
    showFilterUpdate($('input.prbox'));
}

$(document).ready(function() {
						   
	// Show js arrows hidden for non-js
	$(".prevPage").show();
	$(".nextPage").show();
	
	
	$("div.item_text").hover(function(){
		$(this).find('.img_scroller_go').show();								  
	}, function(){
		$(this).find('.img_scroller_go').hide();		
	})
	
	
	
	// Hijak checkboxes into custom
	$(':checkbox').simpleImageCheck({
	image: 'http://media.crookedtongues.com/media/images/generic/icons/checkbox_unchecked.png',
	imageChecked: 'http://media.crookedtongues.com/media/images/generic/icons/checkbox_checked.png'
	});
	
	$('#prtextboxes').hide();
	
	$("#id_filter-price_range_lo").val(current_price_range_lo);
	$("#id_filter-price_range_hi").val(current_price_range_hi);
	
	$("#slider").slider({
		range: true,
		min: price_range_min,
		max: price_range_max,
		step: 5,
		values: [current_price_range_lo, current_price_range_hi],
		slide: function(event, ui) {
			$("#id_filter-price_range_lo").val(ui.values[0]);
			$("#id_filter-price_range_hi").val(ui.values[1]);
			//$("a.ui-slider-handle:first span").html('&pound;'+ui.values[0]);
			//$("a.ui-slider-handle:last span").html('&pound;'+ui.values[1]);
			$("#slider .bottom_fig").html('&pound;'+ui.values[0]);
			$("#slider .top_fig").html('&pound;'+ui.values[1]);
		},
		change: function(event, ui) 
		{
			showFilterUpdate('#slider');
		}

	});

	$("#slider").prepend('<span class="bottom_fig">&pound;'+current_price_range_lo+'</span>').append('<span class="top_fig">&pound;'+current_price_range_hi+'</span>');
	$("a.ui-slider-handle:first").append('<span class="bottom_fig_val range_figures">&pound;'+$("#slider").slider("values", 0)+'</span>');
	$("a.ui-slider-handle:last").append('<span class="top_fig_val range_figures">&pound;'+$("#slider").slider("values", 1)+'</span>');
	
	// Hijak select and restyle to design
	$('#id_sort-sort').sSelect();

	// Enable top recommended product scroller 
	$("div.scrollable").scrollable({keyboard:false});
	
	// gallery image stuff
    $('#id_sort-submit').hide();
    $('#id_sort-sort').change(function(evt){
        $(evt.target.form).submit();
    });
    $('.gallerycontrol').galleryBrowser();
    $('.filter_control').hide();
    $('input:checkbox').click(function() {
        showFilterUpdate(this);
    });
    $('input:radio').click(function() {
        showFilterUpdate(this);
    });
	
    $('#show34').click(function() {
        switchImageTo(1);
		$('#show34').addClass('active')
		$('#showprofile').addClass('inactive')
		$('#showprofile').removeClass('active')
    });
	
    $('#showprofile').click(function() {
        switchImageTo(0);
		$('#showprofile').addClass('active')
		$('#showprofile').removeClass('inactive')
		$('#show34').removeClass('active')
    });
    $('input.prbox').click(function() {
        handlePRchange(this, false);
    });
    $('.prboxlink').click(function(e) {
        e.preventDefault();
        handlePRchange(this, true);
    });
    $('#id_filter-price_range_hi').focus(function() {
        handlePRtextchange();
    });
    $('#id_filter-price_range_lo').focus(function() {
        handlePRtextchange();
    });
    $('#id_filter-price_range_hi').keydown(function() {
        handlePRtextchange();
    });
    $('#id_filter-price_range_lo').keydown(function() {
        handlePRtextchange();
    });
    setCurrentPR();
	
	// Hijak radioboxes into custom
	//$('#pricerange input').customInput();
	
});

