/***********************************************
         CAROUSEL  
***********************************************/

(function($){
  
  
  
  $.fn.myCarousel = function(options) {
      
    var Settings = {
      BaseTime : 1000,
      SlideSpeed:1,
      TimerSpeed:7,
      Paralax:"",
      ParallaxSpeed:280,
      Pips:true,
      AutoScroll : true,
      Buttons:{
        LeftClass:"left-button control-left",
        RightClass:"right-button control-right",
        BtnLeft:"btnLeft",
        BtnRight:"btnRight"
      }
    };
    
    if (options) {
            $.extend(Settings, options);
        };
        
        $(this).each(function(options) {
                        
            
            var Carousel = {
        
        // Properties  
        el:null,
        ChildWidth:0,
        ChildHeight:0,
        ParentWidth:0,
        ParentEl:null,
        ItemCount:0,
        Interval:null,
        Timeout:null,
        NextIndex:0,
        PreviousIndex:0,
        ParalaxElements:[],
        Margins:{
          Left:0,
          Right:0
        },
        Buttons:{
          BtnRight:null,
          BtnLeft:null
        },
        Init:function(){
          
          
          // Carousel.el : The element(<ul />) that will be turned into the Carousel.
          Carousel.ItemCount = Carousel.el.children().length;
          if(Carousel.ItemCount == 0) return false;
          
          Carousel.ChildWidth = Carousel.el.find("li").width();
          Carousel.ChildHeight = Carousel.el.find("li img").height()/2;
          
          
          $wrapper = $("<div class='carouselContainer' />");
          
          var addedMargin = 0;
          
          Carousel.el.children("li").each(function(){
            var leftMargin = $(this).css("margin-left");
            Carousel.Margins.Left = parseInt(leftMargin.slice(0, leftMargin.length -2));
    
            var rightMargin = $(this).css("margin-right");
            Carousel.Margins.Right = parseInt(rightMargin.slice(0, rightMargin.length -2));
            addedMargin += (Carousel.Margins.Left + Carousel.Margins.Right);
          });
          Carousel.ParentWidth = (Carousel.ItemCount*Carousel.ChildWidth) + addedMargin;
          Carousel.el.attr("style", "overflow:hidden;width:" + Carousel.ParentWidth + "px;").wrap($wrapper);
          Carousel.el.find(" > li").css("float","left").css("display","block");
          
          if(Settings.Pips){
            $(".carouselContainer").append(Carousel.CreatePips());
            Carousel.Events.Pips();
          }else{
            Carousel.el.parents(".carousel").prepend(Carousel.CreateNavButtons());
          };
          Carousel.TabFocus();
          
          if(Settings.AutoScroll){
            Carousel.Timer();
          };
        },
        CreateNavButtons:function(){
          
          var btnContainer, btnLeft, btnRight, btnHeight;
            
          btnContainer = $("<div />");
          
          if(Carousel.ChildHeight < 150){
            btnHeight = Carousel.ChildHeight/2+17;
          };
          
          // CREATE THE BUTTON FOR THE RIGHT OF THE CAROUSEL AND APPEND AN EVENT TO IT.
          Carousel.Buttons.BtnRight = $("<a class='" + Settings.Buttons.RightClass + " " + Settings.Buttons.BtnRight + "' style='margin-top:-" + btnHeight + "px'>R</a>");
          Carousel.Buttons.BtnRight.bind("click", function(){
            Carousel.Events.Buttons.Right();            
          });
          // CREATE THE BUTTON FOR THE LAFT OF THE CAROUSEL AND APPEND AN EVENT TO IT.
          Carousel.Buttons.BtnLeft = $("<a class='" + Settings.Buttons.LeftClass + " " + Settings.Buttons.BtnLeft + "' style='margin-top:-" + btnHeight + "px'>L</a>");
//          Carousel.Buttons.BtnLeft.bind("click", function(){
//            Carousel.Events.Buttons.Left();            
//          });
          
          btnContainer.append(Carousel.Buttons.BtnLeft, Carousel.Buttons.BtnRight);
          
          return btnContainer;
          
        },
        CreatePips:function(){
          $pips = $("<div class='pips' />");
          for(var i = 0; i < Carousel.ItemCount; i++){
            if(i != 0){
              $pips.append("<a></a>");
            }else{
              $pips.append("<a class='active'></a>");
            };
          };
          return $pips;
        },
        Events:{
          Pips:function(){
            $(".pips a").each(function(x){
              $(this).bind("click", function(){
                if(Carousel.NextIndex != $(this).index()){
                  Carousel.NextIndex = $(this).index();
                  Carousel.Animations.Init();
                };
              });
            });
          },
          Buttons:{
            Left:function(){
              if(Carousel.NextIndex != 0){  
                Carousel.NextIndex--;
              }else{
                Carousel.NextIndex = Carousel.ItemCount;
              };
      
              
              Carousel.Animations.Init();
            },
            Right:function(){
              
              if(Carousel.NextIndex < Carousel.ItemCount){        
                
                Carousel.NextIndex++;
              }else{
                Carousel.NextIndex = 0;
              };
              
              
              Carousel.Animations.Init();
            }
          }
        },
        UpdatePips:function(index){
          $(".pips a.active").removeClass("active");
          $(".pips a:eq("+  Carousel.NextIndex +")").addClass("active");
        },
        LoadData:{
          Init:function(){}      
        },
        Timer:function(){
          
          // Clear the timeout.
          clearTimeout(Carousel.Timeout);
          Carousel.Timeout = null;
          Carousel.Timeout = setTimeout(function(){
            
            if($("body").data("hasFocus")){
              Carousel.NextIndex ++;
              if(Carousel.ItemCount == Carousel.NextIndex){Carousel.NextIndex = 0;};
    
              Carousel.Animations.Init();
                
                
            }else{
              Carousel.Timer();
            };
          
          }, Settings.BaseTime*Settings.TimerSpeed);
        },
        Animations:{
          Init:function(el){
            
            if(Settings.Animation == "parallax"){
              Carousel.Animations.Parallax.Init();
            }else{
              Carousel.Animations.Scroll.Init(el);
            };
          },
          
          // SIMPLE SCROLLING ANIMATION
          Scroll:{
            Init:function(el){
              
              if(Settings.Pips){
                
                Carousel.el.children("li").animate({
                  
                  "left":"-"+ (Carousel.NextIndex * Carousel.ChildWidth) - (Carousel.NextIndex * Carousel.Margins.Left) - (Carousel.NextIndex * Carousel.Margins.Right) +"px"
                }, 
                Settings.BaseTime/Settings.SlideSpeed);
                Carousel.UpdatePips();
                
              }else{
                Carousel.el.children("li").animate({
                  //"left":"-"+ (Carousel.NextIndex * Carousel.ChildWidth) - (Carousel.NextIndex * Carousel.Margins.Left) - (Carousel.NextIndex * Carousel.Margins.Right) +"px"
                  
                  "left": this.GetBounds(el)
                }, 
                Settings.BaseTime/Settings.SlideSpeed);
              
              };
              if(Settings.AutoScroll){
                Carousel.Timer();
              };
            },
            GetBounds:function(){
              
              
              var ContainerWidth = Carousel.el.parents(".carouselContainer").css("width"),
                ulWidth = Carousel.el.css("width"),
                liWidth = Carousel.ChildWidth,
                liPosition = Carousel.el.children('li').position();
              
              var i = ((parseInt(ContainerWidth.slice(0, ContainerWidth.length-2)) - Carousel.el.css("width")) + Carousel.ChildWidth); 
              
              
              //RIGHT BUTTON
              if(((Carousel.NextIndex * Carousel.ChildWidth) + (Carousel.NextIndex * Carousel.Margins.Left) + (Carousel.NextIndex * Carousel.Margins.Right)) > (Carousel.ParentWidth - parseInt(ContainerWidth.slice(0, ContainerWidth.length-2)))){
                
                var scroll = "-" + (Carousel.ChildWidth - ((parseInt(ContainerWidth.slice(0, ContainerWidth.length-2)) - Carousel.el.width()) + Carousel.ChildWidth));
                
                
                Carousel.Buttons.BtnLeft.unbind("click").bind("click", function(){
                    Carousel.Events.Buttons.Left();
                  });
                if(((Carousel.ParentWidth - Carousel.Margins.Right - parseInt(ContainerWidth.slice(0, ContainerWidth.length-2))) - (Carousel.ChildWidth + Carousel.Margins.Right)) < Carousel.ChildWidth){
                  console.log("Remove right button click even!");
                  Carousel.Buttons.BtnRight.attr("id", "testing");
                  Carousel.Buttons.BtnRight.unbind("click");
                };
                
                return scroll;
              
              }else{
              
                var scroll = "-"+ (Carousel.NextIndex * Carousel.ChildWidth) - (Carousel.NextIndex * Carousel.Margins.Left) - (Carousel.NextIndex * Carousel.Margins.Right) +"px";
                
                
                // REMOVE THE CLICK EVEN.
                if(scroll == "0px"){
                  Carousel.Buttons.BtnLeft.unbind("click");
                }else{
                  Carousel.Buttons.BtnLeft.unbind("click").bind("click", function(){
                    Carousel.Events.Buttons.Left();
                  });
                };
                Carousel.Buttons.BtnRight.unbind("click").bind("click", function(){
                  Carousel.Events.Buttons.Right();
                });
                
                return scroll;
              };
              
            }
          },
          
          // PARALLAX TYPE ANIMATION (IMG ELEMENTS)
          Parallax:{
            Init:function(){
              var currentIndex = Carousel.PreviousIndex, 
                nextIndex = Carousel.NextIndex, 
                currentElement = Carousel.el.children("li:eq(" + currentIndex + ")"), 
                nextElement = Carousel.el.children("li:eq(" + nextIndex + ")"),
                leftPosition="900px",
                slidePosition = "0px";
              
              
              if(currentIndex < nextIndex){            
                leftPosition = "0px";
                slidePosition="900px";            
              };
              
              this.Previous(currentIndex, nextIndex, currentElement, nextElement, leftPosition, slidePosition);
              this.Next(currentIndex, nextIndex, currentElement, nextElement, leftPosition, slidePosition);
              
              
              if(Settings.Pips){
                Carousel.UpdatePips();
              };
              if(Settings.AutoScroll){
                Carousel.Timer();
              };;
            },
            Previous:function(currentIndex, nextIndex, currentElement, nextElement, leftPosition, slidePosition){
              currentElement.find("img").each(function(i){
                var timer = 100 + (Settings.ParallaxSpeed * i);
                $(this).animate({
                  opacity:0,
                  left:leftPosition
                },timer, function(){
                  
                  Carousel.el.find("li").animate({"left":"-"+ nextIndex * Carousel.ChildWidth +"px"}, Settings.BaseTime/3)
                  .not(nextElement).find("img").attr("style", "").css("opacity",0);
                  
                });
              });
            },
            Next:function(currentIndex, nextIndex, currentElement, nextElement, leftPosition, slidePosition){
              nextElement.find("img").each(function(i){
                var thisLeft = $(this).css("left"),
                  timer = 100 + (Settings.ParallaxSpeed);
              
                $(this).css("left", slidePosition).delay(Settings.BaseTime/3).animate({
                  opacity:1,
                  left:thisLeft
                },timer);
              });
              Carousel.PreviousIndex = nextElement.index();
            }        
          }
        },
        TabFocus:function(){
          
          $("body").data("hasFocus", true);
          
          if (/*@cc_on!@*/false) { // check for Internet Explorer
          
            document.onfocusin = function(){$("body").data("hasFocus", true);};
            document.onfocusout = function(){$("body").data("hasFocus", false);};
          
          } else {
          
            window.onfocus = function(){$("body").data("hasFocus", true);};
            window.onblur = function(){$("body").data("hasFocus", false);};
          
          };
        }
        };
            Carousel.el = $(this);
            Carousel.Init();
        });
    };
})(jQuery);


