function _getViewPortSize()
{				
		var viewportwidth;
		var viewportheight; 
		
		if (typeof window.innerWidth != 'undefined')					
		{					
			viewportwidth = window.innerWidth,					
			viewportheight = window.innerHeight					
		}										 																				 					
		else if (typeof document.documentElement != 'undefined'	&& typeof document.documentElement.clientWidth !=	'undefined' && document.documentElement.clientWidth != 0)					
		{				
			viewportwidth = document.documentElement.clientWidth,					
			viewportheight = document.documentElement.clientHeight					
		}
		
		var arr = [];
		arr["w"] = viewportwidth;
		arr["h"] = viewportheight;
		
		return arr;
}

function _centerElementOnViewport(elem)
{	
	var ew = $("#" + elem).width();
	var eh = $("#" + elem).height();	
	
	var vs = _getViewPortSize();
				
	var cw = vs["w"];
	var ch = vs["h"];
			
	$("#" + elem).css("left", ((cw - ew) / 2) + "px");	
	$("#" + elem).css("top", ((ch / 2) - (eh / 2)) + "px");		
	
	if ($.browser.msie && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6)
	{				
		$(window).bind("scroll", {"elem": elem}, function(event){
			centerElementScroll(event.data.elem);
		});
	}
}

function centerElementScroll(elem)
{
	var st = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;	
	
	var vs = _getViewPortSize();
				
	var cw = vs["w"];
	var ch = vs["h"];
		
	var ew = $("#" + elem).width();
	var eh = $("#" + elem).height();
	
	$("#" + elem).css("top", (((ch / 2) - (eh / 2) + st)) + "px");		
}

