// Funcbar.js

// URL-Angaben
var URL_INDEX = "index.php"; // http://www.clicks4charity.net/index.php

// ToDo: Eingabe gültig?
function c4_searchRequest() {
	
		 var searchQueryOrig;
		 searchQueryOrig = $F('c4_fb_enginesIF_input');
     		 var termArray = new Array();
    		 termArray = searchQueryOrig.split(" ");
    		 searchQueryOrig = encodeURIComponent(searchQueryOrig);

	 		 var temp = "";
	 		 searchQuery = "";

    		for(var i=0; i<termArray.length; i++) {
        		// All search terms (after the first one) are to be separated with a '+'
        		if(i > 0)
            	searchQuery += "+";

        		temp = encodeURIComponent(termArray[i]);
        		searchQuery += temp;
    		}		    
		    
		    switch(currentEngine) {
  			    case "amazon":
			        URL = URL_INDEX + "?action=link&network=amazon&URL=" + encodeURIComponent("http://www.amazon.de/gp/associates/link-types/searchbox.html?tag=<<<subID>>>&camp=1874&creative=7026&linkCode=qs1&adid=0W65K7ESVSMQMMTQEW43&keyword=" + searchQuery);
			        break;
			    case "ebay":
			        URL = URL_INDEX + "?action=link&network=affilinet&URL=" + encodeURIComponent("http://partners.webmasterplan.com/click.asp?ref=315206&subid=<<<subID>>>&ent=392&site=1382&type=html&hnb=735&prd=yes&mpro=" + encodeURIComponent("http://search.ebay.de/search/search.dll?shortcut=4&query=" + searchQuery));
			        break;
			    case "kelkoo":
			        URL = URL_INDEX + "?action=link&network=tradedoubler&URL=" + encodeURIComponent("http://clkde.tradedoubler.com/click?p=18186&a=1391059&epi=<<<subID>>>&g=642107&td_kpartnerid=8909944&td_siteSearchQuery=" + searchQuery + "&url=" + encodeURIComponent("http://www.kelkoo.de/ctl/do/search"));
			        break;
			    case "google":
			    default:
			        URL = URL_INDEX + "?action=searchresults&sent=1&client=pub-7592189179018121&forid=1&ie=utf-8&oe=utf-8&cof=" + encodeURIComponent("GALT:#008000;GL:1;DIV:#777777;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:11") + "&hl=de&q=" + searchQueryOrig;
			        break;
			 }
			 window.location = URL;
}

function c4_selectCharity(id) {
			if(id != currentCharity) {
				new Ajax.Request(URL_INDEX + "?action=selectCharityNoResponse&id=" + id, {
				  method: 'get', 
				  onSuccess: function(transport) {
				   if(transport.responseText == "OK") {
						currentCharity = id;
						
						Reflection.remove($("currentCharity"));
						$('currentCharity').remove();
						
						currentCharityContainer = new Element('img', {src: dirGPack + 'charities/' + id + '.gif', id: "currentCharity", width: 100});
						$('c4_fb_currentCharityHolder').insert({top: currentCharityContainer});
						Reflection.add(currentCharityContainer,{height: .75, opacity: .7});

						if(Cookie.get('showFuncbar', 0)) {
							c4_showFuncBar();
						}
					} else {
					   alert("Bei der Auswahl des Spendenempfängers ist ein Fehler aufgetreten.\nBitte versuchen Sie es später noch einmal oder wählen Sie einen anderen Empfänger aus.");
					}
				  },
				 onFailure: function() {
				 	alert("Bei der Auswahl des Spendenempfängers ist ein Fehler aufgetreten.\nBitte versuchen Sie es später noch einmal oder wählen Sie einen anderen Empfänger aus.");
				  }
				});
			}
			$('c4_fb_charitySelectionList').fade({duration: 0.5});
}
		
function c4_selectPartner(id) {
			window.open(URL_INDEX + "?action=link&partner=" + id, "_blank");
			//window.location = URL_INDEX + "?action=link&partner=" + id;
}

		/* ToDo: Bei Charity: Via DOM die Kategorie-IDs finden und aufklappen */
