/*Bootstrap homepage*/
$(document).ready(function() {	

	$("#iz_video_up").click(function(){up_video();});
	$("#iz_video_down").click(function(){down_video();}).css("opacity","0.3");;
	
	$("#iz_gallery_up").click(function(){up_album();});
	$("#iz_gallery_down").click(function(){down_album();}).css("opacity","0.3");;
	
	$("#iz_notif_up").click(function(){up_notifiche();});
	$("#iz_notif_down").click(function(){down_notifiche();}).css("opacity","0.3");;		
});


/*ALBUM**********************************************************************************/

/*Carica la lista di Album di un profilo nel album viewer della gallery*/
function iz_get_PopularGallery(){	
	var albumPoint = $("#iz_gallery_album");
	albumPoint.fadeTo("fast",0.1);
	var offset = $("#iz_gallery_offset").text();
	var step = $("#iz_gallery_step").text();	
	var uid = 1;
	var cl ="";
	$.post(base_url+"index.php/gallery/getPopularAlbumsTotal", {uid: uid},
			function(data){
				$("#iz_gallery_total").text(data.total);
			},"json");

	$.post(base_url+"index.php/gallery/getPopularAlbums", {uid: uid,offset:offset,step:step},
			function(data){	
			if(data.length > 0){
				albumPoint.empty();
				$.each(data,function(i,item){

					var str_template = parseInt(item.template);
					
					switch(str_template){	
						case 4:
							cl = "smooth";
						break;
						
						case 1:
							cl = "curl";
						break;	
						
						case 2:
							cl = "volume";
						break;	
							
						case 3:
							cl = "shape";
						break;										
					}						
					
					var contDiv = $(document.createElement("div"));
					contDiv.attr("class","album_container");
					
					var aImg = $(document.createElement("a"));
					aImg.attr("class","gallery_thumb");
					aImg.attr("href",base_url+"index.php/space/viewer/"+item.username);
					
					var sideDiv = $(document.createElement("div"));
					sideDiv.attr("class","img_side");
					
					var descrDiv = $(document.createElement("div"));
					descrDiv.attr("class","description_gallery");
					
					var titP = $(document.createElement("p"));
					titP.attr('id',item.id);
					titP.attr('class','video_title');
					titP.html(item.name);						
					
					var descrP = $(document.createElement("p"));
					descrP.attr("class","text_video");
					descrP.html(item.description);
					
					var spaceP = $(document.createElement("p"));
					spaceP.attr("class","space_left");
					spaceP.html("SPACE");
										
					var voteP = $(document.createElement("p"));
					voteP.attr("class","voti");
					
					var voteSpan = $(document.createElement("span"));
					voteSpan.text("VOTO");
					if(item.pct != ""){					
						voteP.append(item.pct+"%");
					}
					else {
						voteP.append("0%");
					}	
														
					var userA = $(document.createElement("a"));
					userA.attr("href",base_url+"index.php/space/viewer/"+item.username);
					
					var userP = $(document.createElement("p"));
					userP.attr("class",cl);					
					userP.html(item.username);
					
					userA.append(userP);						
										
					var fixDiv = $(document.createElement("div"));
					fixDiv.attr("class","fix");
										
					var pointDiv = $(document.createElement("div"));
					pointDiv.attr("class","point_right");
					
					//Per ogni item crea un'immagine
					if(item.filepath != null){
						
						//Per ogni item creo un'immagine
					    var img = new Image();
					    $(img).load(function () {
					        $(this).hide();
					        aImg.append(this);
					        $(this).fadeIn();
					    }).error(function () {
					        
					    }).attr({
					            src: base_url+item.filepath
					     }).addClass("img_gallery")
					     .bind('click',function(){izg_view_album(item.id)});
				    }

					contDiv.append(aImg);
					contDiv.append(sideDiv);
						descrDiv.append(titP);
						descrDiv.append(descrP);
						descrDiv.append(spaceP);
						descrDiv.append(userA);
						descrDiv.append(voteP);
					contDiv.append(descrDiv);
					contDiv.append(fixDiv);
					contDiv.append(pointDiv);
					albumPoint.append(contDiv);
					
				});					
			}
			albumPoint.fadeTo("fast",1);
			},"json");		
}

