//var PATHS = { root: '/ivwd/webshop/public_html/', zero:0, something:1 };
$(document).ready(function()
{
	//initially hidden
	$(/*.init-hide, .post:not(:first-child) .post-content*/'.post-content, #nav ul ul').hide();
	//, .subnav li[class~="current_page"] ul
	
	//$('.init-hide').each(function(){ alert( $(this).css('visibility') ); });
	
	/*I mean, if the target attribute of the <a> tag is being phased out, does it really make a difference whether we're setting it with JavaScript instead of HTML?
	Sure, the page will validate against the HTML 4.0 Strict and XHTML 1.0 Strict Document Type Definitions, but aren'it we technically cheating?
	The answer is no. The Document Object Model (DOM), which governs the document objects and attributes that are available to JavaScript code, is a totally separate standard from (X)HTML.*/
	$('a[href][rel="external"]').attr('target', '_blank');
	
	//cycle preparations
	var sizeof = $('.cycle').length;
	if( sizeof )
	{
		var width = 56; //thumbbreedte
		for( var i=0; i < $('.cycle img').length; i++ )
		{
			if( $('.cycle img:eq('+i+')').width() > $('.cycle img:eq('+i+')').height() )
			{
				var height = Math.round( width * ( $('.cycle img:eq('+i+')').height() / $('.cycle img:eq('+i+')').width() ) ); //thumbhoogte
			}
			else
			{
				var left = Math.round( ( $('.cycle img:eq('+i+')').parents('.cycle').width() - $('.cycle img:eq('+i+')').width() ) / 2 );
			}
			
			if( height != undefined && left != undefined )
			{
				break;
			}
		}
		
		//javascript staat aan, addclass preloader…
		$('.cycle').removeClass('loading').addClass('preloader');
	}
	
	//news items in en uit klap
	$('.post h3').click(function()
	{
		$(this).next().toggle().prev().parent().toggleClass('active').siblings().removeClass('active').children('.post-content').hide();
	});
	
	//klap menuutje
	$('#nav>ul>li').hover(function()
	{
		$(this).toggleClass('collapsed');
		$('ul', this).toggle();
	});
	
	//subnav klap menuutje
	$('.subnav .rounded>ul>li:has(ul)>a').click(function()
	{
		$(this).siblings('ul').toggle();
		return false;
	});
	
	//hardcoded week waarop het geuploade programma begint
	var week = 36;
	
	/*if( $('.programma').length )
	{
		$('.programma tr:not(:first-child)').hide().each(function()
		{
			if( week > 52 ) { week = 1; }
			$(this).addClass('week-'+week);
			week++;
		});
	
		week = $('.programma').attr('class').split('-')[1];
		$('.programma tr.week-'+week)
		.add( $('.programma tr.week-'+week).next() )
		.add( $('.programma tr.week-'+week).next().next() )
		.add( $('.programma tr.week-'+week).next().next().next() )
		.add( $('.programma tr.week-'+week).next().next().next().next() )
	.	show();
	
		$('#prog-pagination').show();
		$('#prog-pagination a:not(.opa)').click(function()
		{
			progSwitch( $(this).attr('class') );
			return false;
		});
	}
	
	function progSwitch( classname )
	{
		if( classname == 'all' )
		{
			$('.programma tr').show();
			$('#prog-pagination a').removeClass('opa').filter('.'+classname).addClass('opa');
		}
	}*/
	
	//formulieren
	if( $('.row-wrapper').length )
	{
		$('.row input, .row textarea, .row select').focus(function()
		{
			$(this).parents('.row-wrapper').toggleClass('row-focus');
		}).blur(function()
		{
			$(this).parents('.row-wrapper').toggleClass('row-focus');
		});
		
		$('.row-wrapper').addClass('hover-works');
		
		var el = $('.row-wrapper:eq(0) .row:last input');
		if( !el.val.length )
		{
			el.focus();	
		}
	}
	
	//als pics geladen zijn
	$(window).load(function()
	{
		//cycle, check op meer dan 1 foto wordt in PHP gedaan…
		if( sizeof )
		{
			var thumbset = [];
			
			thumbset[0] = $('.pagination').attr('id').split('-')[2]; //aantal pics in 1 set
			thumbset[1] = 0; //begin pagina voor paginering
			
			var slide = [];
			slide[ 0 ] = false;
			slide[ 1 ] = ( window.location.hash ) ? window.location.hash.split('#')[1] : 1;
			
			//prev / next nav hover
			$('.cycle-nav, .cycle-slideshow').hover(function()
			{
				$(this).children('div').css('display', 'table-cell');
			}, 
			function()
			{
				$(this).children('div').css('display', 'none');
			});
			
			//build thumbset
			$('<div class="cycle-thumbs">').append('<ul>').insertBefore('.cycle-thumbs-wrapper>*:first-child');
			
			//cycle config
			$('.cycle').cycle(
			{
				fx:					'none', //fade
				//speed:			250,
				timeout:			1500,
				startingSlide:		slide[1]-1,
				pager:				$('.cycle-thumbs ul'),
				next:				$('.cycle-next, .cycle-next a'),
				prev:				$('.cycle-prev, .cycle-prev a'),
				pagerAnchorBuilder: function( idx, slide )
				{ 
					// callback fn that creates a thumbnail to use as pager anchor
					return '<li class="set'+ Math.floor( idx / thumbset[0] ) +'"><a href=""><img src="' + slide.src + '" width="'+ width +'" height="'+ height +'" /></a></li>';
				},
				//class="cycle preloader"
				before: function(curr,next,opts)
				{
					if( $(this).height() > $(this).width() )
					{
						$(this).css('margin-left', left+'px')
					}
					
					if( $(this).parents('.cycle').height() !== $(this).height() )
					{
						$(this).parents('.cycle').add( $('.cycle-nav, .cycle-slideshow, .center-this') ).height( $(this).height() )
					}
				},
				after: function(curr,next,opts)
				{
					if( $('.cycle-thumbs li').length && Math.floor( opts.nextSlide / thumbset[0] ) !== thumbset[1] )
					{
						thumbset[1] = Math.floor( opts.nextSlide / thumbset[0] );
						getPage( thumbset[1] );
					}
					
					if( slide[0] === false )
					{
						slide[0] = true;
					}
					else
					{
						window.location.hash = opts.nextSlide+1;
					}
				}
			}).cycle('toggle');
			
			if( $('.pagination li').length )
			{
				//get current page
				getPage( Math.floor( $('.cycle-thumbs li').index( $('.activeSlide') ) / thumbset[0] ) );
			
				//klik op paginering link
				$('.pagination a').click(function()
				{
					getPage( $(this).parents('ul').children('li').index( $(this).parent() ) );
					return false;
				});
			}
			
			$('.cycle-wrapper .init-hide').show();
			
			//$('.cycle-thumbs').height( $('.cycle-thumbs').height() );
		}
		
		function getPage( set )
		{
			$('.cycle-thumbs li').hide().filter( '.set'+ set ).show();
			$('.pagination li').removeClass('active').eq( set ).addClass('active');
		}
		
		$('.cycle-toggle').click(function()
		{
			if( $(this).text() == 'Play' )
			{
				$(this).attr({ title: 'Pause', id: 'cycle-pause'}).text('Pause');
			}
			else
			{
				$(this).attr({ title: 'Play', id: 'cycle-play'}).text('Play');
			}
			
			//$(this).toggleClass('active');
			$('.cycle').cycle('toggle');

			return false;
		});
	});
});
