$(document).ready(function() {

    Site.Common.init();
    Site.Common.navigationInit();
    //Site.Common.cufonInit();
    Site.Common.sideTabs();
    Site.Common.hoverClass($('#primary-navigation li')); // Navigation Dropdown
    Site.Common.hoverClass($('.filters .accordion h4')); // Accordion Head/Switch Hover
    Site.Common.accordion('.filters .accordion');
    Site.Common.spotlightTabs();

    if(typeof DD_belatedPNG != 'undefined') {
        DD_belatedPNG.fix(
            '#content .page-title h1,' +
            '#content #spotlight .nav li strong,' +
            '#content #spotlight .nav li strong span,' +
            '#content #spotlight .tab strong,' +
            '#content #spotlight .tab strong span,' +
            '#logo, ' +
            '#moneysense-cover img, ' +
            '#footer-columns-inner'
        );
    };
    
    /* IE on hover drop bug */
    $('#content .home .main-column .top-stories li h3 a').hover(
        function() {
            $('#content .home .main-column .recent-posts').addClass('recent-posts-fix');
        },
        function() {
        }
    );
    
    /* Open the link in a new page while clicking on the note */
    $('#supplementary-links > ul > li .note').bind("click", function() {
       window.open($(this).prev().attr('href'));
    });
    
    /* Primary navigation cufon pseudohover fix */
      
      $('#primary-navigation > ul > li').hover(function() 
      {
        $(this).find('ul').animate({"opacity": "1"}, "fast");
      }, function() {
          $(this).find('ul').animate({"opacity": "0"}, "0"); 
      });
      $('#primary-navigation > .side-nav > ul > li').hover(function() 
      {
        $(this).find('ul').animate({"opacity": "1"}, "fast");
      }, function() {
          $(this).find('ul').animate({"opacity": "0"}, "0");
      });
      $('#supplementary-links > ul > li').hover(function() 
      {
        $(this).find('.note').animate({"opacity": "1"}, "fast");
      }, function() {
          $(this).find('.note').animate({"opacity": "0"}, "0");
      });
    
    

});


