
/***************************************************************/
    /* time()                                                      */
    /* Return current UNIX timestamp                               */
    /* results 1: timeStamp > 1000000000 && timeStamp < 2000000000 */
    /***************************************************************/

    function time(){
        return Math.floor(new Date().getTime()/1000);
    }

    var cClass;
    var NAVIGATION_CLOSE = {'left':'-190'};
    var NAVIGATION_OPEN = {'left':'0'};
    var LINE_CLOSE = {'width':'57'};

    var hashPattern = /.*#{1}(.*)/;


    var currentSlidePage = 0;

    var busy = false;


    function introCallback(){
        $('#intro').fadeOut(2000,function(){
            $(this).remove();
            //$('#intro').fadeOut(500,function(){

            //});
        });

    }

    var ready = true;
    var switchingContent = false;

    /*************/
    /* resetHome */
    /*************/

    function resetHome(){

        bindHoverHome();

        $("#home_menu li").each(function(i,obj){

            if($(obj).width() > 57 ){
                $("#home_item_description").fadeOut(500,function(){
                    var content = $("#home_content_buffer li#home_item_0").html();
                    $(obj).animate(LINE_CLOSE,500,function(){
                        $(obj).removeClass('selected');
                        $(obj).removeClass('positionOk');
                        $(obj).children('img.circle_arrow').hide();
                        $("#home_item_description").css('top','45px').html(content).fadeIn(500);
                    });
                });
            }
        });
    }

    /***************/
    /* windowClose */
    /***************/

    function windowClose(){
        var x,y;

        if($("#company_wrapper").length > 0 || $("#team_wrapper").length > 0){
            x = time();
            $("#main_navigation").animate({'left':'-190'},1000);
            $("#main_content").addClass('main_content_buffer').attr('id',x);
            $("#" + x).animate({'left':'-950'},1000,function(){
                $(this).remove();
                resetHome();
            });
        }else if($("#solutions_wrapper").length > 0){
            y = time();
            $("#main_content").addClass('main_content_buffer').attr('id',y);
            $("#" + y).fadeOut(2000,function(){
                $(this).remove();
                resetHome();
            });
        }else{
            resetHome();
        }

    }

    /*****************************/
    /* setCompanyItemDescription */
    /*****************************/

    function setCompanyItemDescription(hash,postData){
        $.get(hash,postData,function(data){

            if($("#main_content_buffer_left").length < 1){
                $("#main_wrapper").append('<div id="main_content_buffer_left"></div>');
            }

            $("#main_content_buffer_left").html(data);

            if($("#main_content_buffer_left #form_content").length > 0){
                $("#main_content_buffer_left #company_logo").addClass('general_form');
            }else{
                $("#main_content_buffer_left #company_logo").removeClass();
            }

            data = $("#main_content_buffer_left").html();

            $("#main_content_buffer_left").empty();

            $("#company_item_description").hide().html(data);

            if($("#form_content").length > 0){
                $("#company_logo").addClass('general_form');
            }else{
                $("#company_logo").removeClass();
            }

            $("#company_item_description").show();
            cClass = "#company_item_description";
            bindVerticalScroll();

            $(".up").hide();


            $(".content_wrapper").each(function(){
                var content = $(".content",this);

                if( 0 < content.outerHeight() - $(this).height() ){
                    $(".down",this).show();
                }else{
                    $(".down",this).hide();
                }

            });
        },'html');
    }

    /***********************/
    /* bindClickNavigation */
    /***********************/

    function bindClickNavigation(){

        $("#open_up").click(function(){
            if($("#main_navigation").position().left < 0){
                $("#main_navigation").animate({'left':'0'},1000,function(){
                        $(this).effect('bounce',{'direction':'left','times':'1','distance':'7'},500);
                });                
            }else if ($("#main_navigation").position().left == 0){
                $("#main_navigation").animate({'left':'-190'},500);
            }
        });

        $("#main_navigation a").click(function(){

            if(switchingContent){
                return false;
            }

            $(this).attr('href').match(hashPattern);
            var hash = RegExp.$1;

            var rel = $(this).attr('rel');
            var idParent = $(this).parents('ul').attr('id');
            var title = $(this).html();

            $("#main_content div").removeClass('current');

            $(rel).addClass('current');

            switchingContent = true;

            switch(idParent){
                case 'menu_company':                    

                    if(rel == 'resource_43'){
                        setTeamContent(hash,{'title':title});
                    }else if($("#main_navigation").position().left < 500 || $("#team_wrapper").length > 0){
                        setCompanyContent(hash,{'title':title,'wt':'true'});
                    }else if($("#main_navigation").position().left > 500){
                        setCompanyItemDescription(hash,{'title':title});
                    }
                    break;

                case 'menu_solutions':
                    if($("#solutions_" + rel).length > 0){
                        windowClose();
                    }else{
                        setSolutionsContent(hash,{'color':rel,'title':title});
                    }
                    break;
            }
            switchingContent = false;
        });

        $("#main_navigation a.close").unbind('click').click(function(){
            windowClose();
        });
    }

    /******************/
    /* setTeamContent */
    /******************/

    function setTeamContent(hash,postData){

        if($("#company_wrapper").length > 0){
           $("#company_wrapper").remove();
        }

        $("#main_footer").css('z-index','80');


        $("#main_navigation").animate({'left':'735'},800);

        $.get(hash,postData,function(data){

            if($("#main_wrapper #main_content_buffer_left").length < 1){
                $("#main_wrapper").append('<div id="main_content_buffer_left"></div>');
            }
            $("#main_content_buffer_left").html(data);
            $("#main_content").animate({'left':'950'},1000,function(){
                $(this).remove();
            });

            bindClickTeam();

            $("#main_content_buffer_left").animate({'right':'0'},1000,function(){
            $("#main_content").remove();
            $("#main_content_buffer_left").attr("id",'main_content');
            $("#main_wrapper").append('<div id="main_content_buffer_left"></div>');
            //bindVerticalScroll();

            $('#team_thumb img').hover(
                function(){ $.cursorMessage($(this).attr('alt') + ',<br />' +$(this).attr('longdesc'));}
                ,function(){$.hideCursorMessage;}
                );

                $('#team_thumb').hover(
                function(){}
                ,function(){ $("#cursorMessageDiv").hide(); $.hideCursorMessage;}
                );
            });


            $(".up").hide();


            $(".content_wrapper").each(function(){
                var content = $(".content",$(this));

                if( 0 < content.outerHeight() - $(this).height() ){
                    $(".down",this).show();
                }else{
                    $(".down",this).hide();
                }

            });

        },'html');
    }

    /*****************/
    /* bindClickTeam */
    /*****************/

    function bindClickTeam(){
        $("#team_item_description").html($("#team_content_buffer li:first-child").html());
        $("#team_footer h1").html($("#team_thumb img:first-child").attr('alt'));
        $("#team_item_photo").html('<img src="' + $("#team_thumb img:first-child").attr('src') + '" width="180" height="230" />');

        cClass = "#team_item_description";
        bindVerticalScroll();

        $("#team_thumb img").click(function(){
            var id = $(this).attr('id').split('_').pop();
            $("#team_item_photo img").attr('src',$(this).attr('src'));
            $("#team_item_description").html($("#team_item_" + id).html());
            $("#team_footer h1").html($(this).attr('alt'));
            cClass = "#team_item_description";
            bindVerticalScroll();

            $(".up").hide();


            $(".content_wrapper").each(function(){
                var content = $(".content",$(this));

                if( 0 < content.outerHeight() - $(this).height() ){
                    $(".down",this).show();
                }else{
                    $(".down",this).hide();
                }

            });

        });
    }

    /**********************/
    /* bindVerticalScroll */
    /**********************/

    function bindVerticalScroll(){
        $(cClass + " .up").hide();
        $(cClass + " .content_wrapper").each(function(){
            var content = $(cClass + " .content",$(this));

            if( 0 < content.outerHeight() - $(this).height() ){
                $(cClass + " .down",this).show();
            }else{
                $(cClass + " .down",this).hide();
            }
        });

        $(cClass + " .down").hover(function(){
            ref = $(this);
            inter = setInterval(scrollDown, 60);

        },function(){
            clearInterval(inter);
        });

        $(cClass + " .up").hover(function(){
            ref = $(this);
            inter = setInterval(scrollUp, 60);

        },function(){
            clearInterval(inter);
        });

         if($("#contact_us_form").length > 0){

            $("#contact_us_form div.submit").click(function(){
                $("#contact_us_form").trigger('submit');
            });

            $('#contact_us_form').ajaxForm({
                dataType:  'html',
                beforeSubmit:  validateForm,
                success: processFormData
            });
        }

        if($("#newsletter_form").length > 0){

            $("#newsletter_form div.submit").click(function(){
                $("#newsletter_form").trigger('submit');
            });

            $('#newsletter_form').ajaxForm({
                dataType:  'html',
                beforeSubmit:  validateForm,
                success: processFormData
            });
        }

        if($("#employment_form").length > 0){

            $("#employment_form div.submit").click(function(){
                $("#employment_form").trigger('submit');
            });

            $('#employment_form').ajaxForm({
                dataType:  'html',
                beforeSubmit:  validateForm,
                success: processFormData
            });
        }

    }


    /*******************************/
    /* Ajax form Success Functions */
    /*******************************/

    function processFormData(data){
         $("#company_item_description").html(data);
    }

    function processFormDataSolutions(data){
         $("#contact_us_form").remove();
         $("#form_succes").html(data);
    }

    /*********************/
    /* setCompanyContent */
    /*********************/

    function setCompanyContent(hash,postData){

        $("#main_footer").css('z-index','1');

        if($("#main_content_buffer_left").length < 1){
            $("#main_wrapper").append('<div id="main_content_buffer_left"></div>');
        }

        $.get(hash,postData,function(data){

            $("#main_navigation").animate({'left':'735'},800);

            if($("#solutions_wrapper").length > 0){
                $("#main_content").addClass('main_content_buffer').attr('id','x');
                $("#x").fadeOut(1000,function(){
                    $(this).remove();
                });
            }

            if($("#company_wrapper").length > 0){
                $("#main_content").addClass('main_content_buffer').attr('id','y');
                $("#y").fadeOut(1000,function(){
                    $(this).remove();
                });
            }

            $("#main_content_buffer_left").html(data);

            if($("#main_content_buffer_left #form_content").length > 0){
                $("#main_content_buffer_left #company_logo").addClass('general_form');
            }else{
                $("#main_content_buffer_left #company_logo").removeClass();
            }

            $("#main_content_buffer_left").animate({'right':'0'},1000,function(){
                $("#main_content").remove();
                $("#main_content_buffer_left").attr("id",'main_content');
                $("#main_content_buffer_left").remove();
                $("#main_wrapper").append('<div id="main_content_buffer_left"></div>');
                cClass = "#company_content";
                bindVerticalScroll();
                        $(".up").hide();


                $(".content_wrapper").each(function(){
                    var content = $(".content",$(this));

                    if( 0 < content.outerHeight() - $(this).height() ){
                        $(".down",this).show();
                    }else{
                        $(".down",this).hide();
                    }

                });

            });

            $("#company_wrapper a.privacy_policy").click(function(){
                  $("#main_footer a.privacy_policy").trigger('click');
            });

        },'html');

    }

    /***********************/
    /* setSolutionsContent */
    /***********************/

    function setSolutionsContent(hash,postData){
        $("#main_footer").css('z-index','8');
        $("#main_navigation").animate({'left':'-190'},500);

        $.get(hash,postData,function(data){

            if($("#main_content #company_wrapper").length > 0){
                $("#main_content").addClass('main_content_buffer_left').attr('id','y');
                $("#y").animate({'right':'950'},1000,function(){
                    $(this).remove();
                });
            }

            if($("#main_content #solutions_wrapper").length > 0){
                $("#main_content").addClass('main_content_buffer').attr('id','x');
                $("#x").fadeOut(1000,function(){
                    $(this).remove();

                });
            }

            $("#main_content_buffer_right").html(data).animate({'left':'0'},1000,function(){
                $("#main_content").remove();
                $("#main_content_buffer_right").attr("id",'main_content');
                $("#main_content_buffer_right").remove();
                $("#main_wrapper").append('<div id="main_content_buffer_right"></div>');
                bindClickSolutionsScroll();
            });

        },'html');
    }

    /****************************/
    /* bindClickSolutionsScroll */
    /****************************/

    function bindClickSolutionsScroll(){

        currentSlidePage = 0;

        $(".up").hide();


        $(".content_wrapper").each(function(){
            var content = $(".content",$(this));

            if( 0 < content.outerHeight() - $(this).height() ){
                $(".down",this).show();
            }else{
                $(".down",this).hide();
            }

        });

        if($("#solutions_content .page").length > 1){
            /* hide prev */
            $('#scroll_right').show();
        }


        $("#solutions_content .page").each(function(i,obj){
            $(this).attr('id','slide_' + i);
            if(i != 0){
                $(this).children('.page_wrapper').fadeTo(100,0.5);
            }
        });

        busy = false;


        var w = 800;
        var timeSlideExchange = 2000;

        $("#scroll_left").click(function(){

            if(busy){return false;}

            l = parseInt($("#solutions_content").position().left);



            if(Math.abs(l)%w == 0 || l == 0){

                busy = true;

                $("#solutions_content").animate({'left':l + w},timeSlideExchange,function(){
                    if($("#solutions_content").position().left == 0){
                        /* hide prev*/
                        $('#scroll_left').hide();
                    }
                    if($("#solutions_content").width() > w){
                        $('#scroll_right').show();
                    }

                    $("#slide_" + Math.abs(parseInt($("#solutions_content").position().left))/w + " .page_wrapper").fadeOut(300);

                    $("#slide_" + currentSlidePage + " .page_wrapper").fadeTo(300,0.5,function(){
                        currentSlidePage = Math.abs(parseInt($("#solutions_content").position().left))/w;
                        busy = false;
                    });

                    //alert(Math.abs(parseInt($("#solutions_content").position().left))/w);

                });
            }
        });

        $("#scroll_right").click(function(){

            if(busy){return false;}

            l = parseInt($("#solutions_content").position().left);

            if(Math.abs(l)%w == 0 || l == 0){

                busy = true;

                $("#solutions_content").animate({'left':l - w},timeSlideExchange,function(){
                    if($("#solutions_content").position().left + $("#solutions_content").width() == w){
                        $('#scroll_right').hide();
                    }
                    if($("#solutions_content").position().left < 0){
                        $('#scroll_left').show();
                    }

                    $("#slide_" + Math.abs(parseInt($("#solutions_content").position().left))/w + " .page_wrapper").fadeOut(300);

                    $("#slide_" + currentSlidePage + " .page_wrapper").fadeTo(300,0.5,function(){
                        currentSlidePage = Math.abs(parseInt($("#solutions_content").position().left))/w;
                        busy = false;
                    });

                    //alert(Math.abs(parseInt($("#solutions_content").position().left))/w);
                });
            }
        });

        $(".up").hide();


        $(".content_wrapper").each(function(){
            var content = $(".content",$(this));

            if( 0 < content.outerHeight() - $(this).height() ){
                $(".down",this).show();
            }else{
                $(".down",this).hide();
            }

        });

        $("#solutions_wrapper .down").hover(function(){
            ref = $(this);
            inter = setInterval(scrollDown, 30);

        },function(){
            clearInterval(inter);
        });

        $("#solutions_wrapper .up").hover(function(){
            ref = $(this);
            inter = setInterval(scrollUp, 30);

        },function(){
            clearInterval(inter);
        });

        $("#solutions_wrapper div.close a.close").click(function(){
            windowClose();
        });

        $(".beta.solutions_form li").click(function(){
            var id = $(this).attr('id');
            id = id.split('_');
            $("#menu_solutions a[rel='item_"+id[2]+"']").trigger('click');
        });

        if($("#contact_us_form").length > 0){

            $("#contact_us_form div.submit").click(function(){
                $("#contact_us_form").trigger('submit');
            });

            $('#contact_us_form').ajaxForm({
                dataType:  'html',
                beforeSubmit:  validateForm,
                success: processFormDataSolutions
            });
        }

        $("#solutions_wrapper a.privacy_policy").click(function(){
              $("#main_footer a.privacy_policy").trigger('click');
        });

        $("#solutions_wrapper a.contact_form").click(function(){
              $("#main_footer a.contact_form").trigger('click');
        });
    }

    var inter;
    var ref;

    function scrollDown(){
        var content = $(ref).prev();
        var upRef = $(".up",ref.parent('.content_wrapper'));

        if(Math.abs(content.position().top) < content.height() - $('.content_wrapper').height() + 80){
            upRef.show();
            content.animate({'top':'-=5'},10);
        }else{
            ref.hide();
        }
    }

    function scrollUp(){
        var content = $(ref).next();
        var downRef = $(".down",ref.parent('.content_wrapper'));

        if(content.position().top < 0){
            downRef.show();
            content.animate({'top':'+=5'},10);
        }else{
            ref.hide();
        }
    }

    function bindHoverHome(){

        $("#home_menu li img.circle_arrow").hide();

        $("#home_menu li").unbind('mouseenter').unbind('mouseleave').hover(
            function(){
                $(this).addClass('selected');
                $('img.circle_arrow',this).show();
                $('span.title',this).show();

                $('img.circle_over',this).hide();
            },
            function(){
                $(this).removeClass('selected');
                $('img.circle_arrow',this).hide();
                $('span.title',this).hide();
        });

        if(!homeOverInterval){
            homeOverInterval = setInterval(blinkHome, 600);
        }

    }

    var homeOverInterval;
    var isOver = false;
    var itemPositionOk = false;

    function blinkHome(){

        if(!isOver){
            $("#home_menu li").each(function(){
               if(!$(this).hasClass('selected')){
                   $(this).children('img.circle_over').css('background-color','#fff').fadeIn(400);
               }else if($(this).hasClass('positionOk')){
                   $('img.circle_arrow',this).hide();
               }
            });
           isOver = true;

        }else{
            $("#home_menu li").each(function(){
               if(!$(this).hasClass('selected')){
                   $(this).children('img.circle_over').css('background-color','#fff').fadeOut(600);
               }else if($(this).hasClass('positionOk')){
                   $('img.circle_arrow',this).show();
               }
            });
            isOver = false;
        }


    }


    /*****************/
    /* bindClickHome */
    /*****************/

    function bindClickHome(){


        $("#home_menu li").each(function(i,obj){
            $(this).append('<img class="circle_over" src="includes/css/images/circle_over.png" /><span class="line"></span>');
        });

        $("#home_item_description").fadeOut(500,function(){
            var content = $("#home_content_buffer li#home_item_0").html();
            $("#home_item_description").css('top','45px').html(content).fadeIn(500);
        });


        bindHoverHome();



        $("#home_menu li div,#home_menu li img.circle_arrow").click(function(){
            $("#main_navigation").animate({'left':'-190'},500);

            var t = $(this).parents('li');
            var w = 820 - t.position().left;

            if(t.width() == 57 && ready){
                ready = false;

                $("#home_menu li").each(function(){
                    $(this).removeClass('selected');
                    $(this).removeClass('positionOk');
                    $(this).animate({'width':'57'},600,function(){
                        $("#home_item_description").fadeOut(500);
                    });
                });

                bindHoverHome();

                    t.addClass('selected');
                    t.unbind('mouseenter').unbind('mouseleave');
                    t.children('img.circle_arrow').show();
                    t.children('img.circle_over').hide();
                    t.children('span.title').hide();

                t.animate({'width':'+='+w},1000,function(){

                    t.addClass('positionOk');

                    var title,content;

                    $("#menu_solutions a").each(function(){
                        if($(this).attr('rel') == t.attr('id')){
                            title = $(this).html();
                        }
                    });

                    $("#home_content_buffer li").each(function(){
                        if($(this).attr('id') == 'home_' + t.attr('id')){
                            content = $(this).html();
                        }
                    });

                    var color = t.children('.line').css('background-color');
                    var height = t.position().top;

                    $("#home_item_description").css('top', height);
                        $("#home_item_description").html(content);
                        $("#home_item_description .title").html(title);
                        $("#home_item_description .title").css('color', color);
                        $("#home_item_description").fadeIn(500,function(){
                        ready = true;
                    });
                });
            }else if(ready){
                ready = false;
                var hash;

                $("#menu_solutions a").each(function(){

                    if($(this).attr('rel') == t.attr('id')){
                        $(this).attr('href').match(hashPattern);
                        hash = RegExp.$1;
                        setSolutionsContent(hash,{'color':$(this).attr('rel'),'title':$(this).html()});
                        ready = true;
                    }
                });
            }
        });

        $("#main_footer a.privacy_policy, a.contact_form").unbind('click').click(function(){
            $(this).attr('href').match(hashPattern);
            var hash = RegExp.$1;

            var title = $(this).html();
            if($("#solutions_wrapper").length > 0){
                setCompanyContent(hash,{'title':title,'wt':'true'});
            }else if($("#main_navigation").position().left < 500 || $("#team_wrapper").length > 0){ //735
                setCompanyContent(hash,{'title':title,'wt':'true'});
            }else if($("#main_navigation").position().left > 500){
                setCompanyItemDescription(hash,{'title':title});
            }
        });

        $("#main_header a.close").unbind('click').click(function(){
            $("#main_navigation a.close").trigger('click');
        });
    }
    
    /*******************************************************************************/
    /* BG, BC 8/6/2010 */
    /* form validation functions */
    /*******************************************************************************/
    function displayErrorMessage(text){
        $("#error_message").html("<ul>"+text+"</ul>").show("slow");
    }
        
    function isEmpty(text){
        if(text=='')
            return true;
        else
            return false;
    }
        
    function isEmail(text){
        var emailFilter = /^.+@.+\..{2,}$/;
        var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
            
        if(!(emailFilter.test(text)) || text.match(illegalChars))
            return false;
        else
            return true;
    }
    
    function isPhone(text) {
        var stripped = text.replace(/[\(\)\.\-\ ]/g, '');    

        if (text == '') {
            return false;
        } else if (isNaN(parseInt(stripped))) {
            return false;
        } else if (stripped.length < 10) {
            return false
        }
        return true;
    }
