var TT_Search = {
    currentPage:1, start:1, end:1, pages:1,
    title:[], content:[], url:[], sum:0,
    bookFotos:[], bookVerlags:[], bookVerfassers:[],
    tt_textboxClick : function(){
		$('tt_textsearch').value = '';
	},
    tt_textboxKeyPress : function(e){
        if(e.keyCode==13){
            TT_Search.tt_search();
        }
    },
	tt_handleDivInputSearchOver : function(){
		$('divAdvanceSearch').show();		
	},
	tt_handleDivInputSearchOut : function(){
		$('divAdvanceSearch').hide();
	},
	tt_handleAdvanceSearch : function(){
		window.location = '/TT.CMS/searchs/advancesearch';
	},
    tt_setBookOption : function(foto,verlag,verfasser){
        this.bookFotos      = foto;
        this.bookVerlags    = verlag;
        this.bookVerfassers = verfasser;
    },
    showResult : function(title,content,url,sum){
        this.title   = title;
        this.content = content;
        this.url     = url;
        this.sum     = sum;
        this.btPrevClick = this.btPrevClick.bind(this);
        this.btNextClick = this.btNextClick.bind(this);
        this.pages = Math.round(sum/10);
        var mod    = sum%10;
        if (mod<5) this.pages+=1;
        $('divResultNum').innerHTML = '<h4>'+this.sum+' results found</h4>';
        var html='';
        if(sum<=10){
            for(var i=0; i<parseInt(sum); i++){
                html+='<div class="title"><a target="_blank" href="'+url[i]+'">'+TT_Search.base64_decode(title[i]).stripTags()+'</a></div>';
                html+='<div class="tt_resultContent">'       +TT_Search.base64_decode(content[i]).stripTags().substring(0,400)+' ... '+'</div>';
                html+='<div class="link">'          +url[i].stripTags()+'</div>';
            }
            $('tt_resultAdvanceSearch').update(html);
        }
        else{
            if(this.currentPage<this.pages){
                this.start=(this.currentPage*10)-10;
                this.end=(this.currentPage*10)-1;
                for(var i=this.start; i<=this.end; i++){
                html+='<div class="title"><a target="_blank" href="'+this.url[i]+'">'+TT_Search.base64_decode(this.title[i]).stripTags()+'</a></div>';
                html+='<div class="tt_resultContent">'       +TT_Search.base64_decode(this.content[i]).stripTags().substring(0,400)+' ... '+'</div>';
                html+='<div class="link">'          +this.url[i].stripTags()+'</div>';
                }
            }
            $('tt_resultAdvanceSearch').update(html);
        }
        var inputPrev = new Element('input',{'type':'button','id':'btPrev','class':'btResult','value':'<<'});
        var inputNext = new Element('input',{'type':'button','id':'btNext','class':'btResult','value':'>>'});
        var spanPage = new Element('span',{'id':'spanPage'});
        spanPage.innerHTML = '<b>'+this.currentPage+'/'+this.pages+'<b>';
        var div = new Element('div');
        div.appendChild(inputPrev);
        div.appendChild(spanPage);
        div.appendChild(inputNext);
        $('divButton').appendChild(div);
        Event.observe($('btPrev'),'click',this.btPrevClick,false);
        Event.observe($('btNext'),'click',this.btNextClick,false);
        if(sum>10) $('divButton').show();
        else $('divButton').hide();
    },
    btPrevClick : function(){
        this.currentPage = (this.currentPage>1)?(this.currentPage-1):this.currentPage;
        var htmlStr = '';
        if(this.currentPage<this.pages){
            var st = (this.currentPage*10)-10;
            var en = st+9;
            for(var i=st; i<=en; i++){
                htmlStr+='<div class="title"><a target="_blank" href="'+this.url[i]+'">'+TT_Search.base64_decode(this.title[i]).stripTags()+'</a></div>';
                htmlStr+='<div class="tt_resultContent">'       +TT_Search.base64_decode(this.content[i]).stripTags().substring(0,400)+' ... '+'</div>';
                htmlStr+='<div class="link">'          +this.url[i].stripTags()+'</div>';
            }
        }
        $('tt_resultAdvanceSearch').update(htmlStr);
        var spanPage = '<b>'+this.currentPage+'/'+this.pages+'</b>';
        $('spanPage').update(spanPage);
    },
    btNextClick : function(e){
        this.currentPage = (this.currentPage<this.pages)?(this.currentPage+1):this.currentPage;
        if(this.currentPage<this.pages){
            var st = (this.currentPage*10)-10;
            var en = st+9;
            var html='';
            for(var i=st; i<=en; i++){
                html+='<div class="title"><a target="_blank" href="'+this.url[i]+'">'+TT_Search.base64_decode(this.title[i]).stripTags()+'</a></div>';
                html+='<div class="tt_resultContent">'       +TT_Search.base64_decode(this.content[i]).stripTags().substring(0,400)+' ... '+'</div>';
                html+='<div class="link">'          +this.url[i].stripTags()+'</div>';
            }
            $('tt_resultAdvanceSearch').update(html);
        }
        else{
            var st = (this.currentPage*10)-10;
            var en = this.sum-1;
            var html='';
            for(var i=st; i<=en; i++){
                html+='<div class="title"><a target="_blank" href="'+this.url[i]+'">'+TT_Search.base64_decode(this.title[i]).stripTags()+'</a></div>';
                html+='<div class="tt_resultContent">'       +TT_Search.base64_decode(this.content[i]).stripTags().substring(0,400)+' ... '+'</div>';
                html+='<div class="link">'          +this.url[i].stripTags()+'</div>';
            }
            $('tt_resultAdvanceSearch').update(html);
        }
        var spanPage = '<b>'+this.currentPage+'/'+this.pages+'</b>';
        $('spanPage').update(spanPage);
    },
	tt_search : function(){
		this.currentPage=1; this.pages=1; this.start=1;this.end=1;
		var inputSearch  = $('txtInputSearch').value;
		if (inputSearch=='') return;
        var selectOption = $('OptionsType').value;
        var data = $H({inputSearch:inputSearch,selectOption:selectOption}).toJSON();
        if(selectOption=='books'){
			var selectFoto = $('selectFoto').value;
            var selectVerlag = $('selectVerlag').value;
            var selectVerfasser = $('selectVerfasser').value;
            data = $H({inputSearch:inputSearch,selectOption:selectOption,
                        selectFoto:selectFoto,selectVerlag:selectVerlag,selectVerfasser:selectVerfasser
            }).toJSON();
        }
        else
            if(selectOption=='lexikons'){
                var selectLexikon = $('selectLexikon').value;
                data = $H({inputSearch:inputSearch,selectOption:selectOption,
                            selectLexikon:selectLexikon}).toJSON();
            }
            else
                if(selectOption=='haendlers'){
                    var company  = $('txtCompany').value;
                    var street   = $('txtStreet').value;
                    var houseNum = $('txtHouseNumber').value;
                    var postCode = $('txtPostCode').value;
                    var postCodeArea = $('txtPostCodeArea').value;
                    var area     = $('txtArea').value;
                    var tel      = $('txtTel').value;
                    var fax      = $('txtFax').value;
                    data = $H({inputSearch:inputSearch,selectOption:selectOption,
                            company:company, street:street, houseNum:houseNum, postCode:postCode,
                            postCodeArea:postCodeArea, area:area, tel:tel, fax:fax
                            }).toJSON();
                }
                else
                    if(selectOption=='fotos'){
                        var selectFoto = $('selectFotos').value;
                        data = $H({inputSearch:inputSearch,selectOption:selectOption,
                                    selectFoto:selectFoto}).toJSON();
                    }
                    else
                    	if(selectOption=='events'){
                    		var selectEvent = $('selectEvents').value;
                        	data = $H({inputSearch:inputSearch,selectOption:selectOption,
                                    selectEvent:selectEvent}).toJSON();
                    	}
		Waitting.tt_show();
        new Ajax.Request( '/TT.CMS/searchs/searchAjax', {
            parameters: {data:data},
            onComplete: TT_Search.tt_SearchCallback
        });
    },
    tt_SearchCallback : function(transport){
        Waitting.tt_close();
        try{
            $("content").update(transport.responseText);
        }catch(e){ return 'SearchCallback error';};
    },


    selectChange : function(){
        if($('OptionsType').value == 'books'){
            if($('divHandlerOptions'))
                $('divHandlerOptions').hide();
            if($('divFotoOptions'))
                $('divFotoOptions').hide();
            if($('divContainerSelectLexikons'))
                $('divContainerSelectLexikons').hide();
            if($('divEventOptions'))
                    $('divEventOptions').hide();    
            if(!($('divSelectVerfasserForm'))){
                var htmlFoto = '';
                htmlFoto+='<select id="selectFoto" name="selectFoto" class="tt_selectBookInfo">';
                htmlFoto+='<option value="all">auswählen</option>';
                for(var opt=0; opt<this.bookFotos.length; opt++){
                    htmlFoto+='<option value="'+this.bookFotos[opt]+'">'+this.bookFotos[opt]+'</option>';
                }
                htmlFoto+='</select>';
                var divSelectFoto = new Element('div',{'id':'divSelectFotoForm','class':'tt_divSelectBookInfo'});
                divSelectFoto.update(htmlFoto);
                
                var htmlVerlag = '';
                htmlVerlag+='<select id="selectVerlag" name="selectVerlag" class="tt_selectBookInfo">';
                htmlVerlag+='<option value="all">auswählen</option>';
            	for(var v=0; v<this.bookVerlags.length; v++){
                    htmlVerlag+='<option value="'+this.bookVerlags[v]+'">'+this.bookVerlags[v]+'</option>';
                }
                htmlVerlag+='</select>';
                var divSelectVerlag = new Element('div',{'id':'divSelectVerlagForm','class':'tt_divSelectBookInfo'});
                divSelectVerlag.update(htmlVerlag);
                
                var htmlVerfasser = '';
                htmlVerfasser+='<select id="selectVerfasser" class="tt_selectBookInfo" name="selectVerfasser">';
                htmlVerfasser+='<option value="all">auswählen</option>';
                for(var t=0; t<this.bookVerfassers.length; t++){
                    htmlVerfasser+='<option value="'+this.bookVerfassers[t]+'">'+this.bookVerfassers[t]+'</option>';
                }
            	
                htmlVerfasser+='</select>';
                var divSelectVerfasser = new Element('div',{'id':'divSelectVerfasserForm','class':'tt_divSelectBookInfo'});
                divSelectVerfasser.update(htmlVerfasser);
                
                var label1 = new Element('label',{'class':'label1'});
                label1.innerHTML = 'Foto';
                var label2 = new Element('label',{'class':'label2'});
                label2.innerHTML = 'Verlag';
                var label3 = new Element('label',{'class':'label3'});
                label3.innerHTML = 'Verfasser';
                
                var divContainerSelectBooks = new Element('div',{'id':'divContainerSelectBooks'});
                divContainerSelectBooks.appendChild(label1);
                divContainerSelectBooks.appendChild(label2);
                divContainerSelectBooks.appendChild(label3);
                divContainerSelectBooks.appendChild(divSelectFoto);
                divContainerSelectBooks.appendChild(divSelectVerlag);
                divContainerSelectBooks.appendChild(divSelectVerfasser);
                $('divSelectBooks').appendChild(divContainerSelectBooks);
            }
			$('divContainerSelectBooks').show();
        }
        else
            if($('OptionsType').value == 'lexikons'){
                if($('divContainerSelectBooks'))
                    $('divContainerSelectBooks').hide();
                if($('divHandlerOptions'))
                    $('divHandlerOptions').hide();
                if($('divFotoOptions'))
                    $('divFotoOptions').hide();
                if($('divEventOptions'))
                    $('divEventOptions').hide();    
                if(!($('divContainerSelectLexikons'))){
                    var html='';
                    html+='<div>';
                    html+='<label>Lexikons </label>';
                    html+='<select id="selectLexikon" name="selectLexikon" class="tt_selectBoxSearch">';
                    html+='<option value="Kunst_und_Antiquitaten_Lexikon">Kunst- und Antiquitäten-Lexikon</option>';
                    html+='<option value="Pflegetipps_fur_Antiquitaten">Pflegetipps für Antiquitäten</option>';
                    html+='<option value="Redaktionelle_Erganzungen">Redaktionelle Ergänzungen</option>';
                    html+='</select>';
                    html+='</div>';
                    var divContainerSelectLexikons = new Element('div',{'id':'divContainerSelectLexikons'});
                    divContainerSelectLexikons.update(html);
                    $('divSelectLexikons').appendChild(divContainerSelectLexikons);
                }
				$('divContainerSelectLexikons').show()
            }
        else
            if($('OptionsType').value == 'haendlers'){
                if($('divContainerSelectBooks'))
                    $('divContainerSelectBooks').hide();
                if($('divFotoOptions'))
                    $('divFotoOptions').hide();
                if($('divContainerSelectLexikons'))
                    $('divContainerSelectLexikons').hide();
                if($('divEventOptions'))
                    $('divEventOptions').hide();
                if(!($('divHandlerOptions'))){
                    var html='';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Company Name</label>';
                    html+='<input type="text" size="35" id="txtCompany" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Street</label>';
                    html+='<input type="text" size="35" id="txtStreet" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">House number</label>';
                    html+='<input type="text" size="35" id="txtHouseNumber" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Post code</label>';
                    html+='<input type="text" size="20" id="txtPostCode" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Post code area</label>';
                    html+='<input type="text" size="20" id="txtPostCodeArea" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Area</label>';
                    html+='<input type="text" size="20" id="txtArea" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Tel</label>';
                    html+='<input type="text" size="15" id="txtTel" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    html+='<div class="divHandlerOptions">';
                    html+='<label class="labelHandler">Fax</label>';
                    html+='<input type="text" size="15" id="txtFax" class="tt_txtAdvanceSearchHandler"><br/>';
                    html+='</div>';
                    
                    var divHandlerOptions = new Element('div',{'id':'divHandlerOptions'});
                    divHandlerOptions.update(html);
                    $('divSelectHandler').appendChild(divHandlerOptions);
                }
				$('divHandlerOptions').show();
            }
        else
            if($('OptionsType').value == 'fotos'){
                if($('divHandlerOptions'))
                    $('divHandlerOptions').hide();
                if($('divContainerSelectBooks'))
                    $('divContainerSelectBooks').hide();
                if($('divContainerSelectLexikons'))
                    $('divContainerSelectLexikons').hide();
                if($('divEventOptions'))
                    $('divEventOptions').hide();
                if(!($('divFotoOptions'))){
                    var html='';
                    html+='<div>';
                    html+='<label>Fotos </label>';
                    html+='<select id="selectFotos" name="selectFotos" class="tt_selectBoxSearch">';
                    html+='<option value="templates">----   Templates   ----</option>';
                    html+='<option value="portlets">----   Portlets   ----</option>';
                    html+='<option value="books">----   Books   ----</option>';
                    html+='<option value="events">----   Events   ----</option>';
                    html+='<option value="lexikons">----   Lexikons   ----</option>';
                    html+='<option value="haendlers">----   Haendlers   ----</option>';
                    html+='<option value="all" selected="selected">----   Alle   ----</option>';
                    
                    html+='</select>';
                    html+='</div>';
                    var divFotoOptions = new Element('div',{'id':'divFotoOptions'});
                    divFotoOptions.update(html);
                    $('divSelectFotos').appendChild(divFotoOptions);
                }
				$('divFotoOptions').show();
            }
            else
            	if($('OptionsType').value == 'events'){
            		if($('divHandlerOptions'))
	                    $('divHandlerOptions').hide();
	                if($('divContainerSelectBooks'))
	                    $('divContainerSelectBooks').hide();
	                if($('divContainerSelectLexikons'))
	                    $('divContainerSelectLexikons').hide();
	                if($('divFotoOptions'))
	                    $('divFotoOptions').hide();
	                
	                if(!($('divEventOptions'))){
		                var html='';
		                html+='<div>';
	                    html+='<label>Events </label>';
	                    html+='<select id="selectEvents" name="selectEvents" class="tt_selectBoxSearch">';
	                    html+='<option value="auktionen">Auktinen</option>';
	                    html+='<option value="antikmessen">Antikmessen</option>';
	                    html+='<option value="all" selected="selected">Alle</option>';
		                html+='</select>';
	                    html+='</div>';
	                    var divEventOptions = new Element('div',{'id':'divEventOptions'});
	                    divEventOptions.update(html);
	                    $('divSelectEvents').appendChild(divEventOptions);
                    }
                    $('divEventOptions').show();
            	}
            
            else{
                if($('divHandlerOptions'))
                    $('divHandlerOptions').hide();
                if($('divContainerSelectBooks'))
                    $('divContainerSelectBooks').hide();
                if($('divContainerSelectLexikons'))
                    $('divContainerSelectLexikons').hide();
                if($('divFotoOptions'))
                    $('divFotoOptions').hide();
                if($('divEventOptions'))
                    $('divEventOptions').hide();
                    
            }
    },
    base64_decode : function(data) {
	    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
	 
	    do {  // unpack four hexets into three octets using index points in b64
	        h1 = b64.indexOf(data.charAt(i++));
	        h2 = b64.indexOf(data.charAt(i++));
	        h3 = b64.indexOf(data.charAt(i++));
	        h4 = b64.indexOf(data.charAt(i++));
	 
	        bits = h1<<18 | h2<<12 | h3<<6 | h4;
	 
	        o1 = bits>>16 & 0xff;
	        o2 = bits>>8 & 0xff;
	        o3 = bits & 0xff;
	 
	        if (h3 == 64) {
	            tmp_arr[ac++] = String.fromCharCode(o1);
	        } else if (h4 == 64) {
	            tmp_arr[ac++] = String.fromCharCode(o1, o2);
	        } else {
	            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
	        }
	    } while (i < data.length);
	    
	    dec = tmp_arr.join('');
	    dec = TT_Search.utf8_decode(dec);
	    
	    return dec;
	},
	
	utf8_decode : function(str_data) {
	    var tmp_arr = [], i = ac = c = c1 = c2 = 0;
	    while ( i < str_data.length ) {
	        c = str_data.charCodeAt(i);
	        if (c < 128) {
	            tmp_arr[ac++] = String.fromCharCode(c); 
	            i++;
	        } else if ((c > 191) && (c < 224)) {
	            c2 = str_data.charCodeAt(i+1);
	            tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
	            i += 2;
	        } else {
	            c2 = str_data.charCodeAt(i+1);
	            c3 = str_data.charCodeAt(i+2);
	            tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
	            i += 3;
	        }
	    }
	    return tmp_arr.join('');
	}
}