// JavaScript Document

//$.noConflict();
var scroller;
var scrollerMaxHeight = 0;
var scrollerHeight = 0;
var scrollerAnimate = 0;

var scrollerItems;

$(document).ready(function()
{

	/* TRACKINGCODES */
	//list of logos, moving scroller on homepage.
	$('#Scroller a').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'merken', 'click', 'slider-homepage']);
		}
	});

	//list of logos in the right col
	$('ul.logos-list a').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'merken', 'click', 'menu-rechts']);
		}
	});
	
	//imageviewer triggers
	$('ul.descr-gallery a, ul.big-gallery a').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'producten', 'click', 'product-enlarge']);
		}
	});
	
	//non clickable thumb next to add to cart function
	$('.thumb-descr img').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'producten', 'click', 'thumbnail-links']);
		}
	});
	
	//productgroup dropdown (analyticsProductgroup)
	$('select.analyticsProductgroup').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'dropdown', 'open', 'productgroep']);
		}
	});
	$('select.analyticsProductgroup option').bind('click', function(event){
		if(_gaq){
			_gaq.push(['_trackEvent', 'dropdown', 'submit', 'productgroep']);
		}
		event.stopPropagation();
	});
	
	//maat dropdown (analyticsSize)
	$('select.analyticsSize').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'dropdown', 'open', 'maat']);
		}
	});
	$('select.analyticsSize option').bind('click', function(event){
		if(_gaq){
			_gaq.push(['_trackEvent', 'dropdown', 'submit', 'maat']);
		}
		event.stopPropagation();
	});
	
	//merk dropdown (analyticsBrand)
	$('select.analyticsBrand').bind('click', function(){
		if(_gaq){
			_gaq.push(['_trackEvent', 'dropdown', 'open', 'merk']);
		}
	});
	$('select.analyticsBrand option').bind('click', function(event){
		if(_gaq){
			_gaq.push(['_trackEvent', 'dropdown', 'submit', 'merk']);
		}
		event.stopPropagation();
	});
	
	


	(function(){
		//set email field.
		var a = $('.subscribe a:first');
		a.data('orig-href', a.attr('href'));

		$('.subscribe input[type=text]')
			.watermark('  Houd me op de hoogte!', {className: 'init'})
			.bind('keyup', function(){
				a.attr('href', a.data('orig-href') + $(this).val());
			}).bind('keydown', function(event){
				if(event.which == 13){
					post_to_url(a.data('orig-href').replace(/\?email=.+/i,''), { email: $(this).val() }, 'get');
				}
			});
		
	})();
	
	
	var containers = $('div.container2');
	
	var iC, iI;
	
	scrollerItems = $('div#Scroller ul li a')
	
	containers.each(function(index)
	{
		iC = index;
		var images = $(this).find('ul li');
		
		images.each(function(index)
		{
			iI = index;
			var unique = 'Product' + iC + '_' + iI;
			
			var tag = $(this).find('a');
			
			var enlarge = escape($(this).find('span.imageLink').text());
			
			if (enlarge)
			{
				$(this).find('div.hiddenZoom').attr('id', unique);
				
				tag.attr('href', '/content/flash/flashzoomer_new.swf?imgSrc=' + enlarge);
				
				tag.click(function(event)
				{
					event.stopPropagation();
				});
			}
			else
			{
				$(this).find('img').unwrap();
			}
			
		});
		
		$(this).click(function()
		{
			var redirect = false;
			$(this).find('ul').each(function()
			{
				if ($(this).hasClass('descr-gallery') || $(this).hasClass('big-gallery'))
				{
					redirect = true;
				}
			});
			
			if (redirect == false)
				return;
				
			var linkTo = $(this).find('div.description-box a:first');
			
			if (linkTo.length > 0)
			{
				window.location = linkTo.attr('href');
			}
			
		})
		
		$(this).find('a').click(function()
		{
			if ($(this).attr('rel') == '')
			{
				window.location = $(this).attr('href');
			}
		});
		
	});
	
	if ($('ul.descr-gallery').length > 0)
	{
		Shadowbox.init();
	}
	//formInit();
	
	//Scroller -->
	scroller = $('div#Scroller ul');
	
	scrollerMaxHeight = $('div#Scroller ul').height();
	scrollerHeight = $('div#Scroller').height();
	
	$('div#Scroller ul').css('top', -scrollerHeight / 2);
	
	$('div#Scroller').mousemove(function(event)
	{
		var scrollerTop = $('div#Scroller').position().top;
		var valueY = event.clientY;
		
		var weight = ((valueY - scrollerTop) / scrollerHeight).toFixed(2);
				
		var weighted = 10 * (weight - 0.5);
		
		if (weight > 0.55)
		{
			scrollerAnimate = (-1 - weighted).toFixed(2);
		}
		else if (weight < 0.45)
		{
			scrollerAnimate = (1 - weighted).toFixed(2);
		}
		else
		{
			scrollerAnimate = 0;
		}
	});
	
	
	$('div#Scroller ul li a').mouseenter(function()
	{
		scrollerItems.stop();
		scrollerItems.animate({ opacity: 0.4}, 50);
		
		$(this).stop();
		$(this).animate({ opacity: 1}, 100);
	});	
	$('div#Scroller ul li a').mouseleave(function()
	{
		$(this).stop();
	});
	
	scroller.mouseleave(function()
	{
		scrollerItems.stop();
		scrollerItems.animate({ opacity: 1}, 200);
		
		scroller.stop();
	});
	
	scroller.mouseenter(function()
	{
		slideScroller();
	});
	
});

function slideScroller()
{	
	var newPos = parseInt(scroller.css('top')) + parseFloat(scrollerAnimate);
	if (newPos > 0)
	{
		newPos = 0;
	}
	if (newPos < (-scrollerMaxHeight + scrollerHeight))
	{
		newPos = -scrollerMaxHeight + scrollerHeight;
	}
	
	scroller.animate({ top: newPos }, 1, function () { slideScroller() });	
}

function post_to_url(path, params, method) {
	console.log('form post init')
	method = method || "post"; // Set method to post by default, if not specified.

	$('form#Generated').remove();

	// The rest of this code assumes you are not using a library.
	// It can be made less wordy if you use one.
	var form = document.createElement("form");
	form.setAttribute("id", 'Generated');
	form.setAttribute("method", method);
	form.setAttribute("action", path);
	form.setAttribute("target", "_blank");

	var urlParams = '?';

	for (var key in params) {
		var hiddenField = document.createElement("input");
		hiddenField.setAttribute("type", "hidden");
		hiddenField.setAttribute("name", key);
		hiddenField.setAttribute("value", params[key]);

		urlParams += key + '=' + params[key] + '&';

		form.appendChild(hiddenField);
	}
	
	document.body.appendChild(form);
    form.submit();
}