/*Mostra altri album nell'album viewer (avanti)*/
function up_album(){
	var step = parseInt($("#iz_gallery_step").text());
	var offset = parseInt($("#iz_gallery_offset").text());
	var total = parseInt($("#iz_gallery_total").text());
	var n_off = offset +step;
	
	if(offset+step < total){
		
		if(n_off+step >= total){	
			$("#iz_gallery_up").fadeTo("fast","0.3");
		}
		$("#iz_gallery_down").fadeTo("fast","1.0");

		$("#iz_gallery_offset").text(step+offset);
		
		iz_get_PopularGallery();
	}
}

/*Mostra altri album nell'album viewer (indietro)*/
function down_album(){	
	var step = parseInt($("#iz_gallery_step").text());
	var offset = parseInt($("#iz_gallery_offset").text());
	var n_off = offset - step;
	
	if(offset >= step){
		if(n_off-step < 0){	
			$("#iz_gallery_down").fadeTo("fast","0.3");
		}	
		$("#iz_gallery_up").fadeTo("fast","1.0");
		
		$("#iz_gallery_offset").text(offset-step);
		iz_get_PopularGallery();
	}
}


/*VIDEO************************************************************************/


/*Carica la lista di video di un profilo*/
function iz_get_PopularVideos(){	
	var videoPoint = $("#iz_video");	
	videoPoint.fadeTo("fast",0.1);
	var offset = $("#iz_video_offset").text();
	var step = $("#iz_video_step").text();
	var uid = 1;
	var cl = "";
	$.post(base_url+"index.php/video/getPopularVideosTotal", {uid: uid},
			function(data){
				$("#iz_video_total").text(data.total);
			},"json");
	

	$.post(base_url+"index.php/video/getPopularVideos", {uid: uid,offset:offset,step:step},
			function(data){	
		
			if(data.length > 0){
				videoPoint.empty();
				$.each(data,function(i,item){
					var str_template = parseInt(item.template);
	
					switch(str_template){	
						case 4:
							cl = "smooth";
						break;
						
						case 1:
							cl = "curl";
						break;	
						
						case 2:
							cl = "volume";
						break;	
							
						case 3:
							cl = "shape";
						break;										
					}	
					
					var contDiv = $(document.createElement("div"));
					contDiv.attr("class","video_container");
					
					var aImg = $(document.createElement("a"));
					aImg.attr("href",base_url+"index.php/space/viewer/"+item.username);
					
					var descrDiv = $(document.createElement("div"));
					descrDiv.attr("class","description");
					
					var titP = $(document.createElement("p"));
					titP.attr('class','video_title');
					titP.html(item.title);						
					
					var descrP = $(document.createElement("p"));
					descrP.attr("class","text_video");
					descrP.html(item.description);
					
					var spaceP = $(document.createElement("p"));
					spaceP.attr("class","space_left");
					spaceP.html("SPACE");
					
					var voteP = $(document.createElement("p"));
					voteP.attr("class","voti");
					
					var voteSpan = $(document.createElement("span"));
					voteSpan.html("VOTO");
					
					if(item.pct != ""){					
						voteP.append(item.pct+"%");
					}
					else {
						voteP.append("0%");
					}													
		
					var userA = $(document.createElement("a"));
					userA.attr("href",base_url+"index.php/space/viewer/"+item.username);
					
					var userP = $(document.createElement("p"));
					userP.attr("class",cl);					
					userP.html(item.username);
					
					userA.append(userP);	
															
					var fixDiv = $(document.createElement("div"));
					fixDiv.attr("class","fix");
										
					var pointDiv = $(document.createElement("div"));
					pointDiv.attr("class","point_right");
						

					//Per ogni item crea un'immagine
					if(item.thumb_path != null){
						//Per ogni item creo un'immagine
					    var img = new Image();
					    $(img).load(function () {
					        $(this).hide();
					        aImg.append(this);
					        $(this).fadeIn();
					    }).error(function () {
					       
					    }).attr({
					             src: item.thumb_path
					     }).addClass("img_video");
				    }

					contDiv.append(aImg);
					descrDiv.append(titP);
					descrDiv.append(descrP);
					descrDiv.append(spaceP);
					descrDiv.append(userA);
						voteP.append(voteSpan);
					descrDiv.append(voteP);
					contDiv.append(descrDiv);
					contDiv.append(fixDiv);
					contDiv.append(pointDiv);
					
					videoPoint.append(contDiv);
					
				});					
			}
			videoPoint.fadeTo("fast","1");
			},"json");		
}

