 jQuery(document).ready(function() {

	var btspan = jQuery("#content").find(".bt-span");

	// DOCKABLE ELEMENTS
	function checkDockable(isSpan) {
		var dockableChain = jQuery("#content .gdu.u-last").find(".dockChain");
		var dockable = jQuery("#content .gdu.u-last").find(".dockChain.dockable:first-child");
		var dockCondition = jQuery("#content").find(".gd1 > .md-title");
		var rebuildCont = jQuery("#content").find(".gd2 .gdu.u-first");

		if (dockableChain.is(".dockable:first-child")) {
			if (dockCondition.length > 0) {
				rebuildCont.prepend(dockCondition);
			} else {
				// console.log("no se puede hacer rebuild, probablemente haya span");
			}
			if (isSpan != true) {
				dockable.addClass("docked");
				// console.log("soy dockable, no hay span y estoy en posición de docking. Estoy DOCKADO");
			} else {
				dockable.removeClass("docked");
				// console.log("soy dockable pero no me docko porque hay span");
			}
		} else {
			// console.log("soy dockable pero no estoy en posición de docking");
		}
	}

	// Do the Dockable check
		checkDockable(false);


	// invade column faux effect
	function span () {
		var source = jQuery("#content").find(".spanner-source .spanner-content");
		var dummy  = jQuery("#content").find(".spanner-dummy");
		var target = dummy[0];
		var obj    = source.find(".spannable-slider-block"); /* <- USE AN ARRAY IF NEEDED */
		source.each(function(){   // each.function to force jQuery make new Node OTF for appendChild
			dummy.html("");
			target.appendChild(this); // appendChild to prevent js reexecution with embeds
			attRelease(obj, "spanned");
		});
	}

	// revert invasion
	function unspan () {
		var source = jQuery("#content").find(".spanner-dummy .spanner-content");
		var target = jQuery("#content").find(".spanner-source")[0];
		var obj    = source.find(".spannable-slider-block"); /* <- USE AN ARRAY IF NEEDED */
		source.each(function(){   // each.function to force jQuery make new Node OTF for appendChild
			target.appendChild(this); // appendChild to prevent js reexecution with embeds
			attRelease(obj, "unspanned");
		});
	}

	// set the attributes of inner objects when status releases
	// sample: dimensions of an embed object
	//         or invocate new method ( BBTFSwfObj(bla, bla...) )
	function attRelease (obj, status) {
		if (status == "spanned") {
			// Insert JSON html
			var contentId = BBT.getProperty(jQuery(".pg-story"), "contentId");
			var versionId = BBT.getProperty(jQuery(".pg-story"), "versionId");
			jQuery.get("/nwsmedia.json?contentId=" + contentId + "&versionId=" + versionId +"&mode=spanned", function (_response) {
                              var response = BBT.fixJson(_response);
                              if(response["error"] == "0") {
				  obj.html("");
				  obj.html(response['data']);
	                      } else {
				      alert("error");
			      }
                        });
		} else if (status == "unspanned") {
			// Insert JSON html
			var contentId = BBT.getProperty(jQuery(".pg-story"), "contentId");
			var versionId = BBT.getProperty(jQuery(".pg-story"), "versionId");
			jQuery.get("/nwsmedia.json?contentId=" + contentId + "&versionId=" + versionId +"&mode=unspanned", function (_response) {
                              var response = BBT.fixJson(_response);
                              if(response["error"] == "0") {
				  obj.html("");
				  obj.html(response['data']);
	                      } else {
				      alert("error");
			      }
                        });
		}
	}

	jQuery(btspan).live('click', function() {
			if(jQuery(this).hasClass("unspanned")) {
				span();
				checkDockable(true);
				return false;			       
			}
			if(jQuery(this).hasClass("spanned")) {
				unspan();
				checkDockable(false);
				return false;
			}
	});

 });
