/* Page INIT */
$(document).ready(function () {
	/* Fixing the top menu */
	$('#menu > ul > li > a').each(function (i) {
		var title = $(this).attr('alt')
		$(this).html('')
		$(this).css({background: 'url('+tpl_url+'/images/menu_text/'+title+'.png) 50% 40% no-repeat'})
		
		if(i == 2) {
			$(this).animate({width: '64px'}, 1000);
		} else {
			$(this).animate({width: '110px'}, 1000);
		}
	})
	/* END Fixing the top menu */ 
	
	/* Setting up the menu */
    var menu_speed = 200
    var menu = $('#menu')
    menu.children('ul').children('li').hover(function() {
            $(this).children('ul').show(menu_speed)
    })
    menu.children('ul').children('li').mouseleave(function() {
            $(this).children('ul').hide(menu_speed)
    })
    /* End of Setting up the menu */
	
	$('.archives_box .archive_box:first').show(2000)
	$('.rposts_box').show(1000)
	
	/* First Post's Image */
	$('.storycontent').each(function (i) {
		var img = $(this).find('img:first')
		var header = $(this).parent().find('.header div.img').append(img)
	})
	
	/* END First Post's Image */ 
	
	if($('#wp_page_numbers .row_left').size() == 0) {
		$('#wp_page_numbers ul').prepend('<li><a href="" onclick="return false;"><div class="row_left"></div></a></li>')
	}
	if($('#wp_page_numbers .row_right').size() == 0) {
		$('#wp_page_numbers ul').append('<li><a href="" onclick="return false;"><div class="row_right"></div></a></li>')
	}

	$('.opener').click(function () {
		var comment_box_handler = $(this).attr('alt');
		$(comment_box_handler).toggle(1000)

		//return false;
	})
	
	if($('.rposts_box li').size() > 0) {
		$('.rposts_box li').each(function () {
			var img = $(this).find('img:first')
			img_w = 300
			img_h = 90

			if($(img).size() > 0) {
				$(img).attr('src', thumbler + '?src=../../../../' + $(img).attr('src').match(/\/wp-content\/uploads.*/) + '&w='+ img_w +'&h='+ img_h +'&zc=1')
				
				$(this).find("a:first").html(img.css({display: 'block', border: 0}))
			}
		})
	}
	
	// initialize scrollable
	$("div.scrollable").scrollable();
	
	$('#recent_travels a').lightBox({fixedNavigation: true,
			imageLoading: 'http://laracasey.com/images/lightbox-ico-loading.gif',
			imageBtnClose: 'http://laracasey.com/images/lightbox-btn-close.gif',
			imageBtnPrev: 'http://laracasey.com/images/lightbox-btn-prev.gif',
			imageBtnNext: 'http://laracasey.com/images/lightbox-btn-next.gif'});
			
	$('.rposts_box li img').each(function () {
		img_w = 300
		img_h = 90
		
		//var img_src = $(this).attr('src')
		//var img_src = thumbler + '?src=../../../../' + $(this).attr('src').match(/\/wp-content\/uploads.*/) + '&w='+ img_w +'&h='+ img_h +'&zc=1'
		//$(this).attr('src', img_src)
		$(this).css({width: img_w+'px', height: img_h+'px', display: 'block'});
	})
})
/* END Page INIT */

/* Focusing Text Elements */
function textOnFocus(element) {
	if(element.value == element.defaultValue) {
		element.value = "";
	}
}

function textOnBlur(element) {
	if(element.value == "") {
		element.value = element.defaultValue;
	}
}
/* END Focusing Text Elements */

/* Recent Posts */
var rp_page = 1;
function recent_posts_left(the_url) {
	rp_page--
	if(rp_page < 0) {
		rp_page = 0
		
		$("#recent_posts .rposts_box").fadeOut(500, function () {
	    	$("#recent_posts .rposts_box").html('Here are no more posts.');
	    	$(this).fadeIn(500)
		})
	}
	recent_posts(the_url + '?page=' + rp_page)
}

function recent_posts_right(the_url) {
	rp_page++
	recent_posts(the_url + '?page=' + rp_page)
}

function recent_posts(the_url) {
	$.ajax({
		type: "GET",
		url: the_url,
		cache: true,
		success: function(html){
			$("#recent_posts .rposts_box").fadeOut(500, function () {
	    		$("#recent_posts .rposts_box").html(html);
				    	
				if($('.rposts_box li').size() > 0) {
					$('.rposts_box li').each(function () {
						var img = $(this).find('img:first')
			
						if($(img).size() > 0) {
							$(this).prepend(img.css({display: 'block'}))
						}
						
												
						if($('.rposts_box li').size() > 0) {
							$('.rposts_box li').each(function () {
								var img = $(this).find('img:first')
								img_w = 300
								img_h = 90
					
								if($(img).size() > 0) {
									$(img).attr('src', thumbler + '?src=../../../../' + $(img).attr('src').match(/\/wp-content\/uploads.*/) + '&w='+ img_w +'&h='+ img_h +'&zc=1')
									
									$(this).find("a:first").html(img.css({display: 'block', border: 0}))
								}
							})
						}
					})
				}
				
	    		$(this).fadeIn(500)
			})
	  	}
	});
	
}
/* END Recent Posts */

/* Archives Pagination */
var archive_page = 0;

function activate_archive_page(page) {
	
	$('.archives_box .archive_box').each(function (i) {
		if(page == i) {
			$(this).show(1000)
		} else {
			$(this).hide(1000)
		}
	})
}
function previous_archive() {
	archive_page--
	if(archive_page < 0) archive_page = $('.archives_box .archive_box').size() - 1
	
	activate_archive_page(archive_page)
}
function next_archive() {
	archive_page++
	var pages = $('.archives_box .archive_box').size()
	if(archive_page >= pages) archive_page = 0
	
	activate_archive_page(archive_page)
}
/* END Archives Pagination */

/* Events Calendar */
function show_event(title, the_url) {
	$.ajax({
		type: "GET",
		url: the_url,
		cache: true,
		success: function(html){
			var remove_me = '<div class="remover"></div>'
			title = '<h2>' + title + '</h2>'

			$("#shown_events").html('<div class="shown_event">' + title + remove_me + html + "</div>");
			
			$('.remover').click(function () { $(this).parent().remove() })
	  	}
	});
}

function load_the_post (the_url, post) {

		if($('#post-' + post + ' .storycontent:visible').size() > 0) {
			$('#post-' + post + ' .storycontent:visible').slideUp(1000)
			$('#post-' + post + ' .littlestorycontent').slideDown(1000)
			return;
		} else {
			$.ajax({
			type: "GET",
			url: the_url,
			cache: true,
			success: function(html){
				var content_box = '#post-' + post + ' .storycontent'
				
				$(content_box).hide()
				$(content_box).html(html)
				$(content_box + ' img:first').hide()
				
				
				$('#post-' + post + ' .littlestorycontent').slideUp(1000)
				$(content_box).slideDown(1000)
		  	}
			});
		}
}
/* END Events Calendar */