
$(document).ready(function() {
    //alert("ready");

    $(".header .navigation li.dd").hover(function() {
        $(this).addClass("active");
    }, function() {
        $(this).removeClass("active");
        Cufon.refresh();
    });

    //$('.splash .image .sections ').delay(2000).show('1000');

    $('.splash .image .sections .more').click(function() {

        $(".splash .image .sections .item").removeClass("item_selected");
        $(".splash .image .sections .mask").show();

        $(this).parent().parent().addClass("item_selected");
        $(".splash .image .sections .item_selected .mask").hide();

        $('.splash .proposition').hide();

        /*$('.splash .popup_' + $(this).attr("title")).show();
        $('.splash .popup_' + $(this).attr("title") + " .product_1").show();

        $('.splash .popup .nav li:first').addClass("selected");*/
    });


    $('.splash .image .popup .close ').click(function() {
        $('.splash .products').hide();
        $('.splash .popup').hide();
        $('.splash .popup .nav li').removeClass("selected");


        $(".splash .image .sections .mask").hide();
        $(".splash .image .sections .item").removeClass("item_selected");
        $('.splash .proposition').show();
    });


    /*
    $('.splash .image .popup_medical .nav a').click(function() {
    $('.splash .popup_medical .nav li').removeClass("selected");
    $(this).parent().addClass("selected");

        $('.splash .popup_medical .products').hide();
    $('.splash .popup_medical .product_' + $(this).attr("title")).show();
    });

    $('.splash .image .popup_consumer .nav a').click(function() {
    $('.splash .popup_consumer .nav li').removeClass("selected");
    $(this).parent().addClass("selected");

        $('.splash .popup_consumer .products').hide();
    $('.splash .popup_consumer .product_' + $(this).attr("title")).show();
    });

    $('.splash .image .popup_industrial .nav a').click(function() {
    $('.splash .popup_industrial .nav li').removeClass("selected");
    $(this).parent().addClass("selected");

        $('.splash .popup_industrial .products').hide();
    $('.splash .popup_industrial .product_' + $(this).attr("title")).show();
    });
    */

});


function LoadSectorProducts(classname) {

    /*$('.splash .popup_' + $(this).attr("title")).show();
    $('.splash .popup_' + $(this).attr("title") + " .product_1").show();

        $('.splash .popup .nav li:first').addClass("selected");*/
    $('.splash .popup').hide();
    $('.splash .' + classname).show();
    $('.splash .' + classname + " .products").hide();
    $('.splash .' + classname + " .product_1").show();
    $('.splash .' + classname + ' .nav li').removeClass("selected");
    $('.splash .' + classname + ' .nav li:first').addClass("selected");

    return false;
}

function SelectSectorProduct(obj, productpanel, product) {

    /*
    $('.splash .popup_industrial .nav li').removeClass("selected");
    $(this).parent().addClass("selected");

    $('.splash .popup_industrial .products').hide();
    $('.splash .popup_industrial .product_' + $(this).attr("title")).show();
    */

    $('.splash .' + productpanel + ' .nav li').removeClass("selected");
    $('.splash .' + productpanel + ' .products').hide();
    $(obj).parent().addClass("selected");
    $('.splash .' + productpanel + ' .' + product).show();

}

