/* 
	Carta - Javascript initialization
*/

jQuery.noConflict()

jQuery(document).ready(function(){
	
	/* 
		Cufon text replacement
		http://wiki.github.com/sorccu/cufon/
	*/
	Cufon.replace('#menu strong, #toolbar strong, .pag strong, h1:not(.classic), h2:not(.classic), h3:not(.classic), h4:not(.classic), h5:not(.classic), h6:not(.classic), a#sign-promo');
	Cufon.replace('.pagination .emph', { textShadow: '0 1px rgba(0, 0, 0, 0.3)' });
	
	/* Accordion call */
	jQuery("#featured").wb_accordion()
	
	/* Gallery scripts call */
	galleria()
	
	/* Contact form call */
	wb_contactform
	(
		"form#contact-form",
		"Thank you, We have received your message.",
		"Please, fill out all required fields correctly."
	)
	
	/* CSS fixes for old browsers */
	jQuery("ul#menu li:last-child").css({
		"background": "none",
		"padding-right": 0
	})
	
	/* Search bar style toggle */
	jQuery("#toolbar #search-form .input-text").focus(function(){
		jQuery(this).addClass("focus")
	}).blur(function(){
		if (jQuery(this).val().length < 1) jQuery(this).removeClass("focus")
	})
	
	/* Input's labels system */
	jQuery(".focus-label").each(function(){
		var field = jQuery(this),
			label = field.attr("title"),
			original = {
				"color": field.css("color"),
				"font-style": field.css("font-style")
			},
			labelstyle = {
				"color": "#868686",
				"font-style": "italic"
			}
			
		field
			.val(label)
			.css(labelstyle)
			.addClass("labeled")
			.focus(function(){
				if (field.hasClass("labeled"))
					field
						.val("")
						.removeClass("labeled")
						.css(original)
			})
			.blur(function(){
				if (!field.val().length)
					field
						.val(label)
						.addClass("labeled")
						.css(labelstyle)
			})
	})
	
	/* Detecting Internet explorer 6 */
	var ie6 = (navigator.userAgent.indexOf("MSIE 6") >= 0) ? true : false
	
	/* Messages closing */
	if (!ie6) {
		jQuery(".message:not(.sticky)").prepend('<a class="close" href="#">Close</a>');
		jQuery(".message .close").click(function(){
			jQuery(this).parent(".message").delay(100).slideUp(250)
			return false
		})
	}
	
	/* Internet explorer 6 transparency */
	if (ie6) DD_belatedPNG.fix('img, h1#logo, .drop-shadow, #footer #note, a#sign-promo, #toolbar strong, a#subscribe, span.meta')
	
})

//

jQuery(window).load(function() {
	
	/* 
		Superfish dropdown menu
		http://users.tpg.com.au/j_birch/plugins/superfish/
	*/ 
	jQuery("ul#menu, #toolbar ul").superfish({
		delay: 500,
		autoArrows: false,
		speed: 150,
		animation: { opacity: 'show', height: 'show' }
	})
	
	/*
		Fancybox lightbox
		http://fancybox.net/
	*/
	jQuery("a.zoom").fancybox({
		'titleShow': false,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'easingIn': (jQuery.easing.def) ? "easeOutBack" : "swing",
		'easingOut': (jQuery.easing.def) ? "easeOutBack" : "swing",
		'overlayShow': false
	})
	jQuery("a.fullzoom").fancybox({
		'transitionOut': 'elastic',
		'width': '90%',
		'height': '95%',
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.5,
		'centerOnScroll': true
	});
	
})

/*
	Gallery scripts
*/
function galleria() {

	/* Apply "nopic" class to items without images */
	jQuery(".gallery .entry:not(:has(img))").addClass("nopic");
	
	jQuery(".gallery .entry:not(.nopic)").hover(function(){
	
		jQuery(this)
			.find("img")
			.animate({ 
				marginTop: "-" + jQuery(this).find("span").height()
			}, { 
				queue: false, 
				easing: (jQuery.easing.def) ? "easeOutExpo" : "swing"
			})
		jQuery(this)
			.find(".title")
			.stop(true, true)
			.animate({ 
				bottom: "-35px"
			}, { 
				easing: (jQuery.easing.def) ? "easeOutExpo" : "swing"
			})
			
	}, function(){
			
		jQuery(this)
			.find("img")
			.animate({
				marginTop: "0"
			}, { 
				queue: false, 
				easing: (jQuery.easing.def) ? "easeOutBounce" : "swing"
		})
		jQuery(this)
			.find(".title")
			.animate({
				bottom: "10px"
			}, 150)
			
	})
	
}

/*
	Contact form function
*/
function wb_contactform(form, success, error) {
	
	var form = jQuery(form),
		ready = false,
		name = form.find("input[name=name]"),
		email = form.find("input[name=email]"),
		website = form.find("input[name=website]"),
		message = form.find("textarea[name=message]"),
		copy = form.find("input[name=copy]"),
		submit = form.find("input[type='submit']");
	
	function realmail(address) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(address);
	}
	function check() {
		var dirt = form.find(".required.labeled, .required:text[value='']")
		if (dirt.length < 1 && realmail(email.val())) {
			submit.removeAttr("disabled").val("Submit form")
			return true
		} else {
			var missing = dirt.attr("name")
			var error = missing + ' field is required.';
			if (!missing) var error = "Please, use a valid email address."
			var error = error.charAt(0).toUpperCase() + error.slice(1);
			submit.attr("disabled", "disabled").val(error);
			return false
		}
	}
	check()
	
	form.find("input, textarea").bind("keyup focus blur", check)
	
	if (copy) {
		copy.attr("checked", "checked").parent().hide()
		email.keyup(function(){
			if (realmail(jQuery(this).val())) {
				copy.attr("checked", "checked").parent().slideDown()
			} else {
				copy.removeAttr("checked")
			}
		})
	}
		
	//
	form.submit(function(){
		check()
		
		submit.attr("disabled", "disabled")
		var values = "name=" + name.val() +
					 "&email=" + email.val() + 
					 "&website=" + encodeURIComponent(website.val()) + 
					 "&message="  + encodeURIComponent(message.val()) +
					 "&copy=" + copy.val();
				
		if (check())
		jQuery.ajax({
			url: "php/mail.php",
			type: "GET",
			data: values,
			cache: false,
			success: function(result) {
								
				if (result == 1) {															
					submit.hide();
					form.find("input, textarea").attr("disabled", "disabled")
					form.find(".message").remove()
					form.append('<p class="message success">' + success + '</p>');
					form.find(".message").hide().slideDown()
				} else {
					form.find(".message").remove()
					form.append('<p class="message notice">' + error + '</p>');
					form.find(".message").hide().slideDown()
					submit.removeAttr("disabled").val("Submit form")
				}
				
			}	
		})
		return false
	})
	//
}