function DZ_Site_Cookies(path, domain)
{    
 this.path = path || '/';
  this.domain = domain || null;  

 this.set = function(key,value,seconds){
  if (typeof key != 'string') {
        throw "Invalid key";
    }
    if (typeof value != 'string' && typeof value != 'number') {
        throw "Invalid value";
    }
    if (seconds && typeof seconds != 'number') {
        throw "Invalid expiration time";
    }
    var setValue = key+'='+escape(new String(value));
    if (seconds) {
        var date = new Date();
        date.setTime(date.getTime()+(seconds*1000));
        var setExpiration = "; expires="+date.toGMTString();
    } else var setExpiration = "";
    var setPath = '; path='+escape(this.path);
    var setDomain = (this.domain) ? '; domain='+escape(this.domain) : '';
    var cookieString = setValue+setExpiration+setPath+setDomain;
    document.cookie = cookieString;
 };

 this.get = function(name) {
  	var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while(i < clen){
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return this.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if(i == 0)
            break;
    }
    return null;
 };

 this.clear = function(name){
  if(this.get(name)){
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
 };

 this.getCookieVal = function(offset){
 	 var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
 };          
}

var __dz_site__cookies = new DZ_Site_Cookies();

function NEWSLETTER_POPUP()
{		
	this.id = null;
	
	this.getCookieId = function(){
		return "banner_seen_" + this.id;
	};
	
	this.getCookieLifetime = function(){
		return 24 * 60 * 60;
	};
	
	this.submitted_ok = false;
			
	this.close = function(lifetime) {
		
		if (typeof(lifetime) == "undefined")
		{
			lifetime = this.getCookieLifetime();
		}
		
		__dz_site__cookies.set(this.getCookieId(), 1, lifetime);
		
		if (!this.submitted_ok)
		{
			var fbPopup = new DZ_SITE_POPUP("facebook_popup");
			fbPopup.setPopup(new FACEBOOK_POPUP());								
			
			fbPopup.renderAfter = function(result){
				
				if (result)
				{
					
					try
					{							
						if (document.getElementById("facebook_popup").style.display != "none")
						{
						
							document.getElementById("facebook_popup").focus();	
						}
					}
					catch(err)
					{
					}															
				}
			}
	
			fbPopup.render();						
		}				
	};
	
	this.isCookieSet = function() {
		if (__dz_site__cookies.get(this.getCookieId()))		
		{		
			return true;
		}
		
		return false;
	};
	
	this.validateRequest = function(p){
			
			$("#nlConfirmLink").css("margin-top", "13px");
			$("#popupEmailValidator").css("display", "none");
			
			var email = $("#popup_newsletter_email").val();
			
			var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
			if (email.length == 0 || !filter.test(email))
			{				
				$("#popupEmailValidator").html("Va rugam sa introduceti o adresa de email valida in acest camp");
				$("#popupEmailValidator").css("display", "block");
				$("#nlConfirmLink").css("margin-top", "3px");														
				return false;			
			}	
						
			this.submitted_ok = true;					
						
			p.close(60 * 24 * 60 * 60);
															
			return true;
	};
	
	this.render = function(p) {
		
		if (this.isCookieSet())		
		{		
			return false;
		}
		
		var elem = $("<div/>");
		elem.addClass("popupNewsletter");
		elem.attr("id", p.popup_id);
		
		var elem_content = $("<div/>");
		elem_content.addClass("popupContent");
		
		var lnk = $("<a/>");
		lnk.attr("href", "javascript:void(0)");
		lnk.addClass("popupCloseLink");
		
		elem_content.append(lnk);		
		
		lnk.click(function(){
			p.close(60 * 24 * 60 * 60);
		});
										
		var subscribe_url = "http://nl.bricomall.ro/subscribe/16/37ffcb292ffd0c00f704b082c6d3c64e";
		
		var frm = $("<form/>");
		frm.attr("action", subscribe_url);
		frm.attr("method", "get");
		frm.attr("id", "popupEmailForm");
		frm.attr("target", "_blank");
		
		frm.submit(function(){
			
		});				
		
		frm.bind("submit", {obj: this, arg: p}, function(event){
			return event.data.obj.validateRequest(event.data.arg);
		});
																		
		var inp = $("<input/>");
		inp.attr("type", "text");
		inp.attr("name", "email");
		inp.attr("id", "popup_newsletter_email");
		
		frm.append(inp);
						
		elem_content.append(frm);
		
		inp.bind("keyup", {"p": p}, function(event){
			if (event.which == 27)
			{				
				event.data.p.close(60 * 24 * 60 * 60);
			}
		});
												
		var validator = $("<div/>");
		validator.addClass("popupEmailValidator");
		validator.attr("id", "popupEmailValidator");
		validator.html("Va rugam sa introduceti o adresa de email valida in acest camp");
		validator.css("display", "none");
		elem_content.append(validator);
				
		var lnk_send = $("<a/>");
		lnk_send.attr("href", "javascript:void(0)");
		lnk_send.addClass("sendConfirm");
		lnk_send.attr("id", "nlConfirmLink");				
						
		elem_content.append(lnk_send);
		
		lnk_send.bind("click", {"obj": this, "arg": p}, function(event){
			if (event.data.obj.validateRequest(event.data.arg))
			{
				$("#popupEmailForm").submit();		
			}
		});
								
		var lnk_policy = $("<a/>");
		lnk_policy.attr("href", "http://www.bricomall.ro/politica-de-confidentialitate");
		lnk_policy.attr("target", "_blank");
		lnk_policy.addClass("policy-link");
		lnk_policy.html("Vezi politica de confidentialitate");
		
		elem_content.append(lnk_policy);
		
		elem.append(elem_content);
		
		$("#" + p.container).append(elem);
		
		__dz_site__cookies.set(this.getCookieId(), 1, 24 * 60 * 60);				
				
		return true;
	};
}

function FACEBOOK_POPUP()
{		
	this.id = null;
	
	this.getCookieId = function(){
		return "banner_seen_" + this.id;
	};
	
	this.getCookieLifetime = function(){
		return 60 * 24 * 60 * 60;
	};
			
	this.close = function(lifetime) {
		
		if (typeof(lifetime) == "undefined")
		{
			lifetime = this.getCookieLifetime();
		}
		
		__dz_site__cookies.set(this.getCookieId(), 1, lifetime);						
	};
	
	this.isCookieSet = function() {
		if (__dz_site__cookies.get(this.getCookieId()))		
		{		
			return true;
		}
		
		return false;
	};
			
	this.render = function(p) {
		
		if (this.isCookieSet())		
		{		
			return false;
		}
		
		var elem = $("<div/>");
		elem.addClass("popupFacebook");
		elem.attr("id", p.popup_id);
		
		var elem_content = $("<div/>");
		elem_content.addClass("popupContent");
		
		var lnk = $("<a/>");
		lnk.attr("href", "javascript:void(0)");
		lnk.addClass("popupCloseLink");
		
		elem_content.append(lnk);		
		
		lnk.bind("click", {"p": p}, function(event){
			event.data.p.close(60 * 24 * 60 * 60);
		});
																
		elem.append(elem_content);
		
		var likeboxdiv = $("<div/>");
		likeboxdiv.addClass("likeboxframe");				
		likeboxdiv.html('<iframe src="//www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2FBricoMall&amp;width=292&amp;height=260&amp;colorscheme=light&amp;show_faces=true&amp;border_color&amp;stream=false&amp;header=false" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:292px; height:262px;" allowTransparency="true"></iframe>');
		elem_content.append(likeboxdiv);
										
		$("#" + p.container).append(elem);
		
		elem.bind("keyup", {"p": p}, function(event){
			if (event.which == 27)
			{				
				p.close(60 * 24 * 60 * 60);
			}
		});
													
		__dz_site__cookies.set(this.getCookieId(), 1, 24 * 60 * 60);				
		
		return true;
	};
}

function DZ_SITE_POPUP(id)
{
	this.popup_id = id;
	this.popup = null;
	this.container = "__popupContainer";
	
	this.setPopup = function(popup) {
		this.popup = popup;		
		this.popup.id = this.popup_id;
	}
	
	this.renderAfter = null;
	
	this.showOverlay = function(){		
		if (document.getElementById("_dz_overlay") != undefined)
		{
			document.getElementById("_dz_overlay").style.display = "block";
		}				
	};
	
	this.hideOverlay = function(){
		if (document.getElementById("_dz_overlay") != undefined)
		{
			document.getElementById("_dz_overlay").style.display = "none";
		}
	};
								
	this.close = function(param){
						
		if (this.popup)
		{						
			document.getElementById(this.popup_id).style.display = "none";			
			this.hideOverlay();
			
			if (typeof(this.popup.close) == "function")
			{
				this.popup.close(param);			
			}
		}
	};
			
	this.render = function(disp){
		
		if (typeof(disp) == "undefined")
		{
			disp = true;
		}
		
		var pelem = document.getElementById(this.popup_id);
		
		if (document.getElementById(this.container) == "undefined")
		{			
			return false;
		}
		
		if ((pelem != undefined))
		{
			if (disp)
			{
				this.showOverlay();
													
				_centerElementOnViewport(this.popup_id);	
				document.getElementById(this.popup_id).style.display = "block";
				document.getElementById(this.popup_id).setAttribute("tabindex", "0");	
			}
			
			if (typeof(this.renderAfter) == "function")
			{
				this.renderAfter(true);
			}
			
			return true;
		}
						
		if (this.popup && typeof(this.popup.render) == "function")
		{
			var result = this.popup.render(this);		
			
			if (result && disp)
			{				
				this.showOverlay();
												
				_centerElementOnViewport(this.popup_id);	
				document.getElementById(this.popup_id).style.display = "block";
				document.getElementById(this.popup_id).setAttribute("tabindex", "0");								
			}
			
			if (typeof(this.renderAfter) == "function")
			{
				this.renderAfter(result);
			}	
		}
	};					
}
