$(function(){
   
   //addGlare();
   
   if($('.tweet').length){
      $.getScript('http://redeyeoperations.com/wp-content/themes/REDEYE/jquery.tweet.js',function(){
         $(".tweet").tweet({
            username: "redeyeops",
            join_text: "auto",
            avatar_size: 20,
            count: 2,
            auto_join_text_default: "<br />", 
            auto_join_text_ed: "<br />",
            auto_join_text_ing: "<br />",
            auto_join_text_reply: "<br />",
            auto_join_text_url: "<br />",
            loading_text: "loading tweets..."
         });   
      })
   }
   
   if($('#sliderContainer').length){
      var slider = $('#slider');
      addSliderContent();
      //setting up slider
      $('#noJavascript').remove();
      
      $('#portSwitch').children('ul').find('a').click(function(){
         var uri = $(this).attr('href'),
         type = $(this).html(),
         typeParse = type.split(' ');
         id = 'slider'+typeParse[0];
         removeSliderMeta(id);
         newSliderContent(id, uri, type);
         return false;
      });
      
      slider.find('ul').load('http://www.redeyeoperations.com/slider/ .slide', function(){
         type = 'Web';
         startSlider(slider, type, false);         
      });
      
      $('#prevBtn, #nextBtn')
            .animate({borderWidth: '20px', opacity: 1},750)
            .hover(function(){
               $(this).stop().addClass('hovered');
            },function(){
               $(this).stop().removeClass('hovered')
         })
            .click(function(){
               var meta = $('#sliderMeta');
            meta.animate({opacity: 0},100, function(){
               setTimeout(function(){
                  meta.animate({opacity: 1},300);
               },700)
            });
         });
   
   }
   
   if($('#submit').length){
      if($.browser.msie){
         $('td.label').find('span').css({'margin-bottom': '-11px'});
         $('td.bodyText').find('span').css({'margin-bottom': '-15px'});
      }else if($.browser.mozilla){
         $('td.label').find('span').css({'margin-bottom': '-11px'});
         $('td.bodyText').find('span').css({'margin-bottom': '-5px'});
      }
      $('input[type="text"], textarea')
         .addClass('idleInput')
         .focus(function(){$(this).removeClass('idleInput').addClass('focusInput');})
         .blur(function(){$(this).removeClass('focusInput').addClass('idleInput');});
      $('#submit').click(function(){
         mail();
         return false;  
      });
   }
   
   
   

   if($('body').width() >= 900){
      var pEle = $('#catnav');
      
      pEle.find('li').hover(function(){
         var ele = $(this).children('a');
         if(ele.css('border-bottom-width') != '10px'){
            ele.stop().animate({marginTop: '-20px',borderWidth: '20px'},300);
         }
      },function(){
         var ele = $(this).children('a');
         if(ele.css('border-bottom-width') != '10px'){
            ele.stop().animate({marginTop: '0px', borderWidth: '0px'},100);
         }
      });
      
      pEle.find('li:odd').each(function(){
         var ele = $(this).find('a');
         if(ele.css('border-bottom-width') != '10px'){
            ele.css({'border-bottom-color':'#8ebdbd'})
         }
      })
   }
   
   if($('#blogNav').length){
      var drop = $('.blogDrop'),
         nav = $('#blogNav');
      drop.find('ul').each(function(){
			var h = $(this).height();
			$(this).show().attr({'y': h}).hide();
		});
		
	       drop.hover(function(){
			var h = $(this).find('ul').attr('y');
			$(this).find('ul').css({height: '0px'}).stop().animate({height: h},750,function(){
			  $(this).css({'overflow': 'visible'});  
			});
		},function(){
			$(this).find('ul').show().css({'overflow': 'hidden'}).stop().animate({height: '0px'},300);
		});
		var navTop = nav.offset().top;
		$(window).scroll(function(){
			if(window.pageYOffset > navTop){
				if(nav.attr('class') != 'attached'){
				nav.addClass('attached');
				}else{
					nav.css({opacity: .8}).hover(function(){
						$(this).stop().animate({opacity: 1},100);
					},function(){
						$(this).stop().animate({opacity: .8},100);
					});
				}
			}else{
			    nav.stop().css({opacity: 1});
				if(nav.attr('class') == 'attached'){
                                 nav.removeClass('attached');
				}
			}
      });
   }
   


                        
                        
});

