$(document).ready(function() { 
	var page = ($("#page_title").children("span").html() == null) ? $("#page_title").html() : $("#page_title").children("span").html();
	$('#main_right_content_text').jScrollPane({scrollbarWidth:14, showArrows:true});
	if ($("#viewport").children("ul").children("li").length >= 5) {
		$('#viewport').carousel('#previous', '#next');
	}
	if (page == "Gallery") {
		$(".gallery-image").attr("src", "app-img/1-0.gif");
		$('#links-ul').children(':first').addClass('gallery-selected');
		var first = $('#viewport ul').children(":first");
		first.addClass("selected");
		$(".gallery-image").attr("src", first.children("span").attr("title"));
		$('.gallery-title').html(first.children('span').html());

	}
	
	$('.gallery-link').live("click", function() {
		$(".gallery-image").attr("src", "app-img/1-0.gif");
		$('.gallery-selected').removeClass('gallery-selected');
		$(this).addClass('gallery-selected');
		if (page == "Gallery") {
			
			var dir = $(this).attr("id");
			$.post( "app-elements/galleries.php", {gallery: dir}, 
			function(data) {
				if (data) {
					$("#viewport").html(data);
					if ($("#viewport").children("ul").children("li").length >= 5) {
						$('#viewport').carousel('#previous', '#next');
					}
					$("#viewport li.ribbon-thumb:first").click();
					$(".gallery-image").attr("src", $("#viewport li.ribbon-thumb:first").children("span").attr("title"));
				}
			}
		)
		} else {
			$('#gallery-img').attr('src', $(this).children('span').attr('title'));
			$('#gallery-p').html($(this).children('span').html());
		}
	});
	
	$('#viewport li.ribbon-thumb').live("click", function() {
		$(".gallery-image").attr("src", "app-img/1-0.gif");
		$('#viewport ul li.ribbon-thumb').removeClass('selected');
		var li = $(this).children('span').attr('title');
		$('#viewport li.ribbon-thumb').each(function(i) {
			if ($(this).children('span').attr('title') == li) {
				$(this).addClass('selected');
			}
		});
		if (page == "Gallery") {
			var span = $(this).children("span");
			$(".gallery-image").attr("src", span.attr("title"));
			$(".gallery-title").html(span.html());
		} else if (page == "Case Studies") {
			var main = $('#main_right_content_image');
			var span = $(this).children('span');
			main.children("img").attr('src', span.attr('title'));
		} else {
			var main = $('#main_right_content_image');
			var span = $(this).children('span');
			var title = span.attr('title');
			var img_src = "/Portals/111909/images/" + "/" + span.html();
			main.children('h1').html(span.attr('id'));
			$(".study-img").attr('src', img_src);
			$("#car-holder").html('<h1>' + span.attr('id') + '</h1><p>Learn more about this case study for <span class="study_name">' + span.attr('id') + '</span><a href="/case-studies/' + title + '" class="study_link"> here</a>.</p>');
		}
	});
	
	$(".case-study").live("click", function() {
		var study = $(this).children("span").html();
		$.get( "app-elements/case-study.php", {gallery: study}, 
			function(data) {
				if (data) {
					$("#main_right_content").html(data);
					$('#main_right_content_text').jScrollPane({scrollbarWidth:14, showArrows:true});
					if ($("#viewport").children("ul").children("li").length >= 5) {
						$('#viewport').carousel('#previous', '#next');
					}
				}
			}
		)
	});
	
	//code here for validation of the contact information before passing to php mailer
	if (page == "Contact") {
		$("#phone_number").mask("(999) 999-9999");
	    $("#contact-form").validate({
			rules: {
				firstname: "required",
				lastname: "required",
				phone_number: "required",
				email: {
					required: true,
					email: true
				},
				message: "required"
			},
			messages: {
				firstname: "Please enter your first name",
				lastname: "Please enter your last name",
				email: "Please enter a valid email address",
				agree: "Please accept our policy",
				phone_number: "Please enter a valid phone number, including area code",
				message: "Please enter a question or comment"
			}
		});
	}
})

