//main stuff
$(function() {
	/*$.AjaxifyDebug = true;
	$('.get').ajaxify({
		target: '#content',
		tagToload: '#content',
		onStart:function(o){
		}
	}); */
});


//bgtext
$(function() {
	$('input[bgtext]').each(function() {
		$(this).val($(this).attr('bgtext'));
	});
	$('input[bgtext]').live('focus', function() {
		if($(this).val() == $(this).attr('bgtext')) $(this).val('');
	});
	$('input[bgtext]').live('blur', function() {
		if($(this).val() == '') $(this).val($(this).attr('bgtext'));
	});
});


//results roller
$(function() {
	$('.result').live('click', function() {
		if($(this).find('.more').length == 0) return;
		location.href = $(this).find('.more').attr('href');
		return false;
	});
	$('.result').live('mouseover', function(e) {
		if(e.relatedTarget != e.fromElement) return;
		$(this).css({ borderColor: "#909090" }, 500);
	});
	$('.result').live('mouseout', function(e) {
		//if(e.relatedTarget != e.fromElement) return;
		$(this).css({ borderColor: '#cecece' }, 500);
	});
});


//scroller
$(function() {
	$('.scroller').each(function() {
		var sicount = $(this).find('.scrollitem').length;
		if(sicount == 0 && $(this).html().length < 3) {
			$(this).hide();
		} else if (sicount > 1) {
			$(this).find('.scrollitem:first img').load(function() {
				$(this).closest('.scroller').height($(this).height());
				$(this).closest('.scrollitem').height($(this).height());
			});
			$('.scroller').append('<a class="prev prevscroll" href="#"></a><a class="next nextscroll" href="#"></a>');
		}
	});
	
	$('.nextscroll,.prevscroll').live('click', function() {
		var item = $('.scrollitem:visible');
		var newitem;
		item.fadeOut();
		if($(this).hasClass('prevscroll')) {
			if(item.prev('.scrollitem').length == 1) {
				newitem = item.prev('.scrollitem'); 
			} else {
				newitem = item.parent().find('.scrollitem:last');
			}
		} else {
			if(item.next('.scrollitem').length == 1) {
				newitem = item.next('.scrollitem'); 
			} else {
				newitem = item.parent().find('.scrollitem:first');
			}
		}
		
		newitem.fadeIn();
		if(newitem.attr('changetitle')) { 
			$('#subnav a.active').removeClass('active');
			$('#subnav a[rel="' + newitem.attr('changetitle') + '"]').addClass('active');
		}
	});
});



//nav roll
$(function() {
	$('.megacontainer').prepend($('#nav').clone().removeAttr('id'));
	$('.nav a').live('mouseover', function() {
		if($(this).closest('.megacontainer').length == 0) $('.nav a').removeClass('over');
		
		
		//if we are hovering over a mega menu, show it.
		if($(this).hasClass('ismega')) {
			$('.megacontainer').data('key',$(this).attr('rel'));
			if($(this).closest('.megacontainer').length ==0) showMega(); else setTimeout(showMega,250);
		} else {
			$(this).addClass('over');
		}
		
		//are we inside a mega menu nav? bail..
		if($(this).closest('.megacontainer').length > 0 || !$(this).hasClass('ismega')) return;
		
		
		$(this).attr('timeoutid', setTimeout(function() {
			$('.megacontainer[opening=1]').removeAttr('opening');
			
			$('.megacontainer .nav a').removeClass('over');
			$('.megacontainer .nav a[rel="' + $('#nav a.over').attr('rel') + '"]').addClass('over');
			
			$('.inmega').removeClass('inmega');
			$('.megacontainer').css('left',$('#content').position().left-20).show();
			$('#nav a[rel="'+$('.mega:visible').attr('rel')+'"]').addClass('inmega');
		}, 500));
	});
	$('.megacontainer .notmega').live('mouseover', function() {
		$('.megacontainer').attr('closingnotmega','1');
		$('.megacontainer').attr('timeoutnotmegaid', setTimeout(function() {
			$('.megacontainer[closingnotmega=1]').hide().removeAttr('closingnotmega');
			$('.inmega').removeClass('inmega');
		}, 500));
	});
	$('.megacontent').live('mouseover', function() {
		$('.megacontainer').removeAttr('closingnotmega');
	});
	$('.nav a').live('mouseout', function() {
		if($(this).closest('.megacontainer').length > 0) return;
		$(this).removeClass('over');
		
		$('.mega[rel="'+$(this).attr('rel')+'"]').removeAttr('opening');
		clearTimeout($(this).attr('timeoutid'));
	});
	$('#nav a.ismega').live('click', function() {
		//$('.mega[rel="'+$(this).attr('rel')+'"]').css('left',$('#content').position().left-20).fadeIn();
	});
	$('#nav a').live('click', function() {
		$('.mega').removeAttr('opening');
	});
	$('.mega').live('mouseover', function() {
		$('.megacontainer').data('key',$(this).attr('rel'));
	});
	$('.megacontainer').live('mouseover', function() {
		$(this).removeAttr('closing');
		clearTimeout($(this).attr('timeoutid'));
	});
	$('.megacontainer').live('mouseout', function() {
		$(this).attr('closing','1');
		$(this).attr('timeoutid', setTimeout(function() {
			$('.megacontainer[closing=1]').hide().removeAttr('closing');
			$('.inmega').removeClass('inmega');
		}, 500));
	});
	$('#subnav a').live('mouseover', function() {
		$('#subnav a').removeClass('over');
		$(this).addClass('over');
	});
	$('#subnav a').live('mouseout', function() {
		$(this).removeClass('over');
	});
});
function showMega() {
	key = $('.megacontainer').data('key');
	$('.nav a').removeClass('over')
	$('.nav a[rel="'+key+'"]').addClass('over');
	$('.mega').hide();
	$('.mega[rel="'+key+'"]').show();
	$('.megacontainer').attr('opening','1');
	$('.megacontainer').removeAttr('closingnotmega');
}
