$(document).ready(function() {
    var container = $("[id^=listHome_]");
    var homeId = container.attr("id").replace("listHome_", "");
    var strCategory = container.attr("name");
    var strLang = container.attr("lang");
    
    $("#listHomeEmptyText").hide();
    
    if (homeId > 0) {
        onLoadHomeById(homeId, container, strCategory,strLang);
    }
    else {
        onLoadHome(container, strCategory,strLang);
    }    
});

function renderHomeTemplate(home, strCategory, viewType,strLang) {
    var itemHome = '<div id="home_' + home.homeId + '" class="main-box">';
    itemHome += '<div class="main-box-top"></div>';
    itemHome += '<div class="main-box-content">';
    itemHome += '<h2><a>' + eval("home.title" + strLang)+ '</a></h2>';
    if(viewType)
    	if( viewType.role=="Administrator"){
	    	itemHome += '<img id="btnEdit_' + home.homeId + '" src="./img/table-edit.png" title="Edit" class="button" />';
	    	itemHome += '<img id="btnDelete_' + home.homeId + '" src="./img/table-delete.png" title="Delete" class="button" />';
	    }       
    itemHome += '<div class="paragraph">';
    itemHome += '<div class="media-box">';
    if(String(home.image)!="")	
    	itemHome += '<div><img src="img/home/' + escape(home.image) + '" style="height:250px;"></div>';
    if(String(eval("home.file" + strLang))!=""){	
    	itemHome += '<div><a href="img/home/' + escape(eval("home.file" + strLang)) + '" target="_blank">';
    	if(strLang=="FR")
    		itemHome += "INFORMATIONS D'INSCRIPTION"
    	else
    		itemHome += 'REGISTRATION INFORMATION'
    	itemHome += '</a></div>';      
    }
    itemHome += '</div>' + eval("home.content" + strLang) + '</div>';
    itemHome += '<div class="clear"></div>';
    itemHome += '</div>';
    itemHome += '<div class="main-box-bottom"></div>';
    itemHome += '</div>';
    return itemHome;
}

function onLoadHome(container, strCategory,strLang) {
	$.ajax({
        type: "POST",
        url: "lib/act/GetHome.php",
        data: {},
        dataType: "text/plain",
        success: function(result) {
        	container.empty()
            var obj = eval('(' + result + ')');
            
            var homeArray = obj.home;
            var viewType = obj.viewType;
            if (homeArray.length > 0) {
                for(var i = 0; i < homeArray.length; i++)
                {
                	if(homeArray[i].home.category == strCategory || strCategory=="ALL"){
	                    
                		var home = homeArray[i].home;
	                   
	                    var itemHome = renderHomeTemplate(home, strCategory,viewType,strLang);
	     
	                    container.append(itemHome);
                	}
                }
                
                $("#listHomeEmptyText").hide();
                $("img[id*='btnEdit_']",container).bind("click", function() {
                	var homeId = $(this).attr("id").replace("btnEdit_", "");
                	home_onEdit(homeId,container, strCategory,viewType.userId);
                }); 
                $("img[id*='btnDelete_']",container).bind("click", function() {
                	var homeId = $(this).attr("id").replace("btnDelete_", "");
                	home_onDelete(homeId,container, strCategory);
                });                
            }
            else {
                $("#listHomeEmptyText").show();
            }
            $("img[id='btnAdd']",$("[id=listHome]")).bind("click", function() {
            	home_onAdd(container, strCategory,viewType.userId);
            }); 
        },
        error: function(a, b, c) {
            alert(a + " : " + b + " : " + c);
        }
    });
}

function onLoadHomeById(homeId, container, strCategory,strLang) {
    $.ajax({
        type: "POST",
        url: "lib/act/GetHomeById.php",
        data: {"homeId": homeId},
        dataType: "text/plain",
        success: function(result) {
            var obj = eval('(' + result + ')');
            var viewType = obj.viewType;
           
            if (obj.home.length > 0) {
                
            	var home = obj.home[0].home;
                
                var itemHome = renderHomeTemplate(home, strCategory,viewType,strLang);

                container.append(itemHome);
                
                $("#listHomeEmptyText").hide();
                $("img[id*='btnEdit_']",container).bind("click", function() {
                	var homeId = $(this).attr("id").replace("btnEdit_", "");
                	home_onEdit(homeId,container, strCategory);
                }); 
                $("img[id*='btnDelete_']",container).bind("click", function() {
                	var homeId = $(this).attr("id").replace("btnDelete_", "");
                	home_onDelete(homeId,container, strCategory);
                });               
            }
            else {
                $("#listHomeEmptyText").show();
            }
            $("img[id='btnAdd']",$("[id=listHome]")).bind("click", function() {
            	home_onAdd(container, strCategory);
            }); 
        },
        error: function(a, b, c) {
            alert(a + " : " + b + " : " + c);
        }
    });
}

