function showPopup(div) {

	snippetLauncher.init(div);

}


/*
	snippet controller
--------------------------------------------------------------------- */
var snippetLauncher = {
	
	url : "",
	position : {top: "",left: ""},
	theWindow : {width: "",height: ""},
	
	init: function(div){
		//$(div).click(function(){
			snippetLauncher.launch( $(div) );
			return false;
		//});
	},
	
	launch: function(theObject){
		
		// gather some data
		this.theWindow.width = (( $(document).width() >= $(window).width() ) ? $(document).width() : $(document).width());
		this.theWindow.height = (( $(document).height() >= $(window).height() ) ? $(document).height() : $(window).height());

		this.url = theObject.attr('href');
		this.position = theObject.position("top");

		// Hide select elements in IE6 ( from ThickBox: jquery.com/demo/thickbox/ )
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			//$("body","html").css({height: "100%",width: "100%"});
			$("html").css("overflow","hidden");

			if (document.getElementById("mcqbox-hideselect") === null) {//iframe to hide select elements in ie6
//				$("body").append("<iframe id='mcqbox-hideselect'></iframe><div id='snippet'><a href='#' id='snippet-close'>x close</a><div id='snippet-content'></div></div>");
//				$("body").append("<div id='snippet-bg'>&nbsp;</div>");
				
				$("#snippet-close").click(function(){
					snippetLauncher.close();
				});
			}
		}
		
		else {//all others
			if(document.getElementById("snippet-bg") === null){
				$("body").append("<div id='snippet'><a href='#' id='snippet-close'>x close</a><div id='snippet-content'></div></div>");
				$("body").append("<div id='snippet-bg'>&nbsp;</div>");
				
				$("#snippet-close").click(function(){
					snippetLauncher.close();
				});
			}
		}
	
		$("#snippet-bg").css({height:this.theWindow.height, width:this.theWindow.width});
		$('#snippet').hide();
		
		var popupContent = $(this.url);
		//popupContent.show();
				
		$("#snippet-content").html(popupContent.html());
		snippetLauncher.setPosition();
		$('#snippet').fadeIn();		
		
	
	// $("#snippet-close").click(function(){
	// 	snippetLauncher.close();
	// });
	
	
	$(window).resize(function(){
		$('#snippet-bg').css({
			//width:	(( $(document).width() >= $(window).width() ) ? $(document).width() : $(window).width()) + 'px',
			//height:	(( $(document).height() >= $(window).height() ) ? $(document).height() : $(window).height()) + 'px'
			
			width:  $(document).width(),
			height: $(document).width()
		});
		snippetLauncher.setPosition();
	});

	},
	
	setPosition: function(){
		scrollTop = $(document).scrollTop();
		positionTop = (( $(window).height() - $('#snippet').height() ) /3)+scrollTop;
		positionLeft = ( $(window).width() - $('#snippet').width() ) /2;
		
		/* Shift over to compensate for scrollbar width in Safari */
		if (jQuery.browser.safari) {
			positionLeft = (( $(window).width() - $('#snippet').width() ) /2)-7;
		}
		$("#snippet").css({left: positionLeft+'px', top: positionTop+'px'});
	},
	
	close: function(){
		
		$("#snippet-close").unbind("click");
		
		$("#snippet").fadeOut('fast',function(){
			$("#snippet-bg").fadeOut('slow');
			$('#snippet,#snippet-bg,#mcqbox-hideselect').trigger("unload").unbind().remove();
		});
		
		// reset IE 6 settings
		if (typeof document.body.style.maxHeight == "undefined") {
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
		}
		
		//document.onkeydown = "";
		//	document.onkeyup = "";
		//	return false;
	}
	
};

/* Send to a friend - form validation and error messages */
function validate(){
	
	//clear errors box
	document.getElementById('errors_box').innerHTML = ""; 
	$("#errors_box").addClass('error-alert');

	//look to add error messages to box
	if_field_empty_append_error("pmessage", '"Personalized message" is empty');
	email_validation(document.getElementById("yemail").value, '"Your email" field is invalid', "yemail");
	if_field_empty_append_error("yname", '"Your name" field is empty');
	email_validation(document.getElementById("femail").value, '"Friend\'s email" field is invalid', "femail");

  	errors_box_content = document.getElementById('errors_box').innerHTML;

	//if box empty then show confirmation
	if ( errors_box_content == '' ) {
		document.getElementById('email_friend_form_wrapper').innerHTML = "<h3>Form sent</h3>";
	}
	else {
		document.getElementById('errors_box').innerHTML = "<h3>We spotted an error, please check the items below</h3><ul>" + errors_box_content + "</ul>";
	}
	
}

function if_field_empty_append_error(field, msg) {
	
	is_field_empty = check_field_empty(field);
	if (is_field_empty == true) {
	  append_errors("errors_box", msg);
	  highlight_erroneous_field(field);
	}
	else {
	  $('#label_'+field).removeClass('error');	
	}

}

function email_validation(str, msg, field) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1){
		   append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		}

		else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		}

		else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		    
		}

		 else if (str.indexOf(at,(lat+1))!=-1){
		    append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		 }

		 else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		    
		 }

		 else if (str.indexOf(dot,(lat+2))==-1){
		    append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		    
		 }
		
		 else if (str.indexOf(" ")!=-1){
		    append_errors("errors_box", msg); highlight_erroneous_field(field); return;
		    
		 }
		
		 else {
		   $('#label_'+field).removeClass('error');	
	 	 }
	
}

function check_field_empty(field) {
	
  f = document.getElementById(field);
  l = f.value.length;
	if (l == 0) {
	  return true;
	}
	else {
		return false;
	}

}

function append_errors(id, msg) {
  append_this = document.getElementById(id).innerHTML;
  document.getElementById(id).innerHTML = "<li>" + msg + "</li>" + append_this;
}

function highlight_erroneous_field(field) {
	 label_field = 'label_'+field;	
	$("#"+label_field).addClass('error');
}



