/* js functionality for antwoordservice.nl */

var antwoordservice = {};

antwoordservice.slides = {
	curSlide: '',
	oldPos: 4,
	isIE: false,
	curSlidePos: 0,

	init: function() {
		if ($.browser.msie){// && $.browser.version < 7) {
			this.isIE = true;
		}
		var that = this;
		var newWidth = $(window).width();
		$(window).resize( 
			function() {
				that.resizeSlidesHeader();
			}
		);
		$('.slides').css({'margin-left':newWidth+'px'})
		this.resizeSlidesHeader();

		$('.content_pointers a').click(
			function() {
				return false;
			}
		);		
		$('.content_pointers a').mouseover(
			function() {
				if (that.slideInterval) {
					clearInterval(that.slideInterval);
				}
				that.curSlidePos = $("ul.content_pointers li").index(this.parentNode);
				that.sliderRotate();
				this.blur();
				$('#tel').blur();
				return false;
			}
		);
		$('.content_pointers a').mouseout(
			function() {
				if (that.slideInterval) {
					clearInterval(that.slideInterval);
				}
				that.curSlidePos = $("ul.content_pointers li").index(this.parentNode);
				that.slideInterval = setInterval(function(){that.sliderRotate()}, 6000);
				return false;
			}
		);		
		$('.slides input').focus(
			function() {
				if (that.slideInterval) {
					clearInterval(that.slideInterval);
				}
			}
		);

		$('.slides input').blur(
			function() {
				if (!that.slideInterval) {
					that.slideInterval = setInterval(function(){that.sliderRotate()}, 6000);
				}
			}
		);
		
		$('#call_me_now input').focus(
			function() {
				if (that.slideInterval) {
					clearInterval(that.slideInterval);
				}
			}
		);
		
		$('#call_me_now input').blur(
			function() {
				if (!that.slideInterval) {
					that.slideInterval = setInterval(function(){that.sliderRotate()}, 6000);
				}
			}
		);

		$('.info_button span').mouseover(
			function() {
				$(this.parentNode).addClass('hover_button');
			}
		);
		$('button span').mouseover(
			function() {
				$(this.parentNode).addClass('hover_button');
			}
		);

		$('#ct_1, #ct_2').mouseover(
			function() {
				$(this).addClass('ct_hover');
			}
		);
		$('#ct_1, #ct_2').mouseout(
			function() {
				$(this).removeClass('ct_hover');
			}
		);
		$('#ct_1, #ct_2').click(
			function() {
				var tarLink = '';
				$('a', this).each(
					function() {
						if ($(this).attr('href')) {
							if ($(this).attr('href') != '' && $(this).attr('href') != '#') {							
								tarLink = $(this).attr('href');
							}
						}
					}
				);
				if (tarLink != '') {
					document.location.href = tarLink;
				}
			}
		);		
		$('button').mouseover(
			function() {
				$(this).addClass('hover_button');
			}
		);
		$('button').addClass('hohoho');

		$('.info_button span').mouseout(
			function() {
				$(this.parentNode).removeClass('hover_button');
			}
		);
		$('button span').mouseout(
			function() {
				$(this.parentNode).removeClass('hover_button');
			}
		);
		$('button').mouseout(
			function() {
				$(this).removeClass('hover_button');
			}
		);
		$('input').focus(
			function() {
				if ($(this).val() == 'uw naam' || $(this).val() == 'uw telefoonnummer') {
					$(this).attr('alt', $(this).val());
					$(this).val('');
				}
			}
		);
		$('input').blur(
			function() {
				if ($(this).val() == '' && $(this).attr('alt')) {
					$(this).val($(this).attr('alt'));
				}
			}
		);
		
		$('form').submit(
			function() {
				if ($("input[name='phone']", this).length) {
					var phoneField = $("input[name='phone']", this).get(0);
					var newVal = that.matchPhone($(phoneField).val());
					if (newVal) {
						$(phoneField).css({'border':'0 solid white'});
						_gaq.push(['_trackPageview', '/belmijnu']);
						return true;
					} else {
						$(phoneField).css({'border':'1px solid red'});
						return false;
					}
				}
			}
		);

		$('select').each(
			function() {
				var selectOptions = '<div class="selectoptions">'
				var selValue = '';
				var selText = '';
				for (var i=0; i<this.options.length; i++) {
					selectOptions += '<a href="#'+this.options[i].value+'" class="optionlink">'+this.options[i].text+'</a>';
					if (this.selectedIndex == i) {
						selValue = this.options[i].value;
						selText = this.options[i].text;
					}
				}
				selectOptions += '</div>'
				var selectInput = '<div class="inptext">'+selText+'&nbsp;</div><input class="formselectinp" type="hidden" value="'+selValue+'" name="'+this.name+'"/>';
				var newSelect = '<div class="selectbox">' + selectInput + selectOptions + '</div>';
				$(this).replaceWith(newSelect);
				$('.selectoptions').hide();
				$('.optionlink').click(
					function() {
						$('input',this.parentNode.parentNode).attr('value', $(this).attr('href').split('#')[1]);
						$('.inptext',this.parentNode.parentNode).text($(this).text());
						$(this.parentNode).hide();
						return false;
					}
				);
				$('.inptext').click(
					function() {
						$('.selectoptions',this.parentNode.parentNode).show();
						this.blur();
					}
				);
			}
		);

		this.slideInterval = setInterval(function(){that.sliderRotate()}, 6000);
		this.sliderRotate();
		
		/* fix page height here */
		var winHeight = $(window).height();
		var footerTop = $('.footer_wrapper').offset().top;
		var heightDiv = winHeight - footerTop;
		if (heightDiv > 71) {
			$('.footer_wrapper').css({'height':heightDiv+'px'});
		}
		if (this.isIE) {
			$(".header_links li:last").css({'border-right':'0px'});
		}

	},

	sliderRotate: function() {
		
		var that = this;
		$('.content_pointers .selected').removeClass('selected');
		$('.content_pointers .next').removeClass('next');

		if ($('.content_pointers li').length) {
			if ($('.content_pointers li').length <= this.curSlidePos) {
				this.curSlidePos = 0;
			}
			var baseListItem = $('.content_pointers li').get(this.curSlidePos);
			var me = $('a', baseListItem).get(0);

			$(baseListItem).addClass('selected');
			var nextItem = $(baseListItem).next();
			$('a', nextItem).addClass('next');
			var tarSlideId = $(me).attr('href');
			that.openSlide(tarSlideId, 1000);
		} else {
			var slide = $('.slides li').get(0);
			that.openSlide('#'+$(slide).attr('id'), 1);
			clearInterval(that.slideInterval);
		}
		this.curSlidePos ++;
	},

	matchPhone: function(sPhone) {
		var phonePattern = /^([0]{1}[6]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){7})|([0]{1}[1-9]{1}[0-9]{1}[0-9]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){5})|([0]{1}[1-9]{1}[0-9]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){6})$/i;
		if (sPhone.match(phonePattern)) return true;
		return false;
	},

	openSlide: function(sSlideId, time) {
		if (sSlideId != this.curSlide) {
			var newWidth = $(window).width();

			var curPos = 0;
			for (var i=0; i<$('.slides li').length; i++) {
				var slide = $('.slides li').get(i);
				if ('#'+$(slide).attr('id') == sSlideId) {
					curPos = i;
				}
			}

			var slidesPos = ((newWidth - 960) /2) - (curPos * 960);

			if (curPos == 4) {
				$("#call_me_now").animate({
					marginTop: '-230px'
				}, 1000,
					function() {
						$('#call_me_now_wrapper').css({'height':'10px'});
					}
				);
				$("#call_me_now input[name='phone']").blur();				
			} else if (this.oldPos == 4) {
				if ($('.content_page').length) {
				} else {
					$("#call_me_now").stop(true, false);
					$('#call_me_now_wrapper').css({'height':'230px'});
				}
				$("#call_me_now").animate({
					marginTop: '0px'
				}, time);

			}
			$(".slides").stop(false, false);
			$(".slides").animate({
				marginLeft: slidesPos+'px'
			}, time);
			
			if (this.oldPos) {
				if (this.isIE) {
					$('.slide_content', this.oldPos).stop(true, true);
					$('.slide_img', this.oldPos).stop(true, true);
				} else {
					$(this.oldPos).stop(true, true);
				}
			}			
			
			if (this.curSlide != '') {
				if (this.isIE) {
					$('.slide_content', this.curSlide).stop(true, true);
					$('.slide_img', this.curSlide).stop(true, true);					
					$('.slide_content', this.curSlide).animate({opacity: 0}, time);
					$('.slide_img', this.curSlide).animate({opacity: 0}, time);
				} else {
					$(this.curSlide).stop(true, true);
					$(this.curSlide).animate({opacity: 0}, time);
				}
			}

			if (this.isIE) {
				$('.slide_content', sSlideId).animate({opacity: 1}, time);
				$('.slide_img', sSlideId).animate({opacity: 1}, time);
			} else {
				$(sSlideId).animate({opacity: 1}, time);
			}

			this.curSlide = sSlideId;
			this.oldPos = curPos;
		}
	},
	
	resizeSlidesHeader: function() {
		var newWidth = $(window).width();
		$('.slide_header').css({'width':newWidth+'px', 'margin-left':'-'+((newWidth - 960) /2)+'px'});
		/* causes some weird behavior, no need now */
		/* this.sliderRotate(); */
	}
}

$(document).ready(
	function() {
		antwoordservice.slides.init();
	}
)
