$(function() { var $productBise = $('#loofirpa-bise'), $selectAll = $productBise.find('.select-all'), $removeAll = $productBise.find('.remove-all'), $biseItem = $productBise.find('.product label'), $biseEmailMe = $productBise.find('.butt0n-submit'), $biseEmailInput = $productBise.find('input[type="email"]'), $errorMsg = $productBise.find('.error-msg'), validate_email, url_escape, format_param, build_request_string, selectOption, removeOption, countSelected, validateSelection; if ( !tg.account.is_guest_account && tg.account.email ) $biseEmailInput.val(tg.account.email); selectOption = function( $item ) { var $checkBox = $item.find('input[type="checkbox"]'); $checkBox.prop('checked', true); if ( !$item.hasClass('selected') ) $item.addClass('selected'); }; removeOption = function( $item ) { var $checkBox = $item.find('input[type="checkbox"]'); $checkBox.prop('checked', false); if ( $item.hasClass('selected') ) $item.removeClass('selected'); }; countSelected = function( $items ) { var count = 0; for ( var i = 0, itemsLen = $items.length; i < itemsLen; i++ ) { if( $($items[i]).find('input[type="checkbox"]').prop('checked') ) count++; } return count; }; fire_omniture = function( confirmation ) { if ( typeof confirmation === 'undefined' ) confirmation = false; var s = s_gi( document.location.hostname.indexOf('ffx') === -1 ? 'thinkgeek' : 'devthinkgeek' ), page_name = "Email Me When Back In Stock" + ( confirmation ? ' | Success' : '' ); s.pageName = page_name; s.linkTrackVars = "prop6,prop11,eVar17,eVar34,eVar35"; s.eVar17 = page_name; s.eVar34 = typeof tg.account.account_id !== 'undefined' ? tg.account.account_id : ''; s.eVar35 = tg.account.is_guest_account ? "not logged in" : 'logged in'; s.prop6 = page_name; s.prop11 = ( document.location.pathname.indexOf('/product/') !== -1 ? 'Product | ' : '' ) + document.title.replace(' | ThinkGeek', ''); s.t(); }; validate_email = function( email ) { var regEx = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return regEx.test( email ); }; url_escape = function( string ){ return escape( string ).replace(/\+/g, '%2B'); }; format_param = function( key, value, no_and ) { if ( typeof no_and === 'undefined' ) no_and = false; return ( no_and ? '' : '&' ) + url_escape(key) + '=' + url_escape(value); }; build_request_string = function( $form ) { var request_string = ''; request_string += format_param( 'a', 'u', true ); request_string += format_param( 'id', $form.find('[name="id"]').val() ); $form.find('[name*="sku_"]').each(function(){ var $that = $(this), $thatInputType = $that.attr('type'), $thatSku = $that.attr('name').replace('sku_', ''); if ( $thatInputType === 'checkbox' && $that.prop('checked') ) { request_string += format_param( 'sku_' + $thatSku, 'on' ); } else if ( $thatInputType === 'hidden' ) { request_string += format_param( 'sku_' + $thatSku, 'on' ); } }); request_string += format_param( 'email', $form.find('[name="email"]').val() ); return request_string; }; $biseItem.click(function() { var $that = $(this), $parent = $that.closest('.product'); if ( $parent.hasClass('selected') ) { removeOption( $parent ); } else { selectOption( $parent ); } if ( !$errorMsg.hasClass('hide') && $productBise.find('[name*="sku_"]:checked').length ) { $errorMsg.addClass('hide'); } return false; }); $selectAll.click(function() { var $items = $('.bise .product'); for ( var i = 0, itemsLen = $items.length; i < itemsLen; i++ ) { selectOption( $($items[i]) ); } return false; }); $removeAll.click(function() { var $items = $('.bise .product'); for ( var i = 0, itemsLen = $items.length; i < itemsLen; i++ ) { removeOption( $($items[i]) ); } return false; }); $biseEmailMe.click(function() { fire_omniture(); }); $biseEmailInput.keyup(function() { if ( !$errorMsg.hasClass('hide') ) { if ( validate_email( $(this).val() ) ) { if ( !$productBise.find('[name*="sku_"]:checked').length ) { $errorMsg.text( 'Select some favorites or we\'ll be sad :(' ); } else { $errorMsg.addClass('hide'); } } } }); $productBise.find('form').submit(function() { var $that = $(this), $buttonSubmit = $that.find('.butt0n-submit'), $emailInput = $that.find('[type="email"]'), $errorMsg = $that.find('.error-msg'), request; if ( !$that.find('[name*="sku_"]:checked').length ) { $errorMsg.text( 'Select some favorites or we\'ll be sad :(' ).removeClass('hide'); return false; } if ( !$emailInput.val() || !validate_email( $emailInput.val() ) ) { $errorMsg.text( 'Double check that email address again :)' ).removeClass('hide'); return false; } $productBise.addClass('is-loading'); $buttonSubmit.prop('disabled', true); request = $.ajax({ type: 'POST', url: '/brain/email_bis.cgi?' + build_request_string( $that ) }); request.success(function(data){ var $data = $(data); if ( $data.filter('title').text().toLowerCase().indexOf('thank you') !== -1 ) { var $biseConfirmContent = $data.find('.bise-confirm-content'), $closeButton = $biseConfirmContent.find('.butt0n-close'), closeProtocol = 'https', closeTriggered = false; $closeButton.attr('onclick', 'parent.$.fancybox.close();return false;'); $.fancybox({ content: $biseConfirmContent, overlayOpacity: 0.6, overlayColor: '#000', 'autoSize': false, fitToView: false, onComplete: function() { fire_omniture(true); } }); } else { if ( error_msg = $data.find('.error').text() ) { $errorMsg.text( error_msg ).removeClass('hide'); } else { $errorMsg.text( 'Oops, unknown error!' ).removeClass('hide'); } } }); request.fail(function(data){ $errorMsg.text( 'Uh oh, failed to contact the server monkeys!' ).removeClass('hide'); }); request.always(function() { $buttonSubmit.prop('disabled', false); $productBise.removeClass('is-loading'); }); return false; }); });