function fixPng(){
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/**
 * @author : Jansen Price
 * @date   : 2008-10-03
 */
function toggleSubMenu(elid, state) {
    el = document.getElementById(elid);

    if (el != 'undefined') {
        if (state == 'on') {
            el.style.display = 'block';
        } else {
            el.style.display = 'none';
        }
    }
    return false;
}

/**
 * @author: Jansen Price
 * @date:   2008-10-03
 */
function initSubMenuHovers() {
    //console.log('init...');
    $('#landing_subnav li').bind('mouseover', function() {toggleHover(this, 'on')});
    $('#landing_subnav li').bind('mouseout', function() {toggleHover(this, 'off')});
    $('#dont_know_menu').bind('mouseover', function() {toggleSubMenu('start_sub', 'on')});
    $('#dont_know_menu').bind('mouseout', function() {toggleSubMenu('start_sub', 'off')});
    $('#start_sub').bind('mouseover', function() {
        toggleHover(document.getElementById('dont_know_menu'), 'on');
        toggleSubMenu('start_sub', 'on');
    });
    $('#start_sub').bind('mouseout', function() {
        toggleHover(document.getElementById('dont_know_menu'), 'off');
        toggleSubMenu('start_sub', 'off');
    });
    $('#landing_subnav li a').bind('click', function() {
        setLearnLandingFilter(this);
        return false;
    });
    $('#submenu_long_back ul li a').bind('click', function() {
        setLearnLandingFilter(this);
        toggleSubMenu('start_sub', 'off');
        toggleHover(document.getElementById('dont_know_menu'), 'off');
        return false;
    });
    $('#article_topics_subnav li').bind('mouseover', function() {toggleHover(this, 'on')});
    $('#article_topics_subnav li').bind('mouseout', function() {toggleHover(this, 'off')});
}
$(initSubMenuHovers);

/**
 * @author: Jansen Price
 * @date:   2008-10-03
 */
function toggleHover(el, state) {
    if (el != 'undefined') {
        var a   = $($(el).children('a'));
        var img = $(a.children('img'));

        // If the class is set to active, leave it alone.
        if (a.className == 'active') {
            return false;
        }

        if (state == 'on') {
            if (img.attr('src').indexOf('_hover') == -1) {
                img.attr('src', img.attr('src').replace('.jpg', '_hover.jpg'));
            }
        } else {
            if (!a.hasClass('active')) {
                img.attr('src', img.attr('src').replace('_hover.jpg', '.jpg'));
            }
        }
    }
    return false;
}

/**
 * @author: Jansen Price
 * @date:   2008-10-03
 */
function setLearnLandingFilter(filter) {
    filtername = filter.rel;
    //console.log('Filtering set to ' + filtername + '...');
    var LearnLanding = document.getElementById('LearnLanding');
    if (typeof(LearnLanding) != 'undefined') {
        if (typeof(LearnLanding.filterTiles) != 'undefined') {
            LearnLanding.filterTiles(filtername);
        }
    }
    
    // reset the buttons
    $('#landing_subnav li a').removeClass('active');
    $('#landing_subnav li a img').each(function(i) {
        $(this).attr('src', $(this).attr('src').replace('_hover.jpg', '.jpg'));
    });

    // highlight this one and set to active
    img = $(filter).children('img');
    if (img.length != 0) {
        img.attr('src', img.attr('src').replace('.jpg', '_hover.jpg'));
    }
    $(filter).addClass('active');
    return false;
}

function showSubMenu(type){
	switch(type){
		case 'learn_buddy':
	        document.getElementById('bbuddy_sub').style.display = 'block';
			break;
		case 'learn_buddy_off':
	        document.getElementById('bbuddy_sub').style.display = 'none';
			break;
		case 'learn_nutrition':
	        document.getElementById('nutrition_sub').style.display = 'block';
			break;
		case 'learn_nutrition_off':
	        document.getElementById('nutrition_sub').style.display = 'none';
			break;
		case 'learn_product':
			product_tools();
			break;
		case 'learn_product_off':
			product_off();
			break;
		default:
			//alert('invalid');
	}
}

function product_tools(){
	document.getElementById('product_sub').style.display = 'block';
}
function product_off(){
	document.getElementById('product_sub').style.display = 'none';
}
function learn_submenu(){
	document.getElementById('learn_sub').style.display = 'block';
}
function learn_submenu_off(){
	document.getElementById('learn_sub').style.display = 'none';
}
function connect_submenu(){
	document.getElementById('connect_sub').style.display = 'block';
}
function connect_submenu_off(){
	document.getElementById('connect_sub').style.display = 'none';
}
function create_submenu(){
	document.getElementById('create_sub').style.display = 'block';
}
function create_submenu_off(){
	document.getElementById('create_sub').style.display = 'none';
}
function shop_submenu(){
	document.getElementById('shop_sub').style.display = 'block';
}
function shop_submenu_off(){
	document.getElementById('shop_sub').style.display = 'none';
}

function searchSite(search){
	//siteUrl = window.location.href;
	//pos = siteUrl.indexOf('docroot') + 7;
    //redUrl = siteUrl.substring(0,pos);
    redUrl = document.getElementById('base_url').value;
	loc = redUrl + '/search/' + search;
	window.location = loc;
}
function checkSearchKeyPress(e){
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) //IE
		{
			keynum = e.keyCode;
		}
	else if(e.which) //Netscape/Firefox/Opera
		{
			keynum = e.which;
		}
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	if (keynum == 13) {
		searchSite(document.getElementById('searchBox').value);
	}
}
function changeOutsideLinks(El){
	El.onclick = function() { 
        //document.getElementById('alert_msg').style.display='block'; 
        showAlertMessage('alert_msg');
        linkUrl = this.href;  
        return false;
    };
}
function showAlertMessage(el) {
    var objOverlay = document.getElementById('overlay');
    var objAlert = document.getElementById(el);
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();
    // set height of Overlay to take up whole page and show
    objOverlay.onclick = function () {hideAlertMessage(el); return false;};
    objOverlay.style.height = (arrayPageSize[1] + 'px');
    objOverlay.style.display = 'block';

    objAlert.style.display = 'block';
    //center the dialog
    var hgDialogTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objAlert.clientHeight) / 2);
    var hgDialogLeft = (arrayPageSize[0] - 20) / 2 - (objAlert.clientWidth / 2);
    objAlert.style.top = (hgDialogTop < 0) ? "0px" : hgDialogTop + "px";
    objAlert.style.left = (hgDialogLeft < 0) ? "0px" : hgDialogLeft + "px";
	
}
function hideAlertMessage(el) {
    // get objects
    objOverlay = document.getElementById('overlay');
    objAlert = document.getElementById(el);
    objAlert.style.display = 'none';
    objOverlay.style.display = 'none';
}

