var smartBox = {
	bgnd : {tag : 'div', id: 'bgndId', style : 'display: none; opacity:0.6; filter:alpha(opacity=60); -moz-opacity:0.6; background: gray;'},
	container : {tag : 'div', id: 'contId', style : 'display: none;'},
	containerWidth : false,
	containerHeight : false,
	containerStyle : false,
	contentCallback : null,
	timer : true,
	refreshTime : 100,
	ajaxLoaderPath : false,
	codeInjectorId : 'codeInjectorId',
	lockContentDisplay : false,
	setBackground : function (tag, id, style) {
		this.bgnd = {tag : tag, id : id, style : style};
	},
	getBackground : function () {
		return this.bgnd;
	},
	setContainer : function (tag, id, style) {
		this.container = {tag : tag, id : id, style : style};
	},
	getContainer : function () {
		return this.container;
	},
	setContainerWidth : function (containerWidth) {
		this.containerWidth = containerWidth;
	},
	getContainerWidth : function () {
		return this.containerWidth;
	},
	setContainerHeight : function (containerHeight) {
		this.containerHeight = containerHeight;
	},
	getContainerHeight : function () {
		return this.containerHeight;
	},
	setContainerStyle : function (containerStyle) {
		this.containerStyle = containerStyle;
	},
	getContainerStyle : function () {
		return this.containerStyle;
	},
	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;
	},
	isIE : function () {
	   return (document.all) ? true : false;    
	},
	setLockContentDisplay : function (lock) {
	    this.lockContentDisplay = lock;
	},
	getLockContentDisplay : function () {
	    return this.lockContentDisplay;
	},
	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) {
		var code = '<' + tagName + ' id="' + idName + '" style="' + style + '"></' + 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;
		}
		
		if(!this.isIE() && document.viewport) {
		    dim.width = (document.body.getDimensions().width > dim.width)?document.body.getDimensions().width:dim.width;
		    dim.height = document.body.getDimensions().height;
		}	
		
		return dim;
	},
	getWindowDimensions : function () {
		var dim = {width: 0, height: 0};
		
		if(!this.isIE() && document.viewport) {
		    dim.width = document.viewport.getDimensions().width;
		    dim.height = document.viewport.getDimensions().height;
		} else {
    		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;		  
	},
	getScroll : function () {
		var dim = {top: 0, left: 0};
		
		if(!this.isIE() && document.viewport) {
		    dim = {top : document.viewport.getScrollOffsets().top, left : document.viewport.getScrollOffsets().left};		    
		} else {
    		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);
    				
    		if(content && document.getElementById(this.getContainer().id)) {
    			var container = document.getElementById(this.getContainer().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.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 = this.getDomElementCode(this.getBackground().tag, this.getBackground().id, this.getBackground().style);
		code += this.getDomElementCode(this.getContainer().tag, this.getContainer().id, this.getContainer().style);
		
		codeInjector.innerHTML = code;
		
		var container = document.getElementById(this.getContainer().id);
		
		if(container) {
			document.body.scroll = 'no';
			if(this.getBackground().id) {
				var bgnd = document.getElementById(this.getBackground().id);
				if(bgnd) {				
					bgnd.style.top = '0px'; 
					bgnd.style.left = '0px';
					bgnd.style.width = this.getPageDimensions().width + 'px';
					bgnd.style.height = this.getPageDimensions().height + 'px';
					bgnd.style.position = 'absolute';
					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';			
			
			this.addContent(this.getContentCallback(), true);
			
			this.resize();
			this.addEvent('scroll', window, this.resize);
			this.addEvent('resize', window, this.resize);
		}						
	},	
	hide : function () { 
		if(document.getElementById(this.getContainer().id)) {
			document.getElementById(this.getContainer().id).style.display = 'none';
			document.getElementById(this.getBackground().id).innerHTML = null;
		}
		
		if(document.getElementById(this.getBackground().id)) {
			document.getElementById(this.getBackground().id).style.display = 'none';			
		}

		if(document.getElementById(this.codeInjectorId)) {
			document.body.removeChild(document.getElementById(this.codeInjectorId));
		}
		
		this.enableTimer(false);
	},
	resize : function () {
		if(this.getContainer) {
    		    if(document.getElementById(this.getContainer().id)) {
		    	    if(document.getElementById(this.getBackground().id)) {
				    var bgnd = document.getElementById(this.getBackground().id);
				    bgnd.style.width = this.getPageDimensions().width + 'px';
				    bgnd.style.height = this.getPageDimensions().height + 'px';
			    }
			
			    var container = document.getElementById(this.getContainer().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 = parseInt(container.style.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 = parseInt(container.style.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;
		}	
	},
	displayBox : function (idReferat, fileName, title) {
		this.setContentCallback('dwn.getContent(' + idReferat  + ', \'' + fileName + '\', \'' + title + '\');');
		this.display();
	}				
};

var dwn = {
    urlAddEmail : 'http://www.e-referate.ro/download/addEmail.php',
    baseUrl : 'http://www.e-referate.ro/referate/',
    contId : 'dwnformid',
    emailCookie : 'user_email',
    getContent : function (idReferat, fileName, title) {
	var email = dwn.cookieRead(dwn.emailCookie)?dwn.cookieRead(dwn.emailCookie):'';
	var content = '<div class="downlPopup"><div class="downlPopup_top"><span>Download referat</span><a href="#" title="close x" onclick="smartBox.hide(); return false;">close x</a></div><div class="downlPopup_main"><p class="popRefname">' + title + '</p><p style="font-size: 12px;">Introdu adresa ta de mail daca doresti sa fii la curent cu ultimele lucrari adaugate pe e-referate:</p><form name="dwn_popup" action=""><div class="popEmailform"><input type="text" name="email" value="' + email + '"></div><a class="downlPopbutt" href="#" title="" onclick="dwn.addEmail(document.dwn_popup.email.value, ' + idReferat + ', \'' + fileName + '\'); return false;"><img src="../imgs/download.gif" alt="download"></a></form></div></div>';
        return content;        
    },
    isValidEmail : function (elementValue) {  
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
        return emailPattern.test(elementValue);  
    },
    addEmail : function (email, idReferat, fileName) {
	smartBox.hide();

	if(dwn.isValidEmail(email)) {
	    dwn.cookieWrite(dwn.emailCookie, email, 365);
	}    	
    
        document.location.href = dwn.urlAddEmail +  '?email=' + email + '&id_referat=' + idReferat + '&dwn_url=' + dwn.baseUrl + fileName;

	return;	
    },
    cookieWrite : function (cookieName, cookieValue, daysOfLife) {
    	if (!cookieName) return null;
    	if (daysOfLife) {
            var date = new Date();
            date.setTime(date.getTime() + (daysOfLife*24*60*60*1000));
            var expires = "; expires=" + date.toGMTString();
        } else {
	    var expires = "";
	}
	
	document.cookie = cookieName + "=" + cookieValue + expires + "; path=/";
	return true;
    },    
    cookieRead : function (cookieName) {
	if (!cookieName) return null;
	var cname = cookieName + "=";
	var ca = document.cookie.split(';');
	for (var i=0; i < ca.length; i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(cname) == 0) {
	        return c.substring(cname.length, c.length);
	    }
	}
	return null;
    }
};