
// Block framers
if(top!==self) {top.location.replace(self.location.href);};


// Build pretty search dialogue.
function PostSearch()
{

	if($('#txt_search').val() == "" || $('#hdn_empty_search').val() == "1")
	{
		$('#txt_search').addClass('inactive');
		$('#txt_search').val('Search');
		// Flag 'Search' as empty to handle form field caching and prevent 'Search' being treated as user entry.
		$('#hdn_empty_search').val('1');
		$('#btn_reset').removeClass('active');
	}


	$('#frm_search').submit(function() {
		if($('#txt_search').val() != "")
		{
			// / is encoded to %2F but %2F is only allowed in query strings and forbidden elsewhere in URL so Apache will vomit on any %2F unless we double encode by replacing / with %2F
			strSearchUrl = $('#txt_search').val().replace(/\//g,"%2F");
			// Other troublesome characters
			strSearchUrl = strSearchUrl.replace(/\&/g,"%26");
			strSearchUrl = strSearchUrl.replace(/\#/g,"%23");
			
			// Encode then replace %20 with + for the more popular, cleaner approach
			strSearchUrl = encodeURIComponent(strSearchUrl).replace(/%20/g,"+");
	
			location.href='/search/' + strSearchUrl + '/';
			
		}
		
		return false;
	});
	

	$('#btn_reset').click(function() {
		
		$('#txt_search').addClass('inactive');
		$('#txt_search').val('Search');
		$('#hdn_empty_search').val('1');	
		$('#btn_reset').removeClass('active');
		
		return false;
	});
	
	$('#txt_search').focus(function() {
	
		if($('#txt_search').hasClass('inactive'))
		{
			$('#txt_search').removeClass('inactive');
			$('#txt_search').val('');
			$('#hdn_empty_search').val('');		
			$('#btn_reset').addClass('active');	
		}	
	});
	
	$('#txt_search').blur(function() {
	
		if($('#txt_search').val() == "" || $('#hdn_empty_search').val() == "1")
		{
			$('#txt_search').addClass('inactive');
			$('#txt_search').val('Search');
			// Flag 'Search' as empty to handle form field caching and prevent 'Search' being treated as user entry.
			$('#hdn_empty_search').val('1');
			$('#btn_reset').removeClass('active');
		}
	});		

}

// Build pretty code view
function BuildCodeView()
{
	for (var i=0; i < $('pre.code').length; i++)
	{

		strCode = $('pre.code').eq(i).html();

		arrCode = strCode.split('\n');

		strCodeSource = '<ol class="code_list" id="code_list'+i+'">';
				
		for (var j=0; j < arrCode.length; j++)
		{
			arrCode[j] = arrCode[j].replace(/\t/g,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
			
			if(arrCode[j] == "")
			{
				arrCode[j] = "&nbsp;";
			}
			
			if($('pre.code').eq(i).hasClass('php') || $('pre.code').eq(i).hasClass('js'))
			{
				arrCode[j] = arrCode[j].replace(/(\/\/.*)/g,'<span class="comment">$1<\/span>');
			}
			if($('pre.code').eq(i).hasClass('sh'))
			{
				arrCode[j] = arrCode[j].replace(/(#.*)/g,'<span class="comment">$1<\/span>');
			}
			
			strCodeSource += '<li><span>' + arrCode[j] + '<\/span><\/li>';
		}
		
		strCodeSource += '<\/ol>';			
		// Insert before fixes rendering bug in IE 8					
		$('pre.code').eq(i).before(strCodeSource);
		$('pre.code').eq(i).after('<p id="code_switch'+i+'" class="code_switch" onclick="SwitchCodeView('+i+');">Show Raw<\/p>');
		$('pre.code').eq(i).attr('id','code'+i);	
		$('pre.code').eq(i).hide();
		
	}
	
	$('ol li:nth-child(even)').addClass('odd_line');
}

// Switch between raw and pretty code view
function SwitchCodeView(intCode)
{	
	$('#code'+intCode).toggle();
	$('#code_list'+intCode).toggle();
	
	if($('#code'+intCode).css("display") == "none")
	{
		$('#code_switch'+intCode).html('Show Raw');
	}
	else
	{
		$('#code_switch'+intCode).html('Show Tidy');
	}
}

// Show comments if URL contains comment hash, enable toggle comment click
function ShowComments()
{
	
	$(".post_comments").bind("click",function(){
		if($('#post_comments').is(':hidden'))
		{
			// Fix rendering problems < IE8
			if(jQuery.browser.version < 8 && jQuery.browser.msie)
			{
				$('#post_comments').show();
			}
			else
			{
				$('#post_comments').slideDown('fast');
			}	
		}
		else
		{
			if(jQuery.browser.version < 8 && jQuery.browser.msie)
			{
				$('#post_comments').hide();
			}
			else
			{
				$('#post_comments').slideUp('fast');
			}	
		}			
	});


	if(location.hash.indexOf('#comment_') != -1 || location.hash.indexOf('#post_comments') != -1)
	{
		$('#post_comments').show();
	}	
}

// Build navigation

function Navigation()
{
	// Preload	
	objSliderBackground = new Image();
	objSliderBackground.src = '/media/navigation-page-title.png';

	// Stick link titles in span to fix IE headache
	for (var i=0; i < $('.navigation_page a').length; i++)
	{
		$('.navigation_page a').eq(i).after('<span>' + $('.navigation_page a').eq(i).attr('title') + '</span>');
		$('.navigation_page a').eq(i).attr('title','');
	}
	
	$(".navigation_page a").bind("mouseover",function(){

		$('#navigation_page_title').stop(true,true);
		$('#navigation_page_title p').text($(this).next().text());
		
		$('#navigation_page_title').css('top',$(this).parent().css('top'));
		$('#navigation_page_title').show('slide',{direction:'left'},300);
	
	}).bind("mouseleave",function(){
	
		$('#navigation_page_title').stop(true,true);		
		$('#navigation_page_title').hide('slide',{direction:'left'},300);
	});
	
	
}

// Set Reply

function Reply(intCommentId)
{
	$('#hdn_parent_id').val(intCommentId);
	$('#post_response h3').text('In reply to ' + $('#comment_' + intCommentId + ' .author_name').text());
	$('#cancel_reply').show();
	
	
	// Insert after latest reply or comment		
	if($('.reply_' + intCommentId).length > 0)
	{
		$('#post_response').insertAfter('.reply_' + intCommentId + ':last');
	}
	else
	{
		$('#post_response').insertAfter('#comment_' + intCommentId);
	}

	
}


// Clear reply response
function CancelReply()
{
	$('#hdn_parent_id').val('');
	$('#post_response h3').text('Elucidate us with your wisdom');
	$('#cancel_reply').hide();
	
	$('#post_response').insertAfter('#post_comments ol');
	
	
}


gblnAjaxActive = false;

//Post comment
function PostComment()
{

	// Preload
	objStatusImg = new Image();
	objStatusImg.src = '/media/loading.gif';	

	$('#frm_comment').submit(function() {
	
		$blnSubmitForm = true;
	
		for (var i=0; i < $('input.required').length; i++)
		{
			if($('input.required').eq(i).val() == '')
			{	
				$('input.required').eq(i).addClass('highlight_required');	
				$blnSubmitForm = false;
			}

		}
		
		if($('#txa_comment').val() == '')
		{
			$('#txa_comment').addClass('highlight_required');
			$blnSubmitForm = false;
		}
		
		// Send data if not empty and ajax not processing
		if($blnSubmitForm && gblnAjaxActive == false)
		{
			
			$('#frm_comment').append('<div id="status"><img src="/media/loading.gif" width="32" height="32"><p>Posting<\/p><\/div>');
			
			gblnAjaxActive = true;
			
			$.post("/includes/add_comment.php", $('#frm_comment').serialize(),
				function(data){
					if(data)
					{
						// Check for PHP errors
						if(data !== "Error")
						{	
							// Reply
							if($('#hdn_parent_id').val() !== "")
							{
								// Other replies
								if($('.reply_' + $('#hdn_parent_id').val()).length > 0)
								{
									$('.reply_' + $('#hdn_parent_id').val() + ':last').after(data);
								}
								else
								{
									$('#comment_' + $('#hdn_parent_id').val()).after(data);
								}
								
								CancelReply();
							}
							else
							{
								$('#post_comments li:last').after(data);
							}
							
							$('#txa_comment').val('');
						}
					}
					
					gblnAjaxActive = false;
					
					$('#status').remove();
					
			});
		}
		
		return false;
	});
	

	$("input.required").bind("focus",function(){
		$(this).removeClass('highlight_required');
	});
	
	$("#txa_comment").bind("focus",function(){
		$(this).removeClass('highlight_required');
	});	

}

garrContact = new Array();

garrContact[0] = "mai";
garrContact[1] = "lto:";
garrContact[2] = "k";
garrContact[3] = "yle";
garrContact[4] = "@";
garrContact[5] = "p";
garrContact[6] = "uku";
garrContact[7] = "p";
garrContact[8] = "i.c";
garrContact[9] = "om";

function LoadContact()
{
	$(".email_contact").bind("click",function(){
		strLocation = "";
	
		for (var i=0; i < garrContact.length; i++)
		{
			strLocation += garrContact[i];		
		}
	
		location.href = strLocation;		
	});
}

function GetTweet() {

	$.getJSON("http://twitter.com/statuses/user_timeline/pukupi.json?count=1&callback=?",
		function(json){
			if(json.length > 0)
			{
    			strTweet = json[0].text;
    			//strTweet = strTweet.replace(/\b((https?|ftp):\/\/\S+)/gi,'<a href="$1" rel="nofollow">$1</a>');
    		
    			$("#tweet p").html('<a href="http://twitter.com/pukupi">' + strTweet + '</a>');
    			$("#tweet").show();
    		}	
  	});
  	
}

function FixIe()
{
	if(jQuery.browser.version < 7 && jQuery.browser.msie)
	{
		
		for (var i=0; i < $('#navigation_body img').length; i++)
		{
			$('#navigation_body img').eq(i).attr('src', $('#navigation_body img').eq(i).attr('src').replace('.png','.gif'));
		}
		
		$('#navigation').show();
	}
	else
	{
		$('#navigation').show();
	}
}

$(document).ready(function(){	
	PostSearch();
	BuildCodeView();
	PostComment();
	ShowComments();	
	LoadContact();	
});

$(window).load(function(){
	Navigation();
	FixIe();
	GetTweet();		
});
