$(document).ready(function() {

	// Homepage welcome msg
	
	var displayTime = function (target) {
       
		var localTime = new Date();
        var year = localTime.getYear();
        var month = localTime.getMonth() + 1;
        var date = localTime.getDate();
        var hours = localTime.getHours();
        var minutes = localTime.getMinutes();
        var seconds = localTime.getSeconds();
      
        if (hours > 0 && hours < 12) {
            target.html("Goedemorgen");   //+ year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
		}
		
        if (hours > 12 && hours < 18) {
            target.html("Goedemiddag");  //+ year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
		}
		
        if (hours > 18 && hours < 23) {
            target.html("Goedenavond");  // + year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
		}
    }
	
	if($('#WelcomeMessageHeader').length === 1) {
		displayTime($('#WelcomeMessageHeader'))
	}
	
	
	 // conduit
    Cufon.replace('h1, h2, h3, h4, #carousel span, .block.landing, #WelcomeMessageBox, span.themaHeader', { fontFamily: 'Conduit ITC Medium' });
	Cufon.replace('ul#contactLocations li a, #mainNav a, #homeTeaser .downloadButton, div#introContent a', {fontFamily: 'Conduit ITC Medium',hover:true});

    // customize input:text
    $('input:text, input:password').wrap('<div class="custom clearfix"><span></span></div>');
	
	
	// carousel
    if($('#carousel').length === 1) {
		var length = 6;
		var selected = $('#carousel li').index($('#carousel .selected'));
		var start = ((selected + 1) > length) ? (selected - 1) : 0;

		if ($('#carousel li').length <= length) // hide prev/next if 6 or less items
			$('#carousel .next, #carousel .prev').hide();

		$('#carousel .cases').jCarouselLite({
			btnNext: '#carousel .next',
			btnPrev: '#carousel .prev',
			easing: 'easein',
			speed: 1000,
			visible: 6,
			scroll: 3,
			start: start
		});

		$('#carousel li').not('.selected').each(function() {

			var $title = $('span', $(this)).hide();
			var $img = $('img', $(this));

			$img.css({ opacity: 0.5 });

			$img.hover(function() {
				$title.show();
				$(this).stop().animate({ opacity: 1.0 }, 300);
			},
			function() {
				$title.hide();
				$(this).stop().animate({ opacity: 0.5 }, 300);
			});

		});
	}

    // offset
    $('#content').scroll(function() {

        var pos = 50 + $('#content')[0].scrollTop; // top: 50

        $('#related').stop().animate( // $('h1')
			{'top': pos + 'px' },
			{ 'duration': 500, 'easing': 'easein' }
		);

    });


    // accordion
    $('.accordion h2, .accordion h3').click(function() {
	
      
		if ($(this).parent().hasClass('current')) {

            $(this).siblings('.intro, .main').slideToggle();
            $('.current', $(this).closest('.accordion')).removeClass('current');
            Cufon.refresh();
            return false;

        }

        $(this).closest('.accordion').find('.current').each(function() {
            $('.intro, .main', $(this)).slideToggle();
        });

        $(this).siblings('.intro, .main').slideToggle('normal', function() {
            $('.current', $(this).closest('.accordion')).removeClass('current');
            $(this).parent().addClass('current');
            Cufon.refresh();
        });
		
		/* Show attached alternate content */
	
		if($('#related-content-' + this.title).length != 0) {
			$('#related div.block').css('display','none');
			$('#related-content-' + this.title).css('display','block');
			
			// also display balloon ) if any
			$('#related-content-' + this.title + ' .balloon').css('display','block');
			
		}

    });


    // scale background image
    $("#image img").each(function() {

        if ($(this)[0].complete) {
            $(this).scale();
        }
        else {
            $(this).load(function() {
                $(this).scale();
            });
        }

    });

    $(window).bind('resize', function() {
        $('#image img').scale();
    });


    // related content
    relatedContent.init();
	
	// contact page image switch
	
	if($('#contactLocations')) {
		
		$('#contactLocationImages').css('display','block');
		
		$('#contactLocations a').bind('mouseover',function() {
			
			$('#contactLocations a').parent().removeClass('active');
			Cufon.replace('ul#contactLocations li a', {fontFamily: 'Conduit ITC Medium',hover:true});
			$(this).parent().addClass('active');
			
			if($('#contactLocationImages')) {
				if($('#contactLocationImage')) {
					$('#contactLocationImage').attr('src',$(this).attr('rel'));
					$('#contactLocationImage').attr('alt','Vestiging ' + $(this).attr('rev'));
					$('#contactLocationImage').attr('title','Vestiging ' + $(this).attr('rev'));
				
					$('#contactLocationImages h4').html($(this).attr('rev'));
					
					Cufon.replace('#contactLocationImages h4', {fontFamily: 'Conduit ITC Medium'});
				}
			}
		});
	}
	
	// Download button
	if($('#whitePaperButton')) {
		$('#whitePaperButton').bind('click',function() {
			if($.cookie("ClockworkWelcomeMessageCookie").indexOf("DownloadedWhitepaper=true") === -1) {
				$.cookie("ClockworkWelcomeMessageCookie", $.cookie("ClockworkWelcomeMessageCookie") + "&DownloadedWhitepaper=true", { path: '/', expires: 100 });
			}
		});
	}
	
	// Related accordion content (init)
	if($('ul.accordion h2.active')) {
		$('#related-content-' + $('ul.accordion h2.active').attr('title')).css('display','block');
		
		// also display balloon ) if any
		$('#related-content-' + $('ul.accordion h2.active').attr('title') + ' .balloon').css('display','block');
	}
	
	// Site popup layer
	$('#sitePopupLayer .closeLayer').live('click', function(e) {
		e.preventDefault();
		$('#sitePopupLayer').showSitePopup(0);
	});
	
});