function c4_showSelectionList(type) {
			if(type == null) { type = "partner"; }
			if (type == "partner") {
				$('c4_fb_charitySelectionList').fade({duration: 0.35});
			}
			else {
				$('c4_fb_partnerSelectionList').fade({duration: 0.35});
			}
			var pos = Position.get($('c4_fb_' + type + 'Selector'));
			if (Prototype.Browser.IE) Position.set($('c4_fb_' + type + 'SelectionList'), {left: pos.left-2, top: pos.top-2});
			//else if (Prototype.Browser.IE && type == 'charity') Position.set($('c4_fb_' + type + 'SelectionList'), {left: pos.left-2, top: pos.top-2});
			else Position.set($('c4_fb_' + type + 'SelectionList'), {left: pos.left, top: pos.top});
			if($('c4_fb_' + type + 'SelectionList').empty()) {
				new Ajax.Request(URL_INDEX + "?action=get" + type + "Category", {
				//new Ajax.Request("http://www.clicks4charity.net/funcbar/funcbartest.js", {
				  method: 'get', 
				  onSuccess: function(transport) {
						$('c4_fb_' + type + 'SelectionList').insert(transport.responseText);
						//$('c4_fb_' + type + 'SelectionList').insert(new_content);
						if (type == "partner") {window.setTimeout("c4_Subcategory_init('partner')",1000);} // needs time before beeing able to access the elements just written
						if (type == "charity") {window.setTimeout("c4_Subcategory_init('charity')",1000);} // needs time before beeing able to access the elements just written
						//if(type == "charity") {
						//	c4_switchSubcategory(1, 'charity');
						//	c4_switchSubcategory(2, 'charity');
						//}
				  },
				 onFailure: function() {
				 	alert("Es ist ein Fehler aufgetreten.\nBitte versuchen Sie es später noch einmal.");
				 }
				});
			} 
			//else {
				//if(type == "charity") {
				//	c4_switchSubcategory(1, 'charity');
				//	c4_switchSubcategory(2, 'charity');
				//}
			//}
			$('c4_fb_' + type + 'SelectionList').appear({duration: 0.35});
}
		
		// elements have to be accessed once before first (real) use
function c4_Subcategory_init(type) {
			if(type == null) { type = "partner"; }
			for(var i=1; i<=$('c4_fb_'+type+'_selector_content').descendants().length/9; i++) {		 
			   	// run through all id's (problem: the id's in the partner list are not related to the position)
			   	var idd = $('c4_fb_'+type+'_selector_content').descendants()[(i-1)*9+4].readAttribute('id');
			   	idd = idd.substring(idd.lastIndexOf("Title")+5,idd.length);
			   	$('c4_' + type + 'Subcategory' + idd).hide();
			   }	
}

