/** 
 * Loads in a URL into a specified divName, and applies the function to 
 * all the links inside the pagination div of that page (to preserve the ajax-request) 
 * @param string href The URL of the page to load 
 * @param string divName The name of the DOM-element to load the data into 
 * @return boolean False To prevent the links from doing anything on their own. 
 */ 
function loadPiece(href,divName) {     
    $(divName).load(href, {}, function(){ 
        var divPaginationLinks = divName+" #pagination a, "+divName+" #second_pagination a, "; 
        $(divPaginationLinks).click(function() {      
            var thisHref = $(this).attr("href"); 
            loadPiece(thisHref,divName); 
            return false; 
        });
    }); 
}

function acceptTeam(tournamentteam_id) {
	$("#tournament_ajax_teams").load('/tournaments/accept_team/'+tournamentteam_id);
	return false;
}

function pendTeam(tournamentteam_id) {
	$("#tournament_ajax_teams").load('/tournaments/move_to_pending/'+tournamentteam_id);
	return false;
}
function declineTeam(tournamentteam_id) {
	$("#tournament_ajax_teams").load('/tournaments/remove_team/'+tournamentteam_id);
	return false;
}
function doSearch(string) {
	$("#searchResults").load('/search/doSearch/'+string);
}
function checkUser(string,status) {
	document.getElementById("team_id").value = jQuery.get('/users/exists/'+string).data;
}

$(document).ready(function(){
	
	$("#top_flash_container").slideDown('slow',function() {
		setTimeout('$("#top_flash_container").slideUp("slow");',5000);
	});
	
	$("#search").bind('keypress', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
		
		if(code == 13 && $("#search").val().length > 2) {
			$.post("/search",
			   { search_string: $("#search").val() },
			   	function(data){
					$("#search_result").html(data);
					$("#search_result").css({'display':'block'});
				}
			);
		}
	});
	
	$('a.facebox').facebox();
	
	/*
	$(".logo").click(function() {
		window.location = "http://www.eneric.se";
	});*/
	
	if(window.location.pathname == "/" || window.location.pathname == "/")
		loadPiece("/news/paginator_list","#newsList");
	
	if(window.location.pathname == "/filearchive" || window.location.pathname == "/media/all" || window.location.pathname == "/filearchive/" || window.location.pathname == "/media/all/")
		loadPiece("/media/paginator_list","#mediaList");

	if(window.location.pathname == "/team/media" || window.location.pathname == "/team/media/") {
		loadPiece("/team/image_paginator_list","#imagesList");
		loadPiece("/team/news_paginator_list","#newsList");
		loadPiece("/team/files_paginator_list","#filesList");
	}

	if(window.location.pathname == "/galleries" || window.location.pathname == "/galleries/")
		loadPiece("/galleries/paginator_list","#galleryList");

	if(window.location.pathname == "/archive/teams" || window.location.pathname == "/archive/teams/" || window.location.pathname == "/teams" || window.location.pathname == "/teams/")
		loadPiece("/teams/paginator_list","#teamsList");
	
	if(window.location.pathname == "/archive/users" || window.location.pathname == "/archive/users/")
		loadPiece("/users/paginator_list","#usersList");
		
	if(window.location.pathname == "/polls" || window.location.pathname == "/polls/")
		loadPiece("/polls/paginator_list","#pollsList");
	
	if(window.location.pathname == "/users/accepted" || window.location.pathname == "/users/accepted/")
		loadPiece("/users/paginator_list_admin","#usersList");
	
	if(window.location.pathname == "/users/renew_accepted" || window.location.pathname == "/users/renew_accepted/")
		loadPiece("/users/paginator_list_admin_renew","#usersList");
	
	if(window.location.pathname == "/users/renew_no_action" || window.location.pathname == "/users/renew_no_action/")
		loadPiece("/users/paginator_list_admin_renew_no_action","#usersList");
	
	if(window.location.pathname == "/media/all" || window.location.pathname == "/media/all/")
		loadPiece("/media/paginator_list","#mediaList");
	
	
	if(window.location.pathname == "/tournaments" || window.location.pathname == "/tournaments/")
		loadPiece("/tournaments/paginator_list","#oldTournaments");
	
	if(window.location.pathname.substr(0,11) == "/news/view/" || window.location.pathname.substr(0,12) == "/news/view/") {
		news_id = window.location.pathname.split("/");
		news_id = news_id[news_id.length-1];
		loadPiece("/comments/comments_list/"+news_id,"#commentsList");
	}
    
	if(window.location.pathname.substr(0,16) == "/galleries/view/" || window.location.pathname.substr(0,16) == "/galleries/view/") {
		gallery_id = window.location.pathname.split("/");
		gallery_id = gallery_id[gallery_id.length-1];
		loadPiece("/images/images_list/"+gallery_id,"#imagesList");
	}
	
	$("#user_autocomplete").keyup(function() {
		checkUser(this.value);
	});

	if(window.location.pathname == "/search" || window.location.pathname == "/iol/search") {
		$("#search").keyup(function() {
			if(this.value.length >= 2) {
				document.getElementById("s_message").innerHTML = "";
				doSearch(this.value.replace("%",""));
			} else {
				document.getElementById("s_message").innerHTML = "You need to at least specify minimum two characters for a result";
			}
		});
	}
	
	$(".rowstyle-alt tr:odd").addClass("alt");
});