/***********************************************
        ACCORDION  
***********************************************/  

(function($){
  var Settings = {
    BaseTime:1000,
    SlideSpeed:2,
    OnlyShowOne:true
  };
  var Accordion = {
    className: null,
    Init:function(){
      
      this.className.find("li.job").delegate(".header", "click", function(){
        var index = $(this).parent().index();
        Accordion.className.find("li.job").each(function(x){
          var isSelected = x == index;
          var isOpen = $(this).find(".header").hasClass("active");
          if(!isSelected){
            if(isOpen){
              Accordion.Show($(this), true);
            };
          }else{
            if(isOpen){
              Accordion.Show($(this), true);
            }else{
              Accordion.Show($(this), false);  
            };
          };  
        });
      });
    },
        Show:function($el, slideUp){
          if(slideUp){
            $el.find(".header").removeClass("active").siblings("div.content").slideUp(Settings.BaseTime / Settings.SlideSpeed);
            $el.find(".red-button > span").html("View Job");
          }else{
            $el.find(".header").addClass("active").siblings("div.content").slideDown(Settings.BaseTime / Settings.SlideSpeed);
            $el.find(".red-button > span").html("Close");
          };
          return false;
        }
  };
  $.fn.myAccordion = function(options) {
        if (options) {
            $.extend(Settings, options);
        };
        $(this).each(function() {
          Accordion.className = $(this);
          Accordion.Init();
        });
    };
})(jQuery);

