var pageId = 0;
var nbPages = 0;
var currKeyword = null;

var labels = new Array();
labels['fr'] = {
                    all: 'Tous',
                    results: 'r&eacute;sultats',
                    search: 'Recherche ',
                    scenarios: [
                        'Transdisciplinaire',
                        'Sc. et tech. biologiques',
                        'Sc. et tech. physiques',
                        'Sc. et tech. humaines',
                        'Sc. et tech. de la gestion',
                        'Arts et communication',
                        'Langues et litt&eacute;rature',
                    ]
                };
                
labels['en'] = {
                    all: 'All',
                    results: 'results',
                    search: 'Search ',
                    scenarios: [
                        'Cross curricular',
                        'Biological Sci. and Tech.',
                        'Physical Sci. and Tech.',
                        'Social Sci. and Tech.',
                        'Administrative Sci. and Tech.',
                        'Creative art and Tech.',
                        'Languages and Literature',
                    ]
                };

discSuffix = '&nbsp;&gt;&nbsp;';

        

window.onload = function(){
    $('ContentNavBarText').hide();
    $('ContentNavBarList').hide();
    
    prepareImageSwap($('SwitchLang'),true,true);
    prepareImageSwap($('Search'),true,true,true);
    prepareImageSwap($('ContentNavBarList'),true,true);
    
    initMenu();
    
    if(currSectionId != null)
        getContent({sectionId:currSectionId,subSectionId:currSubSectionId});
}



//Layout
function getContent(request){
    currSectionId = request.sectionId;
    currSubSectionId = request.subSectionId;
    currKeyword = request.keyword;

    var url = 'svrcmd.php?cmd=get&l=' + lang;
    if(request.sectionId != null)
        url += '&sid=' + request.sectionId;
    if(request.subSectionId != null)
        url += '&ssid=' + request.subSectionId;
    if(request.pageId != null)
        url += '&pid=' + request.pageId;
    if(request.keyword != null)
        url += '&kw=' + request.keyword;
    
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: showContent
    });
}

function showContent(transport){
    var result = transport.responseJSON;
    
    $('ContentTitle').update(result.title);
    $('ContentSubTitle').update();
    
    if(result.type == 'text')
        showTextContent(result);
    else
        showListContent(result);
}

function showTextContent(result){
    $('ContentNavBarText').show();
    $('ContentNavBarList').hide();
    
    $('ContentSubTitle').update(result.subTitle);
    $('MainContent').update(result.HTML);
}

function showListContent(result){
    $('ContentNavBarText').hide();
    $('ContentNavBarList').show();

    pageId = result.pageId;
    nbPages = result.nbPages;
    
    var subTitleSuffix = discSuffix + result.nbResults + '&nbsp;' + labels[lang].results;
    
    if(currKeyword){
        $('ContentSubTitle').update(labels[lang].search + '&nbsp;&quot;&nbsp;' + currKeyword + '&quot;&nbsp;' + subTitleSuffix);
    }else if(currSubSectionId == null){
        $('ContentSubTitle').update(labels[lang].all + subTitleSuffix);
    }else{
        $('ContentSubTitle').update(labels[lang].scenarios[currSubSectionId] + subTitleSuffix);
    }
    
    $('MainContent').update(result.HTML);
    $('txtPageId').value = (result.pageId + 1);
    $('NbPages').update(result.nbPages);
}

function pagePrevious(){
    if(pageId > 0){
        pageId--;
        getContent({sectionId:currSectionId,subSectionId:currSubSectionId,keyword:currKeyword,pageId:pageId});
    }
}

function pageNext(){
    if(pageId + 1 < nbPages){
        pageId++;
        
        request = {sectionId:currSectionId,subSectionId:currSubSectionId,keyword:currKeyword,pageId:pageId};
        getContent(request);
    }
}
function pageGoto(){
    var newPageId = parseInt($F('txtPageId'));
    
    if(newPageId <= nbPages && newPageId > 0){
        pageId = (newPageId-1);
        getContent({sectionId:currSectionId,subSectionId:currSubSectionId,keyword:currKeyword,pageId:pageId});
    }
}

function searchKeyword(){
    var keyword = $F('txtSearch');

    getContent({keyword:keyword});
}
//Events
function _getSucces(){}

//Functions
function rollImg(oImg)
{
	//Settings
	var sNormSuffix = '_norm';
	var sOverSuffix = '_roll';
	var iStatusLen = sNormSuffix.length;

	//For manipulation only
	var sCurrentImgName = oImg.src.substring(oImg.src.lastIndexOf('/') + 1,oImg.src.length);
	var sCurrentStatus = sCurrentImgName.substr(sCurrentImgName.lastIndexOf('_'),iStatusLen);
	
	//Will be used for the final string building
	var sPath = oImg.src.substring(0,oImg.src.lastIndexOf('/') + 1);
	var sShortImgName = sCurrentImgName.substr(0,sCurrentImgName.lastIndexOf('_'));
	var sImgExt = sCurrentImgName.substring(sCurrentImgName.lastIndexOf('.'),sCurrentImgName.length);
	
	//New status
	var sNewStatus = ((sCurrentStatus == sNormSuffix) ? sOverSuffix : sNormSuffix);
	
	var sNewImgName = sPath + sShortImgName + sNewStatus + sImgExt;
	
	oImg.src = sNewImgName;
}

function exeOnEnterKey(e, refFunction)
{
	var e = (e)? e : event;
	var nCode;
    
    if(e.charCode)
		nCode = e.charCode
    else
		nCode = (e.which) ? e.which : e.keyCode;
	
	if(nCode == 13 || nCode == 3)
	{
		refFunction();	
	} 
}