$(document).ready(function(){

	// focus/blur search form field
	$("#search input, #search2 input").focus(
		function() {
			if(this.value == this.defaultValue) {
			this.value = "";
			}
		}
	).blur(
		function() {
			if(!this.value.length) {
			this.value = this.defaultValue;
			}
		}
	);
	
	$('body').removeClass('nojs');
		
	// target _blank for external links; rel="external"
	$("a.external, a.pdf").click(function(){
	  this.target = "_blank";
	});

	
	// image button hover
    $('input.bttn').hover(function() {
        $(this).css('background-position', '0 -25px');
    },
		function() {
		    $(this).css('background-position', '0 0');
		}
	);
	
	// submit button hover
    $('input#submit').hover(function() {
        $(this).css('background-position', '0 -27px');
    },
		function() {
		    $(this).css('background-position', '0 0');
		}
	);
	
	// tabs
	if (document.getElementById("tabs")) {
		$.getScript('../assets/js/jquery.cookie.js');
		$.getScript('../assets/js/jquery-ui-1.8.7.custom.min.js', function() {
			$('#tabs').tabs();
		});
	}
	
	// accordion
	if (document.getElementById("accordion")) {
		$.getScript('../assets/js/jquery-ui-1.8.7.custom.min.js', function() {
			$("#accordion").accordion({
				active: false,
				autoHeight: false,
				collapsible: true
			});
		});
	}
	
	if (document.getElementById("submit")) {
		$('button.bttn').hover(
			// mouseover
			function(){ $(this).addClass('hover'); },
			
			// mouseout
			function(){ $(this).removeClass('hover'); }
		);	
	}
	
	if (document.getElementById("contactform")) {
		$("#contactform").validate({
			rules: {
				firstname: {
					required: true
				},
				lastname: {
					required: true
				},
				companyname: {
					required: true
				},
				email: {
					required: true,
					email: true
				},
				zip: {
					required: true
				}
			},
			//errorElement: "div",
			//wrapper: "div",  // a wrapper around the error message
			errorPlacement: function(error, element) {
				offset = element.offset();
				error.insertAfter(element)
				//error.addClass('message');  // add a class to the wrapper
			}
		});
	}

	if (document.getElementById("newsletter-form")) {
		$("#newsletter-form").validate({
			rules: {
				firstname: {
					required: true
				},
				lastname: {
					required: true
				},
				companyname: {
					required: true
				},
				email: {
					required: true,
					email: true
				}
			},
			//errorElement: "div",
			//wrapper: "div",  // a wrapper around the error message
			errorPlacement: function(error, element) {
				offset = element.offset();
				error.insertAfter(element)
				//error.addClass('message');  // add a class to the wrapper
			}
		});
	}
	
	// email a colleague form validation
	if (document.getElementById("email-form")) {
		$("#email-form").validate({
			rules: {
				yourname: {
					required: true
				},
				youremail: {
					required: true,
					email: true
				},
				colleaguename: {
					required: true
				},
				colleagueemail: {
					required: true,
					email: true
				}
			},
			errorPlacement: function(error, element) {
				offset = element.offset();
				error.insertAfter(element)
			}
		});
	}

	// facebook share popup
	$('.facebook a').click(function(ev){
		var u = window.location.href;
		var t = document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
		ev.preventDefault();
		return false;
	});
	
	// newsletter header dropdown
	$('#newsletter-header').mouseover(function() {
		$('#newsletter-header-dd').stop().animate({height:'175px'},{queue:false, duration:500})
	}).mouseout(function(){
		$('#newsletter-header-dd').stop().animate({height:'0px'},{queue:false, duration:300})
	});
	
	// trends tab replacement
	$('#trend-tabs a').click(function() {
		$('#latest-trend').text($(this).text());
	});
 
        var ts = new Date().getTime();
        /* load cheese market stock ticker thingy -- RLH 03/11/2011 */
        $.ajax({
            url: '/today.txt?s=' + ts,
            success: function(data){
                $('a.marquee').html(data);
                $('.marquee').marquee();
            }
        });

});

