$(document).ready(function(){

	if($("ul.dropdown").length) $("ul.dropdown li").dropdown();

	if($("form.validate").length) {

		$("form.validate").submit(function() {

			$(".alert", this).removeClass("alert");

			var msg = '';
			var valid = true;
			var email_pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/i;

			$(this).find(".required").each(function() {
				var value = $(this).val();
				if(value == "" || ($(this).prop("title") && value == $(this).prop("title")) || ($(this).is(".email") && !email_pattern.test(value))) {
					valid = false;
					var label = $(this).prev("label").text() || $(this).prop("title");
					msg += '\n-> '+label.replace(/[\*|:]/gi, "")+' is required';
					$(this).addClass("alert");
				}
			});

			if(!valid) {

				if(msg != '') {
					alert('Please fill in these fields. Thanks.'+msg);
					$(".alert:first", this).focus();
					return false;
				}

				return false;
			}
		});

		$("form.validate input, form.validate textarea").bind("focus", function(){
			$(this).addClass("focus");
			if($(this).val() == $(this).attr("title")) {
				$(this).val("");
			}
		});

		$("form.validate input, form.validate textarea").bind("blur", function(){
			$(this).removeClass("focus");
			if($(this).val() == "") {
				$(this).val($(this).attr("title"));
			}
		});

		$("form.validate .field-select .default").click(function(){
			$(this).parent().toggleClass("field-select-focus");
			$(this).next().toggle();
		});

		$("form.validate .field-select ul li").click(function(){
			var top = $(this).closest(".field-select")
			top.toggleClass("field-select-focus");
			$(".default .value", top).text($(this).text());
			$("input[type=hidden]", top).val($(this).text());
			$(this).parent().toggle();
		});

	}

	if($("#slides1").length) {

		$('#slides1').slides({
			effect: 'slide',
			crossfade: true,
			play: 5000,
			slideSpeed: 1000,
			hoverPause: true,
			pause: 100,
			generatePagination: false,
			generateNextPrev: true
		});
	}

	if($("ul.xoxo").length) {

		$("ul.xoxo > li:not(.widget_calendar)")
			.addClass("box boxB")
			.wrapInner('<div class="boxB-inner" />')
			.wrapInner('<div class="boxB-after" />')
			.wrapInner('<div class="boxB-before">');
	}

	if($('[rel="colorbox"]').length) {

		$('[rel="colorbox"]').colorbox({
			opacity: .7
		});
	}

	if($("a.loading[href]").length) {

		$("a.loading[href]").each(function(){

			var self = $(this);

			$.get($(this).attr("href"), function(data) {
				self.after(data).remove();
			});

		});

	}

	if($(".jcarousel-next-horizontal").length) {
		$(".jcarousel-next-horizontal").wrapInner('<span />');
	}

	if($(".jcarousel-prev-horizontal").length) {
		$(".jcarousel-prev-horizontal").wrapInner('<span />');
	}

	if($(".fancy").length) {

		$(".fancy").each(function(){
			
			if($("a", this).length) {
				$(this).css("cursor", "pointer");
				$(this).click(function(e){
					e.preventDefault();
					var first_link = $("a:first", this);

					if(first_link.attr("target") && first_link.attr("target") == "_blank") {
						window.open(
						  $("a:first", this).attr("href"),
						  '_blank'
						);
					} else {
						location.href = $("a:first", this).attr("href");
					}
				})
			}
		});

	}
	
});

$.fn.dropdown = function() {

	return this.each(function() {

		$(this).hover(function(){
			$(this).addClass("hover");
			$('> .dir',this).addClass("open");
			$('.box:first',this).css('visibility', 'visible');
		},function(){
			$(this).removeClass("hover");
			$('.open',this).removeClass("open");
			$('.box:first',this).css('visibility', 'hidden');
		});

	});

}
