var sbsConfirm;
var hideContent;

$(document).ready(
	function(){
		
		$(window).resize(
			function() {
				var ch = $("#content").height() - $("#title").height();
				$("#hidecontent").height(ch);
			}
		);	

		jQuery.fn.slowEach = function( interval, callback ) {
      var array = this;
      if( ! array.length ) return;
      var i = 0;
      next();
      function next() {
      	if( callback.call( array[i], i, array[i] ) !== false )
        	if( ++i < array.length )
            setTimeout( next, interval );
      }
    }; 

		// figure out the colors for active and hover elements, set into variables
		$('body').append('<span id="active_color" class="active" style="display:none;">&nbsp;</span><span id="hover_color" class="hover" style="display:none;">&nbsp;</span>');
		sHoverColor = $('#hover_color').css('background-color');
		sActiveColor = $('#active_color').css('background-color');
		$('#hover_color, #active_color').remove()

		fBBCorners = function(e) {
			e.append('<span class="bb_tl"></span><span class="bb_tr"></span><span class="bb_bl"></span><span class="bb_br"></span>');
		}		

		fPrepSearchButton = function(e) {
			e.focus(
				function() {
					e.val() ==  "search catalog here..." ? e.val("").css("font-style","normal") : false;
				}
			).blur(
				function() {
					e.val() ==  "" ? e.val("search catalog here...").css("font-style","italic") : false;
				}
			).blur();
		}
		
		fPrepBiggerLink = function(e) {
			set = e.find('*').andSelf().filter('.click:has(a)').biggerlink();
			set.hover(
				function() {
					$(this).addClass("hover");
				},
				function() {
					$(this).removeClass("hover");
				}
			);
		}
		
		// applies rounded corners to all 'bb_corners' elements
		$('.bb_corners').each(
			function(){
				fBBCorners($(this));	
			}
		)
		
		fPrepSearchButton($('#f'));
		fPrepBiggerLink($('body'));


		// dialog box for confirmation
		
		hideContent = function() {
			var ch = $("#content").height() - $("#title").height();
			$("#content").prepend("<div id='hidecontent' style='position:absolute;width:100%;background-color:#ffffff;z-index:500;height:"+ch+"px;opacity:0.0'></div>")
			$(".page_title").clone().prependTo("#hidecontent");
			$("#hidecontent").css("opacity","0.0");
			$("#hidecontent").fadeTo(1000, 0.9);
		}
		
		sbsConfirm = function(caption,themessage,doit,hellno,hollerBack) {
			hideContent();
			$("#content").prepend("<div id='sbsdialog' class='bigbluebox'><table><tr><td colspan='2'><h3 class='center knockout bigtext taller'>"+caption+"</h3></td></tr><tr><td colspan='2'><p class='message fat_pad padding_t2 padding_b2 center'>"+themessage+"</p></td></tr><tr><td width='50%'><a href='#no' id='dontdoit' class='bigtext'>"+hellno+"</a></td><td width='50%'><a href='#yes' id='doit' class='bigtext'>"+doit+"</a></td></tr></table><div class='bbb_tl'></div><div class='bbb_tr'></div><div class='bbb_bl'></div><div class='bbb_br'></div></div>");			
			
			$("#sbsdialog").css("opacity","0.0");
			$("#sbsdialog").fadeTo(200, 0.99);
			
			
			$("#dontdoit").click(
				function() {
					$("#sbsdialog").remove();
					$("#hidecontent").remove();
					return false;
				}
			);
		
			$("#doit").click(
				function() {
					hollerBack();
					$("#hidecontent").remove();
					return false;
				}
			);
			
		}

		// dialog box for user sign in

		$("#sign_in_link").click(
			function() {
				hideContent();
				auth_token = $("#sign_in_link").attr("auth_token") // ruby authenticity token passed in as attribute of sign in link
				
				$("#content").prepend("<div id='sbsdialog' class='bigbluebox'><img class='closebox' src='/images/layout/icon_close.png' style='position:absolute;top:6px;right:6px;z-index:200;cursor:pointer;' /><p class='fat_pad padding_t2'>If you are an established SBS dealer and do not have a user ID and password to sign in with, please contact your SBS sales representative for assistance.</p><p class='fat_pad padding_t2'>Access beyond this point is for authorized Seattle Bike Supply dealers, partners, and their respected associates only. Sharing any of the proprietary information contained within this website in conflict with your dealer agreement is grounds for contract termination.</p><p class='fat_pad padding_t2'>Any party accessing these resources without the explicit permission of Seattle Bicycle Supply or one of it's partners is in violation of Revised Code of Washington 9A.52120, computer trespass in the second degree.</p><form method='post'><input name='authenticity_token' type='hidden' value="+auth_token+" /><div class='padding_t2'><p class='fat_pad bigtext'>User ID: <input type='text' name='signin_form_id' id='signin_form_id' class='blue_input' size='14' /></p><p class='fat_pad bigtext'>Password: <input type='password' name='signin_form_pw' id='signin_form_pw' class='blue_input' size='14' /></p><input type='submit' style='display:inline;width:0px;height:0px;'/></div><table><tr><td width='50%'><a href='#no' id='dontdoit' class='bigtext'>Cancel</a></td><td width='50%'><a href='#yes' id='doit' class='bigtext'>Sign In</a></td></tr></table></form><div class='bbb_tl'></div><div class='bbb_tr'></div><div class='bbb_bl'></div><div class='bbb_br'></div></div>");
			
				$("#sbsdialog").css("opacity","0.0");
				$("#sbsdialog").fadeTo(200, 0.99);
					
				$("#signin_form_id").focus();	
					
				$("#dontdoit, img.closebox").click(
					function() {
						$("#sbsdialog").remove();
						$("#hidecontent").remove();
						return false;
					}
				);
			
				$("#doit").click(
					function() {
						$("#sbsdialog form").submit();
						return false;
					}
				);
				
				return false;
			}
		);

		$("#sign_out_link").click(
			function() {
				sbsConfirm("Confirm Signout","You are about to sign out of the website and end your session with us. Are you sure that's what you want to do?","Yes, Sign me out","No, stay signed in", function(){window.location="/user/logout";} );
				return false;
			}
		);
		
    $('ul.sf-menu').superfish({ 
      delay:					1000,                            // one second delay on mouseout 
      animation:			{opacity:'show',height:'show'},  // fade-in and slide-down animation 
      speed:					'fast',                          // faster animation speed 
      autoArrows:			false,                           // disable generation of arrow mark-up 
      dropShadows:		true                          	 // disable drop shadows 
  	}); 

	}
)