function addGlare(){
   if(!$.browser.msie){
   $('#glow').find('img').css({opacity: 0}).attr('src','http://redeyeoperations.com/wp-content/themes/REDEYE/images/glow.png').load(function(){
      $(this).animate({opacity: 1},1000);
   });
   }else{
      $('#glow').remove();
      $('#header').css({'background-image': 'url(http://redeyeoperations.com/wp-content/themes/REDEYE/images/glow.png)'});
   }
}



function mail(){
 var name = $('#name').val(),
      email = $('#email').val(),
      reason = $('#reason').val(),
      body = $('#body').val(),
      data = 'name='+name+'&email='+email+'&reason='+reason+'&body='+body,
      errors = [''];
         //checking the data
      if(name == ''){
         errors.push('Please Enter a Valid Name<br />');
      }
         
      if(email == ''){
         errors.push('Please Enter a Valid Email<br />');
      }else{
         at = email.split('@');  
         if(at.length < 2){
            errors.push('Please Enter a Valid Email<br />'); 
         }else{
            dot = at['1'].split('.');
            if(dot.length < 2){
               errors.push('Please Enter a Valid Email<br />')
            }
         }
      }
      if(reason == ''){
         errors.push('Please Enter a Reason<br />');
      }
      if(body == ''){
         errors.push('Please Enter a Description<br />');
      }
         
      document.getElementById('errors').innerHTML = errors.join('');
         
      if($('#errors').children().length){
         return false;
      }
         //dont send me blank stuff jerk.
      $.ajax({
         type: 'POST',
         url: 'http://www.redeyeops.com/content/wp-content/themes/REDEYE/mail_sent.php',
         data: data,
         success: function(data){
            $('table').hide();
            $('#mailForms').find('h2:first').hide();
            $('#results').css({opacity: 0}).html('<center><img src="http://www.redeyeoperations.com/wp-content/uploads/2010/01/mailsent.png" alt="mail sent" /><p>Thank You! Your Message has been sent.</p></center>').animate({opacity: 1},100);   
         }
      });  
}

function startSlider(sliderName, type, autoPlay){
   var s = $('.slide');
    s.css({
         'background-image': 'url(http://redeyeoperations.com/loading.gif)',
         'background-position': 'center',
         'background-repeat': 'no-repeat'
         });
      s.find('img').css({opacity: 0}).load(function(){
         $(this).css({opacity: 1});
      });
   
   $.getScript('http://redeyeoperations.com/wp-content/themes/REDEYE/slide.js?nc='+Math.random(999.999),function(){
      var c = $('#sliderContainer');
      sliderName.find('ul').slide();
            c.css({
            'background-image': 'none'
            });
            for(i=0; i <= sliderName.find('li').size()-3; i++){
               $('#slideNav').children('ul').append('<li id="'+ i +'"></li>')
            }
            c.append('<div id="portType">'+type+'</div>');
   });
}

function addSliderContent(){
   $('#sliderContainer').css({
         'background-image': 'url(http://redeyeoperations.com/loading.gif)',
         'background-position': 'center',
         'background-repeat': 'no-repeat'
         }).append('<div id="panelOne"><div id="prevBtn"></div><div id="nextBtn"></div></div><div id="panelTwo"></div><div id="sliderMeta"></div><div id="slideNav"><ul></ul></div>');
   }

function newSliderContent(id, uri, type){
   $('#'+id).children('ul').load(uri + ' .slide', function(){
      var sliderName = $('#'+id);
      startSlider(sliderName, type, false);  
   });  
}

function removeSliderMeta(id){
   $('#slideNav').empty().append('<ul></ul>');
   $('#sliderMeta').empty();
   $('#panelTwo').empty();
   $('#slider, #sliderWeb, #sliderPhoto, #sliderDesign').stop().remove();
   $('#sliderContainer').css({
         'background-image': 'url(http://redeyeoperations.com/loading.gif)',
         'background-position': 'center',
         'background-repeat': 'no-repeat'
         }).append('<div id="'+ id +'"><ul></ul></div>');
   $('#portType').remove();
   
}

