$(function(){	
	
	$('.bigstar,.star').text("");
	
	$('.morenav').hover(function ()
	{
		$(this).addClass('opennav');
		$(this).next().css('marginRight','42px');
	},function ()
	{
		$(this).removeClass('opennav');
		$(this).next().css('marginRight','56px');
	});
	
	$('.href').click(function ()
	{
		path = $(this).attr('url');
		if(path)
		{
			if($(this).attr('new'))
				window.open(path);
			else
				window.location = path;
		}
	});
	
	tabs = $('.tab');
	if(tabs.length != 0)
	{
		if(window.location.hash != '') changeTab();
		$(window).bind('hashchange',changeTab);
	}

	placeholder();
});

function changeTab()
{
	if(window.location.hash == '')
		id = $('.tab:first').attr('href').replace('#','');	
	else
		id = window.location.hash.replace('#','');

	ob = $('.tab[href="#'+id+'"]');
	parid = '#tabcontent';
	
	$(parid+' .curtab').removeClass('curtab');
	$(ob).addClass('curtab');

	$('.tabblock').addClass('hide');
	$('#tab'+id).removeClass('hide');
}

function url(path){window.location = path;return false;}

function placeholder()
{
	var phinput = document.createElement('input');
  	var phcheck = 'placeholder' in phinput;

	if(!phcheck)
	{
		$('input[type=text]').each(function ()
		{
			if(placeholder = $(this).attr('placeholder'))
			{
				$(this).val(placeholder);
			
				$(this).focus(function ()
				{
					if(placeholder == $(this).val())
						$(this).val('');
				});
				$(this).blur(function ()
				{
					if('' == $(this).val())
						$(this).val(placeholder);
				});
			}
		});
	}
}


function isScrolledTo(elem) {
	var docViewTop = $(window).scrollTop(); //num of pixels hidden above current screen
	var docViewBottom = docViewTop + $(window).height();
	
	var elemTop = $(elem).offset().top; //num of pixels above the elem
	var elemBottom = elemTop + $(elem).height();
	
	return ((elemTop <= docViewTop));
}

function stickyScroll()
{
	var catcher = $('#bestMonthWrap');
	
	if(catcher.length != 0)
	{
		var bestmonth 	= $('#bestMonth');
		var premie		= $('#reviewpremie');
		var reviewwrap 	= $('#reviewwrap');
		var stopLow	= (reviewwrap.offset().top + reviewwrap.height()) - premie.height();
		
		$(window).scroll(function(){
		    if(isScrolledTo(bestmonth))
		    {
		        premie.css({'position':'fixed','top':'54px'});
		        bestmonth.removeClass('bestmonthrel');
		        bestmonth.addClass('bestmonthfix');
		    }
		    
		    var stopHeight = catcher.offset().top + catcher.height();
		    if(stopHeight > premie.offset().top)
		    {
		        premie.css({'position':'relative','top':'0px'});
		        bestmonth.addClass('bestmonthrel').removeClass('bestmonthfix');
		    }
		   
		    if(premie.offset().top > stopLow)
		    {
		        premie.css({'position':'relative','top':'0px'});
		        bestmonth.addClass('bestmonthrel').removeClass('bestmonthfix');		        
		    }
		});
	}
}

function moreReviews(supplierid,productid)
{
	$.ajax({
		type: "POST",
		url: rt+"more",
		data: "product_id="+productid+"&supplier_id="+supplierid,
		success: function(data)
		{
			if(data.status == undefined || data.status == 'error')
			{}
			else
			{
				$('.fakeform').before(data.content);
				$('.star').text("");
			}	
			stickyScroll();			
		}
	});	
}

//FORM
$(function(){
	
	//formulier maar 1x verzenden
	$('#theform').submit(function(){$('.submit').attr('disabled',true).val('Bezig met opslaan...');});	
	
	$('.handmadeselect a').each(function(){		
		//set value
		html = $(this).parent().find('.cur b').html();
		if(html && html != '') $(this).html(html);
	
		//add click function
		$(this).click(function ()
		{
			$(this).removeClass('error');
		
			if($(this).hasClass('open'))
			{
				$(this).parent().children('ul').hide();
				$(this).removeClass('open');
			}
			else
			{
				$('.handmadeselect ul').hide();
				$('.handmadeselect a').removeClass('open');
				
				$(this).parent().children('ul').show();
				$(this).addClass('open');
			}
			return false;
		});
	});
	
	//textarea automatisch laten groeien
	$('textarea').jGrow();
});
//hand made select click
function handmadeselect(id,ob,collapse)
{	
	id = '#'+id;
	if(collapse == undefined)
	{
		$(id+' ul').hide();
		$(id+' a').removeClass('open').html($(ob).children('b').html());
	}
	$(id+' .cur').removeClass('cur');	
	$(ob).addClass('cur');
	$(id+' .value').val($(ob).attr('waarde'));
}
function filterDropdown(dropdowns,category)
{
	$.each(dropdowns,function(i)
	{
		$('#i-'+dropdowns[i]+' li').each(function ()
		{
			if($(this).hasClass('add') || $(this).attr('category') == category)
				$(this).show();
			else
				$(this).hide();
		});
		
		$('#i-'+dropdowns[i]+' .value').val("");
		$('#i-'+dropdowns[i]+' a').text('Kies een optie');
	});
}
function cmsselectcustom(id)
{
	ob = $('#'+id+' .add input');
	value = $(ob).val();
	
	if(value == '')
	{
		alert('Je hebt niets opgegeven.');
		return;
	}
	
	$(ob).attr('waarde',value);
	handmadeselect(id,ob);	
	$('#'+id+' a').text(value);
}