function c4_switchSubcategory(id, type, fade) {
			if(type == null) { type = "partner"; }
			if(fade == null) { fade = false; }
			
			if(fade || $('c4_' + type + 'Subcategory'+id).visible()) {
				if (type == "partner" || type == "charity") {
					$('c4_' + type + 'Subcategory'+id).fade({duration: 0.35});
					$('c4_fb_'+type+'_selector_list'+id).fade({duration: 0.35});
					}
				else { $('c4_' + type + 'Subcategory'+id).blindUp({duration: 0.35}); }
				$('c4_' + type + 'Subcategory'+id+'Arrow').writeAttribute("src", dirGPack+"website/fb_selector_arrow_inactive.png");
			} 
			else {
			   // fade open subcategories
			   for(var i=1; i<=$('c4_fb_'+type+'_selector_content').descendants().length/9; i++) {		 
			   	// run through all id's (problem: the id's in the partner list are not related to the position)
			   	var idd = $('c4_fb_'+type+'_selector_content').descendants()[(i-1)*9+4].readAttribute('id');
			   	idd = idd.substring(idd.lastIndexOf("Title")+5,idd.length);
			   	// if visible, fade
			   	if ($('c4_' + type + 'Subcategory'+idd).visible()) { c4_switchSubcategory(idd,type,true); }
			   }
			   // fill and show subcategory
			   if($('c4_' + type + 'Subcategory'+id).empty()) {
					new Ajax.Request(URL_INDEX + "?action=get" + type + "Subcategory&id=" + id, {
					  method: 'get', 
					  //crossSite: true,
					  onSuccess: function(transport) {
					  		// ??? need preload!
					  		$('c4_' + type + 'Subcategory'+id).insert(transport.responseText);
					  		//$('c4_' + type + 'Subcategory'+id).insert(new_content);
					  		$('c4_' + type + 'Subcategory'+id+'Arrow').writeAttribute("src", dirGPack+"website/fb_selector_arrow_active.png");
							if(type == "partner" || type == "charity") {
								var pos1 = Position.get($('c4_fb_' + type + 'Selector'));
								var pos2 = Position.get($('c4_fb_'+type+'_selector_catTitle' + id));
								if (type == "partner") {Position.set($('c4_fb_'+type+'_selector_list'+id), {left: 0, top: pos2.top-pos1.top-15});}
								if (type == "charity") {Position.set($('c4_fb_'+type+'_selector_list'+id), {left: 0, top: pos2.top-pos1.top-36});}
								$('c4_fb_'+type+'_selector_list'+id).appear({duration: 0.35});
								$('c4_' + type + 'Subcategory'+id).appear({duration: 0.35});
							}
							else { $('c4_' + type + 'Subcategory'+id).blindDown({duration: 0.35}); }
					  },
					 onFailure: function() {
					 	alert("Bei der Auswahl ist ein Fehler aufgetreten.\nBitte versuchen Sie es später noch einmal.");
					 }
					});
				} 
				// show subcategory
				else {
					$('c4_' + type + 'Subcategory'+id+'Arrow').writeAttribute("src", dirGPack+"website/fb_selector_arrow_active.png");
					if(type == "partner" || type == "charity") {
						//var pos1 = Position.get($('c4_fb_' + type + 'Selector'));
						//var pos2 = Position.get($('c4_fb_'+type+'_selector_catTitle' + id));
						//if (type == "partner") {Position.set($('c4_fb_'+type+'_selector_list'+id), {left: 0, top: pos2.top-pos1.top-15});}
						//if (type == "charity") {Position.set($('c4_fb_'+type+'_selector_list'+id), {left: 0, top: pos2.top-pos1.top-36});}
						$('c4_fb_'+type+'_selector_list'+id).appear({duration: 0.35});
						$('c4_' + type + 'Subcategory'+id).appear({duration: 0.35});
					}
					else { $('c4_' + type + 'Subcategory'+id).blindDown({duration: 0.35}); }
				}
			}
			var pos = Position.get($('c4_fb_' + type + 'Selector'));
			if (Prototype.Browser.IE) Position.set($('c4_fb_' + type + 'SelectionList'), {left: pos.left-2, top: pos.top-2});
			else Position.set($('c4_fb_' + type + 'SelectionList'), {left: pos.left, top: pos.top});
}

function c4_activateEngine(engine) {
        if(engine == null) { engine = "google"; }
        
        currentEngine = engine;

	$('c4_engine_google').writeAttribute('src', dirGPack + 'engines/funcbar_google_inactive.png');
	$('c4_engine_ebay').writeAttribute('src', dirGPack + 'engines/funcbar_ebay_inactive.png');
	$('c4_engine_kelkoo').writeAttribute('src', dirGPack + 'engines/funcbar_kelkoo_inactive.png');
	$('c4_engine_amazon').writeAttribute('src', dirGPack + 'engines/funcbar_amazon_inactive.png');

        $('c4_engine_' + engine).writeAttribute('src', dirGPack + 'engines/funcbar_' + engine + '_active.png');
        
        c4_fb_inputField('click_input','enginesIF');
        
        Cookie.set('activeEngine', engine);
}

// functions and variables for partnerAc, engines IF
var partnerAcActive = false;
var enginesIFActive = false;

