$(function() {
    $.fn.slideSubMenu = function(){
        var timer, navOpen;

        $(this).find('a[rel]').hover(function(){
            if(timer) { clearTimeout(timer); }
            if(navOpen && navOpen !== $(this).attr("rel")) {
                $(navOpen).slideUp(200);
            }
			navOpen = $(this).attr("rel");
			$(navOpen).slideDown(250);

        },function(){
			timer = setTimeout(function(){
				$(navOpen).slideUp(200);
			}, 500);
        });
		
		$(this).find('a[rel]').each(function() {
		
			$($(this).attr("rel")).hover(function(){
				if(timer) { clearTimeout(timer); }
			},function(){
				timer = setTimeout(function(){
					$(navOpen).slideUp(250);
				}, 500);
			});
		});
    };    
    
	$.fn.checkSearchField = function(){
		var val = $(this).val();
		
		$(this).focus(function() {
			$(this).addClass('sfield');
			if(val === $(this).val()) {
				$(this).val('');
			}
		});
		$(this).blur(function() {
			if($(this).val() === "") {
				$(this).removeClass('sfield');
				$(this).val(val);
			}
		});
	};
	
	$.fn.toggleBox = function(){
		var aBox = null;
		
		$(this).find('span[id]').each(function() {
			var boxId = $(this).attr('id').replace('Button', 'Box');
			$('#'+boxId).hide();
		
			$(this).click(function() {
				var boxId = $(this).attr('id').replace('Button', 'Box');
				var aBoxId = (aBox) ? aBox.attr('id').replace('Button', 'Box') : false;
				if(aBox) {
					window.scrollTo(0, 0);
					$(aBox).hide(200);
				}
				if(aBox === null || aBoxId !== boxId) {
					aBox = $('#'+boxId).show(250, function() {
						var bOffset = $(aBox).offset();
						window.scrollTo(0, bOffset.top);
					});
				}
				if(aBoxId === boxId) { aBox = null; }
			});
		 });
	};
	
	$.fn.headerAni = function(){
		var m = 6, n = Math.round(Math.random() * m), t = $(this);
		
		$(this).css({'position': 'relative', 'width': '890px', 'height': '145px', 'overflow': 'hidden'});
		
		t.find('img').each(function(i) {
			$(this).css({'position': 'absolute', 'top': '0', 'left': '0'});
			if(i !== n) { $(this).fadeOut(); }
		});
		
		t.find('img:eq('+n+')').fadeIn();
		setTimeout(function(){ showImg(); }, 3000);
		
		function showImg() {
			n = ((n+1) > m) ? 0 : n;
			t.find('img:eq('+n+')').fadeIn(2000);
			setTimeout(function(){ hideImg(); }, 3000);
		}
		
		function hideImg() {
			t.find('img:eq('+n+')').fadeOut(2000);
			n++;
			showImg();
		}		
	};
	
});
