/* 
	Carta - Javascript initialization
	Handwritten by Stefano Giliberti <kompulsive@gmail.com>
	<winterbits.com>
*/

jQuery.noConflict()

jQuery(document).ready(function(){
	
	/* Shortcode Accordion */
	jQuery(".sc-accordion").wb_accordion({
		fullwidth: 550
	})
	
	/* Projects scripts call */
	progetti()
		
	/* CSS fixes for old browsers */
	jQuery("ul#menu > li:last-child").css({
		"border": "none",
		"padding-right": 0
	})
	
	jQuery("input[type='submit']").addClass("button");
	
	/* 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('*')
	
})

//

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({
		'titleShow': false,
		'transitionOut': 'elastic',
		'width': '90%',
		'height': '95%',
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.7,
		'centerOnScroll': true
	})
	jQuery("a.youtube").click(function() {
		jQuery.fancybox({
				'padding' : 0,
				'titleShow': false,
				'autoScale' : false,
				'transitionIn' : 'none',
				'transitionOut'	: 'fade',
				'overlayShow': true,
				'overlayColor': '#000',
				'overlayOpacity': 0.7,
				'centerOnScroll': true,
				'width' : 700,
				'height' : 426,
				'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type' : 'swf',
				'swf' : {
				   	'wmode' : 'transparent',
					'allowfullscreen' : 'true'
				}
			})
		return false;
	})
	jQuery("a.vimeo").click(function() {
		jQuery.fancybox({
				'padding' : 0,
				'titleShow': false,
				'autoScale' : false,
				'transitionIn' : 'none',
				'transitionOut'	: 'fade',
				'overlayShow': true,
				'overlayColor': '#000',
				'overlayOpacity': 0.7,
				'centerOnScroll': true,
				'width' : 700,
				'height' : 394,
				'href' : this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
				'type' : 'swf'
			})
		return false;
	})
	jQuery("a.media").fancybox({
		'titleShow': false,
		'transitionOut': 'fade',
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.7,
		'centerOnScroll': true
	})
})

/*
	Gallery & Projects scripts
*/
function galleria() {
	
	jQuery(".gallery .entry").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)
			
	})
	
	//
	
	var see = window.location.hash.substr(1);
	if (see && jQuery(".gallery ." + see).length) {
		filter(see)
	}
	
	var all = "all";
	var li = jQuery(".filter li:has(b)");
	
	li.click(function(){

		var c = jQuery(this).attr("class");
		if (!jQuery(this).hasClass("a")) {
			filter(c)
			jQuery(this).addClass("a").siblings().removeClass("a")
			if (c != all) {
				jQuery(this).filter(".a").css("opacity", 1).siblings(":has(b)").css("opacity", .5)
			} else {
				li.css("opacity", 1)
			}
		}
		
		return false;
	})
	.hover(
	function(){
		jQuery(this).css("opacity", 1).siblings(":has(b)").css("opacity", .5)
	},
	function(){
		if (!li.filter(".a").length) {
			li.css("opacity", 1)
		} else {
			if (!li.filter(".a").hasClass(all)) {
				li.filter(".a").css("opacity", 1).siblings(":has(b)").css("opacity", .5)
			} else {
				li.css("opacity", 1);
			}
		}
	})
	
	function filter(x) {
		jQuery(".gallery")
			.css("height", jQuery(".gallery").height())
			.children(".entry")
			.stop(true, true)
			.hide()
			.removeClass("odd")
			.removeClass("i")
			.each(function(i){
				if (jQuery(this).hasClass(x)) {
					
					jQuery(this).addClass("i")
					reodd()
					jQuery(this).delay(i*50).fadeIn(550)
					
				} else if (x == all) {
					
					jQuery(this).addClass("i")
					reodd()
					jQuery(this).delay(i*50).fadeIn(200)
					
				}
			})
	}
	
	function reodd() {
		jQuery(".gallery .i").each(function(i){
			var z = (i + 1) % 3;
			if (z === 0) {
				jQuery(this).addClass("odd");
			}
		})
	}
		
}

//

function progetti() {
	jQuery(".projects .entry .pic").hoverIntent(function(){
	
		jQuery(this)
			.find(".details")
			.animate({ 
				height: "show"
			}, { 
				queue: false, 
				easing: (jQuery.easing.def) ? "easeOutBounce" : "swing"
			})
			
	}, function(){
			
		jQuery(this)
			.find(".details")
			.stop(true, true)
			.animate({
				height: "hide"
			}, { 
				easing: (jQuery.easing.def) ? "easeOutExpo" : "swing"
			})
			
	})
}

/**
*	Contact Form Function
*	Author: Stefano Giliberti (kompulsive@gmail.com)
*	(winterbits.com)
*/
function wb_contactform(form, success, error, notvalid, required, end) {
	
	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']"),
		action = form.attr("action"),
		sendto = form.find("input[name=sendto]")
		
	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(end)
			return true
		} else {
			var missing = dirt.attr("name")
			var error = required.replace("%", missing);
			if (!missing) var error = notvalid
			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() +
					 "&sendto=" + sendto.val();
				
		if (check())
		jQuery.ajax({
			url: action,
			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(end)
				}
				
			}	
		})
		return false
	})
	//
}
