var Teaser = {
    
    currentId: 1,
    lastId: 4,

    init: function () {
        var intervalTeaser = setInterval(function() {Teaser.rotate()}, 5000);
        Teaser.activate(Teaser.currentId);
    },

    activate: function (buttonId) {
        target = $('.teaserbutton_' + buttonId);
         
        $('.teaseritem').hide();
        $('.teaseritem_' + buttonId).show();
        
        target.siblings().children().removeClass("active"); 
        target.children().addClass("active").pngFix(); 
    },
    
    rotate: function () {
    	Teaser.lastId = Teaser.currentId;
        if (Teaser.currentId++ == 4) {
            Teaser.currentId = 1;
        } 
       Teaser.activate(Teaser.currentId);
    }
}

$(document).ready(function() {
    // Scroll to H2: betalingsregeling
    if (document.location.hash == '#betalingsregeling') {
    	$('html,body').animate({scrollTop : 740},'slow');
    }
		
   $('#homepage_block1 .teaserbutton').pngFix(); 
	
    if ($("#homepage_block1").length > 0) {
    	 Teaser.init();
    }

    //Since IE doesn't understand some basic CSS, add some classes...
    $("#teaserbuttons li:last-child").addClass("last-child");
    
    if ($('#homepage_consumer_block1').length > 0) {
    	
		if($('#iGallery').length > 0){

			$.getScript(websiteGlobal.baseUrl+'/website/js/gallery.js', function(){
				
				$('#iGallery').Gallery({

					autoPlay				: true,
					autoPlayInterval		: 3000,
					
					elmTriggers		: $('#iNavigation a'),
					elmContent		: $('#iImages li'),
					
					iFadeDuration	: 1000

					
				});
			});

			Cufon.replace('#iImages h2', { fontFamily:'thesans' });
		}
		
    }

    //Since IE doesn't understand some basic CSS, add some classes...
    $("#footer li:first-child").addClass("first-child");

    // Google map
    if ($("#google-map").length > 0) {    	

	    var myLatlng = new google.maps.LatLng(52.043814, 4.505978);
	    var myOptions = {
	      mapTypeControl: false,
	      zoom: 13,
	      center: myLatlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    }
	    
	    var map = new google.maps.Map(document.getElementById("google-map"), myOptions);
	    
	    var marker = new google.maps.Marker({
	        position: myLatlng, 
	        map: map, 
	        title:"Stichting BouwGarant"
	    });   
    }
    
    // Menu
    if($('#menu .pulldown').length) {
        pulldownMenu();
    }

    $("#teaserbuttons li").click(function() {
        clearInterval(timedTabChange);
        TabChange.ChangeTabs($(this).attr("id").substring(13));
    });
    
    $(".regionbuilders div").hide();
    
    $(".regionbuilders p em").click(function() {
        $(this).parent().parent().children("div").slideToggle();
    });
 
    if ($('#showallnews').length) {
        $('.newslisting ul li.news-hidden').each(function() {
           if($(this).hasClass('active')) {
               showAllNewsItems();
           }
        });
        $('#showallnews').click(function() {
            showAllNewsItems();
        });
        
        $('#hideallnews').click(function() {
            hideAllNewsItems();
        });
    }
    
});

FAQ = {
	openItem: function (elt) {
		bodyElt = $(elt).find('.faq_body');
		
		if ($(bodyElt).hasClass('active-item')) return;
		
		$('#faqlisting').find('.active-item').slideUp().removeClass('active-item');
		
		$(bodyElt).addClass('active-item').slideDown();
	}
}

function replaceIHKContent(elt) {
    $this = $(elt);
    
    var oldId = $this.siblings('.active').attr('id');
    $this.siblings('#' + oldId ).removeClass(oldId + '-active').removeClass('active').addClass(oldId + '-inactive');
    
    $this.addClass($this.attr('id') + '-active').addClass('active');
    $('#inhetkortcontent').html('<h1>' + $this.children('span').html()  + '</h1>' + $this.children('input').val());
}

function getStreet () {
    if ($('#straat').val() + $('#woonplaats').val() != '') return;
    
    $.ajax({
    	type		: "GET",
    	url			: fullBaseUri + "/brochures/getstreet/" + $('#postcode').val() + $('#huisnummer').val(),
    	timeout     : 1500,
    	success		: function(html){
    		var addrinfo = eval('(' + html + ')');
    		if (addrinfo != undefined && addrinfo.street != undefined) { 
                $('#straat').val(addrinfo.street);
                $('#woonplaats').val(addrinfo.city);
        	    $('#email').focus();
    		}
        }
    });
}

function pulldownMenu() {
    $('#menu .pulldown').hover(
        function(){ 
            $(this).find('ul.menu-sub').show();
        },
        function(){ 
            $(this).find('ul.menu-sub').hide();
        }
    );
}

function searchRedirect (sQuery) {
	document.location.href=websiteGlobal.baseUrl+'/aannemers/'+sQuery;
}

function showAllNewsItems(el) {
    $('.newslisting ul').find('li.news-hidden').show();
    $('.newslisting').find('#hideallnews').show();
    $('#showallnews').hide();
}

function hideAllNewsItems(el) {
    $('.newslisting ul').find('li.news-hidden').hide();
    $('.newslisting').find('#showallnews').show();
    $('#hideallnews').hide();
}


function setRouteLink (elt) {
	var sFormValueStreet = $('#streetaddress').val();
	var sFormValueCity = $('#city').val();
	
	var sStreetAddress = (sFormValueStreet == 'Adres' || sFormValueStreet == '') ? false : sFormValueStreet;
	var sCity = (sFormValueCity == 'Woonplaats' || sFormValueCity == '') ? false : sFormValueCity;
	var sCombined = '';
	
	if (sStreetAddress) {
		sCombined += sStreetAddress;
	}
	if (sStreetAddress && sCity) {
		sCombined += ', ';
	}
	if (sCity) {
		sCombined += sCity;
	}
	$('#saddr').val(sCombined);	
	
	elt.attr('href', 'http://maps.google.com/maps?' + $('#form_route').serialize());
	
}
