function restoreBlockState(blockId) {
	if ($.cookie('blockState-'+blockId) > 0) {
		$("#"+blockId).show();
	}
	else {
		$("#"+blockId).hide();
	}
}

// toggles a block, and saves the actual state in cookie, if second parameter is true
// the saved state should be restored on load with restoreBlockState(blockId);
function toggleBlock(blockId, saveCookie) {
	if( $("#"+blockId+":visible").length ) {
		$("#"+blockId).hide(200);
		if (saveCookie) {
			$.cookie('blockState-'+blockId, 0, { path: "/" });
		}
	}
	else {
		$("#"+blockId).show(200);
		if (saveCookie) {
			$.cookie('blockState-'+blockId, 1, { path: "/" });
		}
	}
}

Adverticum = {
	getTootipManagerArray: function(callback) {
		$.ajax({
			url: "/tooltip/",
			data: {
				action: "get_tooltip_manager_array"
			},
			dataType: "json",
			type: "POST",
			cache: false,
			timeout: 10000,
			success: function(data, textStatus, XMLHttpRequest) {
				callback(data);
			}
		});
	},

	addTooltipsToPage: function(){
		Adverticum.getTootipManagerArray(function(tooltips){
		
			var tip;
			
			// tooltips
			for ( tip in tooltips ) {
				
				// escape "
				var safe_tooltip = tooltips[tip].replace(/"/g, '&quot;');
				
				// paragraphs
				$("div.left-content > p, div.left-content ul li").each(function(){
					
					// regex pattern, global, case-insensitive
					var pattern = new RegExp(tip, 'gi');
					
					// search for pattern
					var pos = -1;                // find position
					var string = $(this).html(); // the (sub)string where we search
					var substrbefore = "";       // the tooltiped part of the string
					
					// we need all occurences
					while ( ( pos = string.search(pattern) ) > -1 ) {
						// protection, to don't write into a tag/attribute
						// ex.: <code class="adserver"> matches, but the script leaves it untouched
						// it uses the count of "<" and ">" characters(must match)
						var lt_cnt = string.substring(0, pos).match(/</g);
						var gt_cnt = string.substring(0, pos).match(/>/g);
						
						if( ( lt_cnt == null && gt_cnt == null) 
							|| ( lt_cnt != null && gt_cnt != null && (lt_cnt.length == gt_cnt.length)) ) {
							// span open tag
							substrbefore += string.substring(0, pos) + '<span class="tooltip_subject" title="' + safe_tooltip +'">';
							// the original key string (keep case)
							substrbefore += string.substring(pos, (pos + tip.length));
							// span close tag
							substrbefore += '</span>';
						} else {
							// don't touch the original substring, just copy
							substrbefore += string.substring(0, (pos + tip.length));
							//console.log( "Match in a tag: " + lt_cnt + " - " + gt_cnt + " - " + string.substring(pos, (pos + tip.length)));
						}
						
						// next substring for searching
						string = string.substr(pos + tip.length);
						// position reset
						pos = -1;
					}
					// concat the string with the remaining part
					string = substrbefore + string;
					
					// put the tooltiped string into the paragraph
					$(this).html(string);
					
				}); // paragraphs
				
				// remove not needed spans
				$('code span.tooltip_subject') /*.filter(function(){return !$(this).parent('p').length;})*/
				.each(function(){
					$(this).removeClass('tooltip_subject');
					$(this).removeAttr('title');
					$(this).removeAttr('class');
				});
			} // tooltips
			
			// enable tooltips
			$('p > span.tooltip_subject, div.left-content ul li > span.tooltip_subject').tooltip();
		});
	}
}


$(document).ready(function() {
	$('a.new-window').live('click', function() {
		window.open($(this).attr('href'));
		return false;
	});

	$("#search_form-submit").click( function(event) {
		event.preventDefault();
		var val = $("#search_form-text").attr("value");
		if ( (typeof(val) != "undefined") && val ) {
			$("#search_form").submit();
		}
	} );
});

