/**
 * Namespace:
 * seth
 * version: 1
 *
 * Global Functions:
 * Pngfix
 * 
 * Methods:
 * save_email
 * send_message
 * featured
 *
 * To Use:
 * $('.email_saver').seth('email_saver');
 * $('.featured').seth('add_tool_tip');
 *
 * Format for email saver div
 * <div class="email_saver">
 *<input type="text" class="text">
 *<input type=" class="submit">
 * </div>
 *
 * Used by:
 * RelentlessTV.com
 * RogerDickerman.com
 * RelentlessAnywhere.com
 *
 * Google Analytics Interactions:
 * 1 - Contacted Us
 * 2 - Gave Email
 * 3 - Dug Deeper
 *
 * 
 *
 * 
 */

(function($){

var methods = {
	save_email : function( options ){
		
		var default_text = 'email';
		
		var settings = {
			'backend' : '/store_email',
			'welcome' : "You have been signed up!"
		};
		
		$.extend(settings, options);

		return this.each(function() {
			// bind text
			var email_text = $(this).find('.text');
			var fade_after = $(this).find('.fade_after');
			var message = $(this).find('.message');
			var email_submit = $(this).find('.submit');
			
			//clear textbox when clicked
			// fill with default text when empty
			
			email_text.bind({
				focus: function(){
					if ($(this).val() == default_text) {
						$(this).css('color', '#333');
						$(this).val('');
					}
				},
				blur: function(){
					if ($(this).val() == "") {
						$(this).css('color', '#666');
						$(this).val(default_text);
					}
				}
			});

			// bind submit click to send ajax
			email_submit.click(function() {
				message.html("Sending...");
				$.post( settings.backend, { email : email_text.val(), page : window.location.pathname }, function(data){
					if (data == "true") {
						_gaq.push(['_setCustomVar', 2, 'Gave Email', window.location.pathname, 1]);
						message.html(settings.welcome);
						fade_after.hide();
					}
					else
						message.html(data);
				});				
			});
		});
	},
	send_message : function(options) {
		var settings = {
			'default_text' : "Send us a Message",
			'backend' : '/send_email',
			'welcome' : "You have been signed up!"
		};
		
		$.extend(settings, options);
		
		return this.each(function(){
				var message = $(this).find('.message');
				var submit = $(this).find('.submit');
				var reveal = $(this).find('.reveal');
				var status = $(this).find('.status');
				var email = $(this).find('.email');
				// if msg box is clicked, clear box, reveal email box and submit button
				message.bind({
					focus: function(){
						if ($(this).val() == settings.default_text) {
							$(this).css('color', '#333');
							$(this).val('');
						}
						reveal.fadeIn();
					},
					blur: function(){
						if ($(this).val() == "") {
							$(this).css('color', '#666');
							$(this).val(settings.default_text);
						}
					}
					
				
				});
				
				submit.click( function(){
					status.show();
					if (message.val() == "" )
					{
						status.html('Please enter a message');
						return;
					}
					if (email.val() == "" )
					{
						status.html('Please enter an email/cell number');
						return;
					}
					
					status.html('Sending...');
					
					// send email via ajax
					$.post(settings.backend, {
						email	:	email.val(),
						msg		:	message.val(),
						source	:	location.href},
						function(data)
						{
							if (data == "true")
							{
								_gaq.push(['_setCustomVar', 1, 'Contacted Us', window.location.pathname, 1]);
								email.val('');
								message.val('');
								status.html("Sent!");
								window.setTimeout(function(){
									$('.fade_after').fadeOut();
								}, 2000);
							}
							else
								status.html("Something went wrong.  We're looking into it.");
						});
				});
		});
	},
	add_tool_tip : function() {
		// add tool tip to featured videos
		return this.each(function() {
			$(this).hover(function() {
				$(this).find('.tip').show();
			}, function() {
				$(this).find('.tip').hide();
			});
		});
	},
	outbound : function() {
		return this.each(function() {
			$(this).click( function() {
				try {
				_gaq.push(['_setCustomVar',  3, 'Dug Deeper', $(this).text() , 1]);
				setTimeout('document.location = "' + $(this).attr('href') + '"', 100);
				}catch(err){ throw new Error('Seth, theres an error: '+err);}
				return false;
			});
		});
	}
};

$.pngfix = function () {
	
	if ($.browser.msie && $.browser.version == '6.0') {
		$.each($("img[src$=.png]"), function () {
			var img = $(this);
			img.css({"width": img.width(),"height": img.height(), "filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.attr("src") + "', sizingMethod='scale')"});
			img.attr("src","http://global.rogerdickerman.com/js/seth/images/blank.gif");
		});
	}
};

$.fn.seth = function(method) {
	// select method from seth namespace
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on seth.js' );
    }
};

})(jQuery)