/*Mostra altri video (avanti)*/
function up_video(){
	var step = parseInt($("#iz_video_step").text());
	var offset = parseInt($("#iz_video_offset").text());
	var total = parseInt($("#iz_video_total").text());
	
	var n_off = offset +step;
	
	if(offset+step < total){
		
		if(n_off+step >= total){	
			$("#iz_video_up").fadeTo("fast","0.3");
		}
		$("#iz_video_down").fadeTo("fast","1.0");

		$("#iz_video_offset").text(step+offset);
		
		iz_get_PopularVideos();
	}
}

/*Mostra altri video (indietro)*/
function down_video(){	
	var step = parseInt($("#iz_video_step").text());
	var offset = parseInt($("#iz_video_offset").text());
	var n_off = offset - step;

	if(offset >= step){
		if(n_off-step < 0){	
			$("#iz_video_down").fadeTo("fast","0.3");
		}	
		$("#iz_video_up").fadeTo("fast","1.0");
		
		$("#iz_video_offset").text(offset-step);
		iz_get_PopularVideos();
	}
}

/*NOTIFICHE***********************************************************/
/*Carica la lista di video di un profilo*/
function iz_get_PopularNotifiche(){	
	
	var notifichePoint = $("#iz_notifiche");	
	notifichePoint.fadeTo("fast",0.1);
	
	var uid = 0;
	var offset = $("#iz_notif_offset").text();
	var step = $("#iz_notif_step").text();
	
	$.post(base_url+"index.php/notifiche/getPopularNotificheTotal", {uid: uid},
			function(data){
				$("#iz_notif_total").text(data.total);
			},"json");
	
	$.post(base_url+"index.php/notifiche/getPopularNotifiche", {offset:offset,step:step},
		
			function(data){	
			var cl = "";
			if(data.length > 0){
				notifichePoint.empty();
				
				$.each(data,function(i,item){
					var str_template = parseInt(item.template);
					
					switch(str_template){	
						case 4:
							cl = "smooth";
						break;
						
						case 1:
							cl = "curl";
						break;	
						
						case 2:
							cl = "volume";
						break;	
							
						case 3:
							cl = "shape";
						break;										
					}	
									
					var contDiv = $(document.createElement("div"));
					contDiv.attr("class","notifiche_container");
					
					var spaceP = $(document.createElement("p"));
					spaceP.attr("class","space_left");
					spaceP.text("SPACE");
					
					var profA = $(document.createElement("a"));
					var nameP = $(document.createElement("p"));
					profA.attr("href",base_url+"index.php/space/viewer/"+item.username);
					nameP.text(item.username);	
					nameP.addClass(cl);					
					
					var descrSpan = $(document.createElement("p"));
					descrSpan.html(item.msg);
					
					var infoP = $(document.createElement("p"));
					infoP.attr("class","info");
					infoP.append(item.created);	
										
					var goA = $(document.createElement("a"));
					goA.html("<img src='"+base_url+"images/botton/vai.gif' /><span>vai alla pagina</span>");
					goA.attr("href",base_url+"index.php/space/viewer/"+item.username);
					
					var pointDiv = $(document.createElement("div"));
					pointDiv.attr("class","point");					
					
					
					contDiv.append(spaceP);
						profA.append(nameP);		
					contDiv.append(profA);
					contDiv.append(descrSpan);
						infoP.append(goA);
					contDiv.append(infoP);
					contDiv.append(pointDiv);			
					notifichePoint.append(contDiv);

				});					
			}
			notifichePoint.fadeTo("fast",1);
			},"json");		
}

/*Mostra altri video (avanti)*/
function up_notifiche(){

	var step = parseInt($("#iz_notif_step").text());
	var offset = parseInt($("#iz_notif_offset").text());
	var total = parseInt($("#iz_notif_total").text());
	var n_off = offset +step;
	
	if(offset+step < total){
		
		if(n_off+step >= total){	
			$("#iz_notif_up").fadeTo("fast","0.3");
		}
		$("#iz_notif_down").fadeTo("fast","1.0");

		$("#iz_notif_offset").text(step+offset);
		
		iz_get_PopularNotifiche();
	}
}

