/*
 * contactable 1.2.1 - jQuery Ajax contact form
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2010-01-18 $
 *
 */
 
//extend the plugin
(function($){

	//define the new for the plugin ans how to call it	
	$.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			url: '/feedback/bug',
			name: 'Name',
			message : 'Message',
			subject : 'A contactable message',
			submit : 'SEND',
			recievedMsg : 'Thank you for your message',
			notRecievedMsg : 'Sorry but your message could not be sent, please try again later.',
			disclaimer: 'Please feel free to get in touch, we value your feedback.',
			hideOnSubmit: false,
                        userid: '',
                        first_name: '',
                        full_name: '',
                        username: '',
			email: '',
                        browser: '',
                        operating_system: '',
                        screen_size: ''

		};

		//call in the default otions
		var options = $.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function() {
			//construct the form
			var this_id_prefix = '#'+this.id+' ';
                        
                        if(options.first_name != '')
                        {
                            greetname = ' ' + options.first_name;
                        }
                        else if(options.username != '')
                        {
                            greetname = ' ' + options.username;
                        }
                        else
                        {
                            greetname = '';
                        }
                        
			$(this).html('<div id="contactable_inner"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p>Hello'+greetname+', please fill out the following bug report form:</p>\
<p><label for="report_type">Type of Issue: <span class="red"> * </span></label><br />\
<select id="report_type" name="report_type">\
    <option>Spelling/grammer problem</option>\
    <option>Missing image</option>\
    <option>Other image issue</option>\
    <option>Can\'t record Fame Spot</option>\
    <option>Fame Spot playback issue</option>\
    <option>Ticker/Social Shout issue</option>\
    <option>iPhone app issue</option>\
    <option>Android app issue</option>\
    <option>Inappropriate/obscene content</option>\
    <option>Account/login issue</option>\
    <option>Other issue</option>\
</select>\
</p>\
    <p><label for="message">'+options.message+' <span class="red"> * </span></label><br /><textarea id="message" name="message" class="message" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="'+options.submit+'"/></p><p class="disclaimer">'+options.disclaimer+'</p></div><div class="close_button"></div></form>');
			//show / hide function
                        var formVisible = false;
                        
			$(this_id_prefix+'div#contactable_inner, ' + this_id_prefix+'.close_button').click(function()
                        {
                            if(!formVisible)
                            {
				$(this_id_prefix+'#overlay').css({display: 'block'});
				$(this_id_prefix+'div#contactable_inner').animate({"marginLeft": "-=5px"}, "fast"); 
				$(this_id_prefix+'#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				$(this_id_prefix+'div#contactable_inner').animate({"marginLeft": "+=387px"}, "slow"); 
				$(this_id_prefix+'#contactForm').animate({"marginLeft": "+=390px"}, "slow");
                                formVisible = true;
                            }
                            else
                            {
				$(this_id_prefix+'#contactForm').animate({"marginLeft": "-=390px"}, "slow");
				$(this_id_prefix+'div#contactable_inner').animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$(this_id_prefix+'#overlay').css({display: 'none'});
                                formVisible = false;
                            }
			});

			//validate the form 
			$(this_id_prefix+"#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					message: {
						required: true
					}
				},
				//set messages to appear inline
					messages: {
						name: "",
						email: "",
						message: ""
					},			

				submitHandler: function() {
					$(this_id_prefix+'.holder').hide();
					$(this_id_prefix+'#loading').show();
$.ajax({
  type: 'POST',
  url: options.url,
  data: {subject:options.subject, url:location.href, report_type:$(this_id_prefix+'#report_type').val(), name:options.full_name, userid: options.userid, username: options.username, email: options.email, browser: options.browser, operating_system: options.operating_system, screen_size: options.screen_size, message:$(this_id_prefix+'#message').val()},
  success: function(data){
						$(this_id_prefix+'#loading').css({display:'none'}); 
						if( data == 'success') {
							$(this_id_prefix+'#callback').show().append(options.recievedMsg);
							if(options.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								$(this_id_prefix+'#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
								$(this_id_prefix+'div#contactable_inner').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								$(this_id_prefix+'#overlay').css({display: 'none'});	
							}
						} else {
							$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
							setTimeout(function(){
								$(this_id_prefix+'.holder').show();
								$(this_id_prefix+'#callback').hide().html('');
							},2000);
						}
					},
  error:function(){
						$(this_id_prefix+'#loading').css({display:'none'}); 
						$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
                                        }
});		
				}
			});
		});
	};
 
})(jQuery);

