/**
 * Takera Style for Drupal 5, Gallery 2 and vBulletin
 * Richard Hähne, takera [dot] de
 */

/*******************************************************************************
GcurSec has to be defined in page-xxx.tpl.php (either "bibo", "galerie", etc.)
******************************************************************************/

// #############################################################################
// Load functions

/* Debug, so that the background for the left sidebar is shown right, also when the sidebar is taller than the content */
function match_contentHeight()
{
	var lcb_height;

	if( $('#collapseobj_sidebar-left').height() > 720 && $('#collapseobj_sidebar-left').css("display") != "none" ){
		lcb_height = $('#collapseobj_sidebar-left').height() + "px";
	} else {
		lcb_height = "720px";
	}

	$('#left-corner-bottom').css("min-height", lcb_height );
	//alert( $('#left-corner-bottom').height() + " und original "+ $('#collapseobj_sidebar-left').height());
}

function match_pagewith()
{
	var header_width;
	var sidebar_width;
	var content_width;
	var teleporter_width;
	var goal_width;
	
	if( $('#collapseobj_sidebarleft').css("display") != 'none' ){
		sidebar_width = $('#collapseobj_sidebarleft').width();
	} else {
		sidebar_width = 0;
	}
	header_width = $('#header').width();
	content_width = $('#contentWidth').width() + 50;
	teleporter_width = $('#teleporter_content').width() + 100;


	if( (header_width - sidebar_width) < content_width || header_width > $('body').width() ){

		goal_width = content_width + "px";
		$('#squeeze').css("width", goal_width);
		goal_width = sidebar_width + content_width + 10 + "px";
		$('#header').css("width", goal_width);
		$('#footer').css("width", goal_width);
		goal_width = sidebar_width + content_width - teleporter_width + "px";
		$('#teleporter_content').css("left", goal_width);
	}
}

/* width for breadcrumb and page-title and footer_message */
function setblockswidth()
{
	var win_width;
	var max_width_pt;
	var max_width_bc;
	var max_width_footer_msg;
	var left_footer_msg;
	
	win_width = $('#header').width();

	if( $('#page-title').css("display") == "block" ){
		max_width_pt = win_width - 2 * $('#page-title').css("left").slice(0,3)  + "px"; //.left().slice doesn't work with IE6
		$('#page-title').css("width", max_width_pt );
	}
	if( $('.breadcrumb').css("display") == "block" ){
		max_width_bc = win_width - 2 * $('.breadcrumb').css("left").slice(0,3)  + "px"; //.left().slice doesn't work with IE6
		$('.breadcrumb').css("width", max_width_bc );
	}
	if( $('#footer_message').css("display") == "block" ){
		max_width_footer_msg = win_width - 2 * $('#footer_message').css("left").slice(0,3)  + "px";
		$('#footer_message').css("width", max_width_footer_msg );
	}
	if( $('#footer_poweredby').css("display") == "block" ){
		max_width_footer_msg = win_width - 2 * $('#footer_poweredby').css("left").slice(0,3)  + "px";
		$('#footer_poweredby').css("width", max_width_footer_msg );
	}
	if( $('#footer_centered').css("display") == "block" ){
		max_width_footer_msg = win_width - 2 * $('#footer_centered').css("left").slice(0,3)  + "px";
		$('#footer_centered').css("width", max_width_footer_msg );
	}
}
$(document).ready( match_contentHeight );
$(document).ready( match_pagewith );
$(document).ready( setblockswidth );
//$(document).resize( setheaderwidth );


// #############################################################################
// Collapsible element handlers & Animation

var prevent_dblclick = false; //needed to prevent user from dblclicking or collapse another block during animation
var correct_collapseimage_out = "none"; //needed to control collapseimage_out during animation, when obj already visible

/**
* Sets the hover image of the collapseimage depending if the collapseobj is hidden or visible
*
* @param	string	Unique ID for the collapse group
*
* @return	boolean	false
*/
function collapseimage_hover(objid2)
{
	obj2 = fetch_object('collapseobj_'+objid2);
	img2 = fetch_object('collapseimg_'+objid2);

	if (obj2.style.display == 'none')
	{
		img_re = new RegExp("_collapsed\\.png$");
		img2.src = img2.src.replace(img_re, '_hover_collapsed.png');
	}
	else
	{
		img_re = new RegExp("\\.png$");
		img2.src = img2.src.replace(img_re, '_hover.png');
	}
	return false;
}
function collapseimage_out(objid3)
{
	obj3 = fetch_object('collapseobj_'+objid3);
	img3 = fetch_object('collapseimg_'+objid3);

	if (obj3.style.display == 'none' || correct_collapseimage_out == obj3 )
	{
		img_re = new RegExp("_hover_collapsed\\.png$");
		img3.src = img3.src.replace(img_re, '_collapsed.png');
	}
	else
	{
		img_re = new RegExp("_hover\\.png$");
		img3.src = img3.src.replace(img_re, '.png');
	}
	return false;
}

