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

var active = 1;

$(document).ready(function() {

    // mid column
    $('#footer-content .column').eq(1).addClass('mid');

    //offices in footer
    $('#offices a').click(function () {
        $('html, body').animate({scrollTop: $("body").attr("scrollHeight") + 'px'}, 800);
        if($(this).parent().find('div').css('display') == 'none'){
            $(this).parent().parent().find('div').slideUp();
            $(this).parent().parent().find('div').removeClass('open');
            $(this).parent().parent().find('a').removeClass('open');
            $(this).parent().find('div').slideToggle();
            $(this).addClass('open');
        } else {
            $(this).parent().find('div').slideToggle();
            $(this).parent().parent().find('div').removeClass('open');
            $(this).parent().parent().find('a').removeClass('open');
        }
    });

    $('.toggledropdown').click(function(){
        if($('#dropdown').css('display') == 'none'){
            $('#tpglogo').addClass('colored');
        }
        else
        {
            $('#tpglogo').removeClass('colored');
        }
        $('#dropdown').slideToggle();
    });

    $('.toggledropdown').mouseover(function(){

       $('#tpglogo').addClass('colored');

    });

    $('.toggledropdown').mouseout(function(){

        if($('#dropdown').css('display') == 'none'){
            $('#tpglogo').removeClass('colored');
        }

    });


        var tabContainers = $('div.tabs > div');
        tabContainers.hide().filter(':first').show();

        $('div.tabs ul.tabNavigation a').click(function () {
                tabContainers.hide();
                tabContainers.filter(this.hash).show();
                $('div.tabs ul.tabNavigation a').removeClass('selected');
                $(this).addClass('selected');
                return false;
        }).filter(':first').click();


    //select
    $('select').each(function() {
        var select = $(this).attr('id');
        $(this).parent().find('span').html($('#' + select + ' option:selected').text());
        $(this).change(function() {
            var selected = $(this).attr('id');
            $(this).parent().find('span').html($('#' + selected + ' option:selected').text());
        });
    });

    if ($('a.training').length > 0) {
        $('a.training').fancybox({
            'overlayOpacity'	: 0.5,
            'transitionIn'		: 'fade',
            'transitionOut'		: 'fade',
            'overlayColor'		: '#000',
            'padding'			: 0,
            'width'             : 762,
            'height'            : 600
        });
    }

    if ($('a.signup').length > 0) {
        $('a.signup').fancybox({
            'overlayOpacity'	: 0.5,
            'transitionIn'		: 'fade',
            'transitionOut'		: 'fade',
            'overlayColor'		: '#000',
            'padding'			: 0,
            'width'             : 762,
            'height'            : 600
        });
    }

    if ($('a.viewlet').length > 0) {
        $('a.viewlet').fancybox({
            'overlayOpacity'	: 0.5,
            'transitionIn'		: 'fade',
            'transitionOut'		: 'fade',
            'overlayColor'		: '#000',
            'padding'			: 0,
            'width'             : 850,
            'height'            : 674
        });
    }

    if ($('a.popup').length > 0) {
        $('a.popup').fancybox({
            'overlayOpacity'	: 0.5,
            'transitionIn'		: 'fade',
            'transitionOut'		: 'fade',
            'overlayColor'		: '#000',
            'padding'			: 20,
            'width'             : 762,
            'height'            : 600
        });
    }

    $('#findSolution').click(function() {
        if ($('#select-devision').val()) {
            $('#solutionFinder').submit();
        } else {
           $('#select-devision').parent().parent().find('label:first').addClass('error');
        }
    });

    $('#select-devision').change(function() {

        var id = $(this).val();
        var action = $('#solutionAction').val();

        if (id) {
            $(this).parent().parent().find('label:first').removeClass('error');
        }

        if ($('#subject-field').length > 0) {
            $('#subject-field').val('');
        }

        var serviceOptions = '<option value="' + $('#select-service').find('option:first').val() + '">' + $('#select-service').find('option:first').text() + '</option>';
        $('#select-service').html(serviceOptions);
        $('#select-service').parent().find('span').html($('#select-service').find('option:first').text());

        var solutionOptions = '<option value="' + $('#select-solution').find('option:first').val() + '">' + $('#select-solution').find('option:first').text() + '</option>';
        $('#select-solution').html(solutionOptions);
        $('#select-solution').parent().find('span').html($('#select-solution').find('option:first').text());

        if (id) {
           $.getJSON(action + '/getservices/devision/' + id, function(data) {
                var len = data.length;
                if (len > 0) {
                    $('#discipline').show();
                    for (var i = 0; i < len; i++) {
                        serviceOptions += '<option value="' + data[i].id + '">' + data[i].title + '</option>';
                    }
                    $('#select-service').html(serviceOptions);
                } else {
                    $('#discipline').hide();
                    $('#select-service').trigger('change');
                }
            });
        }

    });

    $('#select-service').change(function() {
        var id = $(this).val();
        var devision = $('#select-devision').val();
        var action = $('#solutionAction').val();

        var solutionOptions = '<option value="' + $('#select-solution').find('option:first').val() + '">' + $('#select-solution').find('option:first').text() + '</option>';
        $('#select-solution').html(solutionOptions);
        $('#select-solution').parent().find('span').html($('#select-solution').find('option:first').text());

        if (devision) {
           $.getJSON(action + '/getsolutions/devision/' + devision + '/service/' + id, function(data) {
                var len = data.length;
                for (var i = 0; i < len; i++) {
                    solutionOptions += '<option value="' + data[i].id + '">' + data[i].title + '</option>';
                }
                $('#select-solution').html(solutionOptions);
            });
        }

    });

    if ($('#subject').length > 0) {
        $('.subject').click(function() {
            $('#choose-subject').hide();
            $('#subject').show();
        });

        $('#subject-field').blur(function() {
            if ($(this).val() != '') {
                $('#select-devision').val('');
                $('#select-devision').parent().find('span').html($('#select-devision').find('option:first').text());
                $('#select-service').parent().find('span').html($('#select-service').find('option:first').text());
                var serviceOptions = '<option value="' + $('#select-service').find('option:first').val() + '">' + $('#select-service').find('option:first').text() + '</option>';
                $('#select-service').html(serviceOptions);
                $('#select-solution').parent().find('span').html($('#select-solution').find('option:first').text());
                var solutionOptions = '<option value="' + $('#select-solution').find('option:first').val() + '">' + $('#select-solution').find('option:first').text() + '</option>';
                $('#select-solution').html(solutionOptions);
            }
        });

    }




    if ($('#btn-subscribe').length > 0) {
        
        var value = $('#form-email').val();

        $('#form-email').focus(function() {
           if ($(this).val() == value) {
               $(this).val('');
           }
        });

        $('#form-email').blur(function() {
           if ($(this).val() == '') {
               $(this).val(value);
           }
        });

        $('#btn-subscribe').click(function() {

            var data = $('#form-subscribe').serialize();
            var unsubscribe = 0;
            if ($('#form-unsubscribe').is(':checked')) {
                unsubscribe = 1;
            }
            
            $.post($('#form-subscribe').attr('action'), data, function(resp) {
                var object = $.parseJSON(resp);
                if (object.error) {
                    $('#subscribe-error').html(object.error).show();
                } else if (object.success) {
                    $('#subscribe-error').hide();
                    $('#form-subscribe').hide();
                    $('#subscribe-description').hide();
                    if (unsubscribe == 1) {
                        $('#unsubscribe-success').show();
                    } else {
                        $('#subscribe-success').show();
                    }
                }
            });
        });
    }

    var search = $('#searchForm').find ('input:text').val();
    $('#searchForm').find ('input:text').focus(function() {
        if ($(this).val() == search) {
            $(this).val('');
        }
    });

    $('#searchForm').find ('input:text').blur(function() {
        if ($(this).val() == '') {
            $(this).val(search);
        }
    });

    $('#language a').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    $('.load').click(function() {
        
        $(this).addClass('active');
        var load = $(this),
            parent = $(this).parent();
            start = parent.find('.item').length,
            action = $(this).find('input').val(),
            span = $(this).find('span'),
            msg = span.html(),
            html = ''
            div = parent.attr('id');

        span.html('&nbsp;');

        $.ajax({
             url: action,
             data: {
                 'start': start
             },
             type: 'get',
             dataType: 'json',
             cache: false,
             success: function(response) {
                 $.each(response.items,function(i, item) {
                    var id = div + '-' + item.id;
                    html += '<div class="hr"></div><div id="' + id + '" class="item"><span class="dateblock">' + item.date + '</span><h3 class="ptop"><a href="' + item.url + '">' + item.title + '</a></h3></div>';
                });
                $('<div></div>')
                .html(html)
                .appendTo($(parent).find('.items'));
                //$('html, body').animate({scrollTop: $('#news-42').offset().top}, 'slow');
                if (!response.more) {
                    load.hide();
                    $('<div></div>')
                    .addClass('hr')
                    .appendTo($(parent).find('.items'));
                }
             },
             error: function() {
             },
             complete: function() {
                 load.removeClass('active');
                 span.html(msg);
             }
         });

    });
    
    
    if ($('#carousel').length > 0) {
        var interval = setInterval('nextSlide()', 4000);
        $('#bar li a').hover(function() {
            clearInterval(interval);
            var id = $(this).parent().attr('id').replace('div-', '');
            if (id != active) {
                $('#bar').removeClass();
                $('#bar').addClass('state-' + id);
                $('#bar li').removeClass('active');
                $(this).parent().addClass('active');
                $('#carousel #img-' + active).fadeOut('slow');
                $('#carousel #img-' + id).fadeIn('slow');
                active = parseInt(id);
            }            
        });
    }

});

function nextSlide() {
    var next = active + 1;
    if (next == 5) {
        next = 1;
    }
    $('#bar').removeClass();
    $('#bar').addClass('state-' + next);
    $('#bar li').removeClass('active');
    $('#bar li#div-' + next).addClass('active');
    $('#carousel #img-' + active).fadeOut('slow');
    $('#carousel #img-' + next).fadeIn('slow');    
    active = next;
}