/*
EMAIL VALIDATION
*/
(function(window){
  
  var Register = {
    TXTEmail : null,
    TXTBox : null,
    AddEvents:function(){
      Register.TXTBox = Register.TXTEmail = document.getElementById("mc-email");
      document.getElementById("mc-subscribe").onclick = function(){
        if(Register.ValidateEmail(Register.TXTBox)){
          Register.XHR.MakeRequest(Register.TXTBox);
        };
      };
      $("#signup-form form").submit(function(){
        if(Register.ValidateEmail(Register.TXTBox)){
          Register.XHR.MakeRequest(Register.TXTBox);
        };
        return false;
      });
      
    },
    XHR:{
      Setup:function(){
        if (typeof XMLHttpRequest != 'undefined') {
                  var request = new XMLHttpRequest();
              }
              else {
                  if (window.ActiveXObject) {
                      try {
                          var request = new ActiveXObject("Msxml2.XMLHTTP");
                      }
                      catch (err1) {
                          try {
                              var request = new ActiveXObject("Microsoft.XMLHTTP");
                          }
                          catch (err2) {
                              var request = false;
                          };
                      };
                  };
              };
              return request;
      },
      MakeRequest:function(txtBox){
        var xhr = Register.XHR.Setup(),
          url = "http://crm.ops.mindshapes.com/system/msweb/register/msnewsletter/email/"+Register.TXTBox.value;
        xhr.open("GET", url, true);
        xhr.setRequestHeader('Origin', window.location);
        xhr.onreadystatechange = function () {
                  if (xhr.readyState == 4) {
                      if (xhr.status == 200) {
                        Register.XHR.Success("success");
                      };
                  };
              };
              xhr.send(null);
      },
      Success:function(res){
        console.log("Change HTML! : " + res);
        var html = "<div class='inner'><h2><span>Thank you for registering.</span></h2></div>",
          hldrRegister = document.getElementById("signup-form");
        
        
        hldrRegister.setAttribute("class", "success");
        hldrRegister.innerHTML = html;
        
        
      }
    },
    ValidateEmail : function(txtBox){
      var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
      if(reg.test(txtBox.value) == false){
        alert("Please enter a valid email address.");
        return false;
      }else{
        return true;
      };
    }
  };
  if(document.getElementById("signup-form")){
    Register.AddEvents();
  };
})(window);


(function(){
  if(navigator.appVersion.indexOf("MSIE 7.")==-1){
    $(".jsAccordion").myAccordion();
  };
  $(".jsCarousel").myCarousel();
  $(".jsCarouselNormal").myCarousel({Pips:false, AutoScroll:false});
})();


