$ = jQuery;
var curtab = null, 
	curto = null,
	ASN = {
		common : {
			init : function(){
				if( $.browser.msie && $.browser.version < 8 ){
					$('html').addClass('ie6-7');
				}
				
				Cufon
				.replace('.gothambook', {
					fontFamily: 'Gotham Book'
				})
				.replace('.tablenav-pages', {
					fontFamily: 'Gotham Book'
				})
				.replace('.gothambold', {
					fontFamily: 'GothamBold'
				})
				.replace('#content .leftCol h3, #content .leftCol dt', {
					fontFamily: 'GothamBold'
				})
				.replace('.GothamMedium', {
					fontFamily: 'GothamMedium'
				});
				
				$('.scroll-pane')
				.jScrollPane(
					{
						showArrows:true, 
						scrollbarWidth: 15, 
						arrowSize: 18
					}
				);
				
				curtab = $([]);
				
				$('select')
				.each(
					function(){
						$(this)
						.data(
							'bg', 
							$('<div class="select-menu-bg" style="display:none">').appendTo('body')
						);
					}
				)
				.selectmenu(
					{
						style:'dropdown',
						maxHeight: 131,
						menuWidth: 151
					}
				);
				
				$('select[name=search_category]')
				.change(
					function(){
						$(this).closest('form').submit();
					}
				);
				
				$(document)
				.bind('UI-SELECT-OPENED', function(e,that){
					var pos = that.list.position();
					if( $('body').hasClass('home') ){
						pos.left -= 3;
						pos.top -= 4;
					}
					else{
						pos.left -= 4;
						pos.top -= 4;
					}
					that.element
					.data('bg').css( pos ).show();
				});
				$(document)
				.bind('UI-SELECT-CLOSED', function(e,that){
					that.element.data('bg').hide();
				});
			},
			finalize : function(){
				$('.input-focus, #txt_keyword, #input_search')
				.each(
					function(){
						var el = $(this);
						el.data('default', el.val() );
					}
				)
				.focus(
					function(){
						var el = $(this),
							oldval = el.data('default');
						if( el.val() == oldval ){
							el.val('');
						}
					}
				)
				.blur(
					function(){
						var el = $(this),
							oldval = el.data('default');
							
						if( el.val() == '' ){
							el.val(oldval);
						}
					}
				);
				

				var lis = $('#navigation ul > li');
				var curtab = $([]),onf=false,theto=null;
				
				lis
				.hover(
					function(e){
						curtab = $(this);
						curtab.siblings().removeClass('hover');
						$(this).addClass('hover');
						clearTimeout(theto);
					},
					function(e){
						var c = $(this);
						theto = setTimeout(
							function(){
								if( !onf ){
									c.removeClass('hover');
								}
							},250
						);
					}
				);
				
				$('#featuredimages')
				.mouseout(
					function(){
						onf = false;
					}
				)
				.mousemove(
					function(e){
						var el = $(this),
						offset = el.offset(),
						x = e.pageX - (offset.left),
						y = e.pageY - (offset.top);
						
						if( y < 250 ){
							onf = true;
						}else{
							onf = false;
							lis.removeClass('hover');
						}
					}
				);
			}
		},
		home : {
			init : function(){
				$('#home-flash').flash(
					{
						swf : '/wp-content/themes/asn/common/flv/ASNhomepage3.swf',
						width : 940,
						height:280
					});
				$('.select-frame')
				.mouseleave(
					function(){
						var p = $(this).parent();
						p.removeClass('select-menu-open');
					}
				);
				
				$('.select-frame a')
				.click(
					function( e ){
						e.preventDefault();
						var el =  $( this ),
							cur = el.siblings('.selected'),
							txt = el.text(),
							p = el.closest('.select-menu'),
							label = p.find('.select-label');
							
						label.text( txt );
						p.removeClass('select-menu-open');
						cur.removeClass('selected');
						el.addClass('selected');
					}
				);

				$('.select-label')
				.click(
					function( e ){
						e.preventDefault();
						var p = $(this).parent();
						
						if( p.hasClass( 'select-menu-open' ) ){
							p.removeClass('select-menu-open');
						}
						else{
							p.addClass('select-menu-open');
						}
					}
				);
				
	/*

		
		
		$('dl.content')
		.find('dt')
		.mouseenter(
			function(){
				var el = $(this);
				el.siblings('.open').removeClass('open');
				el.addClass('open').next().addClass('open');
			}
		)
		.end()
		.find('dd')
		.mouseenter(
			function(){
				var el = $(this);
				el.siblings('.open').removeClass('open');
				el.addClass('open').prev().addClass('open');
			}
		)
		.end()
		.children()
		.mouseleave(
			function(){
				$(this).removeClass('open').siblings().removeClass('open');
			}
		);
	*/
			}
		},
		page :{
			init : function(){
			}
		}
	};

var UTIL = {
	fire : function(func,funcname, args){
		var namespace = ASN;
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		}
	},
	loadEvents : function(){
		var bodyId = document.body.id;
 
		// hit up common first.
		UTIL.fire('common');
 
		// do all the classes too.
		$.each(
			document.body.className.split(/\s+/),
			function(i,classnm){
				UTIL.fire(classnm);
				UTIL.fire(classnm,bodyId);
			}
		);
 
		UTIL.fire('common','finalize');
	}
}; 
 
// kick it all off here 
$(document).ready(UTIL.loadEvents);
