//
// B14 Site JS Tools V1.2
//
Drupal.behaviors.insertFlashMenus = function(context){
	var menuTags = $('.flashmenu');
	menuTags.each(function() {
		if ($(this).hasClass("flash_is_set"))
			return;
		
		$(this).addClass("flash_is_set");
		var objectId = $(this).attr("id");
		
		// Create menu xml
		var menuData = createFlashXml(this);
		var htmlContent = menuData[0];
		
		// Add identifier as id so swfobject insert the swf file.
		var targetObj = $(this).children(".block-inner").children(".content");
		if (targetObj.attr("type") == "application/x-shockwave-flash")
			return;
		
		var tgtId = targetObj.attr("id");
		if(!tgtId || tgtId == "") {
			$(this).css("margin-bottom", "0px");
			tgtId = objectId + "-flash-content";
			targetObj.attr("id", tgtId);
		}
		
		if (menuData[2] == 1) {
			//alert("RUN: " + $("#" + tgtId));
			$("<div class='menu-description'>"+ menuData[1].replace("\n", "<br />") +"</p>").insertAfter($("#" + tgtId));
		}
		
		if(swfobject.hasFlashPlayerVersion("9.0.0")) {
			// Prepare swfobject flashvars, params and attributes
			var fileUrl = Drupal.settings.basePath+"sites/default/files/swf/B14DrupalMenu.swf";
			var _width = ($(this).width()) -7;
			var _height = $(this).height();
			var version = "9.0.0";
			var flashvars = {"menuXml":escape(htmlContent), "divId": tgtId+"_obj"};
			
			var params = {
				menu:"false",
				quality:"high",
				scale:"noscale",
				salign:"tl",
				wmode:"transparent",
				bgColor:"#FFFFFF",
				version:version,
				allowfullscreen:"false",
				allowscriptaccess:"always",
				width:_width,
				height:_height,
				src:fileUrl
				};
			var attributes = {id:tgtId+"_obj", name:tgtId+"_obj"};
			
			// Insert swfobject
			// Api:
			// swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn)
			swfobject.embedSWF(fileUrl, tgtId, _width, _height, version, 'false', flashvars, params, attributes, embedCallbackFn);
		}
	});
};


Drupal.behaviors.insertFlashHeadlines = function(context){
	if(swfobject.hasFlashPlayerVersion("9.0.0")) {
		var menuTags = $('.big-text');
		var objCount = "obj_"+Math.round(Math.random() * 10000000);
		menuTags.each(function() {
			var objectId = $(this).attr("id");
			
			// Get headline text
			var htmlContent = $(this).html();
			htmlContent = str_replace_reg(htmlContent, "<br>", "");
			
			// Add identifier as id so swfobject insert the swf file.
			var tgtId = $(this).attr("id");
			if(!objectId || objectId == "") {
				tgtId = objCount;
				$(this).attr("id", tgtId);
				objCount += Math.round(Math.random() * 10000);
			}
			
			// Prepare swfobject flashvars, params and attributes
			//var fileUrl = Drupal.settings.basePath+"sites/default/files/swf/B14DrupalHeadline.swf?time" + Math.round(Math.random() * 10000).toString();
			var fileUrl = Drupal.settings.basePath+"sites/default/files/swf/B14DrupalData.swf?time" + Math.round(Math.random() * 10000).toString();
			var _width = $(this).width();
			var _height = ($(this).height() + 30);
			var version = "9.0.0";
			var flashvars = {"data":escape(htmlContent), "divId":tgtId, "useQuote": "0"};
			var params = {
				menu:"false",
				quality:"high",
				scale:"noscale",
				salign:"tl",
				wmode:"transparent",
				bgColor:"#FFFFFF",
				version:version,
				allowfullscreen:"false",
				allowscriptaccess:"always",
				width:_width,
				height:_height,
				src:fileUrl
				};
			var attributes = {};
			
			// Insert swfobject
			// Api:
			// swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn)
			swfobject.embedSWF(fileUrl, tgtId, _width, _height, version, 'false', flashvars, params, attributes, embedHeadCallbackFn);
			
		});
	}
};

// Create menu xml for B14Menu
function createFlashXml(tgtHtml) {
	var liItems = $(tgtHtml).find(".leaf, .collapsed, .expanded");
	
	var htmlContent = '<menu>';
	var count = 0;
	var currentDescription;
	liItems.each(function() {
		if ($(this).parent().css("display") == "none") {
			return;
		}
		var _url = $(this).children("a").attr("href");
		var _title = $(this).children("a").html(); //$(this).children("a").attr("title");
		currentDescription = $(this).children("a").attr("title");
		var _selected = "";
		if (location.pathname == _url || $(this).hasClass("expanded"))
			_selected = " selected='1'";
		htmlContent += '<item url="'+_url+'" name="'+_title+'" pid="'+(_title+count)+'"' + _selected + '></item>';
		count++;
	});

	htmlContent += '</menu>';
	return Array(htmlContent, currentDescription, count);
}

// After embed align the menu with the html elements
function embedCallbackFn(evt) {
	var objId = evt.id;
	$("#"+objId).css("margin-left", "-3px");
}

// After embed align the menu with the html elements
function embedHeadCallbackFn(evt) {
	var objId = evt.id;
	$("#"+objId).css("margin-left", "-3px");
	$("#"+objId).css("margin-top", "-11px");
}

function str_replace_reg(haystack, needle, replacement) {
	var r = new RegExp(needle, 'g');
	return haystack.replace(r, replacement);
}

$(window).resize(function() {
	$('.swftools-swf object').each(function() {
		this.siteResize(null);
	});
});

function setSWFSize(divId, height) {
	var d = $("#" + divId);	
	d.height(height);
};

