Cufon.replace('h2')('.nbpdf');
//
//('a.nav',{
//    hover: true
//})('a.subnav',{
//    hover: true
//})

$(document).ready(function(){
	
    $('#homeaccordion').css({
        displa: 'block'
    });
	
    $('.foil').cycle({
        cleartypeNoBg: true,
        timeout: 4000,
        fx: 'fade'
    });
    $('.projecten').cycle();
	
    $('.cycle').cycle({
        timeout: 3000
    });
	 
    $('.left .cycler').cycle({
        timeout: 3000,
        pager: '#nav',
        containerResize: 1,
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#"><img src="' + slide.src.split('large').join('normal') + '" width="120"  border="0" style="margin-right: 7px" /></a></li>';
        }												   
    });
	
    $('#homecycle').cycle({
        timeout: 3000,
        fx: 'fade',
        //			pager: '#subsiteNav',
        //			pagerAnchorBuilder: function(idx, slide) {
        //				return '#subsiteNav li a:eq('+idx+')';
        //			},
        //			activePagerClass: 'subsitenavActive',
        //			pagerEvent: 'mouseover',
        containerResize: 0
        //			,allowPagerClickBubble: true													   
    });
	
    var subTimeOut = false;
    $('a.nav').hover(
        function(){
            subid = $(this).attr('rel');
            var submenuLeftPosition = $(this).position().left -5;
            clearTimeout(subTimeOut);
            $('.subdiv').css('display', 'none');
            if($(subid).length > 0){
                $(subid).css({
                    left: submenuLeftPosition + 'px',
                    display: 'block'
                });
            }
        },
        function(){
            subid = $(this).attr('rel');
            subTimeOut = setTimeout(function(){
                $(subid).css({
                    display: 'none'
                });	
            }, 500);
        }
        )
	
    $('.subdiv').hover(
        function(){
            clearTimeout(subTimeOut);
            $(this).css({
                display: 'block'
            });
            parentid = $(this).attr('parentid');
            $(parentid).addClass('hoofdNavActive');
        },
        function(){
            $(this).css({
                display: 'none'
            });	 
            parentid = $(this).attr('parentid');
            $(parentid).removeClass('hoofdNavActive'); 
        }
        );
	
    $('#categorieselect').change(function(){
        location.href = $(this).val();
    });
	
//    $('#homeaccordionimages').cycle({
//        fx: 'fade',
//        timeout: 3000,
//        pager: '#imgaccordion',
//        after: function(){
//            $('#homeaccordion').show();
//            $('#homecycle').hide();
//            $('#subsiteNav').hide();
//        },
//        pagerAnchorBuilder: function(idx, slide) {
//            return '#imgaccordion div a:eq('+idx+')';
//        },
//        pagerEvent: 'mouseover'
//    });
	
    /*$('#imgaccordion div a').mouseout(function(){
		$('#homeaccordion').hide();
		$('#homecycle').show();
	});*/
	
    $('#imgaccordion').mouseleave(function(){ 
        $('#homeaccordionimages').cycle('stop');
        $('#homeaccordion').hide();
        $('#homecycle').show();
        $('#subsiteNav').show();
    });
    
     $('#imgaccordion').mouseenter(function(){ 
         $('#homeaccordionimages').cycle({
            fx: 'fade',
            timeout: 3000,
            pager: '#imgaccordion',
            after: function(){
                $('#homeaccordion').show();
                $('#homecycle').hide();
                $('#subsiteNav').hide();
            },
            pagerAnchorBuilder: function(idx, slide) {
                return '#imgaccordion div a:eq('+idx+')';
            },
            pagerEvent: 'mouseover'
        });
    });
	 
     
    $('#imgaccordion_header').click(function(){
       $(this).slideUp(300);
       $('#imgaccordion').slideDown(300);
        $('#homeaccordionimages').cycle({
            fx: 'fade',
            timeout: 3000,
            pager: '#imgaccordion',
            after: function(){
                $('#homeaccordion').show();
                $('#homecycle').hide();
                $('#subsiteNav').hide();
            },
            pagerAnchorBuilder: function(idx, slide) {
                return '#imgaccordion div a:eq('+idx+')';
            },
            pagerEvent: 'mouseover'
        });
    });
	
    /*
	$('#imgaccordion div a').mouseover(function(){
		//$('#imgaccordion div').not(this).animate({
		//	height: 6 	+ 'px'
		//},500).find('img').stop(true).animate({opacity:.4},500);
		//$(this).animate({
		//	height: 203 + 'px'
		//},500).find('img').stop(true).animate({opacity:1},500);
			
			href = $(this).attr('href');
			title = $(this).find('img').attr('title');
			$('#homecycle').css({opacity: 0});
			$('#homecycle').html('<div class="cyclereplace"><img src="'+ href +'" /><span>'+ title +'</span><a class="closebut" href="#">CLOSE X</a></div>');
			$('#homecycle').animate({opacity: 1},500);
			
				
			
			e.preventDefault();
		
	});
	*/
    $('.closebut').click(function(e){
        $('#homeaccordion').hide();
        $('#homecycle').show();
        e.preventDefault();
    });		
    $('#homeaccordion').hide();
    $('#homecycle').show();
    $('#homeaccordionimages').cycle('stop');
    //$('#imgaccordion div:last-child').trigger('click');
	
    var savedCycler = $('#homecycle').html();
	
//    $('#imgaccordion div a').click(function(e){
//        if($(this).parent().height() > 6){
//            href = $(this).attr('href');
//            title = $(this).find('img').attr('title');
//            $('#homecycle').css({
//                opacity: 0
//            });
//            $('#homecycle').html('<div class="cyclereplace"><img src="'+ href +'" /><span>'+ title +'</span><a class="closebut" href="#">CLOSE X</a></div>');
//            $('#homecycle').animate({
//                opacity: 1
//            },500);
//			
//            $('.cyclereplace .closebut').click(function(e){
//                $('#homecycle').animate({
//                    opacity: 0
//                },300, false, function(){
//                    $('#homecycle').html(savedCycler);
//                    $('#homecycle').animate({
//                        opacity: 1
//                    },500);
//					
//                    $('#homecycle').cycle({
//                        timeout: 3000,
//                        fx: 'fade',
//                        pager: '#subsiteNav',
//                        pagerAnchorBuilder: function(idx, slide) {
//                            return '#subsiteNav li a:eq('+idx+')';
//                        },
//                        activePagerClass: 'subsitenavActive',
//                        pagerEvent: 'mouseover',
//                        allowPagerClickBubble: true													   
//                    });
//					
//                });
//                e.preventDefault();
//            });			
//			
//            e.preventDefault();
//        }else{
//            e.preventDefault();
//        }
//    });
	
});

