var smartBox = {
	bgnd : {tag: 'div', id: 'bgndId', style: 'display: none; opacity:0.6; filter:alpha(opacity=60); -moz-opacity:0.6; background: gray;', onclick: false},
	container : {tag: 'div', id: 'contId', style: 'display: none; text-align: center; background: #fff; border: solid 1px; z-index: 6001;', onclick: false},
	containerWidth : false,
	containerHeight : false,
	contentCallback : null,
	timer : true,
	refreshTime : 100,
	ajaxLoaderPath : false,
	codeInjectorId : 'codeInjectorId',
	lockContentDisplay : false,
	selfObject : false,
	resizePeriodically: true,
	topPosition: false,
	setBackground : function (bgnd) {
		this.bgnd = bgnd;
	},
	setContainer : function (cont) {
		this.container = cont;
	},
	setContainerWidth : function (containerWidth) {
		this.containerWidth = containerWidth;
	},
	getContainerWidth : function () {
		return this.containerWidth;
	},
	setContainerHeight : function (containerHeight) {
		this.containerHeight = containerHeight;
	},
	getContainerHeight : function () {
		return this.containerHeight;
	},
	setContentCallback: function (callback) {
		this.contentCallback = callback;
	},
	getContentCallback: function (callback) {
		return this.contentCallback;
	},
	enableTimer: function (timer) {
		this.timer = timer;
	},
	timerIsEnabled : function () {
		return this.timer;
	},
	setRefreshTime : function (refreshTime) {
		this.refreshTime = refreshTime;
	},
	getRefreshTime : function () {
		return this.refreshTime;
	},
	setAjaxLoaderPath : function (path) {
		this.ajaxLoaderPath = path;
	},
	getAjaxLoaderPath : function () {
		return this.ajaxLoaderPath;
	},
	isIE6 : function () {
	   var isIE6Browser = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
	   return isIE6Browser;    
	},
	setLockContentDisplay : function (lock) {
	    this.lockContentDisplay = lock;
	},
	getLockContentDisplay : function () {
	    return this.lockContentDisplay;
	},
	resizeEnable: function (resize) {
	   this.resizePeriodically = resize;
	},
	getResizeStatus: function () {
	   return this.resizePeriodically;  
	},
	setTopPosition: function (topPos) {
	  this.topPosition = topPos;  
	},
	getTopPosition: function () {
	  return this.topPosition;  
	},
	newDomElement : function (tagName, idName) {
		if(tagName && idName) {
			var el = document.createElement(tagName);
			el.id = idName;
			if (document.body.firstChild){
				document.body.insertBefore(el, document.body.firstChild);
			} else {
				document.body.appendChild(el);
			}
			return el;
		} else {
			return false;
		}	
	},
	getDomElementCode : function (tagName, idName, style, onclick) {
		var styleCode = style?'style="' + style + '"':'';
		var onclick = style?'onclick="' + onclick + '"':'';
		var code = '<' + tagName + ' id="' + idName + '" '+ styleCode + ' ' + onclick + '></' + tagName + '>';
		return code;
	},
	getElementDimensions : function (element) {
	    if(!element) return false;
	    var display = element.style.display;
        
        if (display != 'none' && display != null) {// Safari bug
            return {width: element.offsetWidth, height: element.offsetHeight};
        }    
    
        var els = element.style;
        var originalVisibility = els.visibility;
        var originalPosition = els.position;
        var originalDisplay = els.display;
        els.visibility = 'hidden';
        if (originalPosition != 'fixed') {// Switching fixed to absolute causes issues in Safari
            els.position = 'absolute';
        }
            
        els.display = 'block';
        var originalWidth = element.clientWidth;
        var originalHeight = element.clientHeight;
        els.display = originalDisplay;
        els.position = originalPosition;
        els.visibility = originalVisibility;
        
        return {width: originalWidth, height: originalHeight};
	},
	getPageDimensions : function () {
		var dim = {width: 0, height: 0};
		
	    if (document.body.clientWidth && document.body.clientHeight) {
			dim.width = document.body.clientWidth;
			dim.height = document.body.clientHeight;
		} else if(document.body.offsetWidth && document.body.offsetHeight) {
			dim.width = document.body.offsetWidth;
			dim.height = document.body.offsetHeight;
		} else if (document.documentElement.clientWidth && document.documentElement.clientHeight) {
			dim.width = document.documentElement.clientWidth;
			dim.height = document.documentElement.clientHeight;
		}
		
		if(dim.width <  this.getScroll().left + this.getWindowDimensions().width) {
			dim.width = this.getScroll().left + this.getWindowDimensions().width;
		}
		if(dim.height < this.getScroll().top + this.getWindowDimensions().height) {
			dim.height = this.getScroll().top + this.getWindowDimensions().height;
		}
				
		return dim;
	},
	getWindowDimensions : function () {
		var dim = {width: 0, height: 0};
		
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			dim = {width: window.innerWidth, height: window.innerHeight};
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			dim = {width: document.documentElement.clientWidth, height: document.documentElement.clientHeight};
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			dim = {width: document.body.clientWidth, height: document.body.clientHeight};
		}
							
		return dim;		  
	},
	getElementCoordinates : function (elm) {
	    var coordinates = {};          
	    if(elm.style.left && elm.style.top) {
            coordinates = {left: parseInt(elm.style.left), top: parseInt(elm.style.top)};
        } else {
            coordinates = {left: elm.offsetLeft, top: elm.offsetTop};
        }
        return coordinates;
	},
	getScroll : function () {
		var dim = {top: 0, left: 0};
		
    	if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			dim = {top: window.pageYOffset, left: window.pageXOffset};
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			dim = {top: document.body.scrollTop, left: document.body.scrollLeft};
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			dim = {top: document.documentElement.scrollTop, left: document.documentElement.scrollLeft};
		}
	    			
		return dim;
	},
	hasAjaxLoader : function () {
		if(this.getAjaxLoaderPath()) {
			return true;
		} else {
			return false;
		}
	},
	getAjaxLoaderContent : function () {
		var alImg = new Image();
		alImg.src = this.getAjaxLoaderPath();
		
		if(alImg.complete == true) {
			var marginY = parseInt((this.getContainerHeight() - alImg.height)/2);
			if(marginY > 0) {
				var style = 'margin: ' + marginY + 'px auto;';
			} else {
				var style = 'margin: 50px;';
			}
			var ajaxLoaderImg = '<img style="' + style + '" src="' + alImg.src + '">';
			return ajaxLoaderImg;
		} else {
			return false;
		}
	},
	addContent : function (callback, lock) {
	    if(this.getLockContentDisplay() == false) {
	        var content = eval(callback.match(/^[a-zA-Z0-9\.\-\_]+$/)?callback + '();':callback);	        
    			
    		if(content && document.getElementById(this.container.id)) {
    			var container = document.getElementById(this.container.id);
    			container.innerHTML = content;
    			this.setLockContentDisplay(lock);
    			var cWidth = this.getContainerWidth()?this.getContainerWidth():this.getElementDimensions(container).width;
    			container.style.left = (this.getScroll().left + (this.getWindowDimensions().width - cWidth)/2) + 'px';
    			var cHeight = this.getContainerHeight()?this.getContainerHeight():this.getElementDimensions(container).height;
    			container.style.top = (this.getTopPosition())?this.getTopPosition() + this.getScroll().top + 'px':(this.getScroll().top + (this.getWindowDimensions().height - cHeight)/2) + 'px';    			
    		} else {
    			 setTimeout('smartBox.addContent(\'' + callback + '\', ' + lock + ');', this.getRefreshTime());			 
    		}
	    }	   			
	},
	display : function () {
		this.enableTimer(true);
		this.setLockContentDisplay(false);
		
		var codeInjector = this.newDomElement('div', this.codeInjectorId);
		var code = '';
		if(this.bgnd) {
            code += this.getDomElementCode(this.bgnd.tag, this.bgnd.id, this.bgnd.style, this.bgnd.onclick);
		}
		code += this.getDomElementCode(this.container.tag, this.container.id, this.container.style, this.container.onclick);
		
		codeInjector.innerHTML = code;
		
		var container = document.getElementById(this.container.id);
		
		if(container) {
			if(this.bgnd) {
				var bgnd = document.getElementById(this.bgnd.id);
				if(bgnd) {				
					bgnd.style.top = '0px'; 
					bgnd.style.left = '0px';
					if(smartBox.isIE6()) {
    				    bgnd.style.width = this.getPageDimensions().width + 'px';
    				    bgnd.style.height = this.getPageDimensions().height + 'px';
    				    bgnd.style.position = 'absolute';
    				} else {
					   bgnd.style.width = this.getWindowDimensions().width + 'px';
					   bgnd.style.height = this.getWindowDimensions().height + 'px';
					   bgnd.style.position = 'fixed';
    				}   
					bgnd.style.display = 'block'; 
				}	
			}
			
			if(this.getContainerWidth()) {
				container.style.width = this.getContainerWidth() + 'px';
			}
			
			if(this.getContainerHeight()) {
				container.style.height = this.getContainerHeight() + 'px';
			}
			
			if(this.hasAjaxLoader()) {
				this.addContent('smartBox.getAjaxLoaderContent', false);
			}
						
			container.style.position = 'absolute';			
			container.style.display = 'block';			
			
			smartBox.addContent(this.getContentCallback(), true); 
			
			this.addEvent('keypress', window, this.escButtonHandler);
			
			if(this.getResizeStatus()) {
			     this.resize();    
			}			
		}						
	},	
	hide : function () { 
		if(document.getElementById(this.container.id)) {
			document.getElementById(this.container.id).style.display = 'none';
			document.getElementById(this.bgnd.id).innerHTML = null;
		}
		
		if(document.getElementById(this.bgnd.id)) {
			document.getElementById(this.bgnd.id).style.display = 'none';			
		}

		if(document.getElementById(this.codeInjectorId)) {
			document.body.removeChild(document.getElementById(this.codeInjectorId));
		}
		
		this.enableTimer(false);
	},
	resize : function () {
        if(this.container) { 
           if(document.getElementById(this.container.id)) {
                if(this.bgnd) {
        			if(document.getElementById(this.bgnd.id)) {
        				var bgnd = document.getElementById(this.bgnd.id);
        				if(this.isIE6()) {
        				    bgnd.style.width = this.getPageDimensions().width + 'px';
        				    bgnd.style.height = this.getPageDimensions().height + 'px';
        				} else {
        				    bgnd.style.width = this.getWindowDimensions().width + 'px';
        				    bgnd.style.height = this.getWindowDimensions().height + 'px';    
        				}    				
        			}
                }	
        		
        		var container = document.getElementById(this.container.id);
        		
        		if(this.getContainerWidth()) {
        			container.style.width = this.getContainerWidth() + 'px';
        		}
        		if(this.getContainerHeight()) {
        			container.style.height = this.getContainerHeight() + 'px';
        		}
        		
        		var cWidth = this.getContainerWidth()?this.getContainerWidth():this.getElementDimensions(container).width;	
        		var leftLocation =  this.getScroll().left + (this.getWindowDimensions().width - cWidth)/2;
        		var leftInt = this.getElementCoordinates(container).left;
        		container.style.left = (leftInt + (leftLocation - leftInt)/2) + 'px';
        		
        		var cHeight = this.getContainerHeight()?this.getContainerHeight():this.getElementDimensions(container).height;			
        		var topLocation = this.getScroll().top + (this.getWindowDimensions().height - cHeight)/2;
        		var topInt = this.getElementCoordinates(container).top;
        		container.style.top = (topInt + (topLocation - topInt)/2) + 'px';
        	}
        }
	   
        if(this.timerIsEnabled()) {
        	setTimeout('smartBox.resize();', this.getRefreshTime());
        }
	},
	addEvent : function(type, obj, fn) {
		if(obj) {
			if (obj.addEventListener) {
				obj.addEventListener(type, fn, false);
				return true;
			} else if (obj.attachEvent) {				
				var r = obj.attachEvent('on' + type, fn);
				return r;
			} else {
				obj['on' + type] = fn;
				return true;
			}
		} else {
			return false;
		}	
	},
	escButtonHandler : function (e) {
        var kC  = (window.event) ? event.keyCode : e.keyCode;   // MSIE or Firefox?
                    
        var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;  // MSIE : Firefox
        if(kC==Esc) {
             smartBox.hide();
        }   
    },
    processContent : function (transport) {
        if(200 == transport.status) {
            smartBox.qnaContent = transport.responseText;                        
        } else {
            alert('Failed loading popup! Try again!');
        }            
    },
    getContent : function () {
        if(smartBox.qnaContent) {
            return smartBox.qnaContent;
        } else {
            return false;
        }
    },
    processResponse : function (transport) {
        if(200 == transport.status) {
             if(document.getElementById(smartBox.container.id)) {
                document.getElementById(smartBox.container.id).innerHTML = transport.responseText;                           
             } else {
                alert('Failed loading popup! Try again!');
             }
        } else {
            alert('Failed loading popup! Try again!');
        }            
    },
    //********************* begin images code ************************
	displayImageBox : function (src) {
        var bgnd = {tag: 'div', id: 'bgndId', style: 'display: none; opacity:0.6; filter:alpha(opacity=60); -moz-opacity:0.6; background: gray; z-index: 6001;', onclick: 'smartBox.hide();'};
	    var container = {tag: 'div', id: 'contId', style: 'display: none; text-align: center; background: #fff; border: solid 1px; z-index: 6001; top: 50px;'};	    
	    
	    image.setSrc(src);				
		image.setContainerId(container.id);
		
		this.resizeEnable(false); 
		this.setTopPosition(50);
		this.setBackground(bgnd);
		this.setContainer(container);
		this.setAjaxLoaderPath(HTTPTEMPLATE + 'imgs/ajax-loader.gif');
		this.setContentCallback('smartBox.getCallbackContent');
		this.display();
	},
	getCallbackContent : function () {
	    if(image.getImageTag()) {
	        var content = '<div style="padding: 5px">';
	        content += '<div style="padding: 5px; text-align: right;"><a style="font-size: 10px; padding: 0 0 5px 0;" onclick="smartBox.hide(); return false;" href="#">Inchide X</a></div>';
    	    content += image.getImageTag();
    	    content += '</div>';
    	        	        	    
    	    return content;
	    } else {
	        return false;
	    }	    
	},
	//********************* end images code ************************
	//********************* begin qna code *************************
	qnaContent : false,
	getQnaPopupInfo : function (url, params) {
	    if(typeof(params) == 'object') {   	
	       new Ajax.Request (url, {method : 'post',     
                                parameters : {
                                                action : 'save',
                                                idUser : params.idUser.value,
                                                nickname : params.nickname.value,
                                                email : params.email.value,
                                                varsta : params.varsta.value,
                                                sex : params.sex[params.sex.selectedIndex].value,
                                                titlu : params.titlu.value,
                                                intrebare : params.intrebare.value,
                                                qna_captcha : params.qna_captcha.value
                                             }, 
                                onSuccess : smartBox.processResponse              
            });           
	    } else {
	        new Ajax.Request (url, {method : 'post',     
                                parameters : {idUser : params}, 
                                onSuccess : smartBox.processContent              
            });
           
            var bgnd = {tag: 'div', id: 'bgndId', style: 'display: none; opacity:0.6; filter:alpha(opacity=60); -moz-opacity:0.6; background: gray;'};
            this.setBackground(bgnd);
            var container = {tag: 'div', id: 'contId', style: 'display: none; background: #fff; z-index: 6001;'};
            this.setContainer(container);
            this.setAjaxLoaderPath(HTTPTEMPLATE + 'imgs/ajax-loader.gif');
            this.setContentCallback('smartBox.getContent');
            this.display();
	    }
    },
    //********************* end qna code ************************
    //********************* begin feedback code *************************
    getFeedbackPopupInfo : function (url, params) {
        this.resizeEnable(true); 
		this.setTopPosition(false);        
    
	    if(typeof(params) == 'object') {
	       new Ajax.Request (url, {method : 'post',     
                                parameters : {
                                                action : 'trimite',
                                                email : params.email.value,
                                                subiect : params.subiect.value,
                                                propunere : params.propunere.value,                                                
                                                nume : params.nume.value,                                               
                                                feedback_captcha : params.feedback_captcha.value,
                                                url: params.url
                                             }, 
                                onSuccess : smartBox.processResponse              
            });           
	    } else {            	    
	        new Ajax.Request (url, {method : 'post',     
                                onSuccess : smartBox.processContent              
            });           
            var bgnd = {tag: 'div', id: 'bgndId', style: 'display: none; opacity:0; filter:alpha(opacity=0); -moz-opacity:0; background: gray;'};
            this.setBackground(bgnd);
            var container = {tag: 'div', id: 'contId', style: 'display: none; background: #fff; z-index: 10001;'};
            this.setContainer(container);
            this.setAjaxLoaderPath(HTTPTEMPLATE + 'imgs/ajax-loader.gif');
            this.setContentCallback('smartBox.getContent');
            if(!document.getElementById('contId')){
                this.display();
            }                        
	    }
        
    }   
    //********************* end feedback code ************************
};

