var timeline_steps = 4;
var timeline_diff = 0;
var timeline_index = 0;

var huisjes = new Array();
huisjes[0] = [2006, 8];
huisjes[1] = [2007, 4];
huisjes[2] = [2007, 7];
huisjes[3] = [2007, 9];
huisjes[4] = [2008, 6];
huisjes[5] = [2008, 7];
huisjes[6] = [2008, 10];
huisjes[7] = [2008, 12];
huisjes[8] = [2009, 1];
huisjes[9] = [2009, 4];
huisjes[10] = [2009, 5];
huisjes[11] = [2009, 10];
huisjes[12] = [2009, 12];
huisjes[13] = [2010, 3];
huisjes[14] = [2010, 5];
huisjes[15] = [2010, 6];
huisjes[16] = [2010, 7];
huisjes[17] = [2010, 12];
huisjes[18] = [2011, 2];
huisjes[19] = [2011, 4];
huisjes[20] = [2011, 7];
huisjes[21] = [2011, 8];
huisjes[22] = [2011, 9];
huisjes[23] = [2012, 1];

var diensten = ['kantoor', 'klus', 'tuin', 'verhuis', 'pc'];

jQuery.fn.random = function() {
    var randomIndex = Math.floor(Math.random() * this.length);  
    return jQuery(this[randomIndex]);
};

