/* Author: 

*/

(function($){
	var serializeMap = function(arr) {
        var output = {};
        for (var i = 0, l = arr.length; i < l; i++) {
            var item = arr[i];
            output[item.name] = $.trim(item.value);
        }
        return output;
    };

	$.fn.bVal = function(o) {
	    this.bValidator(o.bValidator || {});

	    this.submit(function(e) {
	        e.preventDefault();
	        var $self = $(this),
	            data = $self.data('bValidator');

	        if (!data) return;

	        if (data.isValid()) {
	            var mapped = o.giveMappedInputs ? serializeMap($self.serializeArray()) : null;

	            if (o.onValid) o.onValid($self, data, mapped);
	        }

	    });

	};

	$( '#facebookTab' ).hover(function () {
		console.log( $( this ) );
		$( this ).stop().animate({left: -48, top: -18}, 500);
	}, function () {
		$( this ).stop().animate({left: -203, top: 0}, 500);
	});

	$( '#twitterTab' ).hover(function () {
		$( this ).stop().animate({left: -48, top: 53}, 500);
	}, function () {
		$( this ).stop().animate({left: -203, top: 44}, 500);
	});
	
	$( '#mobileTab' ).hover(function () {
		$( this ).stop().animate({left: -48, top: 108}, 500);
	}, function () {
		$( this ).stop().animate({left: -208, top: 85}, 500);
	});


	jQuery( 'input:text' ).bind( 'focus blur', function( e ) {
		var input	= this,
			element	= e;
			
		if ( e.type === 'focus' && this.value === this.defaultValue ) {
			this.value = '';
		} else if ( this.value === '' ) {
			this.value = this.defaultValue;
		}
	});

	jQuery('form').bind('submit', function(){
		jQuery(this).find('input[type="text"]').each(function(i, input){
			if (input.value === input.defaultValue) {
				jQuery(input).val('');
			}
		})
		return true;
	});

	window.popupPos = function(elem) {
		var topPos = jQuery(window).scrollTop() + 108,
			maxPos = jQuery.getDocHeight() - elem.height() - 120,
			setPos;
		
		topPos > maxPos ? setPos = maxPos : setPos = topPos;
		//jQuery('html, body').animate({scrollTop: setPos}, 'slow');
		return setPos + "px";
	}
	
	jQuery.getDocHeight = function () {
		return Math.max(
			jQuery(document).height(),
			jQuery(window).height(),
			// For opera:
			document.documentElement.clientHeight
		);
	};


	jQuery('#speakerText').click(function () {
		jQuery('#speakWin').css({'display':'block', 'top': popupPos(jQuery('#subscribeWin'))});
		jQuery('#modalBack').css({'display':'block', 'height': jQuery.getDocHeight()});
	});	
	
	jQuery('#sponsorText').click(function () {
		jQuery('#sponWin').css({'display':'block', 'top': popupPos(jQuery('#sponsorWin'))});
		jQuery('#modalBack').css({'display':'block', 'height': jQuery.getDocHeight()});
	});	

	jQuery('#register').click(function () {
		jQuery('#signWin').css({'display':'block', 'top': popupPos(jQuery('#signWin'))});
		jQuery('#modalBack').css({'display':'block', 'height': jQuery.getDocHeight()});
	});	

	if( window.location.hash === "#signup" ) {
		jQuery('#signWin').css({'display':'block', 'top': popupPos(jQuery('#signWin'))});
		jQuery('#modalBack').css({'display':'block', 'height': jQuery.getDocHeight()});
	}
	
	jQuery('.popupClose').each(function () {
		jQuery(this).click(function () {
			jQuery(this).parent('.popupWin').css('display', 'none');
			jQuery('#modalBack').css('display', 'none');
		});
	});

	$('#modalBack').bind( 'click', function() {
		jQuery('.popupWin').css('display', 'none');
		jQuery('#modalBack').css('display', 'none');
	});

	jQuery('#modals').delegate( '.close', 'click', function () {
		jQuery('.popupWin').css('display', 'none');
		jQuery('#modalBack').css('display', 'none');
	});


	$( '#sponForm' ).bVal({
		bValidator : {
			offset: {
				x : -190,
				y : 0
			}
		},
	    onValid : function( form, bValData, valueMap ) {
	    	
		    var $self = form,
				self = form.get(0),
				info = {
					fullName : self.fname.value,
					company : self.company.value,
					email : self.email.value
				};

			$.ajax({
				type: 'POST',
				url: "php/mailer.php",
				data: "subject=Sponsor" + "&fname=" + info.fullName + "&company=" + info.company + "&email=" + info.email,
				dataType: "html",
				success: function () {
					$( '#sponWin' ).find( '.title' ).text( 'THANK YOU!' );
					$( '#sponForm' ).html('<p>Thank you for your submission. An Ad Club representative will be in contact with you shortly. <span class="close">Close &raquo;</span></p>');

				}
			});  

	    }
	});

	$( '#speakForm' ).bVal({
		bValidator : {
			offset: {
				x : -190,
				y : 0
			}
		},
	    onValid : function( form, bValData, valueMap ) {
		    var $self = form,
				self = form.get(0),
				info = {
					fullName : self.fname.value,
					company : self.company.value,
					title : self.title.value,
					email : self.email.value,
					twitter : self.twitter.value,
					facebook : self.facebook.value,
					linkedin : self.linkedin.value
				};

			$.ajax({
				type: 'POST',
				url: "php/mailer.php",
				data: "subject=Speaker" + "&fname=" + info.fullName + "&company=" + info.company + "&title=" + info.title + "&email=" + info.email + "&twitter=" + info.twitter + "&facebook=" + info.facebook + "&linkedin=" + info.linkedin,
				dataType: "html",
				success: function () {
					$( '#speakWin' ).find( '.title' ).text( 'THANK YOU!' );
					$( '#speakForm' ).html('<p>Thank you for your submission. An Ad Club representative will be in contact with you shortly. <span class="close">Close &raquo;</span></p>');

				}
			});  

	    }
	});

	$( '#pushForm' ).bVal({
		bValidator : {
			offset: {
				x : -190,
				y : 0
			}
		},
	    onValid : function( form, bValData, valueMap ) {
		    var $self = form,
				self = form.get(0),
				info = {
					fname : self.fname.value,
					email : self.email.value
				};

			$.ajax({
				type: 'POST',
				url: "php/mailer.php",
				data: "subject=Informed" + "&fname=" + info.fname + "&email=" + info.email,
				dataType: "html",
				success: function () {
					var elem = $('#signWin');
					elem.find('.popupContent').css('height', '465px').find('.hideMe').addClass('hidden');			
					elem.find('.title').css({'font-size': '72px', 'text-align': 'center', 'margin-top': '160px'}).html('THANKS A LOT!');
					elem.find('.subtitle').css('text-align', 'center').html('We will keep you up to date with the latest IDSD news.');
				}
			});  

	    }
	});

	$( '#informJoin' ).click(function(){
		$( '#pushForm' ).submit();
	});

})(jQuery);






















