/* Author: 

*/

(function ($) {

	$.fn.checkSVG = function () {

		var n = this.length;
		if (!n) { return this; };
		//if (!window.Raphael) { return this; }

		var options = {
			borderWidth: 1,
			borderRadius: 2,
			borderColor1: '#333',
			borderColor2: '#fff',
			height: 12,
			backgroundColor: '#fff',
			backgroundOpacity: 0.5,
			backgroundHighlightOpacity: 1,
			highlightDuration: 200,
			markType: "check",
			markColor: '#000',
			markStrokeWidth: 2
		};
		var customOptions = arguments[0] || {};
		$.extend(options, customOptions);
		
		var sw = options.borderWidth; // stroke-width
		var s = options.height; // size
		var is = s - sw; // inner size
		var br = options.borderRadius;
		var bgc = options.backgroundColor;
		var bgo = options.backgroundOpacity;
		var bgh = options.backgroundHighlightOpacity;
		var hd = options.highlightDuration;
		var bc1 = options.borderColor1;
		var bc2 = options.borderColor2;

		var l = s - 2*br; // length of straight segment
		var a = Math.PI * br / 4; // arc length described by border radius, in half
		var isb = l +a; // inset begins at pixel...
		var ise = 3*l + 5*a; // inset ends at pixel length...

		var markPath;
		if (options.markType == "cross") {
			var csp = 3*sw/2 + is*0.15; 
			var cs = is*0.7;
			markPath = "M "+csp+","+csp+" l "+cs+","+cs+" m 0,-"+cs+" l -"+cs+","+cs;
		} else if (options.markType == "check") {
			markPath = "M "+(3*sw/2 + is*0.15)+","+(3*sw/2 + is*0.5)+ " l "+(is*0.2)+","+(is*0.3)+" l "+(is*0.50)+",-"+(is*0.70);
		}
		var mc = options.markColor;
		var msw = options.markStrokeWidth;

		function findCheckSVGObject(obj) {
			var className = " checkSVG ";
			var pn = obj;
			while (pn.parentNode) {
				pn = pn.parentNode;
				if (pn.tagName == "SPAN") { 
					if ( (" " +pn.className + " ").replace(/[\n\t]/g, " ").indexOf( className ) > -1 ) {
						return pn;
					}
				}
			}
			return false;
		}


		while (n--) {
			
			var $chkbox, id, $svgchkbox, paper, r, p1, p2, m, st, fon, foff;

			$chkbox = this.eq(n);
			if (typeof $chkbox.attr("data-csvg") != "undefined") { continue; }
			id = "csvg" + Math.floor(new Date().getTime()*Math.random());
			$svgchkbox = $('<span id="'+id+'" class="checkbox checkSVG" style="display: inline-block; width: '+s+'px; height: '+s+'px; position: relative"></span>');
			$chkbox
				.after($svgchkbox)
				.wrap('<div style="height:0; width: 0; overflow: hidden;" />')
				.parent()
					.css("opacity",0)
				.end()
				.attr("data-csvg", id)
			;

			paper = Raphael($svgchkbox[0], s+br, s+br);

			// background
			r = paper.rect(sw,sw,s,s,br).attr({stroke:"none", fill: bgc, "fill-opacity": bgo});

			// border base
			p1 = paper
				.path("M "+sw+","+(sw+br)+" l 0,"+l+" a "+br+","+br+",0,0,0,"+br+","+br+" l "+l+",0 a "+br+","+br+",0,0,0,"+br+",-"+br+" l 0,-"+l+" a "+br+","+br+",0,0,0,-"+br+",-"+br+" l -"+l+",0 a "+br+","+br+",0,0,0,-"+br+","+br+" z")
				.attr({stroke: bc1, "stroke-width": sw})

			// border inset
			p2 = p1.getSubpath(isb, ise);
			p2 = paper
				.path(p2)
				.attr({stroke: bc2, "stroke-width": sw});
		
			// mark
			m = paper
				.path(markPath)
				.attr({stroke: mc, "stroke-width": msw, "stroke-opacity": ($chkbox[0].checked ? 1 : 0)})
			;
			

			// common attrs
			st = paper.set();
			st.push(r, p1, p2, m).attr({cursor: "pointer"});

			// link resoruces to object
			$svgchkbox[0].linkedObjects = {paper: paper, background: r, mark: m, checkbox: $chkbox};

			// event handling
			fon = function(chk) { if (!chk) return; chk.linkedObjects.background.animate({"fill-opacity": bgh}, hd); }
			check = function(chk, on) { if (!chk) return; chk.linkedObjects.mark.attr({"stroke-opacity": (on ? 1 : 0)}); }
			foff = function(chk) { if (!chk) return; if (chk.linkedObjects.checkbox[0].hasFocus) return; chk.linkedObjects.background.animate({"fill-opacity": bgo}, hd); }
			$chkbox.focus(function() { this.hasFocus = true; fon($("#"+$(this).attr("data-csvg"))[0]); });
			$chkbox.change(function() { check($("#"+$(this).attr("data-csvg"))[0], this.checked); });
			$chkbox.blur(function() { this.hasFocus = false; foff($("#"+$(this).attr("data-csvg"))[0]); });
			st.hover(
				function () { fon(findCheckSVGObject(this.node)); }, 
				function () { foff(findCheckSVGObject(this.node)); } 
			);
			st.click(function () { 
				var $cb = findCheckSVGObject(this.node).linkedObjects.checkbox;
				$cb[0].checked = !$cb[0].checked;
				$cb.change(); 
				$cb[0].focus();
				$cb.focus();
			});
			// finishing touches
			$svgchkbox
				.find("svg").css({position: "absolute", top: "0", left: "0"})
					.find("desc").text("")
			;

		}

		return this;
	}

})(jQuery);

$(function() {

	var $contact_form = $("#contact_form");
	if ($contact_form.length) {
		$contact_form.submit(function() { return false; });
		$("#contact_send").click(function() {
			/* begin: temporal */
			alert("Lo siento, ahora mismo no se pueden mandar mensajes por la web.\n\nPara lo que quieras, tienes mi correo de gmail abajo.\n\nGracias.");
			return false;
			/* end: temporal */
			var $cf = $contact_form
				.clone(false)
				.css("display", "none")
				.append('<input type="hidden" name="csid" value="'+document.csid+'" />')
			;
			$cf.find("textarea").val($contact_form.find("textarea").val());
			$contact_form.after($cf);
			$cf.submit().remove();
		});
	}


	var $checkbox = $("input[type=checkbox]");

	if (window.Raphael && $checkbox.length) { $checkbox.checkSVG(); }

	if (Shadowbox) { Shadowbox.init(); }

});
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

