
/*
 * Result List V2 Zoom/Filter script
 */

if (typeof(Prototype) == "undefined") 
    throw "CLASSNAME requires Prototype";

var Zoom = Class.create({

    initialize: function(options){
    
        zoom = this;
        
        this.opt = Object.extend({
            name: 'Zoom',
            debug: debug, // set in lib.js
            scrollto: false,
            timeout: 45000,
            loadingmessage: '<div id="zoom_ajaxloader" style="position:relative;text-align:center;color:#0c2577;"><img alt="{loading}" src="/5/resources/images/ajaxindicator.gif" style="margin:2px;" /></div>'
        }, options || {});
        
        this.name = this.opt.name;
        this.debug = this.opt.debug;
        
        this.log("NEW Zoom '" + this.name + "' start " + new Date());
		
        t = Object.extend(this,{
            'loading': 'Please wait...'
        });
        
        this.opt.loadingmessage = this.opt.loadingmessage.replace("{loading}",t['loading']);
        
        // set loading so user doesn't start filtering when page hasn't fully loaded yet.
        this.doWait();
        
        //this.setlabels();	// disabled because it is quicker to add the click even in the cf code
		
    	// IE6 is too slow / labels don't have spaces in DE !!
        //if(!isIE6 && $(document.body).hasClassName("locale_de_de")){
        	 //this.log("cleanlabels...");
        	//this.cleanlabels();
		//}
			
		var previousContainer;
		
		//closeLast = this.closeLast;
		showMore = this.showMore;
		showMoreCategories = this.showMoreCategories;
		toggleContainer = this.toggleContainer;
		toggleButton = this.toggleButton;
		toggleZoom = this.toggleZoom;		
		
		//this.log("initiated");
        
    },
    // uses lib.js "dump" function
    log: function(msg){
        if (typeof this == 'object' && this.debug && typeof dump == 'function' && typeof msg != 'undefined') {
            if (typeof isIE6 != 'undefined' && isIE6) {
                // disabled IE6 debug alerts
                //alert(this.name + " : " + msg);
            }
            else {
                dump(this.name + " : " + msg);
            }
        }
    },
    
    setTimer: function(action,timeout){
    	//zoom.log("setTimer action: " + action + " / timeout: " + timeout);
    	zoom.timer = setTimeout(action,timeout);
	},	
	
	doWait: function(){		
		zoom.log("doWait: " + new Date());		
		// default action
		var timeoutAction = "zoom.showError('doWait','timed out...')";
		var timeoutTime = zoom.opt.timeout;				
		// wait for timeout before handling it
		zoom.setTimer(timeoutAction,timeoutTime);		
		// create the "loading" message
		if (zoom.opt.loadingmessage && $("zoom_ajaxloader") == null) {
            if ($('panel_zoom') != null) $('panel_zoom').insert(zoom.opt.loadingmessage); // document.body
        }
    },
    
    stopWait: function(){
    	//zoom.log("stopWait: " + new Date());
    	if($("zoom_ajaxloader") != null){
    		$("zoom_ajaxloader").remove();
    	}	
    	clearTimeout(zoom.timer);
    },
    
    closeLast: function(newLast){
        temp = $('lastCategoryOpened').value;
        if (temp != 0) {
            $(temp).toggle();
        }
        $('lastCategoryOpened').value = newLast;
    },   
    
    showMore: function(divId, urlToLoad){

    	//zoom.log("showMore: " + divId + " / urlToLoad: " + urlToLoad);
    	
    	var ajaxDivId = 'hiddenOffers_' + divId;
        var labelOpenDivId = 'labelOpen_' + divId;
        var labelClosedDivId = 'labelClosed_' + divId;
        var buttonContainer = 'displaymorejobs_' + divId;
        var hidebuttonContainer = false;
		var temp = urlToLoad.split('&');
		var url = urlToLoad;

		// get url without parameters
		if(temp.size()) {
			url = temp.first();
		}
	    
        if(hidebuttonContainer){
	        if($(buttonContainer) != null){
	        	if(urlToLoad != ''){
	        		$(buttonContainer).hide();
	        	}else{
	        		$(buttonContainer).show();
	        	}
	        }
        }
        
        if($(labelOpenDivId) != null){
        	if(urlToLoad != ''){
        		$(labelOpenDivId).hide();
        	}else{
        		$(labelOpenDivId).show();
        	}
        }
        
        if($(labelClosedDivId) != null){
        	if(urlToLoad != ''){
        		$(labelClosedDivId).show();
        	}else{
        		$(labelClosedDivId).hide();
        	}	
        }

        if($(ajaxDivId) != null){
	        
        	$(ajaxDivId).toggle().select(".AjaxLoader").each(function(item){
	            item.update(zoom.opt.loadingmessage);
	        });
		
	        new Ajax.Updater(ajaxDivId, url, { method: 'post', parameters: urlToLoad.toQueryParams() });
        }
        
    },
    
    showMoreCategories: function(divId){
        var ajaxDivId = 'hiddenCategories_' + divId;
        var labelOpenDivId = 'labelOpen_' + divId;
        $(labelOpenDivId).toggle();
        $(ajaxDivId).toggle();
    },
    
    toggleContainer: function(id){    
        var el = $(id + 'Container');
		
		//zoom.log("toggleContainer: " + id);
        if (el == null) return false;
		
		if (typeof zoom.previousContainer != 'undefined' && zoom.previousContainer != id) {
			//zoom.log("zoom.previousContainer " + zoom.previousContainer + " update");
			$(zoom.previousContainer + 'Container').hide().removeClassName('open');
			if(Element.hasClassName($(zoom.previousContainer + 'Button'),"open")){
				zoom.toggleButton(zoom.previousContainer);
			}
		}
		
		el.toggle();
        
 		if(Element.hasClassName(el,"open")){
			//zoom.log("removing open");
			Element.removeClassName(el,'open');
		}else{
			//zoom.log("adding open");
			Element.addClassName(el,'open');
		}	
			
		zoom.toggleButton(id);
		
		if(zoom.opt.scrollto){
			Element.scrollTo(el);
		}
		
		zoom.previousContainer = id;
		
    },
	
	open: function(id){
		var el = $(id);
		el.show();
        Element.addClassName(el,'open');	
	},
	
	close: function(id){
		var el = $(id);
		el.hide();
        Element.removeClassName(el,'open');	
	},
    
    toggleButton: function(id){
        var el = $(id + 'Button');
        //zoom.log("togglebutton: " + id);
        if(Element.hasClassName(el,"open")){
			//zoom.log("removing open");
			Element.removeClassName(el,'open');
		}else{
			//zoom.log("adding open");
			Element.addClassName(el,'open');
		}
	},    
    
    toggleZoom: function(id){
    	var el = $(id);
        //zoom.log("toggleZoom: " + id);
        if (el == null) return false;
		el.toggle();
        Element.toggleClassName(el,'open');		
		zoom.toggleButton(id);  
	},
    
	toggle: function(element){
		$(element).toggle();
	},
	
	checked: function(el){
		if(typeof el == undefined){
			//zoom.log("checked: error: " + typeof el);
			return false;
		}else{	
			////zoom.log("el: " + typeof el);
			if(Element.hasClassName(el,'checked')){
				Element.addClassName(el,'label-checkbox-unchecked');
				Element.removeClassName(el,'checked label-checkbox-checked');
				return true;
			}else{
				Element.removeClassName(el,'label-checkbox-unchecked');
				Element.addClassName(el,'checked label-checkbox-checked');
				return false;
			}
		}	
	},
	
	// not working any more...
    setlabels: function(){
		//zoom.log("setlabels");
		$$('#panel_zoom .label-checkbox-unchecked').each(function(el){
			Event.observe(el, 'click', function(event){
            	//zoom.log("setlabels: click : label-checkbox-unchecked: " + el.inspect());
				zoom.checked(el);	
            });
        });

		$$('#panel_zoom .label-checkbox-checked').each(function(el){
			//zoom.log("setlabels: set : label-checkbox-checked: " + el.inspect());
			Event.observe(el, 'click', function(event){
            	//zoom.log("setlabels: click : label-checkbox-checked: " + el.inspect());
				zoom.checked(el);	
            });
        });
    },
    
    cleanlabels: function(){
    	//zoom.log("cleanlabels");
    	$$('#panel_zoom .checkbox-label').each(function(el){
    		var textnode = el.innerHTML;     		
    		var cleaned;
    		cleaned = textnode.gsub(/[\/,-]+/i, '#{0} ' );    		
    		if(textnode != cleaned){    
    			//zoom.log("cleanlabels: " + textnode + " : updated : " + cleaned);
    			el.update(cleaned.trim());
    		}
    	});
    }
    
});

document.observe('dom:loaded', function(){
	var doZoom = new Zoom();
	zoom.stopWait();
});

Event.observe(window, 'load', function(){
	if(typeof zoom == 'object'){
		zoom.stopWait();		
	}	
});	
