﻿// FAQ SPOILERS

// !!!!!!! Save with UTF-8 !!!!!!!!
// Clickable LABELs in IE
// based on http://web.tampabay.rr.com/bmerkey/examples/clickable-labels.html
// -------------
// requires:
// jQuery.js (jQuery-latest.js)
// CSS styles:  .sp-head; .sp-body; .folded; .unfolded
// -- for tooltips:
// jquery.dimensions.js
// jquery.tooltip.js
// CSS: #tooltip; #tooltip h3; #tooltip div 

function ie6_make_clickable_labels ()
{
	var labels = document.getElementsByTagName("label");
	for (var i=0, len=labels.length; i<len; i++) {
		if (!labels[i].getAttribute("htmlFor")) {
			labels[i].onclick = function() { this.children[0].click(); }
		}
	}
}

function getFirst5words(text)
{
    var result = '';
    if(text != undefined)
    {
        var words = text.split(' ');
        for(i=0; i<Math.min(6, words.length); i++)
            result += words[i] + ' ';
    }
    return result;
}

function initSpoilers(context)
{
	$('.sp-body', context).each(function(){
		var $sp_body = $(this);
		var name = '<span>Ответ:</span> ' + getFirst5words($sp_body.text()) + '&nbsp;<img src="../App_Themes/OpenRazumSoft/bplus.gif" width="16" height="15" align="absmiddle"> ';
		this.title = '';
		// check existence of sp-head
		var $sp_head = $(this).siblings('.sp-head');
		//alert();
		if($sp_head.length==0)
    		$sp_head = $('<div class="sp-head folded">'+ name +'</div>').insertBefore($sp_body);
		
	    $($sp_head).click(function(e){
		    $(this).toggleClass('folded');
		    $(this).toggleClass('unfolded');
		    $sp_body.slideToggle('fast');
	    });
	});
}

$(function(){$('div.xml_body').each(function(){ initSpoilers( $(this) ) }); 
    
    $(".sp-head").tooltip({track: true, bodyHandler: function() {
		return $($(this).siblings('.sp-body')).html();
	}});
    });
