/**
 * 
 */
//window.console.log("test");

var topM = {};
// offset
topM['alpha'] = -1;
topM['imdb'] = -1;
topM['viewed'] = -1;
topM['latest'] = -1;

topM['genres'] = -1;

topM['genre'] = -1;
topM['genreId'] = 0;

topM['genresContent'] = '';
topM['genreContent'] = '';
//$(function() {

	
	$("#m_bygenre").hover(function() {
		if (topM['genres']==-1) {
			// initial data load
			$.ajax({
				type : "GET",
				url : "/base/movie/ajaxLoadGenres",
				data : "",
				success : function(msg) {
					topM['genresContent'] = msg;
					$(".m_bygenre_content").html(msg);
					// alert( "Data Saved: " + msg );
				}
			});
			topM['genres'] = 0;
		}
	}, function() {
	});
	
	
	$("#m_byalpha").hover(function() {
		if (topM['alpha'] == -1) {
			// initial data load
			$.ajax({
				type : "GET",
				url : "/base/movie/ajaxLoadMoreMovies",
				data : "offset=0&sort=alpha",
				success : function(msg) {
					$(".m_byalpha_content").html(msg);
					// alert( "Data Saved: " + msg );
				}
			});
			topM['alpha'] = 0;
		}
	}, function() {
	});
	
	$("#m_byimdb").hover(function() {
		if (topM['imdb'] == -1) {
			// initial data load
			$.ajax({
				type : "GET",
				url : "/base/movie/ajaxLoadMoreMovies",
				data : "offset=0&sort=imdb",
				success : function(msg) {
					$(".m_byimdb_content").html(msg);
					// alert( "Data Saved: " + msg );
				}
			});
			topM['imdb'] = 0;
		}
	}, function() {
	});	
	
	//m_byviewed_content
	$("#m_bylatest").hover(function() {
		if (topM['latest'] == -1) {
			// initial data load
			$.ajax({
				type : "GET",
				url : "/base/movie/ajaxLoadMoreMovies",
				data : "offset=0&sort=latest",
				success : function(msg) {
					$(".m_bylatest_content").html(msg);
					// alert( "Data Saved: " + msg );
				}
			});
			topM['latest'] = 0;
		}
	}, function() {
	});	
	
	$("#m_byviewed").hover(function() {
		if (topM['viewed'] == -1) {
			// initial data load
			$.ajax({
				type : "GET",
				url : "/base/movie/ajaxLoadMoreMovies",
				data : "offset=0&sort=viewed",
				success : function(msg) {
					$(".m_byviewed_content").html(msg);
					// alert( "Data Saved: " + msg );
				}
			});
			topM['viewed'] = 0;
		}
	}, function() {
	});		
	
//});

topM['prevPage'] = function(sort) {
	if (topM[sort] == 0)
		return;
	var newOffset = topM[sort] - 1;
	$.ajax({
		type : "GET",
		url : "/base/movie/ajaxLoadMoreMovies",
		data : "offset=" + newOffset + "&sort=" + sort,
		success : function(msg) {
			$(".m_by" + sort + "_content").html(msg);

			topM[sort] = newOffset;
			// alert( "Data Saved: " + msg );
		}
	});

}

topM['nextPage'] = function(sort) {
	var newOffset = topM[sort] + 1;
	$.ajax({
		type : "GET",
		url : "/base/movie/ajaxLoadMoreMovies",
		data : "offset=" + newOffset + "&sort=" + sort,
		success : function(msg) {
			if (msg != 'empty') {
				$(".m_by" + sort + "_content").html(msg);

				topM[sort] = newOffset;
			} else {
				$(".topm_next_" + sort).remove();
			}
			// alert( "Data Saved: " + msg );
		}
	});
}

//load genre item

topM.prevGenres = function(){
	if(topM['genres']==0) return;
	var newOffset = topM['genres'] -1;
	$.ajax({
		type : "GET",
		url : "/base/movie/ajaxLoadGenres",
		data : "offset="+newOffset,
		success : function(msg) {
			topM['genresContent'] = msg;
			$(".m_bygenre_content").html(msg);
			topM['genres'] = newOffset;	
			// alert( "Data Saved: " + msg );
		}
	});
	
}

topM.nextGenres = function(){
	var newOffset = topM['genres'] +1;
	$.ajax({
		type : "GET",
		url : "/base/movie/ajaxLoadGenres",
		data : "offset="+newOffset,
		success : function(msg) {
			topM['genresContent'] = msg;
			$(".m_bygenre_content").html(msg);
			topM['genres'] = newOffset;	
			// alert( "Data Saved: " + msg );
		}
	});	
}

topM.loadGenre = function(genre_id){
	
	
//	$(".m_bygenre_content").hide();
//	$(".m_genre_content").show();
	topM['genreId'] = genre_id;
	topM['genre'] = 1;
	topM.prevnextGenrePage(0);
	
//	alert("+"+genre_id);
	
}
topM.loadGenres = function(){
	
//	$(".m_genre_content").hide();
//	$(".m_bygenre_content").show();
	$(".m_bygenre_content").html(topM['genresContent']); 
	
}


topM.prevnextGenrePage = function(dir){ // 0 - back , 1 - next

	if(dir){
		var newOffset = topM['genre'] + 1;
	}else{
		if(topM['genre']==0) return;
		var newOffset = topM['genre'] - 1;
	}
	
	$.ajax({
		type : "GET",
		url : "/base/movie/ajaxLoadFromGenre",
		data : "offset="+newOffset+"&genre="+topM['genreId'],
		success : function(msg) {
			if(msg=='empty') return;
			topM['genreContent'] = msg;
			$(".m_bygenre_content").html(msg);
		//	$(".m_genre_content").html(msg);
			topM['genre'] = newOffset;	
			// alert( "Data Saved: " + msg );
		}
	});	
	
}


