
$(document).ready(function() {

	/*** fix all PNGs in IE6 **/
	if(IE6) {
		$("img[src$='.png']").each(function() {
			var src = $(this).attr('src');
			$(this).attr("src", "http://www.dynamit.us/img/spacer.gif").attr("style", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "');");
		});

		$('h1 + p, h2 + p, h3 + p, h4 + p, h5 + p, h6 + p').css('margin-top', '0');
	}

	/** homepage top stories **/
	$('.top_story_link').mouseover(function() {
		$('.top_story_link').removeClass('active');
		$('.top_story_post').hide();

		var current_id = $(this).attr('id');
		post_id = str_replace('_link', '', current_id);

		$(this).addClass('active');
		$('#' + post_id).show();
	});

	/** homepage feature buttons : rollover effect **/
	$('.buttons').hover(function() {
		$(this).addClass('buttons-over');
		$(this).children('.buttons-cap').addClass('buttons-cap-over');
	}, function() {
		$(this).removeClass('buttons-over');
		$(this).children('.buttons-cap').removeClass('buttons-cap-over');
	}).click(function() {
		var my_href = $(this).find('a').attr('href');
		location.href = my_href;
	});


	/** homepage, slideshow. **/

	$('#slideshow').cycle({
		prev:   '#slideshow-prev',
		next:   '#slideshow-next',
		timeout: 0,
		before: function() {
			$('#slideshow-credit').html(this.title);
			$('#slideshow-caption').html(this.alt);
		}
	});


        $('#al').fadeTo('fast', .25);

	/** Story actions **
	$('.btnSend').mouseover(function() {
		return addthis_open(this, 'email', location.href, $('title').html());
	}).mouseout(function() {
		addthis_close();
	}).click(function() {
		return addthis_sendto();
	});
	***/

	$('.btnSend').click( function() {
		return addthis_open(this, 'email', location.href, $('title').html());
        	});


	/** font-size selector **/
	$('.story-font a.small').click(function() {
		$.cookie('mgh_font_size', 'small');
		$('.story-body').removeClass('medium').removeClass('large').addClass('small');
	});

	$('.story-font a.medium').click(function() {
		$.cookie('mgh_font_size', 'medium');
		$('.story-body').removeClass('small').removeClass('large').addClass('medium');
	});

	$('.story-font a.large').click(function() {
		$.cookie('mgh_font_size', 'large');
		$('.story-body').removeClass('medium').removeClass('small').addClass('large');
	});

	var fontSize = $.cookie('mgh_font_size');
	if(fontSize && fontSize.length) $('.story-body').addClass(fontSize);

	/** show image titles **/
	$('.wp-caption').each(function() {
		var title = $(this).find('img').attr('title');
		if(title.length) {
			$(this).find('p.wp-caption-text').prepend('<small>' + title + '</small><br />');
		}
	});
});

var strPortfolioId, intNumPortfolioItems, intNumVisibleItems, intPortfolioMargin, intShift;
function portfolioScroll (strDirection) {
    var intPortfolioWidth;
    intPortfolioWidth = intShift * intNumPortfolioItems;

    if ((strDirection == 'left') && (intPortfolioMargin != 0)) {
        intPortfolioMargin += intShift;
        $('#ar').fadeTo('fast', 1);
        if (intPortfolioMargin == 0) {
            $('#al').fadeTo('fast', .25);
        } else {
          //  alert(intPortfolioMargin);
        }
    }
    if ((strDirection == 'right') && (intPortfolioMargin > -1*((intNumPortfolioItems - intNumVisibleItems) * intShift))) {
        intPortfolioMargin -= intShift;
        $('#al').fadeTo('fast', 1);
        if (intPortfolioMargin == -1*((intNumPortfolioItems - intNumVisibleItems) * intShift)) $('#ar').fadeTo('fast', .25);
    }
    $(strPortfolioId).animate({marginLeft: intPortfolioMargin+'px'}, 'slow', 'swing');
}

function mgh_check_quiz(f) {
	var correct = f.correct.value;
	var choices = f.choices.value.split(',');
	var myanswer = getCheckedValue(f.quiz_answer);

	for(var j = 0; j < choices.length; j++) {
		oid = '#choice_' + choices[j];
		if(choices[j] == correct)
			$(oid).css({ color:'#3b6318',fontWeight:'bold' })
		else if(choices[j] != myanswer)
			$(oid).html('&nbsp;');
	}

	if(myanswer == correct) {
		$('#choice-response').html('Correct!').css({ color:'#3b6318',fontWeight:'bold',fontSize:'14px' });
	} else {
		$('#choice-response').html('Incorrect.').css({ color:'#cc0000',fontWeight:'bold',fontSize:'14px' });
	}

	$('#quiz-question').hide();
	$('#quiz-answer').show();

	return false;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}