sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
} 


function expandcontent(qid, ansid){
document.getElementById(ansid).style.display=(document.getElementById(ansid).style.display!="block")? "block" : "none" 
if (document.getElementById(ansid).style.display == "block") 
{
  document.getElementById(qid).style.backgroundImage= "url(/template/q_open.gif)"
} 
else 
{
  document.getElementById(qid).style.backgroundImage= "url(/template/q_closed.gif)"
}
}

$(function() {

$(window).load(overlaypopup());

$("a[href^='http']").not("[href*='savvymom.ca']").click(function(){
	window.open(this.href);
	return false;
});

$("a[href*=.pdf]").click(function(){
	window.open(this.href);
	return false;
});


$(".share-pinterest").click(function(){
	window.open(this.href);
	return false;
});


$("ul > :first-child").addClass("first");
$("ul > :last-child").addClass("last");


         $('a[href*=#]').click(function() { 
          if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target
            || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
              var targetOffset = $target.offset().top;
              $('html,body')
              .animate({scrollTop: targetOffset}, 1000);
             return false;
            }
          }
        });


//Set custom configurations
var config = {
     sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 100, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

$("ul#nav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#nav li").hoverIntent(config); //Trigger Hover intent with custom configurations


//On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}


$('.slideshow').cycle({ 
    fx:     'fade', 
    timeout: 6000,
    speed:  'slow', 
    pager:  '.pager', 
    activePagerClass: 'activeSlide',
    allowPagerClickBubble: true, 
    pagerEvent: 'mouseover',
       pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '.pager li:eq(' + idx + ') a'; 
    }

});




$('#animation2').cycle({ 
    fx:    'fade', 
 
        pager:      '#pager',
        pagerEvent: 'mouseover',
        fastOnEvent: true,
    pause:           1,      // true to enable "pause on hover" 
    pauseOnPagerHover: 1, // true to pause when hovering over pager link 
        cleartype: true,
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#pager li:eq(' + idx + ') a'; 
    } 
});

$('#animation3').cycle({ 
    fx:    'fade', 
 
        pager:      '#pager',
        pagerEvent: 'mouseover',
        fastOnEvent: true,
    pause:           1,      // true to enable "pause on hover" 
   allowPagerClickBubble: true, 
    pauseOnPagerHover: 1, // true to pause when hovering over pager link 
        cleartype: true,
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#pager li:eq(' + idx + ') a'; 
    } 
});

$('#animationE').cycle({ 
    fx:    'fade', 
 
        pager:      '#pager',
        pagerEvent: 'mouseover',
        fastOnEvent: true,
    pause:           1,      // true to enable "pause on hover" 
    pauseOnPagerHover: 1, // true to pause when hovering over pager link 
        cleartype: true,
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#pager li:eq(' + idx + ') a'; 
    } 
});



$('.events-slideshow').cycle({ 
    fx:     'scrollHorz', 
    timeout: 6000,
    pager:  '#events-slideshow-nav',
	slideExpr: 'a',
	autostop: 1,
	autostopCount: 6

});


var container = $('#carousel'); 
var startPlace = $('a',container).index($('.selected',container)); 

$("#carousel").show();
jQuery('#carousel').jcarousel({
       vertical: true,
      start: startPlace 
    });



$('#cardShow .slides') 
.cycle({ 
    fx:     'fade', 
    timeout: 6000, 
	pause:    true,  
    pager:  '#pager',
      prev:   '#prev',
      next:   '#next',
        pagerAnchorBuilder: pagerFactory
    });

    function pagerFactory(idx, slide) {
        var s = idx > 20 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
    };

$('#pauseButton').click(function() {
    var obj = $(this);
    if (obj.hasClass('pause')) {
        obj.removeClass('pause').addClass('play').text('Play');
        $('#cardShow .slides').cycle('pause'); return false;
    } else if (obj.hasClass('play')) {
        obj.removeClass('play').addClass('pause').text('Pause');
        $('#cardShow .slides').cycle('resume'); return false;
    }
});



				//Full SM Icons Sliding (Hidden to Visible)
				$('.cardThumb2').hover(function(){
					$(".cardSM", this).stop().animate({top:'10px'},{queue:false,duration:160});
				}, function() {
					$(".cardSM", this).stop().animate({top:'-275px'},{queue:false,duration:160});
				});


				$('#editionSelect').hover(
 				function(){
					$("#cities").stop(true,true).slideDown("slow");
				}, function() {
					$("#cities").stop(true,true).animate({opacity:"1.0"}, 1000).slideUp("slow");
				});



