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

	//$("body").append('<div id="chatcontainer"><a id="closechat" href="#">sluiten</a><iframe id="chat" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.mrwebsite.nl/chat"></iframe></div>');

	$('#closechat').click(function(){
		$('#chatcontainer').hide();
		return false;
	});

	$("h6 a").each(function (i) {
		$(this).wrapInner('<span>');
	});
      
	$('#cardcode').focus(function(){
    	$(this).attr('value','');
    	$(this).css('color','#000');
    });
    
	$('#callme1').click(function(){
		var no=$('#no1').val().replace(/ /g,"");
		//$(this).parent().html('<p id="temp">Uw telefoon zou binnen enkele seconden over moeten gaan...<br /><img src="appear/i/spinner.gif" alt="" /></p>');
		//$.get('http://www.pearwebsites.com/extra.php?url='+url, function(thehtml){
		//	$(this).parent().html(thehtml);
		//});		
		$(this).parent().load('http://www.mrwebsite.nl/wordpress/wp-content/themes/metamorphosis/call.php?no='+no);
		return false;
	});
	
	$('#buttons div').hover(function(){
		$('#buttons div').removeClass('selected');
		$(this).addClass('selected');
		$('.wrapper div').hide();
		$('.wrapper #'+$(this).attr('id')+'a').fadeIn();
       // autoscrolling = false;
	},function(){
        //autoscrolling = true;
       // $('.infiniteCarousel').trigger('page',2);
	});
	
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = true;
    
    $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });
    
	$('.wrapper div').hide();
	$('.wrapper #button1a').fadeIn();
	$('#button1').addClass('selected');
	
	$('a:contains("Google Maps")').each(function(){
		$(this).parent().after('<div class="googlemap"><iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+$(this).attr('href')+'&output=embed"></iframe></div>');
		$(this).parent().hide();
	});
	
	
	$('a:contains(".mp4")').each(function(){
	//	$(this).parent().after('<embed id="VideoPlayer" width="1024" height="600" type="application/x-shockwave-flash" src="http://www.mrwebsite.nl/wordpress/wp-content/themes/metamorphosis/videoplayer.swf?file='+$(this).attr('href')+'" allowFullscreen="true" />');
		//$(this).parent().hide();
	});

	$('#cardform').submit(function(){
	
		$('#downloadrapport').click();
		return false;
	})
	
	$('#downloadrapport').click(function(){
		$.get('http://www.mrwebsite.nl/wordpress/wp-content/themes/metamorphosis/analyse.php?x=checkcode&code='+$('#cardcode').attr('value'), function(thehtml){
			if(thehtml=='true'){
				var code=$('#cardcode').attr('value');
				//$('#cardform').html('<p>De tweede en laatste stap is het invullen van <strong>uw e-mailadres</strong>. U ontvangt vervolgens het rapport direct via e-mail.</p><input type="text" id="email" value="uw e-mailadres" /><h6><a href="#" id="sendlink">Rapport verzenden</a></h6><p>Wij hebben een ontzettende hekel aan spam. Uw e-mail adres zal nooit worden overgedragen aan derden. Ook zal u nooit reclame ontvangen van derden via ons. Uw e-mailadres zal alleen bij uitzondering gebruikt worden om u te informeren over uw website en onze diensten.</p>');
				$('#cardform').html('<p class="red">De code is helaas niet meer geldig. De code was geldig tot 22 januari 2010, zoals op de kaart vermeld staat.</p><p>U kunt natuurlijk wel altijd contact opnemen door even te bellen: 020 7173978 of vul uw nummer in rechtsboven op deze pagina.</p>');
				
				$("h6 a").each(function (i) {
					$(this).wrapInner('<span>');
				});
				$('#email').focus(function(){
					$(this).attr('value','');
					$(this).css('color','#000');
				});
				$('#sendlink').click(function(){
					$.get('http://www.mrwebsite.nl/wordpress/wp-content/themes/metamorphosis/analyse.php?x=sendlink&code='+code+'&email='+$('#email').attr('value'), function(thehtml){
						$('#cardform').html('<p>De link naar het rapport is succesvol naar uw e-mailadres verstuurd!</p><p><h6><a href="http://www.mrwebsite.nl">Bekijk de rest van de website.</a></h6></p>');
						$("h6 a").each(function (i) {
							$(this).wrapInner('<span>');
						});
						return false;
					});
				});
				$('#cardform').submit(function(){
				
					$('#sendlink').click();
					return false;
				})
				
			}else{
				$('#downloadrapport').after('<br /><br /><p class="red">Helaas, uw code is onjuist. <br />Probeer het alstublieft nog een keer.</p>');
			}
			return false;
		});		
	
	
	
		//$(this).after('<div id="">Uw rapport wordt nu opgehaald.</div>');
	});
	

    
});

(function ($) {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
          //  $wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
            // 6. bind the back and forward links
            $('a.back', this).hover(function () {
          //      gotoPage(1);
                //return false;
            });
            
            $('a.forward', this).click(function () {
           //     gotoPage(currentPage + 1);
           //     return false;
            });
                        
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
            
            $(this).bind('page1', function () {
                gotoPage(1);
            });
            $(this).bind('page2', function () {
                gotoPage(2);
            });
            $(this).bind('page3', function () {
                gotoPage(3);
            });
            $(this).bind('page4', function () {
                gotoPage(4);
            });
            
        });
    };
})(jQuery);