var relatedContent = {

    id: null,

    init: function() {

        if ($('#related').hasClass('async')) {

            relatedContent.id = $('#body .related > .current > :first-child').attr('id');

            relatedContent.get();
            relatedContent.click();

        }
        else {
            relatedContent.show();
        }

    },

    click: function() {

        $('.related > li > :first-child').click(function() {

            relatedContent.id = $(this).attr('id');

            relatedContent.get();

        });

    },

    get: function() {

        $('#related > ul > li').slideUp('normal', function() {

            $(this).children().remove(); // loading...?

            $.ajax({
                url: '/services/klantweb.aspx?id=' + relatedContent.id,
                cache: false,
                success: function(html) {
                    $("#related li:first").append(html);
                    relatedContent.show();
                }
            });

        });

    },

    show: function() {

        $('#related .block').not('.logo').each(function() {
            $('.balloon', $(this)).hide();
        }).hide();

        $('#related li:first .block').not('.logo').fadeIn('normal', function() {

            $('.balloon', $(this)).slideDown('normal');

            $('#related li:first .logo').slideDown('normal'); // ...

        }).parent().show();

    }

}

$.fn.scale = function() {

    var imageWidth, imageHeight;

    this.css({ width: 'auto', height: 'auto' });

    if ($(window).height() > $(window).width()) {

        imageWidth = $(window).height() * (this.width() / this.height());

        if ($(window).width() > imageWidth) {
            imageWidth = 'auto';
            imageHeight = $(window).width() * (this.height() / this.width());
        }
        else {
            imageHeight = 'auto';
        }

    }
    else {

        imageHeight = $(window).width() * (this.height() / this.width());

        if ($(window).height() > imageHeight) {
            imageHeight = 'auto';
            imageWidth = $(window).height() * (this.width() / this.height());
        }
        else {
            imageWidth = 'auto';
        }

    }

    this.css({ width: imageWidth, height: imageHeight });

}

/* Site popup layer */
/* usage: $('#sitePopupLayer').showSitePopup(1,'title','message'); */

$.fn.showSitePopup = function(state,title,msg) {

	if(state === 1) {
		$(this).addClass('show');
		$('#sitePopupLayer .popupTitle').html(title);
		$('#sitePopupLayer .popupText').html(msg);
	} else if(state === 0) {
		$(this).removeClass('show');
	} else {
		return false;
	}
};