$('#tipShow .slides').cycle({ 
    fx:    'fade', 

         speed:   300, 
         timeout: 6000,
	pause:    true,  
        cleartype: true,
      prev:   '#prev',
      next:   '#next'
    });


var container2 = $('#carousel2'); 
var startPlace2 = $('a',container2).index($('.selected',container2)); 


 $("#carousel2").show(); 
jQuery('#carousel2').jcarousel({
      start: startPlace2 
    });

$("#tipSubmit form").validate();
$("#subscriber_services form").validate();
$("#media_services form").validate();


$('a.colorbox').colorbox({transition:"elastic", opacity: 0.05});
$('a.colorboxF').colorbox({transition:"elastic", opacity: 0.05, iframe:true, width: 870, height: 500});

$("a[rel*=facebox]").colorbox({transition:"elastic", opacity: 0.05, width:500});

$(".prefilled").focus(function() {  
	this.value = '';
});  

$(".prefilled").blur(function() {  
     if ($.trim(this.value) == ''){  
         this.value = (this.defaultValue ? this.defaultValue : '');  
     }  
});  

	$(".toggle").hide();
	$(".trigger").click(function(){
            if ($(this).next(".toggle").is(":visible")) { return false; }
            if (!$(this).next(".toggle").is(":visible")) { 
				 $(".toggle").slideUp('slow'); $(".trigger").removeClass('open'); 
    	         $(this).next(".toggle").slideToggle("slow,"); 
				$(this).addClass('open'); 
				return false;
			}
	});
	$(".trigger2").click(function(){
   	            $(this).next(".toggle").slideToggle("slow,"); 
				$(this).addClass('open'); 
				return false;
	});

var theUrl = window.location.href;
var hashValue = theUrl.split('#')[1];
var $target= $('[id=' + hashValue + ']') ;

if ($target.length && !$target.is(":visible")) { $target.show();}







      $(".featured a").each(function() {
                //set url's href as a variable
                var url = $(this).attr('href');
                 //add onclick tracking event
                $( this ).attr( 'onClick', '_gaq.push(["_trackEvent","Featured","Click","'+url+'"])' );
        });


      $("#home-slideshow a").each(function() {
                //set url's href as a variable
                var url = $(this).attr('href');
                 //add onclick tracking event
                $( this ).attr( 'onClick', '_gaq.push(["_trackEvent","Rotator","Click","'+url+'"])' );
        });

      $("#overlay_middle_inside a").each(function() {
                //set url's href as a variable
                var url = $(this).attr('href');
                 //add onclick tracking event
                $( this ).attr( 'onClick', '_gaq.push(["_trackEvent","Popup","Click","'+url+'"])' );
        });

});


function pageScroll(news)
{
        var $target= $('[id=' + news + ']') ;
            if ($target.length) {
              var targetOffset = $target.offset().top;
              $('html,body')
              .animate({scrollTop: targetOffset}, 1000);
             return false;
            }

}




function overlaypopup() {
  PutVisits('sid');
  if(parseInt(GetCookie('sid')) == 2) {
       openpopup();
     } else {
       return;
  }
}

function openpopup(){
	document.getElementById('overlay_box').style.display ='block';
	document.getElementById('overlay_overlay').style.display ='block';
	document.getElementById('overlay_popcontent').style.display ='block';
}


function closeme(){
	document.getElementById('overlay_box').style.display ='none';
	document.getElementById('overlay_overlay').style.display ='none';
	document.getElementById('overlay_popcontent').style.display ='none';
}


function PutVisits(name) {
	if(!GetCookie(name)) {
		var value = 1;
	} else {
		var value = parseInt(GetCookie(name)) + 1;
	}
	PutCookie(name,value);
}

// generic function to write a cookie
function PutCookie(name,value) { 
	var exp = '';
	var now = new Date();
	then = now.getTime() + (365 * 24 * 60 * 60 * 1000);
	now.setTime(then);
	exp = '; expires=' + now.toGMTString();
	document.cookie = name + "=" + value + exp + '; path=/; domain=www.savvymom.ca';
}

// function to retrieve a cookie
function GetCookie(name) {
	var cookiecontent = '0';
	if(document.cookie.length > 0) {
		var cookiename = name + '=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if(cookiebegin > -1) {
			cookiebegin += cookiename.length;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
	}
	return cookiecontent;
}


