function setCat() {
	var base = $("#base").html();
	$('select.cat').unbind();
	$('select.cat').change(function(){
		var select = $(this);
		if($(this).val() != "-1") {
			$(this).after(' <img src=\"'+base+'img/loader.gif\" />');
			$.get(base+"categorie/browse/format/html/parent/"+$(this).val(), function(data){
				removeAfterThis('select.cat', select);
				if(data != "") {
					select.parent('dd').after(data);
					setCat();
				}
				select.next().remove();
			});
		} else {
			removeAfterThis('select.cat', select);
		}
		return false;
	});
}

function removeAfterThis(el, select) {
	var found = false
	$(el).each(function() {
		if(found) {
			$(this).parent('dd').prev('dt').remove();
			$(this).parent('dd').remove();
		}
		if($(this).attr('id') == select.attr('id')) {
			found = true;
			$(this).attr('name', "categorie_id");
		} else {
			$(this).attr('name', "categorie_id"+$(this).val());
		}
	});	
}

function sendSorting(){
	var order = "";
	$("#table").children('.sort').each(function(){
		order += $(this).attr('id').replace('row','')+"|";
	});
	$.get($('#table').attr('url')+order);
}

function fixRight() {
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
  		return;
	}
	var height = $("#right").height() + 120;
	if($(window).width() < 1000 || height > $(window).height()) {
		//$("#right").css("position","absolute");
	} else {
		//$("#right").css("position","fixed");
	}
}

$(document).ready(function() {
	//setCat();
	$('.afbeelding a').fancybox({
		'hideOnContentClick': true
	});
	$('.video a').fancybox();
	$('.showinfo').click(function(){
		var meerinfo = $(this).parent('.prijs').next('.meerinfo');
		meerinfo.toggle("slide", { direction: "up" }, 1000)
		return false;
	});
	$('.selectCat').change(function(){
		location.href = "/product/browse/cat/"+$(this).val();
	});
	$('#table').sortable({
		handle: '.handle',
		items: '.sort',
		cursor: 'move',
		stop: function() {
			$('#table').children('.row').removeClass('alt');
			$('#table').children('.row:odd').addClass('alt');
			sendSorting();
		},
		tolerance: "pointer",
		placeholder: "hover",
		containment: "document",
		axis: "y"

	});
	$('.picker').datepicker();
	//fixRight();
	$(window).resize(fixRight);
	$('.tip').tooltip({
		showURL: false,
		showBody: " - ",
		left: -10,
		top: 20
	});
	$('.print').click(function() {
		window.print();
	});
	$('.refer').change(function() {
		if($(this).val() == "klant" || $(this).val() == "anders") {
			$('.refer_extra').show();
		} else {
			$('.refer_extra').hide();
		}
	});
	//$("#accordion").accordion({ header: "h2", autoHeight: false });
});