function openInNewWindow() {
    // Change "_blank" to something like "newWindow" to load all links in the same new window
    //if (checkbox == 1){
    //	createCookie('bypass_alert','yes',30);
    //}
    var newWindow = window.open(linkUrl, '_blank');
    newWindow.focus();
    return false;
}

/* Add to links */
    var addTo = {
        destination: encodeURIComponent('http://www.healthsimple.com/learn/bodybuddy'),
        toggle: function() {
            var networks = document.getElementById('shareNetworks');
            if (networks.style.display=='none') {
                $("#social-bar-top").css('visibility', '');
                $("#social-bar-bottom").css('display', '');
                $("#share-roundy").css('background-color', '#f1e7ce');
            } else {
                $("#social-bar-top").css('visibility', 'hidden');
                $("#social-bar-bottom").css('display', 'none');
                $("#share-roundy").css('background-color', '');
            }
            networks.style.display = networks.style.display=='none' ? '' : 'none';
        }
        ,urls : {
            facebook: {title: 'Facebook', url: 'http://www.facebook.com/share.php?u=%url%'},
            delicious:{title: 'del.ico.us', url: 'http://del.icio.us/post?url=%url%&title=%title%'},
            reddit:   {title: 'reddit', url: 'http://reddit.com/submit?url=%url%&title=%title%'},
            digg:     {title: 'Digg', url: 'http://digg.com/submit?phase=2&url=%url%&title=%title%'},
            myspace:  {title: 'MySpace', url: 'http://www.myspace.com/Modules/PostTo/Pages/?t=%title%&c=%20&u=%url%&l=2'},
            spaces:   {title: 'Live Spaces', url: 'http://spaces.live.com/BlogIt.aspx?Title=%title%&SourceURL=%url%'},
            mixx:     {title: 'Mixx', url: 'http://www.mixx.com/submit?page_url=%url%'},
            stumbleupon:{title:'Stumble Upon', url:'http://www.stumbleupon.com/submit?url=%url%'}
        }
        ,currentUrl: undefined
        ,currentType: undefined
        ,post: function(type) {
            var site = addTo.urls[type];
            var shareBox = document.getElementById('shareBox');
            
            if (site.url.match(/%title%/)) {
                var button = document.getElementById('shareBox_button');
                button.value = 'Share on ' + site.title + '!';
                addTo.currentUrl = site.url;
                addTo.currentType = site.title;
                shareBox.style.display = '';
            } else {
                shareBox.style.display = 'none';
                $.get(baseUrl + '/index/sn/out/' + site.title, function() {
                    window.location = site.url.replace('%url%', addTo.destination);
                });
            }
        }
        ,submit: function() {
            var title = document.getElementById('shareBox_title').value;
            // workaround for myspace bug
            if (addTo.currentType=='MySpace') {
                title = title.replace(/\./, '');
            }
            $.get(baseUrl + '/index/sn/out/' + addTo.currentType, function() {
                window.location = addTo.currentUrl
                .replace('%url%', addTo.destination)
                .replace('%title%', encodeURIComponent( title ));
            });
        }
    };

