(function() {
	var styles = ".contNewsLetterWG {" +
		'font-weight:normal;' +
		'padding:10px 2px;' +
		'border:1px solid #bccfef;' +
		'background-color:#ddeef6;' +
		'text-align:center;' +
		'font-family:arial,sans-serif;' +
		'font-size:12px;'
	+ '}' +
	'.inputTextNewsLetterWG {' +
		'width:115px;' + 
		'border: 1px solid #babfc2;' +
		'background-color:#fff;' +
		'color:#000;'+
	'}' +
	'.inputButtonNewsLetterWG {' +
		'font-weight:bold;' + 
		'background-color:#4ea8cd;' +
		'color:#fff;' +
		'border-bottom:1px solid #538795;' +
		'border-right:1px solid #538795;' +
		'border-left: 1px solid #77aec2;' +
		'border-top: 1px solid #77aec2;' +
	'}'+
	'#nlwImgSubmit {border: 0px; cursor:pointer;}'+
	'.popNLWG {'+ 
		   'z-index:2;'+ 
		   'position:absolute;'+ 
		   'border: 2px solid #000;'+ 
		   'text-align:center;'+
		   'background:#000000;'+ 
		'}'+ 
		'.cerrarPopupNLWG {'+ 
		   'float:right;'+  
		   'cursor:pointer; '+
		   'font:Verdana, Arial, Helvetica, sans-serif;'+ 
		   'font-size:14px;'+
		   'font-weight:bold;'+ 
		   'color:#FFFFFF;'+
		   'background-color:#666666;'+ 
		   'width:22px;'+
		   'position:relative;'+
		   'text-align:center;'+ 
		'}'+              
		'.divpopNLWG {'+ 
		   'background-color:white;' + 
		'}'+
		'.divNLWGText {padding:30px;height:100px;text-align:left;}'+
		'.NLWGBoldRed{font-weight:bold; color:#ff0000;'+
		'.NLWGBold{font-weight:bold;';
	
	var style = document.createElement('style');
	style.type = "text/css";
	var headPage = 	document.getElementsByTagName('head')[0];
	headPage.appendChild(style);
	if(style.styleSheet) {
		style.styleSheet.cssText = styles;
	} else {
		style.appendChild(document.createTextNode(styles));
	}
	if (typeof jQuery == 'undefined') {  
	    // jQuery is not loaded
		var scriptJQ = document.createElement('script');
		scriptJQ.type = 'text/javascript';
		scriptJQ.src = '/js/jquery-1.3.min.js';
		headPage.appendChild(scriptJQ);
	}
})();
function NewsLetterWidget(idWG)
{
	if(idWG == undefined)
	{
		idWG = "";
	}
	//var cDiv = document.createElement('div');
	var strDefaultValue = "Your Email";
        var idMarketingOffer = "marketingofferssignup";
        var idGetProyTutWeek = "projectofweeksignup";
	var idCont = "contNewsLetterWG" + idWG;
	var idText = "inputTextNewsLetterWG" + idWG;
	var idBtn  = "inputButtonNewsLetterWG" + idWG;
	
	var idContAll = "contAllNewsletterWG"+idWG;
	var widthContAll = "160";
	var idContMsg = "popNLWG"+idWG;
	var idContMsgText = "divNLWGText"+idWG;

	var htmlButSubmit = '<img id="nlwImgSubmit" style="margin-left:-15px;" src="/shared/newsletter/button.jpg" ' +
			    'onclick="javascript:NewsLetterWGSignUp(document.getElementById(\''+ idText +'\').value, \''+
                            idWG+'\', \''+strDefaultValue+'\', \''+idContAll+'\', \''+widthContAll+'\', \''+idContMsgText+
                            '\', \''+idContMsg+'\',\''+idMarketingOffer+'\',\''+idGetProyTutWeek+'\')" alt="Sign up" ></img>';

	var htmlWG ='<div id="'+idContMsg+'" class="popNLWG" style="display:none">'+ 
	   '<div class="cerrarPopupNLWG" style="width:100%"><div class="cerrarPopupNLWG">X</div></div>'+
	   '<div class="divpopNLWG">'+
	   		'<div class="divNLWGText" id="'+idContMsgText+'"> </div>'+
	   '</div>' +  
	'</div>'+
				'<table id="'+idContAll+'" width="'+widthContAll+'" align="center" border="0" cellpadding="0" cellspacing="0" style="padding-bottom: 45px;"><tr><td style="padding:10px 0px;">'+
				'<div class="contNewsLetterWG" id="'+idCont+'">Get the Craftster Newsletter!' +
					'<p><input type="text" value="'+ strDefaultValue +'" class="inputTextNewsLetterWG" ' +
							'onfocus="NewsLetterWGFocus(this, \'' +strDefaultValue +'\')" ' +
							'onblur="NewsLetterWGBlur(this, \'' +strDefaultValue +'\')" ' +
							'onKeyPress="return enterNLWG(event, document.getElementById(\''+ idText +'\').value, \''+idWG+'\', \''+strDefaultValue+'\', \''+idContAll+'\', \''+widthContAll+'\', \''+idContMsgText+'\', \''+idContMsg+'\',\''+idMarketingOffer+'\',\''+idGetProyTutWeek+'\')" '+
							'name="' + idText + '" id="'+ idText + '"></input></p>' +
//					'<input type="button" value="SIGN UP" name="' + idBtn + '" id="' + idBtn + '" class="inputButtonNewsLetterWG"></input>' +
                                        '<div style="text-align:left; float:right; width: 143px; margin-left: 15px; padding-bottom: 5px;">' +
                                                        '<input style="margin-bottom: 30px; float:left;" value="addtomarketingoffers" type="checkbox" name="marketingofferssignup" id="'+idMarketingOffer+'" class="check" checked="checked" CHECKED>'+
                                                        '<label style="width: 10em; text-align: left;" for="marketingofferssignup">'+
                                                        'Sign up for special offers from partners and advertisers '+
                                                        '</label><span style="cursor: pointer; " onclick="openWindowsMsg(\'4\',\''+idContAll+'\',\''+widthContAll+'\',\''+ idContMsgText +'\',\''+idContMsg+'\')">(?)</span><div style="clear:both;"></div>'+
                                                        '<input style="margin-bottom: 18px; float:left;" value="addCrftProjWeek" type="checkbox" name="projectweeksignup" id="'+idGetProyTutWeek+'" class="check" checked="checked" CHECKED>'+
                                                        '<label style="width: 10em; text-align: left;" for="projectweeksignup">'+
                                                        'Get a new tutorial or pattern every week. '+
                                                        '</label>'+
                                        '</div>' +
					'<div id="wnl_contButSubmit">' +htmlButSubmit+'</div>'+
				'</div></td></tr></table>';
	document.write(htmlWG);
}
function enterNLWG(e, strEmail, idWG, strDefaultValue, idContAll, widthContAll, divContText, contGralMsg, idMarketingOffer, idGetProyTutWeek)
{
	var keycode;
	if (window.event) 
	{
		keycode = window.event.keyCode;
	}
	else if (e) 
	{keycode = e.which;}
	else return true;
	if (keycode == 13)
	{
                NewsLetterWGSignUp(strEmail, idWG, strDefaultValue, idContAll, widthContAll, divContText, contGralMsg,idMarketingOffer, idGetProyTutWeek)
		return false;
	}
	else
		return true;
}


