/*
    @author Carlos Barrantes
*/

jQuery(function($){
   
   $("#nav a").hover(function(){
        $(this).find('span').addClass('hover');
   },function(){
        $(this).find('span').removeClass('hover');
   });
   
   $("#s").val(''); //ensure search box is empty
    
   $("#left-nav").find("h3").click(function(){
    
        $("#left-nav").find("ul").slideUp();
        
        $h3 = $(this);
    
        if(!$h3.next("ul").is(":hidden"))
        {
            $h3.next("ul").slideUp("fast",function(){
                $h3.find('span').get(0).className = 'arrow_right'
            });
        }
        else
        {
            $h3.next("ul").slideDown("fast",function(){
                $("#left-nav").find("h3 span").removeClass('arrow_down').addClass('arrow_right');
                $h3.find('span').get(0).className = 'arrow_down';    
            }); //arrow_down
        }
   });
    var $win = $(window);

    //document dimensions
    var docHeight = $(document).height(),
        docWidth  = $(document).width();

    //lightbox position
    var bTop, bLeft;

    var $overlay; 
    
    var $lightbox = $("<div></div>").attr('id','overlay').css({
                    'display': 'none', 
                    'position': 'absolute',
                    'backgroundColor': '#FFF',
                    'zIndex': 999
    }).appendTo(document.body);
   
   $("div.ask-question a, #topnav a.ask, .site-map-ask").click(function(e){
   
        e.preventDefault();
        
        var h = 440, w = 650;
        
        var ajaxfile = 'http://'+location.host+'/wp-content/themes/soy-detail/_ask_question.php';
        
        bTop    =  (parseInt($win.height()-h)/2) + parseInt($win.scrollTop());
        bLeft   =  ($win.width()-w)/2;
        
        if(!$overlay)
        {
            $overlay = $("<div></div>")
            .css({'height':docHeight+'px',
                'width':docWidth+'px',
                'zIndex':998,
                'position':'absolute',
                'top':0,
                'left':0,
                'backgroundColor':'#000',
                'display':'none'
              }).appendTo(document.body)
                .fadeTo('fast',.5)
                .one('click',close);
                
              $lightbox.css({ 'width': w,
                        'height': h, 
                        'top' : bTop , 
                        'left' : bLeft }).fadeIn('slow')
                            .load(ajaxfile);        
        }
        else
        {
            $overlay.fadeTo('fast',.6)
                .one('click', close);
            
            $lightbox.fadeIn('slow');
        }
        
        $("a.close").live('click',close);
        
        function close(e){

            e.preventDefault();
            $overlay.add($lightbox[0]).fadeOut('fast',function(){
            
                $('#ask-lightbox').show();
                $('#confirmation').hide();
                
                $lightbox.css({'width':w, 'height': h});                                
                
            });    

        }
        
        $(".fm_submit").live('click',function(){
        
            if(!$("#soy-un-mocoso").is(':checked')){
                alert("You must be 13 years of age or older!");
                return;    
            }
            
            $("#overlay").fadeOut('fast', function(){
                $("#ask-lightbox").hide();
                $("#confirmation").show(function(){
                $("#overlay").css({'width':420,'height':150}).fadeIn('fast');
            });
            });
            
            tracker_add( GLOBAL_NAV, click, 'Global Nav: Ask question - Submit' );
            
            $.post(ajaxfile,$("#question_form").serialize());       
            
        }); 
    
   });
   
   var _$overlay;
   
   $("a.view-webinar-lightbox").click(function(e){
   
      e.preventDefault();
      
      var _$lightbox = $("<div></div>").attr('id','ligthbox').css({
                    'display': 'none', 
                    'position': 'absolute',
                    'backgroundColor': '#FFF',
                    'zIndex': 999
      }).appendTo(document.body);
      
      var h = 545, w = 685;
        
      var ajaxfile = 'http://'+location.host+'/wp-content/themes/soy-detail/_webinar.php';
      
      bTop    =  (parseInt($win.height()-h)/2) + parseInt($win.scrollTop());
      bLeft   =  ($win.width()-w)/2;
      
      if(!_$overlay)
      {
            _$overlay = $("<div></div>")
            .css({'height':docHeight+'px',
                'width':docWidth+'px',
                'zIndex':998,
                'position':'absolute',
                'top':0,
                'left':0,
                'backgroundColor':'#000',
                'display':'none'
              }).appendTo(document.body)
                .fadeTo('fast',.5)
                .one('click',close);
                
                   
        }
        else
        {
            _$overlay.fadeTo('fast',.6)
                .one('click', close);
        }
        
        _$lightbox.css({ 'width': w,
                        'height': h, 
                        'top' : bTop , 
                        'left' : bLeft }).fadeIn('slow')
                            .load(ajaxfile);   
        
        $("a.close").live('click',close);
        
        function close(e){

            e.preventDefault();
            _$overlay.add(_$lightbox[0]).fadeOut('fast');
            _$lightbox.remove()

        }
      
   });
   
   //external links -  target="_blank"   
   $('a[href*="http://"]').not('a[href*="'+location.hostname+'"]').attr('target','_blank');
   
   $('.hentry .category-post .more').not('a[href*="'+location.hostname+'"]').removeClass('more').addClass('link-study');
   
   /****** stats ******/ 
   
   var GLOBAL_NAV = 'Global Navigation';
   var SOY_FACTS  = 'Soy Facts';
   var click = 'click';
   var redir = document.URL;
   
   $("ul.toplinks a.ask").click(function(){tracker_add( GLOBAL_NAV, click, 'Global Nav: Header - Ask a question' );});
   $("ul.toplinks a.register").click(function(){tracker_add( GLOBAL_NAV, click, 'Global Nav: Header - Register' );});
   
   $(".logo").click(function(){
   
        tracker_add( GLOBAL_NAV, click, 'Global Nav: Logo - Soy Nutrition' );      
    
   });
   
   $(".sponsor").click(function(){
    
        tracker_add( GLOBAL_NAV, click, 'Global Nav: Sponsered by Silk' );
    
   });
   
   $(".mainnav").delegate('a',click,function(){
    
         var klass = $(this).get(0).className.split(' ',1)[0],
             klass = $.trim(klass), label = null;           
             
         switch(klass)
         {
            
            case 'soyfacts':
                label = 'Global Nav: Top Nav - Soy Facts';
                break;
                
            case 'sciencehealth':
                label = 'Global Nav: Top Nav - Science & Health';
                break
                
            case 'ourexperts':
                label = 'Global Nav: Top Nav - Our Experts';
                break;
                
            case 'resourcestools':
                label = 'Global Nav: Top Nav - Resources & Tools';
                break;
                
            case 'addthis_button':
                label = 'Global Nav: Top Nav - Share it';
                break;                                                                         
            
         }
         
         if(label)
            tracker_add( GLOBAL_NAV, click, label );          
    
   });
   
   $("#siteinfo p").delegate('a',click,function(){
    
        var rel = $(this).attr('rel'),
            rel = $.trim(rel);
                     
        var label = { 
            
            usa : 'Global Nav: Footer - Silk USA',
            canada : 'Global Nav: Footer - Silk Canada',
            map : 'Global Nav: Footer - Site Map',
            policy : 'Global Nav: Footer - Privacy Policy',
            terms : 'Global Nav: Footer - Terms of Use' 
            
        };
        
        label[rel] && tracker_add( GLOBAL_NAV, click, label[rel]);                     
    
   });
   
   $("a.main-sticky-link").click(function(){tracker_add( 'Homepage', click, 'Homepage: Heart health - read more');});
   
   $("#featured .sticky-summary a").eq(0).click(function(){tracker_add( 'Outgoing link', click, 'Resources and Tools - view webinar');})
                            .end().eq(1).click(function(){tracker_add( 'Homepage', click, 'Homepage: Facts - more');})
                            .end().eq(2).click(function(){tracker_add( 'Homepage', click, 'Homepage: Ask experts - more');})
                            
    $("h4.science-help").click(function(){tracker_add( 'Homepage', click, 'Homepage: Science & Health');});
    $("h4.facts-soy")   .click(function(){tracker_add( 'Homepage', click, 'Homepage: Soy Facts');});
    $("h4.ask-experts") .click(function(){tracker_add( 'Homepage', click, 'Homepage: Experts');});                            
    
    var cat = $("#category").val() || "";

    
    $('.related-links').delegate("a", click, _tracker_add);
    $("#left-nav").delegate("li a", click, _tracker_add);
    $('#secondary div[class$="answers"] a').click(_tracker_add);
    
    $("#left-nav h3").click(function(){
        
        tracker_add(cat,'click',cat + ': ' + $(this).text());    
        
    });
    
    $("#soyfacts-sub li a").click(_tracker_add);
    
    function _tracker_add(){
    
        tracker_add( cat, click, cat + " : " + $(this).text());
        
    }
    
    $('.share, .print').click(function(){
        
        var title = $(".hentry h2").eq(0).text() || "";
    
        tracker_add( cat, click, cat + " : " + title + ' ' + $(this).text() );   
        
    });
    
    $('.soy-facts a.more, .cooking-with-soy a').click(function(){
       
       var text = $(this).prev().text();
       
       tracker_add( cat, click, cat + " : " + text );
        
    });
       
  
});

function tracker_add(category, trigger, label){
    
    //console.log(label);
    _gaq.push(['_trackEvent', category, trigger, label]);
    
}

//add this configuration GLOBAL
var addthis_config =
{
                
    services_compact : 'facebook,twitter,google,print,favorites,email'               
                
}