jQuery(document).ready(function($) {

	if($('#wpcf7-f2862-p48-o1')[0]) {
		
		switch(window.location.hash) {
			case '#pc':
				$('input[value="PC Student"]').attr('checked', true);
				break;
			case '#verhuis':
				$('input[value="VerhuisStudent"]').attr('checked', true);
				break;
			case '#kantoor':
				$('input[value="KantoorStudent"]').attr('checked', true);
				break;
			case '#tuin':
				$('input[value="TuinStudent"]').attr('checked', true);
				break;
			case '#klus':
				$('input[value="KlusStudent"]').attr('checked', true);
				break;
		}
	}
	
	
	
	$('.faq dt').click(function() {
		$(this).next().toggle('fast');	
	});
	
	$('.doelgroep_random').attr('src', '/wp-content/themes/mijnstudent/img/dienst_'+diensten[Math.floor(Math.random() * diensten.length)]+'_right.jpg');
	
	$('div.vestigingen_contactpersoon').random().show();
	
	$('.history_item').each(function() {
		
		if($(this).attr('id') > 1) { $(this).hide(); }
	});
	
	var content_width = $('#history_timeline_content').width();
	var timeline_width = $('#history_timeline_wrapper').width();
	timeline_diff = (content_width - timeline_width) / timeline_steps;

	for(var i = 0; i < huisjes.length; ++i) {
		var year = huisjes[i][0];
		var month = huisjes[i][1];
		
		var pos = ((year - 2006) * 369 + (month * 30.75)) - 85;
		$('#history_timeline_content_timeline').append("<div id='" + (i + 1) + "' class='history_timeline_house' style='left: " + pos + "px;'></div>");
	}
	
	$('.history_timeline_house').click(function() {
		
		var id = $(this).attr('id');

		$('.history_item:visible').each(function() {
		
			if($(this).attr('id') == id) { return; }
			
			$(this).fadeOut(function() {
				$('.history_item[id=' + id + ']').fadeIn();
			});
		});
	});
	
	$('#history_button_left img').click(function() {

		if(timeline_index > 0) {
			timeline_index--;

			$('#history_timeline_content').animate({
				left: '+=' + timeline_diff + 'px'
			});
		} else {
			$(this).fadeOut(100, function() { 
				$(this).fadeIn(100, function() { 
					$(this).fadeOut(100, function() { 
						$(this).fadeIn(100); 
					}); 
				}); 
			});
		}

	});
	
	$('#history_button_right img').bind('mousedown', function() {

		if(timeline_index < timeline_steps) {
			
			timeline_index++;
			$('#history_timeline_content').animate({
				left: '-=' + timeline_diff + 'px'
			});
		} else {
		
			$(this).fadeOut(100, function() { 
				$(this).fadeIn(100, function() { 
					$(this).fadeOut(100, function() { 
						$(this).fadeIn(100); 
					}); 
				}); 
			});
		}
	});
	
	$('#home_button_offerte').hover(
		function() { $('img', $(this)).attr('src', '/wp-content/themes/mijnstudent/img/home_right_offerte_hover.jpg'); },
		function() { $('img', $(this)).attr('src', '/wp-content/themes/mijnstudent/img/home_right_offerte.jpg'); }
	);
	
	$('#home_button_bestel').hover(
		function() { $('img', $(this)).attr('src', '/wp-content/themes/mijnstudent/img/home_right_bestel_hover.jpg'); },
		function() { $('img', $(this)).attr('src', '/wp-content/themes/mijnstudent/img/home_right_bestel.jpg'); }
	);
	
	$('#contact_outer dd:not(:first)').hide();
	$('.select_advanced').live('click', function() {
		$('.select_advanced_hover', $(this)).toggle();
	});
	
	$('#contact_outer h2').click(function() {
		$(this).parent().next().slideToggle('fast');
		
		var img = $('.contact_outer_slide', $(this).parent());
		
		if(img.attr('src') == '/wp-content/themes/mijnstudent/img/contact_slide_open.jpg') {
			img.attr('src', '/wp-content/themes/mijnstudent/img/contact_slide_closed.jpg')
		} else {
			img.attr('src', '/wp-content/themes/mijnstudent/img/contact_slide_open.jpg')
		}

	});
	
	$('.select_advanced_hover li').live('click', function() {
		var text = $('span', $(this)).html();
		var element = $('> span', $(this).parent().parent());
		$(element).html(text);
		$(element).removeClass('italic');
		//$(element).addClass('strong');
		$('.select_advanced_image', $(element).parent()).attr('src', '/wp-content/themes/mijnstudent/img/contact_select_left_check.jpg');
		//alert($(this).attr('id'));
		
		var id = $(this).parent().parent().attr('id');
		var selectedId = parseInt($(this).attr('id'));
		
		$(this).parent().parent().next('p.question').html();
		$(this).parent().parent().next('p.question').addClass('strong');
		
		switch(id) {
			case 'dienst':
				selectDienst(selectedId);
				break;
		}

	});
	
	$('.select_advanced_hover').live('mouseleave', function() {
		$(this).hide();
	});
	
	$('.select_advanced').live('mouseleave', function() {
		$(this).children('ul').hide();
	});
	
	$('.select_advanced').live('mouseout', function() {
		//$('.select_advanced_hover', $(this)).hide();
	});
	
	$('#pc_probleem li').live('click', function() {
		$('#persoonsgegevens').next().slideDown();
	});
	
	$('select.advanced').each(function() {
		
		var id = $(this).attr('id');

		var firstHTML = $('option:first', $(this)).html();

		var html = 
			"<div class='select_advanced' id='" + id + "'>" +
				"<img class='select_advanced_image' src='/wp-content/themes/mijnstudent/img/contact_select_left.jpg' />" +
				"<span class='italic'>" + firstHTML + "</span>" +
				"<ul class='select_advanced_hover'>"
		;
		
		$(this).children().each(function() {
		
			var value = $(this).val();
			
			if(value > 0) {
			
				var childId = $(this).attr('id');
				var childText = $(this).html();
				
				html += 
					"<li id='" + value + "'>" +
						"<img src='/wp-content/themes/mijnstudent/img/select_advanced_" + id + "_" + childId + ".png' />" +
						"<span>" + childText + "</span>"
					"</li>"
				;
			}
		});
		html += "</div>";
		
		$(this).after(html);
		$(this).hide();
	});
});
