/*
 *
 * Code by Diffuse Interactive (c)2011
 * www.diffuse.nl
 *
 */

$(document).ready(function() {

    /*
     * service menu
     */
    $('#font-size li').click(function() {
        $(this).parent().find('li.active').removeClass('active');
        $(this).addClass('active');
        var clas = $(this).hasClass('large') ? 'large' : 'normal';
        $('body').removeClass().addClass(clas);
        $.ajax({
             url: $('#logo a').attr('href') + '/accessibility/set/font/' + clas,
             type: 'get',
             dataType: 'json'
        });
    });

    $('#print').click(function() {
        window.print();
    });

    $('#service li').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    $('#search').click(function() {
        $(this).find('input').focus();
    });

    $('#search a').click(function() {
        $('#search form').submit();
    });

    if ($('#highlight').length == 1) {
	
        $('#highlight .overlay .title').html($('#highlight li:first-child span.title').html());

		if ($('#highlight li').length > 1) {
        	var item_width = $('#highlight li').outerWidth(),
	            left_value = item_width * (-1);

	        $('#highlight li:first').before($('#highlight li:last'));
	        $('#highlight ul').css({'left' : left_value});

	        $('#highlight a.left').click(function() {
				$('#highlight .link').removeClass('active');
	            var left_indent = parseInt($('#highlight ul').css('left')) + item_width;
	            $('#highlight ul:not(:animated)').animate({'left' : left_indent}, 200,function() {
	                $('#highlight li:first').before($('#highlight li:last'));
	                $('#highlight ul').css({'left' : left_value});
	                $('#highlight .overlay .title').html($('#highlight li:nth-child(2) span.title').html());
					if ($('#highlight li:nth-child(2) a').length == 1) {
						$('#highlight .link').addClass('active');
					}
	            });
	            return false;
	        });

	        $('#highlight a.right').click(function() {
				$('#highlight .link').removeClass('active');
	            var left_indent = parseInt($('#highlight ul').css('left')) - item_width;
	            $('#highlight ul:not(:animated)').animate({'left' : left_indent}, 200, function () {
	                $('#highlight li:last').after($('#highlight li:first'));
	                $('#highlight ul').css({'left' : left_value});
	                $('#highlight .overlay .title').html($('#highlight li:nth-child(2) span.title').html());
					if ($('#highlight li:nth-child(2) a').length == 1) {
						$('#highlight .link').addClass('active');
					}
	            });
	            return false;
	        });
		} else {
			$('#highlight a.left').hide();
			$('#highlight a.right').hide();
		}
		
		$('#highlight .link').click(function() {
			
			var nth = $('#highlight li').length == 1 ? 1 : 2;
			if ($('#highlight li:nth-child(' + nth + ') a').length == 1) {
				location.href =  $('#highlight li:nth-child(' + nth + ') a').attr('href');
			}
			
		});
		
    }

    if ($('#products').length == 1) {
        var active = 0;
        $('#products li .overlay').click(function() {
            location.href = $(this).parent().find('a').attr('href');
        });
        $('#products li').click(function() {
           if (active == 0 && !$(this).hasClass('active')) {
               active = 1;
               $('#products li.active .info').slideUp();
               $(this).find('.info').slideDown('normal', function() {
                   active = 0;
               });
               $('#products li.active').removeClass('active');
               $(this).addClass('active');
           }
        });
    }

    if ($('#subscribe').length == 1) {
        $('#subscribe').hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
        $('#subscribe').click(function() {
            location.href = $(this).find('a').attr('href');
        });
    }

    if ($('#locations').length == 1) {
        if( $('#list').is(':visible')) {
            $('#locations .list').hide();
        } else {
	        $('#locations .map').hide();
        }
        $('#locations .list').click(function() {
            $('#detailed').hide();
            $('#locations .list').hide();
            $('#list').show();
            $('#locations .map').show();
        });
        $('#locations .map').click(function() {
            $('#list').hide();
            $('#locations .map').hide();
            $('#detailed').show();
            $('#locations .list').show();
        });

        $('#locations #filter select').change(function() {
            
            $(this).parent().find('span').html($('#locations #filter select option:selected').text());
            var action = $(this).parent().find('input').val(),
                val = $('#locations #filter select option:selected').val();

            if (!val) {
                for (i in markersArray) {
                    markersArray[i].setOptions({visible: true});
                }
				mc.clearMarkers();
				for (i in markersArray) {
					mc.addMarker(markersArray[i]);
				}
                $('#list li.loc').show();
                return;
            }

            $.ajax({
                 url: action,
                 data: {
                     'id': val
                 },
                 type: 'get',
                 dataType: 'json',
                 cache: true,
                 success: function(response) {	
					mc.clearMarkers();
                    for (i in markersArray) {
                        markersArray[i].setOptions({visible: false});
                    }
                    $('#list li.loc').hide();
                    $.each(response,function(i, id) {
                        markersArray[id].setOptions({visible: true});
                        $('#loc-' + id).show();
                    });
					for (i in markersArray) {
						if (markersArray[i].getVisible()) {
							mc.addMarker(markersArray[i]);
						}
					}
                 },
                 error: function() { },
                 complete: function() { }
            });
        });
    }

    //slideshow
    if ($('#photos') && !$('#photos').hasClass('simple') && $('#photos li').length > 1) {
        $('#photos').cycle({
            fx: 'fade'
        });
    }

    // actueel
    $('.more2').click(function() {
        $(this).addClass('loading');
        var html = '<span class="comments">3</span><span class="date">12 november 2011</span><h3><a href="#">Lorem ipsum dolor sit amet</a></h3><div class="image"><div class="overlay"></div><img src="images/image6.jpg" alt="" border="0" /></div><p>Nulla eget ultrices turpis. Nam accumsan aliquet feugiat. Pellentesque eu massa vitae augue mattis venenatis nec eu quam. Etiam mattimassa ultricies ad. <a href="#">lees verder &raquo;</a></p></li>';
        $('<li></li>')
        .html(html)
        .appendTo($(this).parent().parent().find('.items'));
        $(this).removeClass('loading');
    });



    $('.more').click(function() {

        $(this).addClass('loading');
        var load = $(this),
            parent = $(this).parent().parent();
            start = parent.find('ul li').length,
            action = $(this).find('input').val(),
            html = ''
            div = parent.attr('id');

        $.ajax({
             url: action,
             data: {
                 'start': start
             },
             type: 'get',
             dataType: 'json',
             cache: false,
             success: function(response) {
                 $.each(response.items,function(i, item) {
                    html = '';
                    if (item.allow_comments == 1) {
                        html += '<span class="comments">' + item.comments + '</span>'
                    }
                    html += '<span class="date">' + item.date + '</span><h3><a href="' + item.url + '">' + item.title + '</a></h3><div class="image"><div class="overlay"></div><img src="' + item.image + '" alt="" border="0" /></div><p>' + item.description + ' <a href="' + item.url + '">lees verder &raquo;</a></p></li>';
                    $('<li></li>')
                    .html(html)
                    .appendTo($(parent).find('ul'));
                });
                if (!response.more) {
                    load.hide();
                }
             },
             error: function() {
             },
             complete: function() {
                 load.removeClass('loading');
             }
         });

    });

    // forms
    $('#form select').change(function() {
        $(this).parent().find('span').html($(this).find('option:selected').text());
    });

    $('#form .checkbox').click(function() {
       if ($(this).hasClass('checked')) {
           $(this).find('input').val(0);
           $(this).removeClass('checked');
       } else {
           $(this).find('input').val(1);
           $(this).addClass('checked');
       }
    });

    // submenu
    if ($('#submenu').length > 0) {
        //$('#submenu ul li ul li.active').parent().parent().addClass('active');
    }

    // comments
    $('#submitComment').click(function() {
        var data = $('#form-comment').serialize();
        $.post($(this).parent().find('input').val(), data, function(resp) {
            $('#form-comment label').removeClass('error');
            for (field in resp) {
                $('#label-' + field).addClass('error');
            }
            if ($('#form-comment .error').length == 0) {
                $('#form-comment').submit();
            }
        });
    });

    // feedback
    if ($('#respond a').length > 0) {
        $('#respond a').fancybox({
            'overlayOpacity'	: 0.5,
            'transitionIn'		: 'fade',
            'transitionOut'		: 'fade',
            'overlayColor'		: '#000',
            'padding'			: 0,
            'width'             : 470,
            'height'            : 340
        });
    }

    // feedback form
    $('#submitFeedback').click(function() {
        $('#form-url').val(parent.window.location.pathname);
        var data = $('#form').serialize();
        $.post($(this).parent().find('input').val(), data, function(resp) {
            $('#form label').removeClass('error');
            for (field in resp) {
                $('#label-' + field).addClass('error');
            }
            if ($('#form .error').length == 0) {
                $('#form').submit();
            }
        });
    });

});