var hottopic = {
    baseurl: '/layouts/v2_Sagentia/ajax.asmx/',
    step1: function() {

        $('#hot-topic-panel #comments-step2').hide();
        $('#hot-topic-panel .comment_add').show();

    },
    step2: function() {
        $('.error').hide();

        if ($('#hot-topic-panel .comment-add-name').val() == "")
            $('#comment-add-name-error').show();
        else if ($('#hot-topic-panel .comment-add-comments').val() == "")
            $('#comment-add-comments-error').show();
        else {
            $.post(this.baseurl + 'ValidateNameAndComment', { name: $('#hot-topic-panel .comment-add-name').val(), comments: $('#hot-topic-panel .comment-add-comments').val() }, function(data) {

                if (data.success) {

                    $('#comment-preview-name').html(data.name + " <strong>" + data.date + "</strong>");
                    $('#comment-preview-comments').html(data.comments);

                    $('#hot-topic-panel .comment_add').hide();
                    $('#hot-topic-panel #comments-step2').show();
                    $('.comment-add-company').focus();


                } hottopic.parseerror(data.error, data.errormsg);
            },
        "json");
        }

    },
    parseerror: function(error, errormsg) {

        switch (error) {
            case "name-comments":
                $('#comment-add-name-error').show();
                $('#comment-add-comments-error').show();
                hottopic.step1();
                break;
            case "email":
                $('#comment-add-email-error').show();
                break;
            case "name":
                $('#comment-add-name-error').show();
                hottopic.step1();
                break;
            case "comments":
                $('#comment-add-comments-error').show();
                hottopic.step1();
                break;
        }

        if (errormsg)
            alert(errormsg);

    },
    postcomment: function(topicid) {

        $('.error').hide();

        if ($('.comment-add-email').val() == "") {
            $('#comment-add-email-error').show();
        } else if (!$('.comment-add-terms').is(":checked")) {
            alert("You cannot post comments unless you agree to our Terms & Conditions.");
        } else {

            $.post(this.baseurl + 'PostComment',
            { topicid: topicid,
                name: $('#hot-topic-panel .comment-add-name').val(),
                comments: $('#hot-topic-panel .comment-add-comments').val(),
                company: $('.comment-add-company').val(),
                email: $('.comment-add-email').val(),
                rememberme: $('.comment-add-rem').is(":checked"),
                receivenotifications: $('.comment-add-notify').is(":checked"),
                signuptocommunications: $('.comment-add-comms').is(":checked")
            }
            , function(data) {
                if (data.success) {
                    $('#hot-topic-panel #comments-step2').hide();
                    $('#hot-topic-panel .comment_add').hide();
                    $('#hot-topic-panel #comment-posted').show();

                    $('#hot-topic-panel .comment_results').html(data.html);
                    $('#hot-topic-panel .comments .icon').html(data.numcomments);


                } else hottopic.parseerror(data.error, data.errormsg);

            },
            "json");
        }

    },
    loadcomments: function(topicid, startat) {
        $.post(this.baseurl + 'LoadComments',
        { topicid: topicid, startat: startat }
        , function(data) {
            if (data.success) {

                $('.comment_results').html(data.html);

            } else alert(data.error);
        },
        "json");
    }


}




var whitepapers = {
    complete: false,
    baseurl: '/layouts/v2_Sagentia/ajax.asmx/',
    parseerror: function(error, errormsg) {

        if (error) {
            for (var i = 0; i < error.length; i++) {
                $('#wp-' + error[i] + '-error').show();
            }
        }

        if (errormsg) alert(errormsg);

    },
    showregistration: function(mediaid) {
        if (this.complete)
            return true;
        else {
            $('.wp-registration-form').show();
            $('.wp-name').focus();
            $.scrollTo('.wp-name', 500);
            $('.wp-download-value').val(mediaid);

            return false;
        }

    },
    register: function() {

        $('.error').hide();

        if ($('.wp-jobtitle').val() == "") $('#wp-jobtitle-error').show();
        if ($('.wp-name').val() == "") $('#wp-name-error').show();
        if ($('.wp-email').val() == "") $('#wp-email-error').show();
        if ($('.wp-company').val() == "") $('#wp-company-error').show();


        if ($('.error:visible').length == 0) {

            if (!$('.wp-terms').is(":checked")) {
                alert("You cannot continue unless you agree to our Terms & Conditions.");
            }
            else {

                $.post(this.baseurl + 'WhitePaperRegistration',
                {
                    name: $('.wp-name').val(),
                    jobtitle: $('.wp-jobtitle').val(),
                    company: $('.wp-company').val(),
                    email: $('.wp-email').val(),
                    rememberme: $('.wp-rememberme').is(":checked")

                }, function(data) {

                    if (data.success) {
                        $('.wp-registration-form').hide();
                        document.location = document.location.href.substring(0, document.location.href.indexOf('.aspx')) + '.aspx?rdownload=' + $('.wp-download-value').val();
                        whitepapers.complete = true;
                    }
                    else
                        whitepapers.parseerror(data.errcodes, data.errormsg);
                },
                "json");

            }
        }




    }
}