var image = {
	src : null,
	width : false,
	height : false,
	containerId : false,
	setContainerId : function (containerId) {
	   this.containerId = containerId;	    
	},
	getContainerId : function () {
	   return this.containerId;  
	},
	setSrc : function (srcPath) {
		var im = new Image();
		im.src = srcPath;
		this.src = im;
	},
	getSrc : function () {
		return this.src;
	},	
	setWidth : function (width) {
		this.width = width;
	},
	getWidth : function () {
		return this.width;
	},
	setHeight : function (height) {
		this.height = height;
	},
	getHeight : function() {
		return this.height;
	},
	getImageDimensions : function () {
		if(this.getSrc().complete) {
			var src = this.getSrc();
			
			if(src) {
				if(src.width && src.height) {
					var dim = {width: 0, height : 0};
					if(this.getWidth() && !this.getHeight()) {
						dim.width = this.getWidth();
						dim.height = src.height * (this.getWidth()/src.width);
					} else if (!this.getWidth() && this.getHeight()) {
						dim.width = src.width * (this.getHeight()/src.height);
						dim.height = this.getHeight();
					} else if(this.getWidth() && this.getHeight()) {
						if(src.width <= this.getWidth() && src.height <= this.getHeight()) {
							dim.width = src.width;
							dim.height = src.height;
						} else if(src.width > this.getWidth() && src.height <= this.getHeight()) {
							dim.width = this.getWidth();
							dim.height = src.height * (this.getWidth()/src.width);
						} else if(src.width <= this.getWidth() && src.height > this.getHeight()) {
							dim.width = src.width * (this.getHeight()/src.height);
							dim.height = this.getHeight();
						} else if(src.width > this.getWidth() && src.height > this.getHeight()) {
							var widthReport = src.width/this.getWidth();
							var heightReport = src.height/this.getHeight();
							if(widthReport > heightReport) {
								dim.width = this.getWidth();
								dim.height = src.height * (this.getWidth()/src.width);
							} else {
								dim.width = src.width * (this.getHeight()/src.height);
								dim.height = this.getHeight();
							}	
						}
					} else if(!this.getWidth() && !this.getHeight()) {
					    dim.width = src.width;
						dim.height = src.height;
					}
					
					if(dim.width > 0 && dim.height > 0) {
						return {width: parseInt(dim.width), height: parseInt(dim.height)};
					} else {
						return false;
					}	
				} else {
					return false;
				}
			}  else {
				return false;
			}
		} else {
			return false;
		}
	},
	getImageTag : function () {
		if(this.getSrc().complete) {
			var dim = this.getImageDimensions();
			
			if(document.getElementById(this.getContainerId())) {
			    var dimCont = smartBox.getElementDimensions(document.getElementById(this.getContainerId()));
			    var marginY = parseInt((dimCont.height - dim.height)/2);
			    if(marginY > 0) {
			    	var marginStyle =  marginY + 'px auto;';
			    } else {
			    	var marginStyle = '0 auto;';
				}	
			} else {
				var marginStyle = '0 auto;';			    
			}
			
			if(dim) {
				var tag = '<img style="cursor: pointer; margin: ' + marginStyle + ';" src="' + this.getSrc().src + '" width="' + dim.width + '" height="' + dim.height + '">';
				return tag;
			} else {
				return false;
			}	
		} else {
			return false;
		}		
	}	
};