function c4_fb_inputField(event,type) {
	switch (event) {
		case "mouseover":
			$('c4_fb_' + type + '_image').setAttribute('src', dirGPackLang + 'website/fb_input_active2.png');
			break;
		case "mouseout":
			if (type == "partnerAc") {
				if (!partnerAcActive) {
					$('c4_fb_' + type + '_image').setAttribute('src', dirGPackLang + 'website/fb_input_inactive2.png');
					}
				}
			else if (type == "enginesIF") {
				if (!enginesIFActive) {
					$('c4_fb_' + type + '_image').setAttribute('src', dirGPackLang + 'website/fb_input_inactive2.png');
					}
				}
			break;
		case "click_input":
			if (type == "partnerAc") { 
				partnerAcActive = true;	
				// ToDo: Localisation			
				if ($F('c4_fb_partnerAc_input') == "Name des Anbieters") {
					$('c4_fb_partnerAc_input').clear();
				} 
			}
			else if (type == "enginesIF") { enginesIFActive = true; }
			// because IE has a wierd background:transparent implementation
			$('c4_fb_' + type + '_input').enable();
			$('c4_fb_' + type + '_input').select();
			c4_fb_inputField("mouseover",type);
			break;
		case "blur_input":
			if (type == "partnerAc") { 				
				// ToDo: Localisation
				if($F('c4_fb_partnerAc_input') == "") {
				   	$('c4_fb_partnerAc_input').value = "Name des Anbieters";
				}
				partnerAcActive = false; 
				c4_fb_autocomp.setTimeoutPeriod(500); 
				c4_fb_autocomp.resetTimeout();
				c4_fb_autocomp.setTimeoutPeriod(autoTimeout);				
				}
			else if (type == "enginesIF") { enginesIFActive = false;}
			c4_fb_inputField("mouseout",type);
			break;
		case "mousedown_submit":
			if (type == "partnerAc") { partnerAcActive = true; }
			else if (type == "enginesIF") { enginesIFActive = true;}
			c4_fb_inputField("mouseover",type);			
			if (type == "partnerAc") { }
			else if (type == "enginesIF") { c4_searchRequest(); }
			break;			
		default:
			break;
	}
}
		   
var autoTimeout = 10000;
		   
// Initialisierung

