//This file is overly commented for those that come after to understand what is 
//going on. It might be overkill but I want to make sure others that need to
//inherit this code can understand what is going on.

//Used to correct IE png transparency issues.
DD_belatedPNG.fix('#logo img, #menu-primary a, #menu-primary li ul, #billboard-text img');

jQuery(document).ready(function(){
    
    var base_url = '';
    var env = '';
    var animation_speed = 500;
    var delay_value = 300;
    
    switch(window.location.hostname)
    {
        case 'maicinc.com':
        case 'www.maicinc.com':
            base_url = 'http://www.maicinc.com'; //Do not include trailing slash
            env = 'production';
        break;
        
        case 'maic.local':
            base_url = 'http://maic.local'; //Do not include trailing slash
            env = 'development';
        break;
    }
    
    
    if ( jQuery.browser.msie )
    {
       if (parseInt(jQuery.browser.version, 10) == 6)
       {
           var ie6 = true;
           jQuery('#view-our-services').attr('src', base_url+'/wp-content/themes/maic/images/btn-view-our-services.png');
           jQuery('body').addClass('ie6');
       }

    }
    
    
    //Temporarily disable all the links in the primary nav if we are on the production server
    if (env == 'production')
    {
        jQuery('#menu-primary a').click(function(e){
            //e.preventDefault();
        })
    }
 
    /*-------------------------------------------------------------------------
    / If the .current class is present append an arrow to that item
    /-------------------------------------------------------------------------*/
    if(ie6)
    {
        jQuery('li.current').prepend('<img src="'+ base_url );
    }
    else
    {
        jQuery('li.current').prepend('<img src="'+ base_url );
    }
    
    /*-------------------------------------------------------------------------
    / Add the current body class to the global scope
    /-------------------------------------------------------------------------*/
     var $body_class = jQuery('body').attr('class').split(' ').slice(-1); 
     
    /*-------------------------------------------------------------------------
    / Check for url hash existence.
    /-------------------------------------------------------------------------*/

     if (window.location.hash)
    {
        var hash = window.location.hash;
        slug = hash.replace('#','');
        load_content(slug);
        set_current_button(slug);
    }
    else
    {

        var url = window.location.href;
        var slug = url.split('/').slice(-1); 
        load_content(slug);
        set_current_button(slug);
    }
       
    
    /*-------------------------------------------------------------------------
    / Load content based on slug.
    /-------------------------------------------------------------------------*/
    
    function load_content(slug)
    {
        jQuery('.loading').fadeIn('slow');
         jQuery.post('/framework/ajax.php/content/'+ slug, function(data){
         //jQuery('#ajax-content').fadeOut('fast');
         setTimeout(function(){
                jQuery('#ajax-content').html(data);
                jQuery('.loading').delay(delay_value).fadeOut('fast');
            }, animation_speed);
         jQuery('#ajax-content').delay(delay_value + 200).fadeIn('slow');
         jQuery('#partial-wrapper').delay(delay_value + 300).fadeIn('slow');
        });
    }
    
    /*-------------------------------------------------------------------------
    / Right Submenu on Page Template link clicked
    /-------------------------------------------------------------------------*/
    
    jQuery('#sub-menu li a').click(function(e){
           
            var url = jQuery(this).attr('href');
            var slug = jQuery(this).attr('rel');
             //window.location = url;
             load_content(slug);
             set_current_button(slug);
             e.preventDefault();
        });
    
    /*-------------------------------------------------------------------------
    / Set Current Button
    /-------------------------------------------------------------------------*/
    
    function set_current_button(slug)
    {
        //Remove the "current" class from the list items
        jQuery('#sub-menu li').removeClass('current');
        
        //Now set the "current" class to the list item parent of the anchor
        jQuery('#sub-menu a[rel='+slug+']').parent().addClass('current');
        
        //Remove the gray arrows on all anchors (should only be one)
        jQuery('.gray-arrow').remove();
        
        //Now add the arrow to the current anchor just clicked
        if (ie6)
        {
            jQuery('#sub-menu a[rel='+slug+']').prepend('<img src="'+ base_url);
        }
        else
        {
            jQuery('#sub-menu a[rel='+slug+']').prepend('<img src="'+ base_url);
        }
    }
   
    /*-------------------------------------------------------------------------
    / View Our Services Button Clicked
    /-------------------------------------------------------------------------*/
   
    //Run this code when the view our services button is clicked
    jQuery('#view-our-services').click(function(){
        
        var $menu = jQuery('#services-menu');
        var $button = jQuery(this);
        
        if ($menu.is(':hidden'))
        {
            //Let's change the button image src to show the arrow pointing up.
            if(ie6)
            {
                $button.attr('src', base_url +'/wp-content/themes/maic/images/btn-view-our-services-open.png');
            }
            else
            {
                $button.attr('src', base_url +'/wp-content/themes/maic/images/btn-view-our-services-open.png');
            }
            //Get the current left margin value of the slider item with the class of .acquisitions
            var current_margin =  jQuery('#slider-items .acquisitions').css('margin-left');
            
            //If the margin is set to 800 it means the slider is hidden all the way to the right
            //Let's animate it out to a left margin of 0. This is because it is the first
            //time the menu is loading so we want the user to see the first item by default.
            if (current_margin == '800px')
            {
                jQuery('#slider-items .acquisitions').delay(delay_value + 300).animate({
                    marginLeft: 0
                }, animation_speed ,'easeInOutCubic');
            }
           
           //Let's fade out the image and text in the background so it doesn't create confusion
           jQuery('#banner-image img, #billboard-text img').delay(delay_value + 300).fadeOut('slow');
           
           //Let's add a class to teh first item in the slider menu so we can add a highlighted state
           jQuery('a.acquisitions').parent().addClass('current');
           
           //Let's add a small gray arrow to the front of the anchor tag so we can show the selected item.
           if (ie6)
           {
                jQuery('a.acquisitions').prepend('<img src="'+ base_url );
           }
           else
           {
                jQuery('a.acquisitions').prepend('<img src="'+ base_url);
           }
        }
        else
        {
             //Let's change the button image src back to the arrow pointing left
             
             if (ie6)
             {
                $button.attr('src', base_url +'/wp-content/themes/maic/images/btn-view-our-services.png');
             }
             else
             {
                 $button.attr('src', base_url +'/wp-content/themes/maic/images/btn-view-our-services.png');
             }
             
             //Let's set the left margin of the slider to 800 which will push it all the way to right
             jQuery('#slider-items .slider').animate({
                    marginLeft: 800
                },'normal','swing');
             
             //Let's fade the background image and text back in
             jQuery('#banner-image img, #billboard-text img').fadeIn('slow');
             
             //Let's remove the "current" class on the list items
             jQuery('#services-menu li').removeClass('current');
             
             //Let's remove the gray arrow from the dom
             jQuery('.gray-arrow').remove();
        }

        //perform the slide toggle of showing or hiding the services menu
        $menu.slideToggle({
            duration: animation_speed, 
            easing: 'easeInOutCubic'
        });
    });
    
    /*-------------------------------------------------------------------------
    / Services Menu Anchor Clicked
    /-------------------------------------------------------------------------*/
    
    //When an item on the services menu is clicked run this function
    jQuery('#services-menu li a').click(function(e){
        
        //Keep the link from trying to navigate anywhere
        e.preventDefault();
         
        //Remove the "current" class from the list items
        jQuery('#services-menu li').removeClass('current');
        
        //Now set the "current" class to the list item parent of the anchor
        jQuery(this).parent().addClass('current');
        
        //Remove the gray arrows on all anchors (should only be one)
        jQuery('.gray-arrow').remove();
        
        //Now add the arrow to the current anchor just clicked
        if(ie6)
        {
            jQuery(this).prepend('<img src="'+ base_url );
        }
        else
        {
            jQuery(this).prepend('<img src="'+ base_url );
        }
        
        //Get the current margin of all sliders visible on the stage
        jQuery('.slider').each(function(){
            
            var current_margin =  jQuery(this).css('margin-left');
     
            //If any of the sliders have a left margin of 0 it means they are visible so
            //let's set the margin to 800px before showing the one the user clicke to see
            if (current_margin == '0px')
            {
                jQuery('.slider').animate({
                    marginLeft: 800
                }, animation_speed,'easeInOutCubic');
            }
        });
        
       //Get the class name of the anchor that was just clicked. It will be matched
       //up with the slider item that has the same class so we can display it.
        var css = jQuery(this).attr('class');
        
        //Get the slider we want to display by using the css class defined on the 
        //anchor that was clicked.
        var $slider = jQuery('#slider-items .'+css);
        
        //Check to see of the margin is 0
        //Use the ternary operator to test for this. If the it is equal to 0 then set it to the
        //outerwidth of the slider which should be 724 or so based on the image size if it isn't set it to 0
        var margin = parseInt($slider.css('marginLeft'), 10) == 0 ? $slider.outerWidth() : 0;
        
        //Finally let's animate the slider out to margin value defined above
        //The 800 represents the speed of the animation and the 'easeInOutCubic'
        //relates to the easing plugin type of easing
        $slider.animate({
          marginLeft: margin
        } , animation_speed,'easeInOutCubic');
    });
    
    /*-------------------------------------------------------------------------
    / Rewrite the urls in the primary menu to work with our load content mechanism
    /-------------------------------------------------------------------------*/
    
    
    
    jQuery('#menu-primary.menu>li>a').click(function(){
       
        var url = jQuery(this).attr('href');
        var slug = url.replace(base_url, '');
        slug = slug.replace('/index.php/','');
        if (window.location == url)
        {
            load_content(slug);
            return false;
        }
        
        return true;
    });
    
    
    /*-------------------------------------------------------------------------
    / Listen for click events on submenu
    /-------------------------------------------------------------------------*/
    
    jQuery('#menu-primarys .sub-menu li a').click(function(e){
        
         //Keep the link from trying to navigate anywhere
        e.preventDefault();
        var url = jQuery(this).attr('href');
        var slug = url.split('/').slice(-1); 
        url = url.replace(slug, '');
        
        window.location.replace(url +'#'+ slug);
        window.location = url +'#'+ slug;
        
    });
    
    /*-------------------------------------------------------------------------
    / Force blog link to redirect to blog template not load in via ajax
    /-------------------------------------------------------------------------*/
    
    jQuery('a[rel=blog]').click(function(e){
        e.preventDefault();
        jQuery('.loading').hide();
        window.location = base_url +'/index.php/category/blog';
    });
    
    /*-------------------------------------------------------------------------
    / Handles the contact form submission
    /-------------------------------------------------------------------------*/
    
     jQuery('#contact-form').submit(function(e){
        e.preventDefault(); 
        jQuery('#contact-submit').hide();
        jQuery('#loading-bar').show();
        var str = jQuery(this).serialize();
        jQuery.post('/framework/ajax.php/process_form', str, function(data){
            jQuery('#loading-bar').hide();
            jQuery('#contact-submit').show();
            if (data == 'OK')
            {
                alert('Your email was sent successfully!');
            }
            else
            {
                alert('There was a problem sending your email.'+ data);
            }
        });
     });
   
    
});
