(function($){
    function screen(){
        var wHei = $(document).height(),
        fHei = $('#footer').outerHeight(),
        cHei = wHei - fHei - 52;
    //$('#contenter').height(cHei);	//这个语句在ie下面会导致 contactus 的slideToggle卡死
    }
    //$(window).resize(screen);
    function contactus(){
        var $bt = $('#bt_contactus'),
        $bt2 = $('#bt_contactus2'),
        block = $('#contactus'),
        speed = 500;
        $bt.click(function(){
            block.slideToggle(speed,function(){
                $bt.hide();
                $bt2.show().css('top','0').animate({
                    top:'-22px'
                },200);
            });
        });
        $bt2.click(function(){
            block.slideToggle(speed,function(){
                $bt2.hide();
                $bt.show().css('top','-22px').animate({
                    top:'0'
                },200);
            });
        });
    }
    function nav() {
        $("#navcation").lavaLamp({
            fx: "backout",
            speed: 700,
            click: function(event, menuItem) {
            //return false;
            }
        });
    };
    /* slide show home */
    function slide_home(){
        var autoplay = true,
        speed = 800,
        autodelay = 4000,
        $slide_wrap = $('.slide_wrap'),
        $items = $('.slide_item'),
		$Navigation = $('.navigation'),
        lineheight = $items.height(),
        linewidth = $items.width();
		is_click = false;

        $slide_wrap.css({
            'width':linewidth*$items.length
        });
        /* left scroll */
        function autoscroll(){
			if(is_click)
			{
				return;
			}
            $slide_wrap.animate({
                'margin-left':'-'+linewidth+'px'
            },speed,function(){
                $slide_wrap.css({
                    'margin-left':'0'
                }).find('div.slide_item:first').appendTo($slide_wrap);

				id = $slide_wrap.find('div.slide_item:first').attr('id');
				//alert(id);
				$(".navigation").each(function(i){
					   if(this.id == 'Navigation+' + id)
					   {
						   this.src = "/wp-content/themes/fabriqate/static/images/index_nav_dot1.png";
						   }
					   else
					   {
						   this.src = "/wp-content/themes/fabriqate/static/images/index_nav_dot0.png";
						   }

					 });
                var autodelay = autodelay + speed;// 修复越来越快的问题 每隔5秒是包含了滚动的时间在内的
            });
        };
        /* up scroll
			function autoscroll(){
				$slide_wrap.animate({'margin-top':'-'+lineheight+'px'},speed,function(){
					$slide_wrap.css({'margin-top':'0'}).find('div.slide_item:first').appendTo($slide_wrap);
					var autodelay = autodelay + speed;// 修复越来越快的问题 每隔5秒是包含了滚动的时间在内的
				});
			};
			*/
        /* feed scroll */
        function feedscroll(){
            var speed2 = 300,
            autodelay2 = 3000,
            scrollwidth = 200+'px',
            $left = $('#feed_l'),
            $right = $('#feed_r'),
            $leftbar = $('#feed_line02'),
            $rightbar = $('#feed_line01');
            /* 自动滚动
					function feed(){
						$rightbar.animate({'margin-left':'0'},speed2).delay(autodelay2).animate({'margin-left':'-'+scrollwidth},speed2);
						$leftbar.delay(speed2).animate({'margin-left':'-'+scrollwidth},speed2).delay(autodelay2).animate({'margin-left':'0'},speed2);
					}
					setInterval(feed,autodelay2);
					*/
            $leftbar.hover(function(){
                $leftbar.animate({
                    'margin-left':'-'+scrollwidth
                },speed2);
            //$rightbar.delay(speed2).animate({'margin-left':'0'},speed2).delay(autodelay2).animate({'margin-left':'-'+scrollwidth},speed2);
            },function(){
                $leftbar.delay(speed2).animate({
                    'margin-left':'0'
                },speed2);
            });
            $rightbar.hover(function(){
                $rightbar.animate({
                    'margin-left':'0'
                },speed2);
            //$leftbar.delay(speed2).animate({'margin-left':'-'+scrollwidth},speed2).delay(autodelay2).animate({'margin-left':'0'},speed2);
            },function(){
                $rightbar.delay(speed2).animate({
                    'margin-left':'-'+scrollwidth
                },speed2)
            });


        };
        /* 修复 hover 一开始鼠标就停在上面的时候，clearInterval 失效*/
        $items.bind('mouseenter mouseleave', function() {
            clearInterval(autotime);
        });

        $items.hover(function(){
			//alert(autotime);
            clearInterval(autotime);
        },function(){
            autotime = setInterval(autoscroll,autodelay);
        });

		$Navigation.mouseover(function(){

				/*$items = $('.slide_item'),
        		linewidth = $items.width();
				*/
				//alert(autotime);
				is_click = true;
				clearInterval(autotime);
				 var na = this;
				 $len = linewidth;
				 var obj = Array();
				 is_find = false;
				 $('.slide_item').each(function(i){

					   if('Navigation+' + this.id == na.id)
					   {
						   $len = (i+1)*linewidth;
						   // alert('Navigation+' + this.id);
					 		//alert(na.id);
							//alert(i);
							is_find = true;


						}
						else
						{
							if(!is_find)
							obj[i] = this;
							}


					 });
				/*switch(this.id)
				{
					case 'Navigation+item00':
					 $len = 1*linewidth;
					 alert(1);
					break;
					case 'Navigation+item01':
					$len = 2*linewidth;
					alert(2);
					break;
					case 'Navigation+item02':
					$len = 3*linewidth;
					alert(3);
					break;
					case 'Navigation+item03':
					$len = 4*linewidth;
					alert(4);
					break;
					default:
					$len = linewidth;
				}*/
				   // alert($len);
				$slide_wrap = $('.slide_wrap');
				 $slide_wrap.animate({
                'margin-left':'-'+$len+'px'
            },"fast",function(){
                $slide_wrap.css({
                    'margin-left':'0'
                });
				for (key in obj)   {
					  $(obj[key]).appendTo($slide_wrap);//s += a[key] + "&ltBR>";
				   }

				   id = $slide_wrap.find('div.slide_item:first').attr('id');
				//alert(id);
				$(".navigation").each(function(i){
					   if(this.id == 'Navigation+' + id)
					   {
						   this.src = "/wp-content/themes/fabriqate/static/images/index_nav_dot1.png";
						   }
					   else
					   {
						   this.src = "/wp-content/themes/fabriqate/static/images/index_nav_dot0.png";
						   }

					 });


				});
				is_click = false;
				 autotime = setInterval(autoscroll,autodelay);
		});
        if( autoplay === true ){
            var autotime = setInterval(autoscroll,autodelay);
            feedscroll();
        }
    };


    /*  tab control */
    function tabcontrol(mid,cid){
        this.mid = mid;
        this.cid = cid;
        var $block = $(cid),
        $menu = $(mid),
        speed = 500,
        index = 0;

        $menu.find('a.bt_default').click(function(){
            var index = $(this).index();
            $(this).addClass('bt_active').siblings().removeClass('bt_active');
            $block.find('.tab_content').hide().eq(index).fadeIn(speed);
        //$block.eq(index).fadeIn(speed).siblings('.tab_content').hide();
			if($(this).text() == 'map')
			{
				showMap();
			}
        })

    }
    /* slide show left right scroll*/
    function oSlideshow(){
        /* option set*/
        var autoplay = true,
        speed = 500,
        autodelay = 3000,
        scroll = 1,
        index = 0;
        var	$content = $('.slide_show').find('.slide_content'),
        $items = $content.find('.slide_item'),
        $left = $('.slide_show').find('.slide_left'),
        $right = $('.slide_show').find('.slide_right'),
        scrollwidth = '648px';//$items.width()*scroll + 'px'
		time = 0;
		$click = false;
		$direction = 'left';

        var slideauto;
        /* 左边箭头点击触发 关闭自动播放*/
        $left.click(function(){
           /* if(slideauto){
                clearInterval(slideauto);
            }*/
			$click = true;
			d = new Date();
   			t = d.getTime();
			if(time != 0 && ((t-time) < 1000) )
			{
				return;
				}
				time = t;

			 //clearInterval(slideauto);
			 $direction = 'left';
            leftloop();
        }).mouseout(function(){
           // slideauto = setInterval(leftloop,autodelay);
		   	$click = false;
			$direction = 'left';
			setTimeout(leftloop,autodelay);

        });
        /* 右边边箭头点击触发 关闭自动播放*/
        $right.click(function(){
           /* if(slideauto){
                clearInterval(slideauto);
            }*/
			$click = true;
			d = new Date();
   			t = d.getTime();
			if(time != 0 && ((t-time) < 1000) )
			{
				return;
				}
				time = t;
			//clearInterval(slideauto);
			$direction = 'right';
            rightloop();
        }).mouseout(function(){
           // slideauto = setInterval(rightloop,autodelay);
		   	$click = false;
			$direction = 'right';
			setTimeout(rightloop,autodelay);
        });
        /* 第1次点击触发 自动播放*/
        //$('.tab_menu .bt_default').eq(1).one('click',function(){
        if(autoplay === true){
           // slideauto = setInterval(leftloop,autodelay);
			setTimeout(leftloop,autodelay);
        }
        //})
        /* 往左边循环播放 */
        function leftloop(){
			if($direction != 'left')
			{
				return;
			}
            $content.animate({
                'margin-left':'-'+scrollwidth
            },speed,function(){
                $content.css({
                    'margin-left':0
                }).find('div..slide_item:first').appendTo($content);
                var autodelay = autodelay + speed;
            });
			if(!$click && $direction == 'left')
			{
				setTimeout(leftloop,autodelay);
			}

        }
        /* 往右边循环播放 */
        function rightloop(){

			if($direction != 'right')
			{
				return;
			}
            $content.find('div.slide_item:last').prependTo($content).css({
                'margin-left':'-'+scrollwidth
            });
            $content.animate({
                'margin-left':scrollwidth
            },speed,function(){
                $content.css({
                    'margin-left':0
                });
                $content.find('div.slide_item:first').css({
                    'margin-left':0
                });
                var autodelay = autodelay + speed;
            });
			if(!$click && $direction == 'right')
			{
				setTimeout(rightloop,autodelay);
			}

        }
    };
    /* technology & service page */
    function technology(){
        var $menuBlock = $('div.tech_list'),
        $menu = $('div.tech_list li').find('a'),
        $menuLi = $('div.tech_list').find('li'),
        $block = $('div.tech_detail_wrap'),
        $back = $('#tech_back'),
        $item = $block.find('.tech_item'),
        scrollHeight = '500px',
        speed = 500,
        speed2 = 200,
        index = 0;

        $back.hide();
        $item.hide();
        $menu.click(function(){
            var index = $(this).parent().index();
            $menuBlock.animate({
                'margin-top':'-'+scrollHeight
            },speed,function(){
                $back.css({
                    'margin-left':'0px'
                }).fadeIn(1000);;
            })
            $block.find('.tech_item').hide().eq(index).show();
            $block.show().animate({
                'margin-top':'0'
            },speed);
			
			/*****2011-09-07*****/
			try {
				$('#phrase_panels').css('top','0px');
 
			  }
			  catch(e) {
			   
			  }
			
			/*********/

        });
        //返回所有
        $back.click(function(){
			
				/****2011-09-07******/
			try {
				$('#phrase_panels').css('top','-180px');
 
			  }
			  catch(e) {
			   
			  }
			
			/*********/
            $(this).fadeOut(500,function(){
            $menuBlock.animate({
                'margin-top':'0'
            },speed,function(){
                //$back.hide();
                $block.hide();
            });

        })});

		$('div.box.tech_menu').find('a').click(function(){

			 $id = 'tech_item'+ $(this).attr('id');
			 $visibleObj = $('div.box.tech_box').find('.tech_item:visible');
			 if($id != $visibleObj.attr('id'))
			 {

             	$visibleObj.hide();
				$('#'+$id).show();
				//$('div.box.tech_box').prepend($('#'+$id));

			 }

        });
    };
    /* work & people page */
    function work()
	{
        var $line = $('.work_detail_line'),
        $menu = $('.work_list').find('li'),
        $desc = $menu.find('.work_desc'),
        $detail = $('.work_detail'),
        $more = $menu.find('.bt_more2'),
        $pro_back = $('#pro_back'),
        speed = 200,
        speed2 = 500,
        scrollHeight = $('.work_list').height();
		
		$height_old = $('.workpage').height();

        $menu.hover(function(){
            $desc.hide();
            $(this).find('.work_desc').fadeIn(speed);
        },function(){
            $(this).find('.work_desc').fadeOut(speed);
            return false;
        });
        //项目详细描述  Maneeram: 'top':20px replace by 0px 
        $menu.click(function(e){
            var index2 = $(this).index();
            $detail.css({
                'top':scrollHeight
            }).show().animate({
                'top':'0px' 
            },speed2,function(){
                $pro_back.css({
                    'left':'-10px',
					'display':'block'
                }).fadeIn(1000);
            });
            $line.hide().eq(index2).show();
			/************2011-07-22***********/
			try {
				$('#workpage').height( $line.eq(index2).height()+120);
  
			  }
			  catch(e) {
			   
			  }

			
			
			/**********2011-07-22*************/
           // $line.find('.work_detail_menu a').removeClass('tab_active').eq(0).addClass('tab_active');
           // $line.eq(index2).find('.work_tab_content').hide().eq(0).show();
            e.stopPropagation();
        });
        //返回所有项目
        $pro_back.click(function(){
            $pro_back.fadeOut(500);
			(function (){
                $detail.animate({
                    'top':scrollHeight+scrollHeight
                },speed2+speed,function(){
                    $(this).hide();
					
					/*************2011-07-22**************/
					try {
				$('#workpage').height($height_old);
  
			  }
			  catch(e) {
			   
			  }
			  /*************2011-07-22**************/
					
                });
            })();

        });
        //项目详细描述的tab切换
        function tab(){
            var $block = $('.work_detail_content'),
            $menu2 = $('.work_detail_menu'),
            speed = 200,
            index = 0;
            $menu2.find('a').click(function(){
                var index = $(this).index();
                $block.find('.work_tab_content').hide();
                $(this).addClass('tab_active').siblings().removeClass('tab_active');
                $(this).parent().next().find('.work_tab_content').eq(index).fadeIn(speed);
            });
        }
        tab();

    }
    /* about page */
    function about(){
        var menu = $('.side_menu .about_m'),
        block = $('.about_list .about_block'),
        speed = 300,
        index = 0;
        num=null;
		//alert(location);
		
		/*

		if(location.href.indexOf('#aboutus')>-1)
		{
                num=0;
                }
                if(location.href.indexOf('#approach')>-1)
		{
                 num=2;
                }
                if(location.href.indexOf('#people')>-1)
		{
                 num=4;
                }
                if(location.href.indexOf('#awards')>-1)
		{
                 num=5;
                }
                 
                if(location.href.indexOf('#careers')>-1)
		{
                 num=6;
                }
                if(location.href.indexOf('#events')>-1)
		{
                 num=3;
                }
                if(location.href.indexOf('#people')>-1)
		{
                 num=4;
                }
                if(location.href.indexOf('#press')>-1)
		{
                 num=4;
                }
                if(location.href.indexOf('#expertises')>-1)
		{
                 num=1;
                }
                if(location.href.indexOf('#story')>-1)
		{
                 num=1;
                }
		if(num!=null)
		{
		menu.removeClass('about_act').eq(num).addClass('about_act');
        block.hide().eq(num).fadeIn(speed);
		}*/
		menu.click(function(){
            var index = menu.index(this);
            menu.removeClass('about_act').eq(index).addClass('about_act');
			if($(this).attr('href') == '#')
			{
				block.hide().eq(index).fadeIn(speed);
			}
			else
			{
				window.location.href = $(this).attr('href'); 
			}
            
        })
    }
    /* form focus*/
    function formfocus(){
        /* do it */

	//focusEle('input.inp_default');
       //focusEle('textarea');
        focusEle('input.inp_search');
		 /*$('#inp_default_name').focus(function(){
			 var text = $(this).text();
             $(this).val('');
			})*/
        function focusEle(e){
            this.e = e;
            $(e).focus(function(){
                var text = $(this).text();
                $(this).val('');
            });
        }
    }
    function fqv5ContactForm(){
        var options = {
            beforeSubmit:  showRequest,  // pre-submit callback
            success:       showResponse  // post-submit callback
        };
        // bind to the form's submit event
        $('#fqv5-contact-form').submit(function() {
            $('#fqv5-contact-form').ajaxSubmit(options);
            return false;
        });
        $('.bt_default').click(function(){
            $('#fqv5-contact-form').submit();
        });
		
		/************2011-07-08**************/
		$('#inp_default_name').focus(function(){
			$name = $(this).val();
            if( $name == 'Name')
			{
				$(this).val('');
				}
        }).focusout(function() {
			$name = $(this).val();
            if( $name == '')
			{
				$(this).val('Name');
				}
			});
			
		$('#inp_default_email').focus(function(){
           $email = $(this).val();
            if( $email == 'Your email')
			{
				$(this).val('');
				}
        }).focusout(function() {
			$email = $(this).val();
            if( $email == '')
			{
				$(this).val('Your email');
				}
				});
				
				
		$('#inp_default_subject').focus(function(){
            $Subject = $(this).val();
            if( $Subject == 'Subject')
			{
				$(this).val('');
				}
        }).focusout(function() {
			$Subject = $(this).val();
            if( $Subject == '')
			{
				$(this).val('Subject');
				}
				});
				
		$('#inp_default_message').focus(function(){
            $Message = $(this).val();
            if( $Message == 'Message')
			{
				$(this).val('');
				}
        }).focusout(function() {
			$Message = $(this).val();
            if( $Message == '')
			{
				$(this).val('Message');
				}
				});;
		
		/************2011-07-08**************/
		
    }
    // pre-submit callback
    function showRequest(formData, jqForm, options) {
        // formData is an array; here we use $.param to convert it to a string to display it
        // but the form plugin does this for you automatically when it submits the data
        //        var queryString = $.param(formData);

        // jqForm is a jQuery object encapsulating the form element.  To access the
        // DOM element for the form do this:
        // var formElement = jqForm[0];
        error =0;
        if(formData[0].value=="Name" || formData[0].value.length==0){
            error =1;
            $('#inp_default_name').attr({
                'value': "Name"
            });
            $('#inp_default_name').addClass('error');
        }
        if(formData[1].value=="Your email" || formData[1].value.length==0){
            error =1;
            $('#inp_default_email').attr({
                'value': "Your email"
            });
            $('#inp_default_email').addClass('error');
        }else{
            var email = formData[1].value;
            var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
            flag = pattern.test(email);
            if(!flag){
                error =1;
                $('#inp_default_email').attr({
                    'value': "Invalid email"
                });
                $('#inp_default_email').addClass('error');
            }
        }
        if(formData[2].value=="Subject" || formData[2].value.length==0){
            error =1;
            $('#inp_default_subject').attr('value','Subject');
            $('#inp_default_subject').addClass('error');
        }
        if(formData[3].value=="Message" || formData[3].value.length==0){
            error =1;
            $('#inp_default_message').attr('value','Message');
            $('#inp_default_message').addClass('error');
        }

        if(error>0){
            // alert('About to submit: \n\n' + error);
            return false;
        }

        // here we could return false to prevent the form from being submitted;
        // returning anything other than false will allow the form submit to continue
        return true;
    }

    // post-submit callback
    function showResponse(responseText, statusText, xhr, $form)  {
        if(responseText.search(/error1/)>0){
            $('#inp_default_name').attr({
                'value': "Name"
            });
            $('#inp_default_name').addClass('error');
        }else if(responseText.search(/error2/)>0){
            $('#inp_default_subject').attr('value','Subject');
            $('#inp_default_subject').addClass('error');
        }
        else if(responseText.search(/error3/)>0){
            $('#inp_default_message').attr('value','Message');
            $('#inp_default_message').addClass('error');
        }
        else if(responseText.search(/error4/)>0){
            $('#inp_default_email').attr({
                'value': "Your email"
            });
            $('#inp_default_email').addClass('error');
        }
        else if(responseText.search(/error5/)>0){
            $('#inp_default_email').attr({
                'value': "Invalid email"
            });
            $('#inp_default_email').addClass('error');
        }else{
			$('#fqv5-contact-form').children().hide()
		    $('#fqv5-contact-form').append(
		'<div id="emptyForm"><div style="padding-top: 30px;padding-left: 10px;" class=\"place_form\">' + responseText + '<br/><br/><br/><a style="margin-left:30px;" href=\"#\" id="back" onclick="$(\'#emptyForm\').remove();$(\'#fqv5-contact-form\').children().show();" class=\"bt_default\">back</a></div></div>');
        }
    }
    $(document).ready(function(){
        tabcontrol('#tab_menu','#tab_content');
       // oSlideshow();
        technology();
        work();
        about();
        contactus();
        formfocus();
        fqv5ContactForm();
        //放在后面加载不了
       //slide_home();
        nav();
    });

})(jQuery);



