// JavaScript Document

$(document).ready(function() {
	debug('ready');
	
//	test = ( $('meta[name="remote-address"]').attr('content') == '194.120.14.105' ) ? true : false;
	
	$('body').click(function(e){							 
		if( $(e.target).is('#vacatures') || $(e.target).parents('#vacatures').length > 0 ){
			
		}
		else{
			$('#status').removeClass('active')
			$('#vacatures').animate({height:'40px'});
		}
	});
	
	/*** WIE ***/
	$('a[href=#pagina-1]').click(function(){
		$('#pagina-2').css('display','none');
		$('#pagina-1').css('display','block');
		return false;
	});
	
	$('a[href=#pagina-2]').click(function(){
		$('#pagina-1').css('display','none');
		$('#pagina-2').css('display','block');
		return false;
	});
	
	
	/*** MENU ***/
	$('#menu li a').click(function(){
		$('#wie #pagina-1 h1').css('background-image','url(/images/slogans/afgewogen.png)');
		$('#wie #pagina-2 h1').css('background-image','url(/images/slogans/wij_werken.png)');
		
		$this = $(this);
		
		$('#menu li').removeClass('active').removeClass('active2');
		$this.parent().addClass('active');
			
		var h = $(window).height() - $('#header').outerHeight();
		if(h < 500 ){ h = 500; }
		
		$('html').css('overflow','hidden');
		$('.content').css({'height':h+'px', 'display':'none', 'margin-top':$(window).height()+'px'});
		$( $this.attr('href') ).css('display','block').animate({'marginTop':'0'}, function(){
			if( $this.attr('href') == '#wat' ){
				$this.parent().removeClass('active').addClass('active2');
			}
			else if( $this.attr('href') == '#waar' ){
				$('#gmaps').jmap('init', {
					'mapType':'map',
					'mapZoom':13,
					'language': "nl",
					'mapEnableType': false,
					'mapEnableDoubleClickZoom': true,
					'mapEnableScrollZoom': false,
					'mapEnableSmoothZoom': true,
					'mapCenter':[51.68567,4.21176], 
					'mapShowjMapsIcon':false
				}, function (map, element, options){
					
					var html =	'<div class="balloon" style="width:300px;">';
					html +=			'<div class="left">';
					html +=				'<strong>Quest Media</strong>';
					html +=				'<br />Handelsterrein 27';
					html +=				'<br />3255 LD Oude-Tonge';
					html +=			'</div>';
					html +=			'<div class="right">';
					html +=				'<a href="mailto:info@quest-media.nl">info@quest-media.nl</a>';
					html +=				'<br />T: 0187 64 0161';
					html +=				'<br />F: 0187 64 0164';
					html +=			'</div>';
					html +=		'</div>';
					
					var marker = {'pointLatLng':[51.68567,4.21176], 'pointHTML':html, 'pointIcon':{'iconImage':'/images/common/gmapIcon.png'}};					
					
					$('#gmaps').jmap('AddMarker',marker);
					
				});
			}
			$('html').css('overflow','auto');
		});
		
		return false;
	});
	
	
	var page = location.pathname.replace('/web/show/','').replace('/send','').replace('/','');
	if(page == 'wie' || page == 'wat' || page == 'waar'){
		$('#menu li a[href=#'+page+']').click();
	}
	else{
		$('#menu li a').eq(0).click();
	}
	
	$(window).resize(function(){
		var h = $(window).height() - $('#header').outerHeight();
		$('.content').css({'height':h+'px'});
	});
	
	
	/*** JCAROUSEL ***/
	$('.button').cycle({ 
		fx:      'curtainY', 
		speed:    800, 
		timeout:  2500 
	});
	
	$('#vacatures .button, #vacatures .close').click(function(){
		if( $('#status').is('.active') ){
			$('#status').removeClass('active');
			$('#vacatures').animate({height:'40px'});
		}
		else{
			$('#vacatures').animate({height:'534px'}, function(){
				$('#status').addClass('active');
			});
		}
		return false;
	});
	

	/*** VCAROUSEL ***/
	if( $('#vacatures-window').length > 0 ){
	
		var vItemW = parseInt($('#vacatures-window > ul > li').eq(0).css('width'));
		var vItemML = parseInt($('#vacatures-window > ul > li').eq(0).css('margin-left'));
		var vItemMR = parseInt($('#vacatures-window > ul > li').eq(0).css('margin-right'));
		var vItemPL = parseInt($('#vacatures-window > ul > li').eq(0).css('padding-left'));
		var vItemPR = parseInt($('#vacatures-window > ul > li').eq(0).css('padding-right'));
		var vItemBL = parseInt($('#vacatures-window > ul > li').eq(0).css('border-left-width'));
		var vItemBR = parseInt($('#vacatures-window > ul > li').eq(0).css('border-right-width'));
		var vItemWidth = vItemW + vItemML + vItemMR + vItemPL + vItemPR + vItemBL + vItemBR;
		var vWidth = (vItemWidth * $('#vacatures-window > ul > li').length);
		var equal = ( $('#vacatures-window > ul > li').length % 2 == 0 ? 0 : 1 );
		var vIndex = Math.ceil( ($('#vacatures-window > ul > li').length + 0) / 2);
		$('#vacatures-window >  ul').append($('#vacatures-window >  ul >  li:lt('+(vIndex-1)+')'));
		
		initVacatures();
	}

	function initVacatures(){
		setupVacatures();
		activeItemVacatures(vIndex);
		
		$(window).resize(function(){
			//setupVacatures();
		});
		
		$('#vacatures .next').click(function(){
			nextItemVacatures(1, vIndex+1);
			return false;
		});
		
		$('#vacatures .prev').click(function(){
			prevItemVacatures(1, vIndex);
			return false;
		});
		
		$('ul.news li a').click(function(){
			$('#vacatures .button').click();
			
			var curentIndex = $("#vacatures-window > ul > li").index( $( '#vacatures-window > ul > li.active' ) );
			var goToIndex = $("#vacatures-window > ul > li").index( $('#newsitem-'+$(this).attr('href').replace('/web/show/nieuws/','')) );
	
			if(goToIndex < curentIndex){
				i = curentIndex - goToIndex;
				prevItemVacatures(i, vIndex);
			}
			else if(goToIndex > curentIndex){
				i = goToIndex - curentIndex;
				nextItemVacatures(i, vIndex+i);
			}
			return false;
		});
	
	}

	function setupVacatures(){
		
		var vLeft = 0 - (vItemWidth * (vIndex));
		$('#vacatures-window >  ul').css({width:vWidth+'px', left: vLeft+'px'});
	}
	
	function prevItemVacatures(i, index){
		for(j=0; j<i; j++){
			$('#vacatures-window >  ul').prepend( $('#vacatures-window >  ul >  li').eq( $('#vacatures-window >  ul >  li').length - 1 ).css({width:'0px', marginLeft:'0px', marginRight:'0px'}) );
			$('#vacatures-window >  ul >  li').eq(0).animate({width:vItemW+'px', marginLeft:vItemML+'px', marginRight:vItemMR+'px'}, 500);
		}
	
		activeItemVacatures(index);
	}

	function nextItemVacatures(i, index){
		$('#vacatures-window >  ul >  li:lt('+i+')').animate({width:'0px', marginLeft:'0px', marginRight:'0px'}, 500, function(){
			for(j=0; j<i; j++){
				$('#vacatures-window >  ul').append( $(this).css({width:vItemW+'px', marginLeft:vItemML+'px', marginRight:vItemMR+'px'}) );
			}
		});
		
		activeItemVacatures(index);
	}

	function activeItemVacatures(index){
		$('#vacatures-window >  ul >  li').removeClass('active');
		
		$('#vacatures-window >  ul >  li').eq(index).addClass('active');
	}	
	
	
	// QMCAROUSEL 2
	
	var QMCAROUSEL = function(){ debug('start QMCAROUSEL');
	// VARS
		var indexCount = 12;
		var pages = Math.ceil( $('#indexList ul.list li').length / indexCount );
		var qmItemW,qmItemML,qmItemMR,qmItemPL,qmItemPR,qmItemBL,qmItemBR,qmItemWidth,qmWidth,equal,activeIndex = 0;
		
	// FUNCTIONS
		function init(){ debug('initialize');	
			
			$('#qmCarousel ul').removeClass('active').css('display','none'); // css in css
			$( $('#indexList ul.list li a.active').attr('href') ).addClass('active').css('display','block'); // css in css

			qmItemW	 = parseInt($('#qmCarousel ul.active li').eq(0).css('width'));
			qmItemML = parseInt($('#qmCarousel ul.active li').eq(0).css('margin-left'));
			qmItemMR = parseInt($('#qmCarousel ul.active li').eq(0).css('margin-right'));
			qmItemPL = parseInt($('#qmCarousel ul.active li').eq(0).css('padding-left'));
			qmItemPR = parseInt($('#qmCarousel ul.active li').eq(0).css('padding-right'));
			qmItemBL = parseInt($('#qmCarousel ul.active li').eq(0).css('border-left-width'));
			qmItemBR = parseInt($('#qmCarousel ul.active li').eq(0).css('border-right-width'));
			
			qmItemWidth = qmItemW + qmItemML + qmItemMR + qmItemPL + qmItemPR + qmItemBL + qmItemBR;
			
			qmWidth = (qmItemWidth * $('#qmCarousel ul.active li').length+1);
			equal  = ( $('#qmCarousel ul.active li').length % 2 == 0 ? 0 : 1 );
			activeIndex = Math.ceil( ($('#qmCarousel ul.active li').length + equal) / 2);
			
			$('#qmCarousel ul.active').append($('#qmCarousel ul.active li:lt('+(activeIndex)+')'));
			
			setup();
			activeItem(activeIndex);
		}
		
		function setup(){ debug('setup');
			var l = (($(window).width() / 2) - (qmItemWidth / 2));
			var qmLeft = l - (qmItemWidth * (activeIndex));
			
			$('#qmCarousel ul.active').css({width:qmWidth+'px', left: qmLeft+'px'});
		}
	
		function prevItem(i, index){
			for(j=0; j<i; j++){
				$('#qmCarousel ul.active').prepend( $('#qmCarousel ul.active li').eq( $('#qmCarousel ul.active li').length - 1 ).css({width:'0px', marginLeft:'0px', marginRight:'0px'}) );
				$('#qmCarousel ul.active li').eq(0).animate({width:qmItemW+'px', marginLeft:qmItemML+'px', marginRight:qmItemMR+'px'}, 500);
			}
		
			activeItem(index);
		}
	
		function nextItem(i, index){
			$('#qmCarousel ul.active li:lt('+i+')').animate({width:'0px', marginLeft:'0px', marginRight:'0px'}, 500, function(){
				for(j=0; j<i; j++){
					$('#qmCarousel ul.active').append( $(this).css({width:qmItemW+'px', marginLeft:qmItemML+'px', marginRight:qmItemMR+'px'}) );
				}
			});
			
			activeItem(index);
		}
	
		function activeItem(index){
			$('#qmCarousel ul.active li:lt('+(index+2)+'):gt('+(index-2)+')').each(function(i){
				$(this).find('span.item').each(function(j){
					if(j > 0){ var display = 'none'; }else{ var display = 'inline-block'; }
					$(this).replaceWith('<img src="'+$(this).text()+'" class="'+$(this).attr('class')+'" style="display:'+display+';" />');
				});		
			});
			
			$('#qmCarousel ul.active li').removeClass('active');
			$('#indexList div').css('display','none');
			
			$('#qmCarousel ul.active li').eq(index).addClass('active');
			$( $('#qmCarousel ul.active li.active a:has(img.info)').attr('href') ).css('display','block');
		}
	
	// EVENTS
		$(window).resize(function(){
			setup();
		});
		
		$('#indexList ul.list li a').click(function(){ debug('CHANGE CAROUSEL');
			$('#indexList ul.list li a').removeClass('active');
			$(this).addClass('active');
			
			init();
			
			return false;
		});
		
		$('#qmCarousel ul li h1 a').click(function(){
			$('#qmCarousel ul.active li.active h1 a').removeClass('active');									   	
			$(this).addClass('active').parents('li').eq(0).find('img.item').css('display','none').eq(parseInt($(this).text())-1).fadeIn();
			return false;
		});
		
		$('#qmCarousel .next').click(function(){
			$('#qmCarousel ul.active li.active').find('a.close:has(img)').click();
			nextItem(1, activeIndex+1);
			return false;
		});
		
		$('#qmCarousel .prev').click(function(){
			$('#qmCarousel ul.active li.active').find('a.close:has(img)').click();
			prevItem(1, activeIndex);
			return false;
		});
		
		$('a.info:has(img)').click(function(){
			$('#qmCarousel ul.active li.active a.close:has(img)').css('display','block');
			$(this).css('display','none');
			$('#indexList').animate({width:'659px'}, 500).find($(this).attr('href')).css('display','block');
			return false;
		});
		
		$('a.close:has(img)').click(function(){
			$('#qmCarousel ul.active li.active a.info:has(img)').css('display','block');
			$(this).css('display','none');
			$('#indexList').animate({width:'0px'}, 500, function(){
				$($(this).attr('href')).css('display','none');
			});
			return false;
		});
		
		init();
	};
	
	QMCAROUSEL();

				
	$('a[rel="extern"]').attr('target', '_blank');
	
	$('a.next, a.prev').pngfix();
	
	$('#contact').click(function(){
		if( $('.contact').width() == 216){
			$('.contact').removeClass('active').animate({width:'716px'});
		}
		else{
			$('.contact').addClass('active').animate({width:'216px'});
		}
		return false;
	});
	
	$('input[name=naam]').focus(function(){	if( $(this).val() == 'Naam' ){ $(this).val(''); } });
	$('input[name=email_adres]').focus(function(){ if( $(this).val() == 'E-mailadres' ){ $(this).val(''); }  });
	$('input[name=telefoon]').focus(function(){	if( $(this).val() == 'Telefoon' ){ $(this).val(''); }  });
	$('textarea[name=bericht]').focus(function(){ if( $(this).val() == 'Bericht' ){ $(this).val(''); }  });
	
	// Form checker
	$('form').append('<input type="hidden" name="sid" value="'+$('meta[name=sid]').attr('content')+'" />');
	$('form').submit(function(){					
		var txt = '';

/*
		if( $('input[name=naam]').val() == 'Naam' ){ $('input[name=naam]').val(''); } 
		if( $('input[name=email_adres]').val() == 'E-mailadres' ){ $('input[name=email_adres]').val(''); } 
		if( $('input[name=telefoon]').val() == 'Telefoon' ){ $('input[name=telefoon]').val(''); } 
		if( $('textarea[name=bericht]').val() == 'Bericht' ){ $('textarea[name=bericht]').val(''); } 
*/
		
		// standaard form check
		$(this).find('input, textarea').each(function(){
			if( $(this).attr('title') && $(this).val().length == 0 ){
				txt += ' - ' + $(this).attr('title') + '\n';
			}
		});
		
		if(txt && txt.length > 0){
			var header = '___________________________________________________________\n\n De volgende velden zijn verplicht: \n\n';
			var footer = '___________________________________________________________\n\n ';
			alert(header + txt + footer);
			return false;
		}
	});

	$('#vacatures-window > ul > li > div.scroll').jScrollPane({
		scrollbarWidth : 8,
		scrollbarMargin : 10,
		wheelSpeed : 18,
		showArrows : true,
		arrowSize : 8,
		animateTo : false,
		dragMinHeight : 1,
		dragMaxHeight : 99999,
		animateInterval : 100,
		animateStep: 3,
		maintainPosition: true,
		scrollbarOnLeft: false
	});
	
	
});

var carouselFix = function(){
	debug( $('#qmCarousel > ul > li img').length );
	if( $('#qmCarousel > ul > li img').length == 0){
		debug('test');
	}
}

var debug = function(text){
	if($.browser.mozilla && typeof(console) != 'undefined'){
		console.info(text);	
	}	
}