$(document).ready(function() {
	$("#youtube .video-item").hover(function() {
		$(this).children("a.thumb").children(".play_btn").show();
	}, function() {
		$(this).children("a.thumb").children(".play_btn").hide();
	});
	
	$("#youtube .video-item").click(function() {
		var id = $(this).attr("id");
		$.ajax({
			type: "GET",
			url: "ajax/loadYoutube.cfm",
			data: "id="+id,
			success: function(data) {
				$("#youtube_player_holder").html(data);
			}
		});
	});
	
});

/**
 * autoload()
 * - object which is used to load google site search results 
 *   by ajax when user scrolls the searchresults page.
 */
autoload = function() {
	this.inProgress = false;
	this.ajaxLoader = "";
	this.holder = "";
	this.page = 1;
	this.itemsPerPage = 10;
	this.query = "";
	this.lastRow = 0;
	this.serverPostPage = "";
	this.noMoreResultsMessage = "<p>Inga fler sökresultat.</p>";
	
	// Hides pagination
	this.hidePagination = function(div) {
		$(div).hide();
	}
	
	// Sets number of item per page.
	this.setItemsPerPage = function(nr) {
		this.itemsPerPage = nr;
	}
	
	// Sets ajaxLoader icon.
	this.setAjaxLoader = function(div) {
		this.ajaxLoader = div;
	}
	
	// Sets holder for new content to load into.
	this.setHolder = function(div) {
		this.holder = div;
	}
	
	// Sets target page for ajax posts.
	this.setServerPostPage = function(url) {
		this.serverPostPage = url;
	}
	
	// Sets "no more results" message.
	this.setNoMoreResultsMessage = function(text) {
		this.noMoreResults = text;
	}
	
	// Sets query.
	this.setQuery = function(query) {
		this.query = query;
	}
	
	// Sets lastrow.
	this.setLastRow = function(lastRow) {
		this.lastRow = lastRow;
	}
	
	// Shows ajaxLoader icon.
	this.showAjaxLoader = function() {
		if(this.ajaxLoader != "") { $(this.ajaxLoader).show(); }
	}
	
	// Hides ajaxLoader icon.
	this.hideAjaxLoader = function() {
		if(this.ajaxLoader != "") { $(this.ajaxLoader).hide(); }
	}
	
	// Initiates autoload function.
	this.init = function() {
		autoload = this;
		$(window).scroll(function() {
			if (!autoload.inProgress  && $(window).scrollTop() > $(document).height() - $(window).height() - 200) {
				autoload.inProgress = true;
				autoload.showAjaxLoader();
				autoload.page++;
				$.ajax({
					type: "GET",
					url: autoload.serverPostPage,
					cache: false,
					contentType: "application/x-www-form-urlencoded;charset=utf-8",
					data: "perpage="+autoload.itemsPerPage+"&page="+autoload.page+"&query="+autoload.query+"&lastRow="+autoload.lastRow,
					success: function(data) {
						$(autoload.holder).append(data);
						if($(autoload.holder).children("#counter").html() == autoload.itemsPerPage) {
							autoload.inProgress = false;
							autoload.hideAjaxLoader();
						} else {
							$(autoload.ajaxLoader).html(autoload.noMoreResultsMessage);
						}
						$(autoload.holder).children("#counter").remove();
					}
				});
			}
		});
	}
}
