$.fn.checkForm = function() {
	Result = true;
	this.find('.required').each(function() {
		This = $(this);
		Parent = This.parent();
		tag = this.tagName.toLowerCase();
		
		if (tag == "textarea") {
			if (This.val() == "") {
				This.addClass("error");
				Result = false;
			} else if (This.val() == This.html()) {
				This.addClass("error");
				Result = false;
			} else {
				This.removeClass("error");
			}
		} else if (This.val() == "") {
			Parent.addClass("error");
			Result = false;
		} else {
			Parent.removeClass("error");
		}
	});
	return Result;
};

function onBefore(currSlideElement, nextSlideElement, options, forwardFlag) {
	This = $(this);
	Title_top = $("#showcase_title");
	Title_top.animate({
		opacity: 0
	}, 400);
	toggleLoading();
}
function onAfter(currSlideElement) {
	This = $(this);
	Title_top = $("#showcase_title");
	Title_top.html(This.find('.title').html()).animate({
		opacity: 1
	}, 200);
	toggleLoading();
	
	id = This.attr("id");
	navClass = 'nav_'+id;
	
	$('.nav').hide();
	$('.'+navClass).show();
}
function toggleLoading() {
	Loading = $("#loading");
	if (Loading.attr("src").match(/loading.jpg$/)) {
		Loading.attr("src", "/wp-content/themes/cs2/images/loading.gif");
	} else {
		Loading.attr("src", "/wp-content/themes/cs2/images/loading.jpg");
	}
}

$(function() {
	$("#showcase_container > div").show();
	$.get("/posts/", function(d) {
		data = $(d).find("#showcase_container");
		$("#showcase_container").append(data.html());
		
		$("#showcase_container > div .img").scrollview({
			grab:"/wp-content/themes/cs2/images/openhand.cur",
			grabbing:"/wp-content/themes/cs2/images/closedhand.cur"
		});

		$('#showcase_container').cycle({
			fx: 'scrollHorz',
			timeout: 7000,
			next: 'a.showcase_nav.next',
			prev: 'a.showcase_nav.prev',
			before: onBefore,
			after: onAfter
		});

		$('#showcase_container div div.img').each(function(i) {
			This = $(this);
			parentId = This.parent().attr("id");

			navClass = '.nav_'+parentId;
			navContent = $('<ul class="nav"></ul>');
			navContent.addClass(navClass.replace(".", ""));
			$("#showcase_nav").append(navContent);
			This.cycle({
				fx: 'scrollLeft',
				timeout: 0,
				pager: navClass,
				pagerAnchorBuilder: function(idx, slide) { 
				        return '<li><a href="#">&nbsp;</a></li>'; 
				}
			});

			$("#showcase_nav .nav:first").show();
		});
	});
	
	$("#showcase").mouseenter(function() {
		$('#showcase_container').cycle('pause');
	}).mouseleave(function() {
		$('#showcase_container').cycle('resume');
	});
	
	$("#contact form label input, #contact form textarea").focus(function() {
		This = $(this);
		if (This.is('textarea')) {
			El = This;
			if (This.val() == "Message") {
				This.val("");
			}
		} else {
			El = $(this).parent();	
		}
		El.addClass("active");
	}).blur(function() {
		This = $(this);
		if (This.is('textarea')) {
			El = This;
			if (This.val() == "") {
				This.val(This.html());
			}
		} else {
			El = $(this).parent();	
		}
		El.removeClass("active");
	});
	
	$("#contact_form").submit(function() {		
		This = $(this);
		Data = $("#contact_form").serialize();
		Url = This.attr("action");
		
		Contact = $("#contact");
		Loading = $("#loading2");
		Thanks = $('#contact_thanks');
		Thanks.hide();
		Contact.append(Thanks);
		
		if (!This.checkForm()) {
			Thanks.removeClass("title_image").html("Please fill the required fields.").show();
			return false;
		}
		
		Loading.show();
		
		$.post(Url, Data, function(data) {
			if (data == "Sent") {
				Thanks.addClass("title_image").html("Thanks for contact us! We\'ll get back to you ASAP.").show();
				$("#contact_form")[0].reset();
			} else if (data != "") {
				Thanks.removeClass("title_image").html(data).show();
			} else {
				Thanks.removeClass("title_image").html("<b>Error:</b> Try again.").show();
			}
			Loading.hide();
		});
		
		return false;
	});
});