// JavaScript Document

$(function() {
	$("a.iframe").fancybox();
});

$(document).ready(function() {
  $('div.fullList> div').hide();
  $('div.fullList> p').click(function() {
 $(this).next('div').slideToggle('slow')
 .siblings('div:visible').slideUp('slow');
  });
});

$(document).ready(function() {
  $('div.getWidget> div').hide();
  $('div.getWidget> h4').click(function() {
 $(this).next('div').slideToggle('slow')
 .siblings('div:visible').slideUp('slow');
  });
});

var pageSize = 4;
var totalPages = 0;

$(function() {
    $(".authorNames").each(function() {
        var lastChild = $(this).children().eq($(this).children().length - 1);
        lastChild.text(lastChild.text().replace(", ", ""));
    });


    $(".pageMe").each(function() {
        var children = $(this).children();
        var pageSize = 4;
        var pages = Math.ceil(children.length / pageSize);
        totalPages = pages;

        if (pages > 1) {
            $(".pagination").html("<a class=\"pageLink pageFirst\" href=\"#\">First</a><a class=\"pageLink pagePrev\" href=\"#\">&lt;</a>");
            for (var i = 1; i <= pages; i++) {
                $(".pagination").html($(".pagination").html() + "<a href=\"#\" class=\"pageLink pageNumber page"+i+"\">" + i + "</a>");
            }
            $(".pagination").html($(".pagination").html() + "<a class=\"pageLink pageNext\" href=\"#\">&gt;</a><a class=\"pageLink pageLast\" href=\"#\">Last</a>");

            doPage(1);
        }
    });

    $(".pageLink").click(function() {
        var page = 0;
        if ($(this).hasClass("pagePrev")) {
            page = parseInt($(".activePage:first").text());
            page--;
        } else if ($(this).hasClass("pageNext")) {
            page = parseInt($(".activePage:first").text());
            page++;
        } else if ($(this).hasClass("pageFirst")) {
            page = 1;
        } else if ($(this).hasClass("pageLast")) {
            page = totalPages;
        } else {
            page = parseInt($(this).text());
        }

        doPage(page);

        return false;
    });
});

function doPage(page) {
    $(".pageLink").removeClass("activePage");
    $(".pageLink").filter(function() { return $(this).text() == String(page); }).addClass("activePage");
    var children = $(".pageMe").children();
    children.hide();
    var start = (page - 1) * pageSize;
    var end = start + pageSize - 1;

    if (end > (children.length - 1)) { end = children.length - 1; }

    for (var i = start; i <= end; i++) { children.eq(i).show(); }

    $(".pagePrev, .pageNext, .pageFirst, .pageLast").show();
	var pageplus = page + 1;
	var pageplusplus = page + 2;
	var pageplus2= page + 2;
	var pageplus3 = page + 3;
	var pageminus = page - 1;
	var pageminus2 = page - 2;
    if (page == 1) {
        $(".pagePrev, .pageFirst").hide();
		$(".pageNumber").hide();
		$(".page"+page).show();
		$(".page"+pageplus).show();
		$(".page"+pageplus2).show();
		$(".page"+pageplus3).show();
    }
    if (page == totalPages) {
        $(".pageNext, .pageLast").hide();
		$(".pageNumber").hide();
		$(".page"+page).show();
		$(".page"+pageminus).show();
		$(".page"+pageminus2).show();
    }else {
		$(".pageNumber").hide();
		$(".page"+pageplus).show();
		$(".page"+pageplus2).show();
		$(".page"+pageplus3).show();
		$(".page"+page).show();
		$(".page"+pageminus).show();
		$(".page"+pageminus2).show();
	}
	window.scrollTo(0,0);
}


function pageselectCallback(page_index, jq){
	var new_content = jQuery('#hiddenresult div.result:eq('+page_index+')').clone();
	$('#Searchresult').empty().append(new_content);
	return false;
	}
           
//function initPagination() {
//	var num_entries = jQuery('#hiddenresult div.result').length;
//	$("#Pagination").pagination(num_entries, {
//	callback: pageselectCallback,
//	items_per_page:1 // Show only one item per page
//	});
//	}
//            
//$(document).ready(function(){      
//	initPagination();
//	});




//function limitChars(textid, limit, infodiv)
//{
//	var text = $('#'+textid).val();	
//	var textlength = text.length;
//	if(textlength > limit)
//	{
//		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
//		$('#'+textid).val(text.substr(0,limit));
//		return false;
//	}
//	else
//	{
//		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
//		return true;
//	}
//}
//
//$(function(){
// 	$('#textLimit').keyup(function(){
// 		limitChars('textLimit', 280, 'charlimitinfo');
// 	})
//});


$(document).ready(function() {
            $('#ta').keyup(function() {
                var len = this.value.length;
                if (len >= 150) {
                    this.value = this.value.substring(0, 250);
                }
                $('#charLeft').text(250 - len);
            });
        });




