/* THE [R]EEEP-[S]EARCHER-[S]SETUP @ atomar level */

var rss;

/* THE AJAX-XMLHTTP-REQUEST-THING-OBJECT */

var AjaxObject =  {
    setCommand:function(Command){
        this.Command = Command;
    },
    setSearchType:function(SearchType){
        this.SearchType = SearchType;
    },
    getSearchType:function(){
        return this.SearchType;
    },
    setForm:function(form, upload){
        YAHOO.util.Connect.setForm(form, upload);
    },
    setMethod:function(method){
        this.method = method;
    },
    handleSuccess:function(o){
        switch(this.Command){
            case "Search" :
                if(this.SearchType=="Normal"){
                document.getElementById("maincontent").innerHTML=(o.responseText);
                } else
                if(this.SearchType=="Community"){
                document.getElementById("maincontent").innerHTML=(o.responseText);
                }
            break;
        }
    },
    handleFailure:function(o){
        // shouldn't happen
    },
    startRequest:function(sUrl) {
        document.getElementById("maincontent").innerHTML = "<div style='padding-left:192px;'><img src='"+URI_ROOT+"images/ajax-loader.gif'></div>";
        YAHOO.util.Connect.asyncRequest(this.method, sUrl, callback);
    }
};

/*  THE CALLBACK-OBJECT OF THE AJAX-XMLHTTP-REQUEST-THING-OBJECT */

var callback =
{
    success:AjaxObject.handleSuccess,
    failure:AjaxObject.handleFailure,
    timeout: 50000,
    scope: AjaxObject
};

/* SUBMITS A SEARCH */

function submitSEARCH(createSetup,SearchType){
	if(document.getElementById("querystring").value==""){
		return;
	}
    if(SearchType!="" && SearchType!==undefined){
        AjaxObject.setSearchType(SearchType);
    }
    if(createSetup){
        if(AjaxObject.getSearchType()=="Community"){
	        createReepCommunitySearcherSetup();
        } else 
        if(AjaxObject.getSearchType()=="Normal"){
            createReepSearcherSetup();
        }
	}    
	AjaxObject.setCommand("Search");
	AjaxObject.setForm(document.getElementById("search"));
	AjaxObject.setMethod("POST");
	if(document.getElementById("hottopicsbar")){
		document.getElementById("wrapper").removeChild(document.getElementById("hottopicsbar"));
	} else {
		if(document.getElementById("topics")){
			document.getElementById("wrapper").removeChild(document.getElementById("topics"));
		}
	}
//	document.getElementById("maincontent").style="";
//	document.getElementById("maincontent").innerHTML = "";
	//AjaxObject.startRequest("proxy.php");
	document.getElementById("maincontent").innerHTML="";
	document.getElementById("maincontent").removeAttribute("style");
	document.getElementById("footer").className = "";
	AjaxObject.startRequest(URI_ROOT+"proxy.php");
}

/* SUBMIT A SEARCH FROM THE RESULTPAGE*/

function submitSEARCHFROMDOWNUNDER(){
	if(document.getElementById("Results_querystring").value==""){ 
		document.getElementById("Results_querystring").style.borderColor="#ff0000";
		return; 
	} else {
		document.getElementById("Results_querystring").style.borderColor="#999999";
	}
	document.getElementById("querystring").value = document.getElementById("Results_querystring").value;
	submitSEARCH(true);
}


/* THE NORMAL SEARCH SEETUP */

function createReepSearcherSetup(){
    rss = new ReepSearcherSetup();
    event_ = new AssetType("event","0","");
    rss.AddAssetType(event_);
    organisation_ = new AssetType("organisation","0","");
    organisation_.AddFilter(new AssetTypeFilter("PARTNER","1"));
    rss.AddAssetType(organisation_);
    news_ = new AssetType("news","0","");
    rss.AddAssetType(news_);
    article_ = new AssetType("article","0","");
    rss.AddAssetType(article_);
    project_ = new AssetType("project","0","");
    rss.AddAssetType(project_);
    document_ = new AssetType("document","0","");
    rss.AddAssetType(document_);
    document.getElementById("ReepSearcherSetup").value=YAHOO.lang.JSON.stringify(rss);
}

/* THE COMMUNITY SEARCH SETUP */

function createReepCommunitySearcherSetup(){
    rss = new ReepSearcherSetup();
    /* SYSTEMUSER */
    SystemUser = new AssetType("systemuser","0");
    SystemUser.AddFilter(new AssetTypeFilter("ACTIVATED","1"));
    rss.AddAssetType(SystemUser);
    /* SYSTEMGROUPS gibt es nicht mehr
    rss.AddAssetType(new AssetType("systemgroup","0")); */
    /* ORGANISATIONS */
    rss.AddAssetType(new AssetType("organisation","0"));
    /* EVENTS */
    rss.AddAssetType(new AssetType("event","0"));
    /* LINKS */
    rss.AddAssetType(new AssetType("link","0"));
    /* DOCUMENTS */
    rss.AddAssetType(new AssetType("document","0"));
    /* NEWS */
    rss.AddAssetType(new AssetType("news","0"));
    /* ARTICLE */
    rss.AddAssetType(new AssetType("article","0"));
    /* PROJECT */
    rss.AddAssetType(new AssetType("project","0"));

    document.getElementById("ReepSearcherSetup").value=YAHOO.lang.JSON.stringify(rss);
}