$(window).load(function(){
	
    if(fotoSplash != ''){
        $('#fotosplash').delay(3000).fadeOut(500);
    }
	
	
    if($('#gmap').length > 0){
        doGoogleMaps();
    }	
	
    if($('#gmap2').length > 0){
        doGoogleMaps2();
    }	
	
    if($('div.sliderGallery').length > 0){
        var container = $('div.sliderGallery');
        var ul = $('ul', container);
        var totalwidth = 0;
	
        $('ul.items li').each(function() {
            totalwidth = totalwidth + $(this).width();								 
        });
		
        $('.sizer').width(totalwidth);			
			
        var cw = 980; 
        var lw = totalwidth; 
        var mousepos = 1;			
		
        container.mousemove(function(e) { 
            var diff = e.pageX - mousepos; 
            if (diff > 4 || diff < -4) { 
                mousepos = e.pageX - container.offset().left - 100; 
                newX = '-' + ((lw - cw) / 780) * mousepos;
                diff = parseInt(Math.abs( ul.css('left').substring(0,4) - newX )); 
                if(mousepos <= 780) {
                    ul.stop().css({
                        'left':newX + 'px'
                        }); 
                }
            }
        });
		
	
        $(document).bind("idle.idleTimer", function() {
            startCycle();
        });
        $.idleTimer.start(1000);
			
    }
	
    $('#calculateroute').click( function(){
        setRoute($('#directions'));
    });
	
});

$(window).resize(function(){
    });


var bounds;
var infowindow;
var map;

var location1 = '<table><tr><td><strong>Euramax Coated Products BV</strong></td></tr><tr><td>Industrieweg 6</td></tr><tr><td>6045 JG  Roermond</td></tr><tr><td>The Netherlands</td></tr><tr><td>Tel:  + 31 475 370 303</td></tr><tr><td>Fax: + 31 475 323 923</td></tr><tr><td>info@euramax.nl</td></tr></table>';
var location2 = '<table><tr><td><strong>Euramax Coated Products Ltd</strong></td></tr><tr><td>Brunel Road, Earlstrees Industrial Estate</td></tr><tr><td>Corby, Northamptonshire, NN17 4JW</td></tr><tr><td>United Kingdom</td></tr><tr><td>Tel:  + 44 1536 400 800</td></tr><tr><td>Fax: + 44 1536 400 427</td></tr><tr><td>info@euramax.co.uk</td></tr></table>';

