var dl; 
//Dialog object
function popup(settings)
{

//the obj implementation purposely avoided chaining
//to keep it simple

//dependent on JQuery v1.3.2 or CSS has also been removed
//to ensure a independent obj that can be deployed on any page

//object initialized after document elements is loaded fully

_this_popup = this;

var props = 
{
	b : document.getElementsByTagName('body')[0],
	debug:false,

setPopup:function(){
		_this_popup.b.innerHTML =  _this_popup.b.innerHTML + _this_popup.static_div_popup();
		$("#popup .cancel_btn").click(function(e){
				$("#popup").hide();
				$("#background").hide();
				e.stopImmediatePropagation();
		});
		
	},

setBackground:function(){
		_this_popup.b.innerHTML = _this_popup.static_div_background() + _this_popup.b.innerHTML;
	},

setSignIn:function(){
		_this_popup.clear();
		_this_popup.setTitle('Sign In to Phototour');
		_this_popup.setSubTitle('Using Phototour Account');
		_this_popup.setOnSubmit('return check_email();');
		_this_popup.setFormAction('user/login_validate.php?l='+document.URL);
		_this_popup.setInput( _this_popup.static_trform_signin() );
		_this_popup.setFooter( _this_popup.static_div_facebook() );
	},

showPopup:function(){
		$("#popup").show();
		$("#background").show();
	},

showSignIn:function(){
		_this_popup.setSignIn();
		$("#popup").show();
		$("#background").show();
	},

setWidth:function(str){
		var l= Math.floor(($('body').width()-str)/2);
		str= str+"px";
		//note: assuming total width of #container is set to 982
		$("#popup").css('width',str).css('left',l);	
	},

setTitle:function(str){
		$("#popup .title").html(str);
	},

setSubTitle:function(str){
		$("#popup .sub_title").html(str);
	},

setInput:function(str){
		$( "#popup_add_input" ).html( str );
	},

setFooter:function(str){
		$( "#popup .footer" ).html( str );
	},

setFormAction:function(str){
		$( "#popup .form" ).attr( 'action' , str );
	},

setOnSubmit:function(str){
		$( "#popup .form" ).attr( 'onSubmit' , str );
	},

hide:function(){
		$( "#popup" ).hide();
		$( "#background" ).hide();
	},

clear:function(){
		_this_popup.setWidth('320');
		_this_popup.setFormAction('');
		_this_popup.setOnSubmit('return false;');
		_this_popup.setTitle('');
		_this_popup.setSubTitle('');
		_this_popup.setInput('');
		_this_popup.setFooter('');
	},

static_trform_signin : function(){
var k = ['<tr>',
	'<td><label>Email:</label></td>',
	'<td><input type="text" id="user_email" name="user_email"/></td>',
	'</tr>',
	'<tr>',	
	'<td><label>Password:</label></td>',
	'<td><input type="password" id="user_password" name="user_password"/>',
	'<input type="hidden" id="form_submit" name="form_submit" value="true" /></td>',
	'</tr>',
	'<tr>',
	'<td></td>',
	'<td><input type="submit" id="submit" value="Sign In" /></td>',
	'</tr>'].join('');
return k;
	},
	
static_div_facebook : function(){
var k =['<div class="facebook_sign_in" style="padding:8px 0px">',
	'<div style="font-size:13px;padding-bottom:8px;">OR Sign In via Facebook</div>',
	'<div><a href="#" onclick="fblogin();return false">',
	'<img style="border:none" src="design/index/facebook.jpg"/></a></div>',	
	'</div>'].join('');
	
	return k;	
	},

static_div_popup : function(){
var k = ['<div id="popup" style="position:fixed;top:80px;left:331px;',
		'width:320px;z-index:5000000;',
		'background-color:#FFFFFF;',
		'font:13px Arial,sans-serif;display:none;">',
'<div class= "blue_outer_border" style="border:1px solid #000000;',
					'background-color:#C1D9FF;position:relative;padding:6px">',
'<div class= "blue_inner_border" style="background-color:#FFFFFF;position:relative;">',	
'<div class="cancel_btn" style="position:absolute;right:2px;top:2px;cursor:pointer;">',
	'<img src="design/blue_cross.jpg" />',
	'</div>',
	'<div class="title" style="font-size:16px;font-weight:900;',
					'background-color:#D1E9FF;padding:8px 0px 8px 6px;',
					'color:#000000;margin:0px;">',
		'Phototour',
	'</div>',
'<div class="padding" style="padding-left:6px;">',	
	'<div class="sub_title" style="font-size:16px;padding:8px 0px 0px 0px;"></div>',	
	'<form class="form" action="" method="post" ><table style="padding:8px 0px;width:100%;">',
	'<tbody id="popup_add_input">',
	'</tbody>',
	'</table></form>',	
	'</div><div class="footer" style="padding-left:6px;"></div><div style="clear:both;"></div>',
'</div></div></div>'].join('');
	
	return k;	

	},

static_div_background:function(){
		var k = ['<div id="background" style="position:fixed;top:0px;left:0px;',
			'width:100%;height:100%;z-index:5000000;',
			'background-color:#000000;opacity:0.3;filter:alpha(opacity=30);display:none;"></div>'].join('');
	
		return k;
	}

	
}


//initializing object methods and properties	
	for( var p in props )
	{
		this[p] = ( typeof settings[p] == 'undefined' )? props[p] : settings[p];
	}

if( !document.getElementById( "popup" ) )
{
	//this.setPopup();
}
else
{
	if( this.debug ) alert( 'there is already an element with id=popup in the DOM' );
}

if( !document.getElementById( "background" ) )
{
	this.setBackground();
}
else
{
	if( this.debug ) alert( 'there is already an element with id=background in the DOM' );
}

}

function check_email()
{
	var user_email = $("#user_email").attr("value");
	var user_pass = $("#user_password").attr("value");

	var user_msg ;
	var check_valid = true;
	if(user_email=="" && user_pass == ""){
		user_msg="Please enter your user email & password.";
		check_valid = false;
	}else if(user_email==""){
		user_msg="Please enter your user email.";
		check_valid = false;
	}else if(user_pass==""){
		user_msg="Please enter your user password.";
		check_valid = false;
	}
	if(check_invalid_char(user_email) && check_invalid_char(user_pass)){
		user_msg= user_msg + "Invalid characters(symbols & spaces) not allowed in email & password.";
		check_valid = false;
	}else if(check_invalid_char(user_email)){
		user_msg = user_msg + "Invalid characters(symbols & spaces) not allowed in email.";
		check_valid = false;
	}else if(check_invalid_char(user_pass)){
		user_msg = user_msg + "Invalid characters(symbols & spaces) not allowed in password.";
		check_valid = false;
	}	

 	if(check_valid){
		return true;
	}else{
		alert( user_msg );
		return false;
	}
	
}

function check_invalid_char(str){
str = escape(str);
str = "a" + str; //weird bug that, when str escape has first character as ? it does not work
var tester = str.search(/(<|>|{|}|%|\$|\+|\*|~|&)/);
//alert(tester);
if(tester >0){
return true;
}else{
return false;
}
}