var showColorIsActive = false;
function showColor(link, img, mouseoutImg) {
    if (showColorIsActive==true) {
        return;
    }
    showColorIsActive = true;

    var gray = document.getElementById('img_' + img + '_gray');
    var color= document.getElementById('img_' + img + '_color');
    
    if (gray.style.display=='none') {
        gray.style.display = '';
        color.style.display = 'none';
        link.style.color = '';
    } else {
        gray.style.display = 'none';
        color.style.display = '';
        link.style.color = '#FC9517';
    }

    showColorIsActive = false;
}

$(initNoteHovers);

function initNoteHovers() {								
    $('.info').hover(
        function() {
        $(this).find(".note").fadeIn('fast');
    },
        function() {
        $(this).find(".note").fadeOut('fast');
    });
}

$(initStarRating);

function initStarRating() {
    $('#star_rating').bind('mouseout', function() {
        $('#one_stars').removeClass('active');
        $('#two_stars').removeClass('active');
        $('#three_stars').removeClass('active');
    });
    $('#star_rating ul li a#one_stars').bind('mouseover', function() {
        $('#one_stars').addClass('active');
        $('#two_stars').removeClass('active');
        $('#three_stars').removeClass('active');
    });
    $('#star_rating ul li a#two_stars').bind('mouseover', function() {
        $('#one_stars').addClass('active');
        $('#two_stars').addClass('active');
        $('#three_stars').removeClass('active');
    });
    $('#star_rating ul li a#three_stars').bind('mouseover', function() {
        $('#one_stars').addClass('active');
        $('#two_stars').addClass('active');
        $('#three_stars').addClass('active');
    });
    $('#star_rating ul li a#one_stars').bind('click', function() {
        return addRating(this, 1);
    });
    $('#star_rating ul li a#two_stars').bind('click', function() {
        return addRating(this, 2);
    });
    $('#star_rating ul li a#three_stars').bind('click', function() {
        return addRating(this, 3);
    });
}

function addRating(ael, rating) {
    $.get(ael.href, function(data) {
        $('#star_rating').html(data);
        return false;
    });
    return false;
}