Event.observe(window, 'load', function() {	
		   
		   Reflection.add($("currentCharity"),{height: .75, opacity: .7});
			
			// ToDo: NoResults-Meldung lokalisieren
			c4_fb_autocomp = new AutoComplete('c4_fb_partnerAc_input', {
				script: URL_INDEX + '?action=getPartnersAutocomplete&json=true&limit=10&',
				varname:'input',
				json:true,
				minchars:1,
				shownoresults:true,
				noresults:"Keine Partner gefunden",
				delay:250,
				timeout:autoTimeout,
				maxentries:10,
				useNotifier: true,
				callback: function (obj) { c4_selectPartner(obj.id); }
			});
			
			if(Cookie.get('activeEngine') == null) {
				Cookie.set('activeEngine', "google");
			}
	
	
		    // Charity-Auswahl
		    Event.observe('c4_fb_charitySelector', 'click', function(){
					c4_showSelectionList('charity');
		    });
		    /*
		    Event.observe('c4_fb_charitySelectionListTable', 'mouseout', function(){
		        $('c4_fb_charitySelectionList').fade({duration: 0.25});
		    });
		    */

				// IF ACTIVE MENU != 17
				if (activeMenu != 17) {
		    // Partner-Auswahl
		    Event.observe('c4_fb_partnerSelector', 'click', function(){
		        c4_showSelectionList('partner');
		    });
		    /*
		    Event.observe('c4_fb_partnerSelectionListTable', 'mouseout', function(){
		        $('c4_fb_partnerSelectionList').fade({duration: 0.25});
		    });
		    */

			 // Engine-Auswahl
		    Event.observe('c4_engine_google', 'click', function(){
		        c4_activateEngine("google");
		    });
		    Event.observe('c4_engine_ebay', 'click', function(){
		        c4_activateEngine("ebay");
		    });
		    Event.observe('c4_engine_kelkoo', 'click', function(){
				  c4_activateEngine("kelkoo");
		    });
		    Event.observe('c4_engine_amazon', 'click', function(){
				  c4_activateEngine("amazon");
		    });
		    // Sucheingabe bei Engines
		    /*Event.observe('c4_fb_enginesIF_input', 'mouseover', function(){ 
		        c4_fb_inputField('mouseover','enginesIF');
		    });
		    Event.observe('c4_fb_enginesIF_input', 'mouseout', function(){ 
		        c4_fb_inputField('mouseout','enginesIF');
		    });*/
		    Event.observe('c4_fb_enginesIF_input', 'click', function(){ 
		        c4_fb_inputField('click_input','enginesIF');
		    });
		    Event.observe('c4_fb_enginesIF_input', 'blur', function(){ 
		        c4_fb_inputField('blur_input','enginesIF');
		    });
		    /*Event.observe('c4_fb_enginesIF_submit', 'mouseover', function(){ 
		        c4_fb_inputField('mouseover','enginesIF');
		    });
		    Event.observe('c4_fb_enginesIF_submit', 'mouseout', function(){ 
		        c4_fb_inputField('mouseout','enginesIF');
		    });*/
		    	// mousedown, because blur_input will be call and the inactive image displayed
		    	// to do: fix this
		    Event.observe('c4_fb_enginesIF_submit', 'mousedown', function(){ 
		        c4_fb_inputField('mousedown_submit','enginesIF');
		    });
		    /*Event.observe('c4_fb_enginesIF_image', 'mouseover', function(){ // necessary for IE
		        c4_fb_inputField('mouseover','enginesIF');
		    });
		    Event.observe('c4_fb_enginesIF_image', 'mouseout', function(){ 
		        c4_fb_inputField('mouseout','enginesIF');
		    });*/
		    Event.observe('c4_fb_enginesIF_image', 'click', function(){ 
		        c4_fb_inputField('click_input','enginesIF');
		    });
		    
		    // Sucheingaben abschicken bei keypress ENTER
		    Event.observe('c4_fb_enginesIF_input', 'keypress', function(Event){
		        if(Event.keyCode == 13) {
		        	c4_searchRequest();
		        }		    
		    });
		   
		    }
		    // END IF ACTIVE MENU != 17
		    
		    // Sucheingabe bei Partner
		    /*Event.observe('c4_fb_partnerAc_input', 'mouseover', function(){ 
		        c4_fb_inputField('mouseover','partnerAc');
		    });
		    Event.observe('c4_fb_partnerAc_input', 'mouseout', function(){ 
		        c4_fb_inputField('mouseout','partnerAc');
		    });*/
		    Event.observe('c4_fb_partnerAc_input', 'click', function(){ 
		        c4_fb_inputField('click_input','partnerAc');
		    });
		    Event.observe('c4_fb_partnerAc_input', 'blur', function(){ 
		        c4_fb_inputField('blur_input','partnerAc');
		    });
		    /*Event.observe('c4_fb_partnerAc_submit', 'mouseover', function(){ 
		        c4_fb_inputField('mouseover','partnerAc');
		    });
		    Event.observe('c4_fb_partnerAc_submit', 'mouseout', function(){ 
		        c4_fb_inputField('mouseout','partnerAc');
		    });*/
		    // mousedown, because blur_input will be call and the inactive image displayed
		    	// to do: fix this
		    Event.observe('c4_fb_partnerAc_submit', 'mousedown', function(){ 
		        c4_fb_inputField('mousedown_submit','partnerAc');
		    });
		    /*Event.observe('c4_fb_partnerAc_image', 'mouseover', function(){ // necessary for IE
		        c4_fb_inputField('mouseover','partnerAc');
		    });
		    Event.observe('c4_fb_partnerAc_image', 'mouseout', function(){ 
		        c4_fb_inputField('mouseout','partnerAc');
		    });*/
		    Event.observe('c4_fb_partnerAc_image', 'click', function(){ 
		        c4_fb_inputField('click_input','partnerAc');
		    });

		    

});