/**
* Toggles the collapse state of an object, and saves state to 'xxx_collapse' cookie
*
* @param	string	Unique ID for the collapse group
*
* @return	boolean	false
*/
function toggle_collapse_block(objid)
{
	if (!is_regexp)
	{
		return false;
	}

	if ( prevent_dblclick == false ) {

		obj = fetch_object('collapseobj_' + objid);
		img = fetch_object('collapseimg_' + objid);
		cel = fetch_object('collapsecel_' + objid);

		if (!obj)
		{
			// nothing to collapse!
			if (img)
			{
				// hide the clicky image if there is one
				img.style.display = 'none';
			}
			return false;
		}
	

		if (obj.style.display == 'none')
		{
			//obj.style.height = 'auto';

			var showblock = function(e) {
				if (img)
				{
					img_re = new RegExp("_collapsed\\.png$");
					img.src = img.src.replace(img_re, '.png');
				}
				if (cel)
				{
					cel_re = new RegExp("^(thead|tcat)(_collapsed)$");
					cel.className = cel.className.replace(cel_re, '$1');
				}
				obj.style.display = '';
				save_collapsed(objid, false);
				prevent_dblclick = false;
				correct_collapseimage_out = "none";
				match_contentHeight();
			}
			prevent_dblclick = true;
			correct_collapseimage_out = obj;
			$(obj).slideDown("normal",showblock);
		}
		else
		{
			var hideblock = function(e) {
				if (img)
				{
					img_re = new RegExp("\\.png$");
					img.src = img.src.replace(img_re, '_collapsed.png');
				}
				if (cel)
				{
					cel_re = new RegExp("^(thead|tcat)$");
					cel.className = cel.className.replace(cel_re, '$1_collapsed');
				}
				obj.style.display = 'none';
				save_collapsed(objid, true);
				prevent_dblclick = false;
				match_contentHeight();
			}
			prevent_dblclick = true;
			$(obj).slideUp("normal",hideblock);
		}
	}
	return false;
}


/**
* Toggles the collapse state of an object, and saves state to 'xxx_collapse' cookie
*
* @param	string	Unique ID for the collapse group
* @param	boolean	If true, "collapseobj_" will not be added to object id
*
* @return	boolean	false
*/
function toggle_collapse_sidebar(objid)
{

	if (!is_regexp)
	{
		return false;
	}

	if ( prevent_dblclick == false ) {

		obj = fetch_object('collapseobj_' + objid);
		img = fetch_object('collapseimg_' + objid);

		//fetch #squeeze & #center to reset the size of the main content
		squeezev = fetch_object('squeeze');
		centerv = fetch_object('center');

		// fetch left header and footer to change background image, v b/c of compatibility with IE6
		headerv = fetch_object('headerleft');
		footerv = fetch_object('footerleft');

		if (obj.style.display == 'none')
		{
			prevent_dblclick = true;
			save_collapsed(objid, false);
			if (img)
			{
				img_re = new RegExp("_collapsed\\.png$");
				img.src = img.src.replace(img_re, '.png');
			}

			// change background image when sidebar-left is expanded
			headerv.style.background = 'transparent url(./images/misc/taverne/page01.png) no-repeat';
			footerv.style.background = 'transparent url(./images/misc/taverne/page14.png) no-repeat';

			obj.style.display = '';
			squeezev.style.margin = '0 0 0 206px';
			centerv.style.margin = '0 0 0 -196px';
			$('#' + objid).show(500);

			var showsidebar = function(e)
			{
				match_pagewith();
				setblockswidth();
				match_contentHeight();
				prevent_dblclick = false;
			}
			setTimeout(showsidebar,500);
		}
		else
		{
			prevent_dblclick = true;
			save_collapsed(objid, true);

			$('#' + objid).hide(500);
			var hidesidebar = function(e)
			{
				obj.style.display = 'none'
				squeezev.style.margin = '0';
				centerv.style.margin = '0';

				// change background image when sidebar-left is collapsed
				headerv.style.background = 'transparent url(./images/misc/taverne/page01_collapsed.png) no-repeat';
				footerv.style.background = 'transparent url(./images/misc/taverne/page14_collapsed.png) no-repeat';

				if (img)
				{
					img_re = new RegExp("\\.png$");
					img.src = img.src.replace(img_re, '_collapsed.png');
				}
				match_pagewith();
				setblockswidth();
				match_contentHeight();
				prevent_dblclick = false;
			}
			setTimeout(hidesidebar,500);
		}
	}
	return false;
}

/**

	Function for Spoiler tag

*/

function toggle_collapse_spoiler(objSpoiler)

{
	var SpoilerHeader = objSpoiler.parentNode;
	if (SpoilerHeader.nodeName != "DIV")
	{
		SpoilerHeader = SpoilerHeader.parentNode;
	}
	var SpoilerBody = SpoilerHeader.parentNode.getElementsByTagName("div")[1];
	SpoilerHeader = null;
	if (SpoilerBody.style.display != "")
	{ 
		SpoilerBody.style.display = ""; 
	}
	else
	{ 
		SpoilerBody.style.display = "none";
	}
}

/**
* Teleporter Animation
*/
$(document).ready(function(){
    $('#teleporter_text').click (function () {
        if(!$.browser.msie) {
            $('#teleporter_content').show(300);
        } else {
            $('#teleporter_content').show();
        }
	    $(".teleporter_logo").attr("src", "./images/misc/taverne/teleporter_logo_click.png");        
    });
    $("#teleporter_text").hover(function () {
        $(this).attr("src", "./images/misc/taverne/teleporter_hover.png");
    }, function () {
        $(this).attr("src", "./images/misc/taverne/teleporter.png");
    });
    $("#teleporter_content #close").click (function () {
        if(!$.browser.msie) {
            $('#teleporter_content').hide(300);
        } else {
            $('#teleporter_content').hide();
        }
        $(".teleporter_logo").attr("src", "./images/misc/taverne/teleporter_logo.png");
    });
    $("#teleporter_content #close").hover(function () {
        $(this).attr("src", "./images/misc/taverne/teleporter_content_close_h.png");
    }, function () {
        $(this).attr("src", "./images/misc/taverne/teleporter_content_close.png");
    });
});

/**
* Tooltip
*/
$(document).ready(function(){
    $('.bbcode_tooltipp').tooltip({
        track: true,
        delay: 0,
        showURL: false, 
        opacity: 0.6, 
        fixPNG: true,
        showBody: " || ", 
        top: -15,
        left: 5 
    });
});