/* UPDATES THE ASSETTYPE'S STARTAT OF THE ACTUAL SEARCHER-SETUP */

function setStartAt(StartAt, AssetTypeName){
    rss.UpdateAssetType(StartAt, AssetTypeName);
    rss.SetDisplay(AssetTypeName);
    document.getElementById("ReepSearcherSetup").value=YAHOO.lang.JSON.stringify(rss);
    submitSEARCH(false);
}


/* DISPLAY OPEN-CLOSED FUNCTION START */

function displayDetails(ResultDiv){
    if(document.getElementById(ResultDiv+"Icon").src.indexOf("reeep_none.gif")!=-1){
	    document.getElementById(ResultDiv+"Icon").src=URI_ROOT+"images/reeep_block.gif";
	    document.getElementById(ResultDiv).style.display="block";
	} else {
	    document.getElementById(ResultDiv+"Icon").src=URI_ROOT+"images/reeep_none.gif";
	    document.getElementById(ResultDiv).style.display="none";
	}
}


/* THE GRIM REEPER-SEARCHER-SETUP START */

/*  This Object is what is actually sent to ze server in ze json-format
 *  It holds the different AssetTypes, that are 2B searched
 */

function ReepSearcherSetup(){
    this.reepSearcherTitle = "aloha 21 // welcome 23";
    this.class_ = "at.punkt.reep.util.AssetTypeSetup";
    this.assetTypes = new Array();
    this.display = "";
    this.AddAssetType = AddAssetType;
    this.UpdateAssetType = UpdateAssetType;
    this.SetDisplay = SetDisplay;
    this.GetTitle = GetTitle;
}

/* ADDS AN ASSETTYPE TO THE SEARCHER-SETUP */

function AddAssetType(AssetType){
    this.assetTypes.push(AssetType);
}

/* UPDATES THE START AT OF A SINGLE ASSETTYPE IN THE SEARCHER-SETUP */

function UpdateAssetType(StartAt, AssetTypeName){
     for(var i = 0; i < this.assetTypes.length; i++){
         if(this.assetTypes[i].GetName()==AssetTypeName){
             this.assetTypes[i].SetStartAt(StartAt);
         }
     }
}

/* SETS WHICH ASSETTYPE IS 2B DISPLAYED [OPEN] IN THE RESULT-DISPLAY */

function SetDisplay(AssetTypeName){
     this.display = AssetTypeName;
}

/* GETS THE TITLE OF THE SEARCHER-SETUP */

function GetTitle(){
     return this.reepSearcherTitle;
}

/* THE GRIM REEPER-SEARCHER-SETUP END */


/* THE NOT SO GRIM ASSETTYPE START */

/* Holds Info on the AssetType itself, like where to start, how to filter,... */

function AssetType(Name,StartAt) {
    this.class_ = "at.punkt.reep.util.AssetType";
    this.name = Name;
    this.startAt = StartAt;
    this.hitsPerPage = 10;
    this.totalHits = 0;
    this.filter = new Array();
    this.SetHitsPerPage = SetHitsPerPage;
    this.SetStartAt = SetStartAt;
    this.GetName = GetName;
    this.AddFilter = AddFilter;
}

/* SET THE HITSPERPAGE FOR THIS ASSETTYPE */

function SetHitsPerPage(HitsPerPage){
    this.hitsPerPage = HitsPerPage;
}

/* SETS THE STARTAT FOR THIS ASSETTYPE */

function SetStartAt(StartAt){
    this.startAt = StartAt;
}

/* ADDS A FILTER TO THIS ASSETTYPE */

function AddFilter(AssetTypeFilter){
    this.filter.push(AssetTypeFilter);
}

/* GETS THE NAME OF THIS ASSETTYPE */
function GetName(){
    return this.name;
}

/* THE NOT SO GRIM ASSETTYPE END */

/* THE TOTALLY UN-GRIM ASSETTYPEFILTER START */

/* Holds AssetTypeFilters in the form of Key-Value-Pairs [ KEY=LuceneFieldKEY, VALUE=LuceneFieldVALUE ] */

function AssetTypeFilter(key,value){
    this.class_ = "at.punkt.reep.util.AssetTypeFilter";
    this.key = key;
    this.value = value;
}

/* THE TOTALLY UN-GRIM ASSETTYPEFILTER END */

function init(){
	initialize();
	createReepSearcherSetup();
}


function clearField(text, field) {
	if (field.value == text) {
		field.value = '';
	}
}

