jQuery(document).ready(function($) {

// Add pngfiximg class to required items (IE6)
$('#wide-header img').addClass('pngfix');
$('#wide-intro img').addClass('pngfix');
$('#wide-primary img').addClass('pngfix');
$('#wide-secondary img').addClass('pngfix');
$('#wide-footer img').addClass('pngfix');
$('.mic ul li').addClass('pngfix');
$('.fbuttons li a').addClass('pngfix');
$('.fbuttons li a span').addClass('pngfix');
$('.thumb').addClass('pngfix');
$('.maincarousel a').addClass('pngfix');
$('.maincarousel img').addClass('pngfix');
$('div.posttags').addClass('pngfix');

// min/max list items
$(".sidebar ul ul").css({display: "none"}); // Opera Fix
$('.sidebar li').each(function(){
	if ($(this).children('ul').length > 0) {
	$(this).children('ul').hide();
	$(this).css("background","none");
	$(this).prepend('<a href="#" class="listcontrol"></a>');
	$(this).children('.listcontrol').click(function() {
		$(this).parent().children('ul').slideToggle(500);
	});
	}
});

$('.listcontrol').toggle(
	function() {
	$(this).css({'background-position' : 'bottom'});
	},
	function() {
	$(this).css({'background-position' : 'top'});
	}
);

// ajaxing comment listing
$('.thread-even').each(function(){
if ($(this).children().length > 1) {
	$(this).children('div:first').before('<div class="subcomments"><a href="#subcomments">' + collapseText + '</a></div>');
	$(this).children('div.subcomments').children('a').click(function() {
	$(this).parent().parent().children('.children').slideToggle(500);
	$(this).text($(this).text() == collapseText ? expandText : collapseText);
	});
}
});

$('.thread-odd').each(function(){
if ($(this).children().length > 1) {
	$(this).children('div:first').before('<div class="subcomments"><a href="#subcomments">' + collapseText + '</a></div>');
	$(this).children('div.subcomments').children('a').click(function() {
	$(this).parent().parent().children('.children').slideToggle(500);
	$(this).text($(this).text() == collapseText ? expandText : collapseText);
	});
}
});

// dropdown menu (unlimited sub-levels)
$("#menu ul").css({display: "none"}); // Opera Fix
$("#menu li").hover(function(){
if ($(this).parent().attr("id") == 'menu') {
$(this).removeClass('menu-top');
$(this).addClass('menu-bottom');
}
$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(500);
},function(){
if ($(this).parent().attr("id") == 'menu') {
$(this).addClass('menu-top');
$(this).removeClass('menu-bottom');
}
$(this).find('ul:first').css({visibility: "hidden"});
});

// tabs generator script
function newTabs(contentdiv, tablist) {

$(contentdiv).css({display: "none"}); // Opera Fix
$(contentdiv + ':first').fadeIn(500);
$(tablist + ' li:first').addClass('active');
$(tablist + ' li a').click(function(){
	if ($(this).parent().hasClass('active')) {
	} else {
	var currentTab = $(this).attr('href');
	$(tablist + ' li').removeClass('active');
	$(this).parent().addClass('active');
	$(contentdiv).hide();
	$(currentTab).fadeIn(500);
	return false;
	}
});

}
newTabs('.main-intro', '.hometabs');
newTabs('.content', '.tabs');
newTabs('.newscontent', '.newtabs');

// switch next & prev buttons
$('.fbuttons li a').click(function() {

if ($(this).attr('href') == '#next') {
	var nextDIVlabel = '#' + $('#' + $(this).closest('div.main-intro').attr('id')).next('div').attr('id');
	$('.hometabs li').removeClass('active');
	$('li > a[href$=' + nextDIVlabel + ']').parent().addClass('active');
	$('.main-intro').hide();
	$(nextDIVlabel).fadeIn(500);
}

if ($(this).attr('href') == '#prev') {
	var prevDIVlabel = '#' + $('#' + $(this).closest('div.main-intro').attr('id')).prev('div').attr('id');
	$('.hometabs li').removeClass('active');
	$('li > a[href$=' + prevDIVlabel + ']').parent().addClass('active');
	$('.main-intro').hide();
	$(prevDIVlabel).fadeIn(500);
}

});

// custom carousel
function newCarousel(divclass, nextclass, prevclass) {

	$(divclass).css({display: "none"}); // Opera Fix
	$(divclass + ':first').fadeIn(500);
	$(nextclass + ',' + prevclass).click( function (ev) {
	ev.preventDefault();
	var $visibleItem = $(divclass + ':visible');
	var total =  $(divclass).length;
	var index = $visibleItem.prevAll().length;
	$(this).attr('href') === '#next' ? index++ : index--;
	if (index === -1){
	index = total-1;
	}
	if (index === total){
	index = 0;
	}
	$visibleItem.hide();
	$(divclass + ':eq(' + index + ')').fadeIn(500);
	});
	
}
newCarousel('.showarea', 'a.rightarrow', 'a.leftarrow');
newCarousel('.review', 'a.nextreview', 'a.prevreview');

// Clearing field inputs
function clearInput (cssClass) {
	// Get the input field and assign it to a variable
	var focusField = $(cssClass);
	
	focusField.focus( function(){
		// Assign $(this) to a variable, allowing DOM to do less lookup
		var el = $(this);
		var val = el.val();
		
		// Using $ data object, check to see if value has been 
		// assigned to placeholder data object and if not it should be assigned a value.
		if (!el.data('placeholder')) {
			el.data('placeholder', val);
		}
		
		if(val === el.data('placeholder')) {
			el.val('');
		}
	});
	
	// Execute when user leaves the input field
	focusField.blur( function () {
		// Assign $(this) to a variable, allowing DOM to do less lookup
		var el = $(this);
		if (el.val() === '') {
			el.val(el.data('placeholder') || '');
		}
	});
}
clearInput('.email');

// Equal heights with $
// make sure the $ is pointing to $ and not some other library
(function($){
    // add a new method to $

    $.fn.equalHeight = function() {
       // find the tallest height in the collection
       // that was passed in (.column)
        tallest = 0;
        this.each(function(){
            thisHeight = $(this).height();
            if( thisHeight > tallest)
                tallest = thisHeight;
        });

        // set each items height to use the tallest value found
        this.each(function(){
            $(this).height(tallest);
        });
    }
})($);
$('.equal').equalHeight();

// Gallery zoom-in page processing
$("a.group").fancybox({
	'zoomSpeedIn': 300,
	'zoomSpeedOut': 300
});

});