var vacancies = {
    baseurl: '/layouts/v2_Sagentia/ajax.asmx/',
    show: function(sec) {
        //        $('.vacancies-panel').slideUp();
        //        $('.vacancies-panel').slideDown();

        $('.vacancies-panel .filters a').removeClass('selected');

        if (sec == 'ALL') {
            $('.vacancies-panel .item').show();
            $('.vacancies-panel .filters a').removeClass('selected');
        }
        else {
            $('.vacancies-panel .item').hide();
            $('.vacancies-panel .item.' + sec).show();
        }

        $('.vacancies-panel .filters a.' + sec).addClass('selected');

    },
    checkext: function(filename, ext) {

        var fileext = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : null;
        if (fileext) {
            var arr = ext.split(';');
            for (var i = 0; i < arr.length; i++) {
                if (fileext[0].toLowerCase() == arr[i].toLowerCase())
                    return true;
            }
        }
        return false;
    },
    apply: function(ext) {

        $('.error').hide();


        if ($('.apply-name').val().length == 0)
            $('.apply-name-error').show();

        if ($('.apply-phone').val().length == 0)
            $('.apply-phone-error').show();

        //        if ($('.apply-cl').val().length == 0)
        //            $('.apply-cl-error').show();
        //        else if (!this.checkext($('.apply-cl').val(), ext))
        //            $('.apply-cl-ext-error').show();

        if ($('.apply-cv').val().length == 0)
            $('.apply-cv-error').show();
        else if (!this.checkext($('.apply-cv').val(), ext))
            $('.apply-cv-ext-error').show();

        if ($('.apply-hear').val().length == 0)
            $('.apply-hear-error').show();

        if (!$('.apply-terms input').is(":checked"))
            $('.apply-terms-error').show();

        if ($('.apply-email').val().length == 0)
            $('.apply-email-error').show();
        else {
            try {
                if ($('.error:visible').length == 0) {

                    jQuery.ajax({
                        url: this.baseurl + 'EmailValidation',
                        type: "POST",
                        dataType: "json",
                        data: { email: $('.apply-email').val() },
                        success: function(result) {
                            if (!result.success)
                                $('.apply-email-error').show();
                        },
                        error: function(err) {

                        },
                        async: false
                    });

                }
            } catch (err) { }
        }

        return $('.error:visible').length == 0;
    }

}


function limitText(event, limitField, limitCount, limitNum) {
    if ($('#' + limitField).val().length > limitNum) {
        $('#' + limitField).val($('#' + limitField).val().substring(0, limitNum));
    }

    $('#' + limitCount).html(limitNum - $('#' + limitField).val().length);
}

var Continuum = {
    height: 0,
    init: function() {
        var eq = 0,
            show = 0,
            animating = false,
            intro = true;
        this.evenHeights(".panel");
        this.evenHeights(".continuum .headings li a");
        this.height = $(".panel").height();
        $(".continuum .headings li a, .continuum .intro .column img").hover(function() {
            $(".continuum .headings li a").addClass("inactive");
            eq = $(this).parent().index();
            $(".continuum .headings li a:eq(" + eq + ")").removeClass("inactive");
        }, function() {
            $(".continuum .headings li a").removeClass("inactive");
        }).click(function() {
            show = $(this).parent().index();
            if (animating === false) {
                animating = true;
                function changeView() {
                    $(".continuum .shown").animate({
                        opacity: 0
                    }, 300, function() {
                        $(this).hide();
                        $(".continuum .panel:eq(" + show + ")").show().animate({
                            opacity: 1
                        }, 300, function() {
                            animating = false;
                        }).addClass("shown");
                    }).removeClass("shown");
                }
                if (intro === true) {
                    intro = false;
                    $(".continuum .intro").animate({
                        height: Continuum.height
                    }, 300);
                    changeView();
                } else { changeView(); }
            }
            return false;
        });

    },
    evenHeights: function(target) {
        /// <summary>Preloads images</summary>
        /// <param name="target" type="String">Elements to give equal height</param>
        var highest = 0;
        $(target).each(function() {
            if ($(this).height() > highest) {
                highest = $(this).height();
            }
        });
        $(target).each(function() {
            $(this).css("min-height", highest + "px");
        });
    }
}
$(document).ready(function() {
    var ourServices = $(".continuum").length;
    if (ourServices > 0) {
        Continuum.init();
    }
});
