
/*
 * QuickPoll Controller Script  
 */

if (typeof(Prototype) == "undefined" || typeof(domFunction) == "undefined") 
    throw ("QuickPoll : Required scripts are missing");

var QuickPoll = Class.create({
    initialize: function(name, options){
        this.name = name;
        QuPo = this;
        this.thO = Object.extend({
            debug: debug,
            script: "/index.cfm?event=QuickPoll.dspQuickPoll_AJAX",
            loadingmessage: '<div id="quickpoll_ajaxloader"><em>Loading...</em></div>',
            asynchronous: false,
            caching: true,
            useLightBox : true,
            LightBoxHeight : 400,
            LightBoxWidth : 500
        });
        this.cache = null;
        this.openContent = null;
        this.refresh = true;
        this.thD = this.thO.debug;
        this.caching = this.thO.caching;
        this.QPB = this.QuickPollBlock = $('QuickPollBlock');
        this.QP = this.QuickPoll = $('QuickPoll');
        this.QC = this.QuickPollContainer = $('QuickPollContainer');
		
        this.loadingcontainer = this.QP;
        
        this.IsIE6 = (typeof isIE6 != 'undefined' && isIE6 == true) ? true : false;
        this.script = this.thO.script;
        
        this.useLightBox = this.thO.useLightBox;
        
        // other page elements that influence QP
        this.pc = $('Panel_Communities');
        this.pf = $('Panel_Featured');
        this.pt = $('Panel_TakeAction');
        this.tl = $('TopListings');
        this.rl  = $('RecruiterLogos');        
        
        // required elements for the QP
        if(this.QPB == null || this.QP == null || this.QC == null){
        	return false;
        }        
        
        this.QPparentNode = $(this.QPB.parentNode);	
        
		if(this.pt != null){
			this.ptHeight = this.pt.getHeight();
        	//this.log("saved height of PanelTakeAction: " + this.ptHeight);
		}else{
			// set the height of the parent of the QPB
			this.ptHeight = this.QPparentNode.getHeight();
			//this.log("ptHeight: " + this.ptHeight);			
			//this.log("saved height of QPparentNode: " + this.QPparentNode.inspect() + " = " + this.ptHeight +"px");
		}
        
        if (typeof ajaxDoEvent != 'function') 
            ajaxDoEvent = QuPo.ajaxDoEvent;
        if (typeof openQuickpoll != 'function') 
            openQuickpoll = QuPo.openQuickpoll;
        if (typeof closequickPoll != 'function') 
            closequickPoll = QuPo.closequickPoll;
        if (typeof SubmitQuickPoll != 'function') 
            SubmitQuickPoll = QuPo.SubmitQuickPoll;
        if (typeof QuickPollUpdate != 'function') 
            QuickPollUpdate = QuPo.QuickPollUpdate;
        if (typeof AddComment != 'function') 
            AddComment = QuPo.AddComment;
        if (typeof signalInappropriate != 'function') 
            signalInappropriate = QuPo.signalInappropriate;
        if (typeof popupAlert != 'function') 
            popupAlert = QuPo.popupAlert;
        if (typeof showCurrentQuickPoll != 'function') 
            showCurrentQuickPoll = QuPo.showCurrentQuickPoll;
        
        this.log(new Date());
        
        this.closedContent = (typeof this.QP != 'undefined') ? this.QP.innerHTML : null;
        
        if (this.thO.asynchronous) {
            //this.log("asynchronously");
            this.ajaxDoEvent();
            //this.thO.asynchronous = false;
        }        
        
    },
    
    
    log: function(msg){
    	if(!this.thD){
    		return false;	
    	}	
        if (typeof this == 'object' && typeof dump == 'function' && typeof msg != 'undefined') {
            if (typeof isIE6 != 'undefined' && isIE6) {
                //alert(this.name + " : " + msg);
            }
            else {
                dump(this.name + " : " + msg);
            }
        }
    },
    
    
    openQuickpoll: function(handler){
        
    	var content;
    	
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        
        if (QuPo.script == null) {
        	//    return false;
        	//QuPo.log("QuPo.script: " + typeof QuPo.script);
        }
        
        //QuPo.log("handler : " + typeof QuPo.script);
        
        // Disabled hiding of other elements
	    if(!QuPo.useLightBox){
	    	
        	//QuPo.log("openQuickpoll: PanelCommunities first?");
	        if (QuPo.pc != null && !QuPo.pc.hasClassName("first")) {
	            //QuPo.log("openQuickpoll: hide PanelCommunities");
	            QuPo.pc.style.visibility = 'hidden';
	        }
	        //QuPo.log("openQuickpoll: PanelFeatures first?");
	        if (QuPo.pf != null && !QuPo.pf.hasClassName("first")) {
	            //QuPo.log("openQuickpoll: hide PanelFeatures");
	            QuPo.pf.style.visibility = 'hidden';
	        }
	        //QuPo.log("openQuickpoll: TopListings first?");
	        if (QuPo.tl != null && !QuPo.tl.hasClassName("first") && (QuPo.tl.parentNode == QuPo.QPB.parentNode)) {
	            //QuPo.log("openQuickpoll: hide TopListings");
	            QuPo.tl.style.visibility = 'hidden';
	        }
	        //QuPo.log("openQuickpoll: Panel TakeAction last?");
	        if (QuPo.pt != null && QuPo.pt.hasClassName("last")) {
	            //QuPo.log("openQuickpoll: hide PanelFeatures (last)");
	            QuPo.pt.select('div.columnContent div.columnInside').collect(function(element){
	                element.hide();
	            });
	        }
	    }
	        
        if (QuPo.cache == null) {
            //QuPo.log("openQuickpoll: no cache: updating content");
            QuPo.refresh = true;
        }
        else {
            QuPo.refresh = false;
        }
        
        Element.addClassName(QuPo.QPB,"loading");
        
        //QuPo.log("openQuickpoll: update QC: refresh " + QuPo.refresh);
            
        // get the QuickPoll content
        content = QuPo.QuickPollUpdate();      
        
        if(content != null && content != ''){
        
           // hide the teaser
	       if(!QuPo.useLightBox){
	    	   QuPo.QP.hide();
	       }
	       
	       //QuPo.log("openQuickpoll: show QP container");
	        
	       Element.removeClassName(QuPo.QPB,"loading"); 
	       
	       if(!QuPo.useLightBox){
	    	   QuPo.QC.show();
	       }
	    	   
	       Element.addClassName(QuPo.QC,"quickPollOpen");
	       Element.addClassName(QuPo.QPB,"open");       
	       
	       if(QuPo.useLightBox){	    	  
	    	   
	    	   //QuPo.log("typeof Lightbox : " + typeof Lightbox );
	    	  
	    	    if(typeof Lightbox != 'undefined' && typeof Lightbox == 'function'){
		       		
		    	   try{
		    		   
		    		   QuPo.lightbox = new Lightbox({
		    			   identifier:"clickbox"+QuPo.name,
		    			   type:"message",
		    			   dialog:QuPo.QC.innerHTML,
		    			   height:QuPo.thO.LightBoxHeight,
		    			   width:QuPo.thO.LightBoxWidth,
		    			   heightAuto:false
		    		   });
		    		   QuPo.lightbox.activate();
		       		
		    	   }catch(e) {
		    		   //QuPo.log("Problem: Cannot create Lightbox : " + e.message);
		    	   }
		       		
		       }else{		    	   
		    	   //QuPo.log("Problem: Cannot create Lightbox : typeof Lightbox : " + typeof Lightbox );
		    	   return false;		    	   
		       }   
			       
		       // remove the container from the DOM because we are going to show it in the lightbox
			   try {
			   	QuPo.QC.remove();
			   } catch(e){  	
			   }
		       
	       }
		       
	       //QuPo.log("openQuickpoll: done");
	       return true;
    
        }else{        	
        	//QuPo.log("openQuickpoll: error: no content");
        	return false;
        }
			
    },   
    
    
    setPanelHeight: function(){
        
    	var margin = 150; // fixed margin
    	
        QuPo.QCHeight = parseFloat(QuPo.QC.getHeight());
        
        // when we have the recruiterlogo's, add the height to the margin
		/*if(Element.hasClassName($(document.body),"frontV2a") && QuPo.RecruiterLogos != null){
			//QuPo.log("Added height of the recruiterlogos as margin: " + QuPo.RecruiterLogos.getHeight());
			margin = parseFloat(margin) + parseFloat(QuPo.RecruiterLogos.getHeight());
		} */
        
        //QuPo.log("setPanelHeight: height QC: " + Number(QuPo.QCHeight) + "px");
        
		 var addedMargin = parseFloat(QuPo.QCHeight) + parseFloat(margin);
		
		if (QuPo.pt != null) {
			QuPo.pt.setStyle({
				height: QuPo.QCHeight + margin + 'px'
			});
			//QuPo.log("setPanelHeight: height Panel_TakeAction " + addedMargin + "px");
		}else{
			QuPo.QPparentNode.setStyle({
				height: QuPo.QCHeight + margin + 'px'
			});
			//QuPo.log("setPanelHeight: height QPparentNode " + addedMargin + "px");
		}      
        
    },    
    
    closequickPoll: function(handler){
    	
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        
        if (QuPo.script == null) {
            return false;
        }
        
		//QuPo.log("closequickPoll: hide QC;  QuPo.script : " +  typeof QuPo.script);
        
        // hide the QuickPoll Container
        if(QuPo.QC != null){
        	QuPo.QC.hide();
        }
        	
        Element.removeClassName(QuPo.QC,"quickPollOpen");
        Element.removeClassName(QuPo.QPB,"open");        
        
        if(QuPo.useLightBox && QuPo.lightbox != 'undefined'){
        	QuPo.lightbox.deactivate();        	
        }
        
        if(!QuPo.useLightBox){
			
        	//QuPo.log("closequickPoll: hide QP; put old content back in QC");
	        if (QuPo.closedContent) 
	            
	        	QuPo.QC.update(QuPo.closedContent);
			
			if (QuPo.pc != null) {
			 	//QuPo.log("closequickPoll: show pc");
				QuPo.pc.style.visibility = 'visible';
			}
			if (QuPo.pf != null) {
				//QuPo.log("closequickPoll: show pf");
				QuPo.pf.style.visibility = 'visible';
			}
			if (QuPo.tl != null) {
				//QuPo.log("closequickPoll: show tl");
				QuPo.tl.style.visibility = 'visible';
	        }
	        
			if(QuPo.pt != null){
				//QuPo.log("closequickPoll: reseting PanelTakeAction height to " + QuPo.ptHeight);
			}else{
				//QuPo.log("closequickPoll: reseting QPparentNode " + QuPo.QPparentNode.inspect() + " height to " + QuPo.ptHeight + "px");
			}
			
			QuPo.QPparentNode.setStyle({
	            height: QuPo.ptHeight + 'px'
	        });
        }
			
	   	//QuPo.log("closequickPoll: show CP");
        if(QuPo.QP != null){
        	QuPo.QP.show();
        }
        	
		//QuPo.log("closeQuickpoll: done");
		
    },    
    
    QuickPollContentChanged: function(content, cache){
        return (content != cache) ? true : false;
    },    
    
    QuickPollUpdate: function(handler, params){
        
    	var content;
        var result;
        var target; // used for the lightbox content update
        
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        
        if (QuPo.script == null)
            return false;
        
        if (QuPo.refresh) {       	
			
            if (typeof params == 'undefined') {               
            	//QuPo.log("QuickPollUpdate: ajaxDoEvent");
                result = QuPo.ajaxDoEvent(handler);
            } else {
            	//QuPo.log("QuickPollUpdate: ajaxDoEventParam");
                result = QuPo.ajaxDoEventParam(handler, params);
            }
            
            if(typeof result != undefined){
            	content = result;
            	// reset open cache
    			QuPo.openContent = content;
    			
            }else{
            	content = (typeof QuPo.cache != undefined) ? QuPo.openContent : null;
            }
            
            //QuPo.log("QuickPollUpdate: refresh: result: " + typeof result + " : content: " + content);
            
            if(content != null){
            	QuPo.openContent = content;
            }	
            
        }else{
            //QuPo.log("QuickPollUpdate: re-using openContent");
            content = QuPo.openContent;
        }
        
        try {
        	
        	if(content != null){
            	
            	//QuPo.log("QuickPollUpdate: update the QP container: " + content.length);
            	result = QuPo.QC.update(unescape(content));
            	
            	// refresh the lightbox
            	if(QuPo.useLightBox){
            		
            		// find the lightbox dialog container
            		target = $$("#dialog_message .theText")[0];
            		
            		//alert("target: " + typeof target);
            		
            		if(typeof target == 'object'){
                		//QuPo.log("QuickPollUpdate: using lightbox " + result.inspect() + " copying to " + target.inspect());
            			target.update(unescape(content));
            		}
            		
            	}
            	
            	// display QC container
            	if(!QuPo.useLightBox && QuPo.QC != null){
            		//alert("QuPo.QC: " + typeof QuPo.QC);
            		QuPo.QC.show();
            	}
            		
            	QuPo.stopWait();
            	
            	//alert("return: " + result);
            	
            	return result;
            
            }else{
            	//QuPo.log("QuickPollUpdate: content = null");
            	QuPo.stopWait();
            	return QuPo.openContent;
            }
            
        } catch (e) {
            //QuPo.log("Error: QuickPollUpdate: " + e.message);
            QuPo.stopWait();
            return e.message;
        }      
        
    },    
    
    showCurrentQuickPoll: function(){
        QuPo.QuickPollUpdate('/index.cfm?event=QuickPoll.dspQuickPoll_AJAX');
    },    
    
    ajaxDoEvent: function(handler){
    	
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        var response;
        
        if (QuPo.script == null) 
            return false;
        
        //QuPo.log("ajaxDoEvent: " + QuPo.script + " / QuPo.thO.asynchronous: " + QuPo.thO.asynchronous);
        
        try{
	        new Ajax.Request(QuPo.script, {
	            method: 'post',
	            asynchronous: QuPo.thO.asynchronous,
	            onFailure: function(){
	        		response = "Error: Ajax Failure!";  
	        		//QuPo.log(response);
	            },
	            onCreate: function() {
	            	 QuPo.doWait();
	            	 //QuPo.log("ajaxDoEvent: Ajax Created");
				},
	            onSuccess: function(transport){
					response = escape(transport.responseText.evalJSON());
					/*if(response != null && response.length){
						////QuPo.log("ajaxDoEvent: response: " + response.length);
						QuPo.cache = response;
	                	////QuPo.log("ajaxDoEvent: Ajax Success!");
		            }else{
	                	////QuPo.log("Error: ajaxDoEvent: response null");
		            }*/              
	            }
	        });
	        
       }catch(e){
        	//QuPo.log("Error: ajaxDoEvent: " + e.message);
       }
       
       QuPo.stopWait();
       return response;       
       
    },    
    
    ajaxDoEventParam: function(handler, params){
    	
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        var response;
        if (QuPo.script == null) 
            return false;       
        if (!QuPo.thO.asynchronous) 
            QuPo.doWait();
        //QuPo.log("ajaxDoEventParam: Ajax.Request " + QuPo.script + " / QuPo.thO.asynchronous: " + QuPo.thO.asynchronous);
        try{
	        new Ajax.Request(QuPo.script, {
	            method: 'post',
	            parameters: params,
	            asynchronous: QuPo.thO.asynchronous,
	            onFailure: function(){
	        		response = "Error: Ajax Failure!";    
	        		//QuPo.log(response);
	            },
	            onCreate: function() {
	           	 	QuPo.doWait();
	           	 	//QuPo.log("Ajax Created");
				},
	            onSuccess: function(transport){
	                response = escape(transport.responseText.evalJSON());
	                /*if(response != null && response.length){
	                	QuPo.cache = response;
	                	//QuPo.log("Ajax Success!");
	                }else{
	                	//QuPo.log("Error: response null");
		            }*/
	            }
	        });
        }catch(e){
        	//QuPo.log("Error: ajaxDoEventParam: " + e.message);
        }
        
        QuPo.stopWait();
        return response;
        
    },    
    
    SubmitQuickPoll: function(handler){
        var pa = document.QuickPollForm.PollAnswer;
        if (typeof pa == 'undefined') {
        	//QuPo.log("SubmitQuickPoll: pa is undefined");
        	return false;
        }           
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        if (QuPo.script == null) {
        	//QuPo.log("SubmitQuickPoll: no script");
        	return false;
        }    
        ////QuPo.log("SubmitQuickPoll: pa : " + typeof pa);
        var submitted = false;
        for (i = 0; i < pa.length; i++) {
           //QuPo.log("SubmitQuickPoll: checking submitted value " + pa[i].value + " ( " + i + ") : " + pa[i].checked);
            if (pa[i].checked == true) {
                var params = 'PollAnswer=' + pa[i].value;
                //QuPo.log("SubmitQuickPoll: ajaxDoEventParam : handler: " + QuPo.script + ", params : " + params);
                submitted = true;
            }
        }
        if (submitted) {
            QuPo.refresh = true;
            QuPo.QuickPollUpdate(handler, params);
            //QuPo.log("SubmitQuickPoll: reset openContent");
            QuPo.openContent = QuPo.cache;
        }
    },
    
    
    AddComment: function(handler){
        QuPo.CF = $('CommentsForm');
        if (QuPo.CF == null) 
            return false;
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        if (QuPo.script == null) 
            return false;
        //QuPo.log("AddComment: " + QuPo.CF.id);
        var params = QuPo.CF.serialize();
        //QuPo.log("AddComment: ajaxDoEventParam : handler: " + QuPo.script + ", params : " + params);
        QuPo.refresh = true;
        QuPo.QuickPollUpdate(QuPo.script, params);
    },
    
    
    doWait: function(container){
       
    	if(container != ''){    		
    		// container must be an element with an id
    		if($(container) != null){
    			QuPo.loadingcontainer = container;
    		}
    	}
    	
    	//QuPo.log("doWait: " + new Date());
        
        if (QuPo.thO.loadingmessage && QuPo.QPB != null) {
            
        	// look for existing loading div
        	if($("quickpoll_ajaxloader") == null ){
        		
        		//QuPo.log("doWait: this.QP: " + this.QP.inspect() + " / QuPo.thO.loadingmessage: " + QuPo.thO.loadingmessage);
        		
	        	//if(Element.hasClassName(this.QP,"quickPollClose") || Element.hasClassName(this.QC,"quickPollOpen")){
        		QuPo.loadingcontainer.insert(QuPo.thO.loadingmessage);
	        	//}
        	
	        	//QuPo.loadingdiv = $("quickpoll_ajaxloader");
	        	
        	}	
        }
        
    },
    
    
    stopWait: function(){
        //QuPo.log("stopWait: " + new Date());
        if ($("quickpoll_ajaxloader") != null) {
        	$("quickpoll_ajaxloader").remove();
        }
    },
    
    
    signalInappropriate: function(handler){
        QuPo.iF = $('inappropriateForm');
        if (QuPo.iF == null) 
            return false;
        QuPo.script = (typeof handler != 'undefined') ? handler : QuPo.script;
        if (QuPo.script == null) 
            return false;
        //QuPo.log("signalInappropriate: " + QuPo.iF.id);
        var params = QuPo.iF.serialize();
        //QuPo.log("signalInappropriate: ajaxDoEventParam : handler: " + QuPo.script + ", params : " + params);
        QuPo.refresh = true;
        QuPo.QuickPollUpdate(QuPo.script, params);
    },
    
    
    popupAlert: function(url){
        if (typeof url == 'undefined') 
            return false;
        w = window.open(url, "StepStone", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,scrollbar=yes,resizable=yes,copyhistory=yes,width=640,height=500");
        w.focus();
    },
    
    
    setQuickPoll: function(){    	
    	
    	var QuickPollLeftOffsetLimit = 500; // over 500 means we are on the right side of the page
    	var CandidateQuoteBlock = $('CandidateQuote');
    	var OloBlock = $('OloBlock');
    	var CallToActionBlock = $('CallToAction');
    	
    	var QuickPollParentId = Element.identify(QuPo.QPparentNode);
    	
    	//QuPo.log("setQuickPoll: QuPo.QPparentNode: " + QuickPollParentId);
    	//QuPo.log("setQuickPoll: QuPo.QuickPollBlock.offsetLeft: " + QuPo.QuickPollBlock.offsetLeft + "px");
    	
    	if(typeof QuPo != undefined && QuPo.QuickPollBlock != null && QuPo.QuickPollContainer != null){	
    		    		
    		// exceptions for the placement of the QuickPoll
    		if(QuickPollParentId == 'visibility-products-quickpoll'){   				
    		
				//QuPo.log("setQuickPoll: in 'visibility-products-quickpoll'");
				
	    		// Todo: get the parentwidth automatically and calculate the max
	    		if(QuPo.QuickPollBlock.offsetLeft >= QuickPollLeftOffsetLimit){
	    			
	    			//QuPo.log("setQuickPoll: offsetLeft: " + QuPo.QuickPollBlock.offsetLeft);   			
	    			
	    			// set the width to be the width of the candidate quote
	    			if(CandidateQuoteBlock != null){
	    				var newwidth = CandidateQuoteBlock.getWidth() - 20; // 20 = margin
	    				////QuPo.log("setQuickPoll: CandidateQuoteBlock: " + newwidth);
	    				Element.setStyle(QuPo.QuickPoll,{'width':newwidth + 'px'});
	    			}   			
	    				
	    			Element.setStyle(QuPo.QuickPollBlock,{
	    				marginRight:0
	    			});
	    			
	    			Element.setStyle(QuPo.QuickPollContainer,{
	    				position:'absolute',
	    				zIndex: 10000,
	    				top: 0,
	    				right: 0
	    			});
	    			
	    		}/*else{
	    			////QuPo.log("offsetLeft: " + QuPo.QuickPollBlock.offsetLeft); 
	    		}*/
	    		
			} // QuickPollParentId
    		
    	} // QuickPollBlock
    	
    } // setQuickPoll
    
});

Event.observe(window, 'load', function(){   
	var startQuickPoll = new QuickPoll('QuickPollFront');
	try{
		if(typeof QuPo != undefined){
			QuPo.setQuickPoll();
		}
	}catch(error){
		dump("Error: QuickPoll : " + error.message);
	}	
});	

