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');
    }
	
	enQueteTeaser.init();
		
   $('#homepage_block1 .teaserbutton').pngFix(); 
	
    if ($("#homepage_block1").length > 0) {
    	 Teaser.init();
    }
	
	if ($("h1.cufonhead").length > 0) {
    	 Cufon.replace('h1.cufonhead', { fontFamily:'thesans' });
    }
	if ($('.facebook').length > 0){
		Cufon.replace('h1, h2, h3', { fontFamily:'thesans' });
	}

    //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 ($('#project_date_start').length) {
    	addProject.init();
    }
    
    if ($('#showallnews').length) {
        $('.newslisting ul li.news-hidden').each(function() {
           if($(this).hasClass('active')) {
               showAllNewsItems();
           }
        });
        $('#showallnews').click(function() {
            showAllNewsItems();
        });
        
        $('#hideallnews').click(function() {
            hideAllNewsItems();
        });
    }
	
	if ($('.addthisBar').length > 0) {
        if($.browser.msie && parseInt($.browser.version) == 7) {
            $('.addthisBar').addClass('addthisBarIe');
        }else if($.browser.opera) {
            $('.addthisBar').addClass('addthisBarOpera');
        }
        $.getScript("http://s7.addthis.com/js/250/addthis_widget.js?username=plag");
    }

    $(".optinemail").hide();
    
    $("#optin").click(function() {
    	if ($(this).attr('checked')) {
            $(".optinemail").show();
        } else {
            $(".optinemail").hide();
        }
    });
    

});

var addProject = {

		init: function() {
			$.datepicker.setDefaults({
				   showOn: 'focus',
				   dateFormat: 'dd-mm-yy',
				   buttonImageOnly: true,
				   buttonImage: '../img/icon-calender.gif',
				   buttonText: 'Kies datum in popup'
			});

			$('#project_date_start').datepicker({
				onSelect: function(sDate, inst) {
				addProject.updateEndDate();
				}
			}).datepicker('option',$.datepicker.regional.nl).datepicker('option', 'dateFormat', 'dd-mm-yy');
			
			$('#project_date_end').datepicker({
				minDate:new Date
			}).datepicker('option',$.datepicker.regional.nl).datepicker('option', 'dateFormat', 'dd-mm-yy');
		},
		
		updateEndDate: function() {
			var aSplitted = $('#project_date_start').val().split('-');
			aSplitted[0] = parseInt(aSplitted[0]);
			aSplitted[1] = parseInt(aSplitted[1]);
			aSplitted[2] = parseInt(aSplitted[2]);
			$('#project_date_end').datepicker('option','minDate', new Date(aSplitted[2],aSplitted[1]-1,aSplitted[0]+1));
		}

};

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());
	
}

var enQueteTeaser = {
	init: function() {
		$('#enqTeaserCorner').mouseenter(function(){
		$(this).addClass('enqTeaserCornerOpened'); 
			
		});
		$('#enqTeaserCorner').mouseleave(function(){
			if($(this).hasClass('enqTeaserCornerOpened')){
				$(this).removeClass('enqTeaserCornerOpened');
			}
		});
		
		
	}
};
