/*
Copyright (c) 2009 Ylab, www.ylab.nl
*/
jQuery(document).ready(function($){
	var msie7 = $.browser.msie && $.browser.version < 8;

	function closeProduct(){
		var $this = $('.product.status2');
		$this.find('.hidden,.productmemo').slideUp('slow', function(){
			$this.find('.hideDetails').hide();
			$this.find('.showDetails').show();
		});
		$this.find('.preview').fadeIn();
		$this.removeClass('status2').addClass('status1');
	}

	function productIn(){
		var $this = $(this);
		if($this.hasClass('status1')){
			$this.find('.frame').addClass('frameover');
		}
	};

	function productOut(){
		$(this).find('.frame').removeClass('frameover');
	};

	function productClick(event){
		//event.preventDefault();
		event.stopPropagation();
		var $this = $(this);
		if($this.hasClass('status1')){
			closeProduct();
			$this.removeClass('status1');
			if(msie7){
				$('.thumbsframe').hide();
			}
			$('.frame', this).removeClass('frameover');
			$('.preview', this).fadeOut();
			$('.hidden', this).slideDown('slow', function(){
				$('.video', this).show();
				if(msie7){
					$('.thumbsframe').show().css({position:'relative'});
				}
			});
			$this.addClass('status2');
		}
	};

	function shoutDelClick(event){
		event.preventDefault();
		var urlAjax = '/ajax/delete-shout.json.php' + this.search;
		var $divShout = $(this).parent().parent();
		jQuery.getJSON(urlAjax, function(data){
			if(data.id){
				$divShout.slideUp();
			}
		});
	}

	function shoutBack(data, textStatus){
		if(textStatus == 'success'){
			var html = '<div class="shout">';
			html += '<div class="shoutprops">' + data.date + ' / ' + data.screenname + '<a href="/ajax/delete-shout.php?id=' + data.id + '" class="shoutDel">Verwijderen</a></div>';
			html += '<p>' + data.shout + '</p>';
			html += '</div>';
			$('#frmShout').html(html).slideDown();;
			$('.shoutDel').click(shoutDelClick);
		}else{
			debugAlert(textStatus);
		}
	}

	function prepareShout(){
		//shout
		$('#frmShout').submit(function(){
			if(!this.shout.value){
				return false;
			}
			var properties = {
				product:this.product.value,
				shout:this.shout.value
			};
			$.post('/ajax/shout.json.php', properties, shoutBack, 'json')
			$(this).slideUp();
			return false;
		});
		$('.shoutDel').click(shoutDelClick);
	}

	function showProductDetail(event){
		event.preventDefault();
		event.stopPropagation();
		var productid = this.hash.substring(1);
		var $product = $(this).hide().parent().parent();
		$product.find('.productmemo').slideDown('slow', function(){
			$(this).find('.shoutLoader').load('/includes/shouts.inc.php?product=' + productid, prepareShout);
			$product.find('.hideDetails').fadeIn()
		});
	}

	function hideProductDetail(event){
		event.preventDefault();
		event.stopPropagation();
		var $product = $(this).hide().parent().parent();
		$product.find('.productmemo').slideUp('slow', function(){
			$product.find('.showDetails').fadeIn()
		});
	}

	function saveRating(el, productid, rateid, rate){
		var urlAjax = '/ajax/saverating.json.php?productid='+productid+'&rateid='+rateid+'&rate='+rate;
		jQuery.getJSON(urlAjax, function(data){
			if(data.result){
				$(el).parent().children().removeClass('avg').each(function(i){
					if(i <= data.rate){
						$(this).addClass('on');
					}else{
						$(this).removeClass('on');
					}
					this.href = (this.pathname + '?' + productid + ',' + data.id + ',' + i);
				});
			}else{
				alert('Fout bij het opslaan van de rating.');
			}
		});
	}

	function saveFavorite(el, productid, rateid, makeFavorite){
		var urlAjax = '/ajax/savefavorite.json.php?productid='+productid+'&rateid='+rateid+'&favorite='+makeFavorite;
		jQuery.getJSON(urlAjax, function(data){
			if(data.result){
				if(data.favorite){
					$(el).addClass('on').html('Favoriet');
				}else{
					$(el).removeClass('on').html('Als favoriet markeren');
				}
				el.href = (el.pathname + '?' + productid + ',' + data.id + ',' + (data.favorite ? 0 : 1));
			}else{
				alert('Fout bij het bewerken van de lijst met favorieten.');
			}
		});
	}

	$('.product.status1').hover(productIn, productOut).click(productClick);
	$('.showDetails').click(showProductDetail);
	$('.hideDetails').click(hideProductDetail);
	$('.mainprops').hover(function(event){
		$(this).addClass('frameover').parent().removeClass('frameover');
	},function(event){
		$(this).removeClass('frameover').parent().addClass('frameover');
	});
	$('.mainprops a').click(function(event){
		event.stopPropagation();
	});

	//rating
	$('.rate').each(function(i){
		n = i%5;
		var gt = i < 5 ? '' : ':gt(' + (i-n-1) + ')';
		var $buddies = $('.rate' + gt + ':lt(' + (n+1) + ')');
		$(this).hover(function(){
			$buddies.addClass('over');
		},function(){
			$buddies.removeClass('over');
		}).click(function(event){
			event.preventDefault();
			var ids = this.search.substring(1).split(',');
			saveRating(this, ids[0], ids[1], ids[2]);
		});
	});

	//fave
	$('.fave').hover(function(){
		$(this).addClass('over')
	},function(){
		$(this).removeClass('over')
	}).click(function(event){
		event.preventDefault();
		var ids = this.search.substring(1).split(',');
		saveFavorite(this, ids[0], ids[1], ids[2]);
	});

	//share
	$('.share').click(function(event){
		event.preventDefault();
		//form id is part to show in the lightbox
		showEditForm(this.href + ' #frmShare', this.title, prepareShareForm);
	});

	function readFilter(id){
		var i = parseInt($('#' + id).slider('value'));
		i = Math.min(i, 9);
		i = Math.max(i, 0);
		$.cookie(id, i);
		return i;
	}

	function applyFilter(){
		closeProduct();

		bud = readFilter('sliderBudget');
		num = readFilter('sliderNumber');

		bud = (bud + 1) * 100000;//1.000-10.000
		num = (10-num)* 1000;//reverse order 10.000-1.000
		//max average price = 10.000 / 1.000 = 10 euro, see config.php

		var hid = 0, vis=0;
		$('.product').each(function(i){
			var props = this.id.split(':');
			props[1] = parseInt(props[1]);//minimumorder
			props[2] = parseInt(props[2]);//price (cents)
			var bVis = (num * props[2]) <= bud;
			if(bVis){
				$(this).slideDown();
				vis++;
			}else{
				$(this).slideUp();
				hid++;
			}
		});
		if(hid){
			$('.filterinfo').html(vis + (vis == 1 ? ' artikel voldoet' : ' artikelen voldoen') + ' aan je voorwaarden. <a href="#" class="modest">alles tonen</a>').show();
			$('.filterinfo a').click(resetFilter);
		}else{
			$('.filterinfo').hide();
		}
		$('#emptyset').css('display', vis ? 'none' : 'block');
		$('#maxPrice').html('maximale stukprijs <span>' + toEuro(Math.round(bud/num)/100) + '</span>');
		$('#sliderNumber a').attr('title', num);
		$('#sliderBudget a').attr('title', bud/100);
	}

	function resetFilter(event){
		event.preventDefault();
		$('#sliderNumber').slider('value', 10);
		$('#sliderBudget').slider('value', 10);
		applyFilter();
	}

	function showBudget(value){
		$('#tipBudget').html(': ' + (1 + parseInt(value)) + '.000');
	}

	function showNumber(value){
		$('#tipNumber').html(': ' + (10 - parseInt(value)) + '.000');
	}

	//slider
	if($('.product').length > 1 && $('#productfilter').size()){
		$('#sliderBudget').slider({
			min:1,
			max:9,
			step:1,
			value:$.cookie('sliderBudget') || 9,
			change:applyFilter,
			slide:function(event, ui){showBudget(ui.value);}
		});
		$('#sliderNumber').slider({
			min:0,
			max:9,
			step:1,
			value:$.cookie('sliderNumber') || 9,
			change:applyFilter,
			slide:function(event, ui){showNumber(ui.value);}
		});
		showBudget($('#sliderBudget').slider('value'));
		showNumber($('#sliderNumber').slider('value'));
		$('#productfilter').show();
		applyFilter();
	}

	//open first entry after 1 sec
	if($('#noresults').size() == 0){
		window.setTimeout("$('.product:visible:first').click();", 1000);
	}
});