var Site = {

    Common: {
        
        /**
         * General Init.
         */
        init: function(e) {
            $('body').removeClass('nojs').addClass('js');
            
            var currentLevel = readCookie('style') - 0;
            if (currentLevel == 0) { currentLevel = 3; }
            
            /* FontSize Switcher Init */
            $('.font-size a').click(function() {
                if ($(this).hasClass('font-bigger') && currentLevel < 5) {currentLevel++;}
                if ($(this).hasClass('font-smaller') && currentLevel > 1) {currentLevel--;}
                switchStylestyle(currentLevel);
                return false;
            });
            
            var c = readCookie('style');
            if (c) switchStylestyle(c);
            else switchStylestyle(3);
            switchCurrent(c);
            
            /* Print Init */
            $('.print a').click(function() {
                window.print();
                return false;
            });
            
            /* Lightbox init */
			
			// conflict with AdJuggler
			
        /*  	$("a.lightbox").fancybox({
              'overlayShow' : true,
              'zoomSpeedIn' : 600,
              'zoomSpeedOut' : 500
            });*/
            
            /* Child Selector support */
            $('#primary-navigation > ul > li > a, #primary-navigation .side-nav > ul > li > a').addClass('child');
            $('#primary-navigation .side-nav > ul, #primary-navigation > ul, #primary-navigation > ul > li > a').addClass('child');
            
            /* First & Last Child support */
            $('#primary-navigation > ul > li:first-child').addClass('first-child-fl');
            $('#content .secondary-content .box-toplist h3:first-child, #primary-navigation ul li:first-child').addClass('first-child');
            $('#primary-navigation ul li:last-child, #footer-navigation ul li:last-child').addClass('last-child');
            $('#content .posts li:last-child, #content .home .main-column .top-stories li:last-child').addClass('last-child');
            $('#content .posts .thumb:last-child').addClass('thumb-last-child');
            
            /* Sharethis text update */
            $('#content .page-tools .share span a span').html('Share');
            $('#content .share-and-more .share ul a.stbutton span').html('More');
            
            /* Pagination */
            $(".specific .previous a").addClass("prev");
            $(".specific .nextous a").addClass("next");
            
            /* Article-List - making the left dashed lines go to the bottom */
            $('.article-listpage').parent().parent().addClass('wrapper-inner2-dashed');
            
        },
        
        /* Spotlight tabs - homepage */ 
        spotlightTabs: function () {
          var spotlight = $('#spotlight');
          if (spotlight.length) {
            spotlight.tabs({ fx: { opacity: 'toggle', duration: 200} });
            spotlight.tabs('rotate', 3500);
          }   
        },

        
        /**
         * Initialize Navigation
         */
        navigationInit: function() {
            $('#primary-navigation > ul > .current-cat, #primary-navigation > ul > .current-cat-parent, #primary-navigation > .side-nav > ul > .current-cat, #primary-navigation > .side-nav > ul > .current-cat-parent').parent().addClass('current-state');
            
            $('#primary-navigation ul .current-cat, #primary-navigation ul .current-cat-parent').prev().addClass('current-cat-prev');
            $('#primary-navigation > ul.current-state, #primary-navigation > .side-nav > ul.current-state').hover(
              function() {
                $(this).removeClass('current-state');
              },
              function() {
                $(this).addClass('current-state');
              }
            )
            $('#primary-navigation ul li').each(function() {
              regexp = /cat-item-[^ ]*/gi;
              category_class = this.className.match(regexp);
              if (category_class) {
                $(this).attr("id",category_class);
              }
            });
            $('#primary-navigation ul li, #supplementary-links ul li').hover(
                function() {
                    $(this).addClass('hover');
                    $(this).prev().addClass('prev-hover');
                },
                function() {                  
                    $(this).removeClass('hover');
                    $(this).prev().removeClass('prev-hover');
                }
            );
            $('#primary-navigation .side-nav ul li:first-child, #supplementary-links ul li:first-child, ').hover(
                function() {
                    $(this).parent().addClass('parent-hover');
                },
                function() {
                    $(this).parent().removeClass('parent-hover');
                }
            );
        },
        
        /**
         * Class onHover
         * adds a "hover" class while hovered. Used for dropdowns
         */
        hoverClass: function(e) {
            $(e).hover(function() {
                $(this).addClass('hover');
            },
            function() {
                $(this).removeClass('hover');
            });
        },
        
        /**
         * Initialize Cufon
         */
        cufonInit: function() {
            if(typeof Cufon != 'undefined') {
                Cufon.set('fontFamily', 'Beton');
                Cufon.replace(
                    '#sidebar .box h3,' +
                    '.posts-title h2,' +
                    '#sideTabs li,' +
                    '.must-reads h3,' +
                    '.search-results .results-title h2,' +
                    '.page-title h1,' +
                    '#footer-columns h3,' +
                    '#idc-container h3 span#idc-commentcount_label,' +
                    '#comments #IDCommentsNewThreadCover h3'
                );
                $('body').addClass('cufon');
            }
        },
        
        /**
         * Initialize Side Tabs
         */
        sideTabs: function() {
            
            function resetTabs() {
                $('#sideTabs li a').each(function() {
                    var selectID = $(this).attr('href');
                    if($(this).hasClass('selected')) {
                        $(selectID).addClass('panel-selected').show();
                    } else {
                        $(this).removeClass('selected');
                        $(selectID).removeClass('panel-selected').hide();
                    }
                });
            }
            
            resetTabs();
            
            $('#sideTabs li a').hover(
                function() {
                    $(this).parent().siblings().children().removeClass('selected');
                    $(this).addClass('selected');
                    resetTabs();
                },
                function() {}
            );
            
            $('#sideTabs li a').click(
              function() {
                return false;
              });
            
        },
        
        /**
         * Accordion Handler
         */
        accordion: function(e) {
            $(e).each(function() {
                $(this).find('.switch').click(function() {
                    if($(this).hasClass('open')) {
                          $(this).removeClass('open').next().slideToggle(200);
                      } else {
                          $(this).parent().find('.panel').hide();
                          $(this).parent().find('.switch').removeClass('open');
                          $(this).addClass('open').next().slideToggle(200);
                      }
                    return false;
                }).next().hide();
            });
        }
        
    }

};

/* Font Size Switcher Functions */

  /* Create the cookie */
    function switchStylestyle(styleName)
    {
    	createCookie('style', styleName, 365);	
    	switchCurrent(styleName);
    }
  
  /* Switch the body class according to the new font size level */
    function switchCurrent(c)
    {
      $('body').removeClass('font-size-null').removeClass('font-size-1').removeClass('font-size-2').removeClass('font-size-3').removeClass('font-size-4').removeClass('font-size-5').addClass('font-size-'+c);
    }

  // cookie functions http://www.quirksmode.org/js/cookies.html
  function createCookie(name,value,days)
  {
  	if (days)
  	{
  		var date = new Date();
  		date.setTime(date.getTime()+(days*24*60*60*1000));
  		var expires = "; expires="+date.toGMTString();
  	}
  	else var expires = "";
  	document.cookie = name+"="+value+expires+"; path=/";
  }
  
  function readCookie(name)
  {
  	var nameEQ = name + "=";
  	var ca = document.cookie.split(';');
  	for(var i=0;i < ca.length;i++)
  	{
  		var c = ca[i];
  		while (c.charAt(0)==' ') c = c.substring(1,c.length);
  		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  	}
  	return null;
  }
  function eraseCookie(name)
  {
  	createCookie(name,"",-1);
  }
  // /cookie functions