function doGoogleMaps() {
    bounds = new google.maps.LatLngBounds();
    var myOptions = {
        zoom:2, 
        disableDefaultUI:true, 
        center:new google.maps.LatLng(0,0), 
        mapTypeId:google.maps.MapTypeId.ROADMAP
        };
    map = new google.maps.Map(document.getElementById("gmap"), myOptions);
    infowindow = new google.maps.InfoWindow();
    for(var i = 0; i < projecten.length; i++) {
        addMarker(projecten[i]);
    }
} 

function doGoogleMaps2() {
	
    bounds = new google.maps.LatLngBounds();
    bounds.extend(new google.maps.LatLng(51.17127, 6.03930));
    bounds.extend(new google.maps.LatLng(52.51024, -0.69184));
	
    var myOptions = {
        zoom:14, 
        disableDefaultUI:false, 
        center:new google.maps.LatLng(51.17127, 6.03930), 
        mapTypeId:google.maps.MapTypeId.ROADMAP
        };
    map = new google.maps.Map(document.getElementById("gmap2"), myOptions);
    infowindow = new google.maps.InfoWindow();
  
    var marker1 = new google.maps.Marker({
        position:new google.maps.LatLng(51.17127, 6.03930), 
        map:map, 
        title:'Euramax Coated Products BV'
    });
    google.maps.event.addListener(marker1, 'click', function() {
        var content = '<div class="gmInfowindow">'+location1+'</div>';
        infowindow.setContent(content);
        infowindow.open(map, marker1);
    });
    var marker2 = new google.maps.Marker({
        position:new google.maps.LatLng(52.51024, -0.69184), 
        map:map, 
        title:'Euramax Coated Products Ltd'
    });
    google.maps.event.addListener(marker2, 'click', function() {
        var content = '<div class="gmInfowindow">'+location2+'</div>';
        infowindow.setContent(content);
        infowindow.open(map, marker2);
    });
    map.fitBounds(bounds);
}

var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
		

function setRoute($this){
    var start = $this.find("[name=adres]").val() + ", " + $this.find("[name=city]").val() + ", " + $this.find("[name=country]").val();
    var destid  = parseInt($this.find("[name=office]").val());
    switch(destid) {
        case 1:
            var dest = new google.maps.LatLng(51.17127, 6.03930);
            break;
        case 2:
            var dest = new google.maps.LatLng(52.51024, -0.69184);
            break;
    }
		
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("routebeschrijving"));
		
    var request = {
        origin:start, 
        destination: dest,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            $('#routebeschrijving').prepend('<br/><a href="http://maps.google.nl/maps?saddr='+start+'&daddr='+dest+'&hl=en&pw=2" target="_blank"><img src="images/print.gif"/> Print these directions</a><br/>')
        }else{
            alert("Your adres cannot be found, please try again.");
        }
    });
}


function addMarker(p) {
	
	
    if(p[3] != "0" && p[3] != "" && p[4] != "0" && p[4] != "") {
        var latlng = new google.maps.LatLng(p[3], p[4]);
        if(p[8] != 0) {
            bounds.extend(latlng);
            map.fitBounds(bounds);
        } else {
            map.setCenter(latlng);
        }
        //var icon = new google.maps.MarkerImage('images/wereldbol.png', new google.maps.Size(32,32), new google.maps.Point(0,0), new google.maps.Point(17,32) );
        //console.log(p[5]);
        var icon = new google.maps.MarkerImage(p[5], new google.maps.Size(32,32));
    
        var marker = new google.maps.Marker({
            position:latlng, 
            icon:icon, 
            map:map, 
            title:p[1]
            });
        google.maps.event.addListener(marker, 'click', function() {
            var content = '<div class="gmInfowindow">' + unescape('<strong style="font-size:12px">' + p[1] + '</strong><div class="p">' + unescape(p[2]));
            content += '</div></div>';
            infowindow.setContent(content);
            infowindow.open(map, marker);
        });
    }
}

function startCycle() {
    var container = $('div.sliderGallery');
    var ul = $('ul', container);
    var totalwidth = 0;

    $('ul.items li').each(function() {
        totalwidth = totalwidth + $(this).width();								 
    });
    $('ul', container).animate({
        left: '-' + (totalwidth - 980) + 'px'
        }, 80000, false, function() {
        $('ul', container).animate({
            left: '0px'
        }, 80000, false, function() {
            startCycle();																	  
        });																  
    });
}	

