$(document).ready(function(){
	var fxs = [
		{fx:'scrollUp', easing:'easeInOutBack'}, 
		{fx:'shuffle', easing:'jswing'}, 
		{fx:'fade', easing:'jswing'}, 
		{fx:'scrollDown', easing:'easeInOutBack'}, 
		{fx:'scrollLeft', easing:'easeInOutBack'}
	];

	$('.slideshow').each(function(i){
		var slides = $(".slides", this);
		var controls = $(".controls", this);
		var glossy = $(".glossy", this);
		var playing = false;
		
		controls.css("opacity", 0);
		
		$('.playpause', controls).click(function() {
			slides.cycle(playing?'pause':'resume');
			$(this).removeClass(playing?'pause':'play').addClass(playing?'play':'pause');
			playing = !playing;
			return false; 
		}).addClass("play");
		
		var t;
	    $(this).hover(
			function() {
				t = setTimeout(function(){
					controls.stop().animate({opacity: 0.8}, 600);
					if (!$.browser.msie)
						glossy.stop().animate({opacity: 0}, 1500);	
					else
						glossy.hide();
				}, 400);
			},
			function() { 
				if (t) clearTimeout(t);
				t = null;
				controls.stop().animate({opacity: 0}, 600);	
				if (!$.browser.msie)
					glossy.stop().animate({opacity: 1}, 1500);
				else
					glossy.show();
			}
	    );
	
	 	var f = fxs[i%fxs.length];
	
		// bitch way of doing a "concat"..
		var n = $('.next', controls);
		n.length++;
		n[1] = this;
		
		slides.cycle({
			fx: f.fx,
			direction: 'down',
			easing: f.easing,
			speed:   800, 
			timeout: 3000, 
			next:   n, 
			prev:   $('.prev', controls)
		}).cycle('pause');
	});

	$("#menu li.current").css('background-color', 'white').animate({
		backgroundColor: 'rgb(255, 0, 139)'
	}, 1000, 'linear');
	$("#menu li.current a").animate({
		color: 'white'
	}, 1000, 'linear');
	
	$(".flickrimage a").fancybox();
	
	$("h3.subject, #works .attributes h2, #largecontent h2, #smallcontent h2, #projects h2").prepend("<span class='glossy'></span>");
	$(".flickrimage").prepend("<span/>");
	$(".shorty li:first-child").addClass("firstchild");
	$(document).pngFix();
	
	var professionale = [
		{ name: "larghezza", value: 0 },
		{ name: "stretch", value: 1 },
		{ name: "opera", value: 0 },
		{ name: "safari", value: 0 },
		{ name: "rollover", value: 1 },
		{ name: "menu", value: 0 },
		{ name: "font", value: 1 },
		{ name: "sifr", value: 1 },
		{ name: "css_optimized", value: 0 },
		{ name: "css_comment", value: 1 },
		{ name: "tipo_codice", value: 0 },
		{ name: "seo", value: 1 },
		{ name: "caricamento", value: 1 },
		{ name: "xhtml_comment", value: 1 }
	];
	var deluxe = [
		{ name: "larghezza", value: 0 },
		{ name: "stretch", value: 1 },
		{ name: "opera", value: 0 },
		{ name: "safari", value: 0 },
		{ name: "rollover", value: 1 },
		{ name: "menu", value: 0 },
		{ name: "font", value: 1 },
		{ name: "sifr", value: 1 },
		{ name: "css_optimized", value: 0 },
		{ name: "css_comment", value: 1 },
		{ name: "tipo_codice", value: 1 },
		{ name: "seo", value: 0 },
		{ name: "caricamento", value: 0 },
		{ name: "xhtml_comment", value: 1 }
	];

	var calculatePrice = function() {
		var sum = 0;
		$("#psd2html .rowoption input[@type='radio']:checked").each(function() { sum += parseInt($(this).val()); });
		$(".totalprice .price").html("&euro;"+sum+"!");
	};

	var setRadios = function(s) {
		$.each(s, function() {
			$("input[@name='"+this.name+"']:nth("+this.value+")").attr("checked", "checked");
		});
		calculatePrice();
	};
		
	$("input[@name='pacchetto']:nth(0)").change(function() {setRadios(professionale);});
	$("input[@name='pacchetto']:nth(1)").change(function() {setRadios(deluxe);});

	setRadios(professionale);

	$("#psd2html legend").click(function(){
		if ($(this).siblings(".options").css("height") != "auto") return;
		$("#psd2html .options").slideUp();
		$(this).siblings(".options").slideDown();
	});
	$("#psd2html .options").hide();
	
	$("#psd2html .rowoption input[@type='radio']").change(function(){calculatePrice();});
	
	$("a.targetblank").click(function(e) {
		window.open($(this).attr("href"));
		e.preventDefault();
		return false;
	})
});