function NewsLetterWGSignUp(strEmail, idWG, strDefaultValue, idContAll, widthContAll, divContText, contGralMsg, idMarketingOffer, idGetProyTutWeek)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(strEmail) == false) {
		openWindowsMsg("3", idContAll, widthContAll, divContText, contGralMsg);
	}
	else
	{	  
                document.getElementById('wnl_contButSubmit').innerHTML = "<img src='/shared/ajax-loader.gif' alt='loading...'/>";
		$.getJSON("/newsletter/newsLetterAction.php", {email: strEmail, marketingoffer: document.getElementById(idMarketingOffer).checked, projectofweek: document.getElementById(idGetProyTutWeek).checked}, function(data){
			//result: 1 => 0k, 0 => mail registered, -1 mail not exist in db
			//-2 wrong mail, -3 error mail river, -4 json error
			//alert(data.result);
			var strMsg = "";
	    	switch(data.result)
	    	{
	    		case "1":
	    		case "0":
	    			strMsg = "2"; //ok
	    			break;
	    		case "-1":
	    			strMsg = "1";
	    			break;
	    		case "-2":
	    			strMsg = "3";
	    			break;
	    		case "-3":
	    			strMsg = "3&er=mr";
	    			break;
	    		case "-4":
	    			strMsg = "3&er=json";
	    			break;
	    		default:
	    			strMsg = data.result;
	    	}
	    	openWindowsMsg(strMsg, idContAll, widthContAll, divContText, contGralMsg);
	    	if(strMsg=="2") //ok
	    	{
	    		document.getElementById("inputTextNewsLetterWG"+idWG).value = strDefaultValue;
	    	}
                var idText = "inputTextNewsLetterWG" + idWG;
		var idContMsg = "popNLWG"+idWG;
                var idContMsgText = "divNLWGText"+idWG;
                document.getElementById('wnl_contButSubmit').innerHTML = '<img id="nlwImgSubmit" style="margin-left:-15px;" src="/shared/newsletter/button.jpg" ' +
			    'onclick="javascript:NewsLetterWGSignUp(document.getElementById(\''+ idText +'\').value, \''+
                            idWG+'\', \''+strDefaultValue+'\', \''+idContAll+'\', \''+widthContAll+'\', \''+idContMsgText+
                            '\', \''+idContMsg+'\',\''+idMarketingOffer+'\',\''+idGetProyTutWeek+'\')" alt="Sign up" ></img>';
	    });
	}
}
function openWindowsMsg(strMsg, idContAll, widthContAll, divContText, contGralMsg)
{
	var divTxt = document.getElementById(divContText);
	switch(strMsg)
	{
		case "1":
			divTxt.innerHTML = "<br /><br /><p>Thanks for subscribing to the Craftster Newsletter!</p>";
			break;
		case "2":
			divTxt.innerHTML =  "<p class='NLWGBold'>Success! Consider yourself subscribed</p>"+
								"<p>The Craftster monthly newsletter spotlights the hottest projects, "+
								"articles, tutorials, contests, craft swaps, challenges and new features. "+
								"Can we get a woot woot!</p>";
			break;
		case "3":
			divTxt.innerHTML = "<p class='NLWGBoldRed'>Uh oh!</p>"+
							   "<p>That email address is invalid. Please enter a valid email address... pretty please.</p>";
			break; 
        case "4":
                        divTxt.setAttribute("style","height: 100px;");                       
			divTxt.innerHTML = "<p style='font-size:13.5px;'>Occasionally, we are approached by companies with special discounts and "+
                                           "offers for our community. If we think you'd be interested in receiving these offers,"+
                                           " we'd like to send them to you by email. Please note we will never sell, share or disclose "+
                                           "your email address and other personal information to anyone.</p>";
			break;
	}
	var popUp_width = 400; 
	   //width
	   $("#"+contGralMsg).css('width', popUp_width + 'px');
	   $("#"+contGralMsg).hide();
	   //popup position
	   var contWidget = document.getElementById(idContAll);
	   var posWG =findPos(contWidget); 
	   var w = posWG[0] - (popUp_width)+ parseInt(widthContAll);
	   
	   $("#"+contGralMsg).css("left",w + "px");
	   $("#"+contGralMsg).css("top",(posWG[1]+10) + "px");
	   mostrar(contGralMsg);
	 //Function to close popup 
	   $("#"+contGralMsg).click(function (){ 
	      $(this).fadeOut('slow'); 
	   });
}
function NewsLetterWGFocus(txt, strDefaultVal)
{
	if(txt.value == strDefaultVal){
		txt.value = "";
	}
}
function NewsLetterWGBlur(txt, strDefaultVal)
{
	if(txt.value == ""){
		txt.value = strDefaultVal;
	}
	txt.value = txt.value.replace(/^\s*|\s*$/g,"");
}


function mostrar(contGralMsg) { 
   $("#"+contGralMsg).fadeIn('slow'); 
} //checkHover
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);

		return [curleft,curtop];
	}
}
