var pg_url;
var new_pg_url;
var pg_arrow_navi = 0;

$(document).ready(function() {
	if (typeof QUICK_JUMP_TO_PAGE !== 'undefined') {
	    if ($('link[rel="last"]').length > 0) {
            var pg_url = $('link[rel="last"]');
		    var pg_type = 'last';
	        var pg_num = parseInt(pg_url.attr('title').replace(/\D+/g,""));
	    } else if ($('link[rel="prev"]').length > 0) {
		    var pg_url = $('link[rel="prev"]');
		    var pg_type = 'prev';
	        var pg_num = parseInt(pg_url.attr('title').replace(/\D+/g,"")) + 1;
	    }

	    if (pg_url !== undefined) {
		    if (!isNaN(pg_num)) {
                if (pg_num >= 6) {
                    var last_pg_url = pg_url.attr('href');
			        $("span.pages").after('<a class="dropdown_button" href="#" title="'+QUICK_JUMP_TO_PAGE.conf.head+'...">&#8618;</a><div class="dropdown"><div class="pointer"><div class="pointer-inner"></div></div><ul class="dropdown-contents"><li>'+QUICK_JUMP_TO_PAGE.conf.head+':</li><li class="page-jump-body"><input name="page-jump-number" min="1" max="'+pg_num+'" title="'+QUICK_JUMP_TO_PAGE.conf.info+'" class="page-jump-number" data-url="'+last_pg_url+'" data-type="'+pg_type+'" data-page="'+pg_num+'" type="number"><input class="page-jump-button" value="'+QUICK_JUMP_TO_PAGE.conf.btn+'" type="button"></li></ul></div>');		
			    }	
		    }
	    }
		
		pg_arrow_navi = QUICK_JUMP_TO_PAGE.conf.navi;
	}
});

$(document).on("click", ".dropdown_button", function(event) {
	var input_box = $(this).siblings(".dropdown");
    if ( $(input_box).css('display') == 'none' ) {
        if ($('.dropdown').is(":visible")) {
			$('.dropdown').hide();
			$('.dropdown_button').removeClass('dropdown-visible');
		}
		$(input_box).animate({ height : "show" }, 100, "swing");
		$(this).addClass('dropdown-visible');	
		$(input_box).find('input.page-jump-number').focus();
    } else {
        $(input_box).animate({ height : "hide" }, 100, "swing");
		$(this).removeClass('dropdown-visible');
    }
	
	event.preventDefault();
});	

$(document).mouseup(function (e) {
    var page_jump_box = $(".dropdown");
    if (page_jump_box.has(e.target).length === 0 && $(page_jump_box).is(":visible") && e.target.className !== 'dropdown_button dropdown-visible'){
        page_jump_box.hide();
		$('.dropdown_button').removeClass('dropdown-visible');
	}
});

$(document).on("click", ".page-jump-number", function(event) {
    $(this).focus();
});

$(document).on("click", ".page-jump-button", function(event) {
	var input_page = parseInt($(this).siblings("input.page-jump-number[type=number]").val());
    if (!isNaN(input_page) && input_page > 0 && input_page <= $(this).siblings('input.page-jump-number').attr('data-page')) {
		jump_to_selected_page($(this).siblings('input.page-jump-number').attr('data-url'), $(this).siblings('input.page-jump-number').attr('data-type'), $(this).siblings('input.page-jump-number').attr('data-page'), input_page);
    } else {
		$(this).siblings("input.page-jump-number[type=number]").val('');  
	}
});

$(document).on("keypress", ".page-jump-number", function(event) {
  if (event.which === 13 || event.keyCode === 13) {
      event.preventDefault();
      var input_page = parseInt($(this).val());
	  if (!isNaN(input_page) && input_page > 0 && input_page <= $(this).attr('data-page')) {
		  jump_to_selected_page($(this).attr('data-url'), $(this).attr('data-type'), $(this).attr('data-page'), input_page);
      } else {
		  $(this).val('');  
	  }
   }
});

$(document).on("keydown", function(e) {
	if (pg_arrow_navi > 0) {
		if (pg_arrow_navi == 1 || pg_arrow_navi == 2 && e.ctrlKey || pg_arrow_navi == 3 && e.ctrlKey && e.shiftKey) {
			if (e.keyCode == 39 && $('link[rel="next"]').length > 0) {
			    window.location.href = $('link[rel="next"]').attr('href');
            } else if (e.keyCode == 37 && $('link[rel="prev"]').length > 0) {
			    window.location.href = $('link[rel="prev"]').attr('href');
	        }
		}
	}
});

function jump_to_selected_page(url, type, last_page, new_page) {
	if (url && type && last_page && new_page) {
        if (type == 'last') {
			var cur_page = last_page;
		} else {
			var cur_page = last_page - 1;
		}
		
	    if (url.indexOf('p='+cur_page) != -1) {
			var new_pg_url = url.replace('p='+cur_page, 'p='+new_page);
        } else if (url.indexOf('p'+cur_page) != -1) {
			var new_pg_url = url.replace('p'+cur_page, 'p'+new_page);
		} else if (url.indexOf('page/'+cur_page) != -1) {
			var new_pg_url = url.replace('page/'+cur_page, 'page/'+new_page);
		}
		
		if (new_pg_url !== undefined) {
			window.location.href = new_pg_url;
		}
	}
	
	return false;
}
