// These functions are extensions onto the jQuery object, which we can call like
// standard jQuery methods.
jQuery.fn.extend({
		
	// Attempts to apply the IE6 specific alphaImageLoader on supplied elements
	fix_png: function(new_src) {
		// Return an unmolested object if your browser doesn't suck
		if (!$.browser.msie || parseInt($.browser.version) != 6 ) return $(this);

		// Grab each PNG, apply the appropriate filter on top of the image, and remove 
		// the orgiginal image, replacing it with the supplied transparent GIF
		return $(this).each(function() {
			$(this)
			.css({ filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr('src') + "', sizingMethod='scale');"})
			.attr({ src: new_src }) // Transparent pixel GIF
		})
	},

	// Slaps a basic text string, and faded color, to input text fields, and clears both effects on focus
	search_text: function(default_text, fade_color) {
		if ($('body').hasClass('template_home')) return false // This should never be triggered on the homepage
		if (!fade_color) fade_color = '#666' // Set a default text color
		return $(this).each(function() {

			// Only do stuff if it's an input field and we have text
			if ($(this).is('input') && default_text) {

				// When called, drop our default text in, and set the text color
				$(this)
				.val(default_text)
				.css({color: fade_color})

				// On focus, remove the text and custom text color
				.focus(function() {
					if ($(this).val() == default_text) {
						$(this)
						.val('')
						.css({color: ''})
					}
				})

				// On blur, restore the text if we don't have a valid search entry
				.blur(function() {
					if ($(this).val() == default_text || $(this).val() === '') {
						$(this)
						.val(default_text)
						.css({color: fade_color})
					}
				})
			}
		})
	}
})


// Fetch all MP3 links on the page and override their click behaviour to open and MP3 player window
function audio_links() {
	$('a[href$=.mp3]').each(function() {
		var link = '/audio_player.php?file=' + encodeURIComponent($(this).attr('href')) + '&title=' + encodeURIComponent($('.article_header_text').text());
		$(this).click(function() {
			window.open(link, 'mp3_player', 'width=400,height=125')
			return false;
		})		
	})
}

// Execute this code on DOM ready (like window.load, only earlier)
$(function() {
	// Adding classes to homepage items
	$('.template_home #list_0 .article_content a[href*="/news/"]').parent().parent().addClass("news");
	
	$('.template_home #list_0 .article_content .title').each(function(){
		var title = $(this).text().toLowerCase();
		if(title.indexOf("photo:")>-1 || title.indexOf("photos:")>-1) {
			$(this).parent().removeClass("news").addClass("photos");
		} else if (title.indexOf("video:")>-1 || title.indexOf("videos:")>-1) {
			$(this).parent().removeClass("news").addClass("videos");
		} else if (title.indexOf("audio:")>-1) {
			$(this).parent().removeClass("news").addClass("audio");
		}
	});
	
	$('.template_home #list_0 .article_content a[href*="/editorials/"]').parent().parent().addClass("editorial");

	// Disable Kontera Ads on the important stuff
	$('div.title, h1, div.header,div.article_cover_content,div.cover-thumb-screen').addClass('KonaFilter')

	// Apply our PNG fix for the logo and all PNG images
	$('img[src$=logo.png], input[src$=.png]').fix_png('/images/trans.gif')

    // Drop a default string into our search boxes 
	var default_text = 'Try searching for your article'
	$('#search_0 input.text').search_text(default_text) 

	// Silently ignore all search inputs unless we have a valid search string
	$('#search_0 input.button, #global-search_0 input.button, #result_0 input:submit').each(function() {
		$(this).click(function() {
			// Store a reference to the sibling text box
			var input_box = $(this).parent().find('input:text')

			if (input_box.val() === '' || input_box.val() == default_text) {
				return false
			}
		})
	})

	// Add links to audio player
	audio_links();
})