/*Mostra altri video (indietro)*/
function down_notifiche(){	
	var step = parseInt($("#iz_notif_step").text());
	var offset = parseInt($("#iz_notif_offset").text());
	var n_off = offset - step;
	
	if(offset >= step){
		if(n_off-step < 0){	
			$("#iz_notif_down").fadeTo("fast","0.3");
		}	
		$("#iz_notif_up").fadeTo("fast","1.0");
		
		$("#iz_notif_offset").text(offset-step);
		iz_get_PopularNotifiche();
	}
}

/*POPULAR SPACE***********************************************************/

function iz_get_PopularSpace(){	
	
	var spacePoint = $("#iz_space");	
	spacePoint.fadeTo("fast",0.1);
	
	var uid = 0;
	var offset = $("#iz_space_offset").text();
	var step = $("#iz_space_step").text();
	
	$.post(base_url+"index.php/space/getPopularSpaceTotal", {uid: uid},
			function(data){
				$("#iz_space_total").text(data.total);
			},"json");
	
	$.post(base_url+"index.php/space/getPopularSpace", {offset:offset,step:step},
			function(data){	
			if(data.length > 0){
				spacePoint.empty();
				
				$.each(data,function(i,item){
					
					var str_template = parseInt(item.template);
					
					switch(str_template){	
						case 4:
							var cl = "smooth";
						break;
						
						case 1:
							var cl = "curl";
						break;	
						
						case 2:
							var cl = "volume";
						break;	
							
						case 3:
							var cl  = "shape";
						break;										
					}	

									
					var contDiv = $(document.createElement("div"));
					contDiv.attr("class","profile");
					
					var spaceP = $(document.createElement("p"));
					spaceP.attr("class","space");
					spaceP.text("SPACE");
					
					var profA = $(document.createElement("a"));
					var nameP = $(document.createElement("p"));
					profA.attr("href",base_url+"index.php/space/viewer/"+item.username);
					nameP.text(item.username);	
					nameP.addClass(cl);					
					
					var descrSpan = $(document.createElement("p"));
					descrSpan.html(item.msg);
					
					var voteP = $(document.createElement("p"));
					voteP.attr("class","voti");
					
					var voteSpan = $(document.createElement("span"));
					voteSpan.text("VOTO");
					
					if(item.pct != ""){					
						voteP.append(item.pct+" ");
					}
					else {
						voteP.append("0");
					}					
				
					var pointDiv = $(document.createElement("div"));
					pointDiv.attr("class","point");					

					var aImg = $(document.createElement("a"));
					aImg.attr("href",base_url+"index.php/space/viewer/"+item.username);
					//Per ogni item crea un'immagine
					if(item.thumb_path != null){
						//Per ogni item creo un'immagine
					    var img = new Image();
					    $(img).load(function () {
					        $(this).hide();
					        aImg.append(this);
					        $(this).fadeIn();
					    }).error(function () {
					       
					    }).attr({
					             src: base_url+item.thumb_path
					     }).addClass("img_profile");
				    }					
					
					contDiv.append(aImg);
					contDiv.append(spaceP);
						profA.append(nameP);		
					contDiv.append(profA);
					contDiv.append(descrSpan);
						voteP.append(voteSpan);
					contDiv.append(voteP);		
					spacePoint.append(contDiv);

				});					
			}
			spacePoint.fadeTo("fast",1);
			},"json");		
}

/*Mostra altri video (avanti)*/
function up_space(){

	var step = parseInt($("#iz_space_step").text());
	var offset = parseInt($("#iz_space_offset").text());
	var total = parseInt($("#iz_space_total").text());
		
	if(offset+step < total){
		$("#iz_space_offset").text(step+offset);
		iz_get_PopularSpace();
	}
}

/*Mostra altri video (indietro)*/
function down_space(){	
	var step = parseInt($("#iz_space_step").text());
	var offset = parseInt($("#iz_space_offset").text());
	
	if(offset >= step){
		$("#iz_space_offset").text(offset-step);
		iz_get_PopularSpace();
	}
}

