// JavaScript Document
$( document ).ready(function(){
	if( $( '.promo_item' ).length < 6 ){
		$( '#previous_promo' ).css( { 'visibility':'hidden' });
		$( '#next_promo' ).css( { 'visibility':'hidden' } );
	}else{
		$( '#previous_promo' ).bind( 'click', function(){previous_promo();} );
		$( '#next_promo' ).bind( 'click', function(){next_promo(); } );
	}
	
	// cbo opties verbergen.
	$( '#cbo_artikel_options' ).slideToggle( 0 );
	$( '#cbo_merk_options' ).slideToggle( 0 );
	$( '#cbo_prijs_options' ).slideToggle( 0 );
	
	// cbo click events instellen.
	$( '#sel_cbo_artikel' ).bind( 'click', function(){ $( document ).unbind( 'click' ); slide_options( 'cbo_artikel_options' ); } );
	$( '#sel_cbo_merk' ).bind( 'click', function(){ $( document ).unbind( 'click' ); slide_options( 'cbo_merk_options' ); } );
	$( '#sel_cbo_prijs' ).bind( 'click', function(){ $( document ).unbind( 'click' ); slide_options( 'cbo_prijs_options' ); } );
	
	// li click events instellen
	$( '.options ul li' ).bind( 'click', function(){ check_selected( this ); } );
	
	active_menu( 'navigatie' );
	
	$('#slideshow').cycle({
		  fx:     'fade',
		  speed:  'slow',
		  timeout: 5000
		  /*pager:  '#slider_nav',
		  pagerAnchorBuilder: function(idx, slide) {
			//return '#slider_nav li:eq(' + (idx) + ') a';
		  }*/
	});
});

function next_promo(){
	$( '.promo_item:last' ).insertAfter( '#previous_promo' ); //laatste vooraan plaatsen
	$( '.promo_item:first' ).css( { 'margin-left':'-162px'} ); //eerste verbergen
	$( '.promo_item:first' ).animate( {
						   	'margin-left':'0'
						   }, 500, 'swing');
}

function previous_promo(){
	$( '.promo_item:first' ).animate( {
						   	'margin-left':'-162'
						   }, 500, 'swing', function(){
							   			$( '.promo_item:first' ).css({ 'margin-left':'0' });
										$( '.promo_item:first' ).insertBefore( '#next_promo' );
						   				});
}

function slide_options( e ){
	$( '.options' ).not( ':hidden' ).not( '#' + e ).slideToggle( 200, 'swing' );
	
	$( '#' + e ).slideToggle( 200, 'swing', function(){
									 if( $( '#' + e ).is( ':hidden' ) ){
										$( document ).unbind( 'click' );
									 }else{
										$( document ).bind( 'click', function(){
											$( document ).unbind( 'click' );
										  	$( '#' + e ).slideToggle( 200, 'swing' );
										  } );
									 }
									 });
}

function check_selected( e ){
	var text = $( e ).html();
	var val = $( e ).attr( 'title' );
	
	var parent_id = $( e ).parent().parent().attr( 'id' );
	
	$( '#' + parent_id + ' ul li' ).removeClass( 'selected' );
	
	$( e ).addClass( 'selected' );
	
	switch( parent_id ){
		case 'cbo_artikel_options':
			$( '#sel_cbo_artikel' ).html( text );
			$( '#cboArtikels' ).val( val );
			break;
		case 'cbo_merk_options':
			$( '#sel_cbo_merk' ).html( text );
			$( '#cboMerken' ).val( val );
			break;
		case 'cbo_prijs_options':
			$( '#sel_cbo_prijs' ).html( text );
			$( '#cboPrijs' ).val( val );
			break;
	}
}

function active_menu( containerID ){
	var container = document.getElementById( containerID );
	if( container ){
		var anchors = container.getElementsByTagName( 'a' );
		for( var i = 0; i < anchors.length; i++ ){
			var a_href = anchors[i].href;
			var huidige_pagina = location.href;
			//php gets verwijderen...
			huidige_pagina = huidige_pagina.split( '?' );
			huidige_pagina = huidige_pagina[0];
			//anchor id's verwijderen
			huidige_pagina = huidige_pagina.split( '#' );
			huidige_pagina = huidige_pagina[0];
			
			var last_char = huidige_pagina.substring( huidige_pagina.length - 1 );
			if(  last_char == '/' ){
				huidige_pagina += 'index.php';
			}
			if( a_href == huidige_pagina ){
				$( anchors[i] ).addClass( 'active' );
				$( anchors[i] ).parent().addClass( 'active' );
			}
		}
	}
}
