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

$(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.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;
                for (var i = 0; i < len; i++) {
                    serviceOptions += '<option value="' + data[i].id + '">' + data[i].title + '</option>';
                }
                $('#select-service').html(serviceOptions);
            });
        }

    });

    $('#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 (id && 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 = {};
            data['email'] = $('#form-email').val();
            var unsubscribe = 0;
            if ($('#form-unsubscribe').is(':checked')) {
                unsubscribe = 1;
            }
            data['unsubscribe'] = unsubscribe;
            
            $.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();
                    }
                }
            });
        });
    }

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

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



});