/////////////////////////////////////////////////////////////////
//
// Bookmarking function
//
// //////////////////////////////////////////////////////////////


function bookmark(title, url){
  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
  } 
  else if( window.external ) { // IE Favorite
    window.external.AddFavorite( url, title); 
  }
  else if(window.opera && window.print) { // Opera Hotlist
    return true; 
  }
}

/////////////////////////////////////////////////////////////////
//
// The Main init function for global stuff
//
// //////////////////////////////////////////////////////////////
jQuery(document).ready(function(){

document.getElementById('edit-search-theme-form-1').value = 'Search Best Health';

var searchRight = document.getElementById('edit-search-block-form-1');

if(searchRight)
	{
		document.getElementById('edit-search-block-form-1').value = 'Search Best Health';
	}

$('#edit-search-block-form-1').focus(function() {
	var curr_text_val = $("#edit-search-block-form-1").val();
	if(curr_text_val == 'Search Best Health')
		document.getElementById('edit-search-block-form-1').value = '';

});

$('#edit-search-block-form-1').blur(function() {
	var curr_text_val = $("#edit-search-block-form-1").val();
	if(curr_text_val == '')
		document.getElementById('edit-search-block-form-1').value = 'Search Best Health';

});


$('#search-block-form').submit(function() {
	curr_val = $("#edit-search-block-form-1").val();
	if(curr_val == 'Search Best Health')
		document.getElementById('edit-search-block-form-1').value = '';
});


	
$('#edit-search-theme-form-1').focus(function() {
	var curr_text_val = $("#edit-search-theme-form-1").val();
	if(curr_text_val == 'Search Best Health') {
		
		document.getElementById('edit-search-theme-form-1').value = '';
		document.getElementById('edit-search-theme-form-1').style.color= '#000000';
		
	}

});

$('#edit-search-theme-form-1').blur(function() {
	var curr_text_val = $("#edit-search-theme-form-1").val();
	if(curr_text_val == '') {
		document.getElementById('edit-search-theme-form-1').value = 'Search Best Health';
		document.getElementById('edit-search-theme-form-1').style.color= '';
	}
	else
		document.getElementById('edit-search-theme-form-1').style.color= '#000000';

});


$('#search-theme-form').submit(function() {
	curr_val = $("#edit-search-theme-form-1").val();
	if(curr_val == 'Search Best Health')
		document.getElementById('edit-search-theme-form-1').value = '';
});

	/* External links */
	jQuery('a[rel=external]').attr('target','_blank');
	
	
	$('a#postComment').click(function(){
		var commentsForm = jQuery("#commentFormWrapper");
		if(jQuery(commentsForm).css('display') == 'none')
		{
			jQuery(commentsForm).fadeIn();
		}
		else
		{
			jQuery(commentsForm).fadeOut();
		}
		
		return false;
	});
	
	
	$('#header ul.main_nav > li').hover(
		function(){
			$(this).children('.main').addClass('on');
			$(this).children('ul').show();
		},
		function(){
			$(this).children('.main').removeClass('on');
			$(this).children('ul').hide();
		}
	);

	$('#footer ul > li').hover(
		function(){
			$(this).children('.main').addClass('on');
			$(this).children('ul').show();
		},
		function(){
			$(this).children('.main').removeClass('on');
			$(this).children('ul').show();
		}
	);

	
	
	$('.toolbox .icones .print a').bind('click', function(){ window.print();return false;	});
	
	
	// 
	$(".toolbox:first").append($("#comment-form"));
	$("#comment-form").css('display','block');
	$(".toolbox .send").append($("#besthealth-send-to-friend-form"));
	$(".toolbox .send > a").click(function(){
		if(jQuery("#besthealth-send-to-friend-form").css('display') == 'none')
		{
			jQuery("#besthealth-send-to-friend-form").fadeIn();
		}
		else
		{
			jQuery("#besthealth-send-to-friend-form").fadeOut();
		}
		return false;
	});
	
	
	/* Re-order slideshow */
	$('#besthealth-orderingslideform').wrap('<div id="reorder_wrapper"></div>').wrap('<div id="reorderform"></div>').css('display','block');
	$('#reorder_wrapper').prepend('<a href="link" onclick="return openReorder(this)">Re-order slides</a>');
	
	
	/* Tweak pour Forums */
	//$('#forum_container script').each(function(){ $(this).appendTo('head');	});
	//$('#forum_container link').each(function(){	$(this).appendTo('head');	});
	
	
	
	
	
	/* Homepage Community tabs */
	$('#blog_tab_bloc').css('display','block');
	$('#blog_tab').parent('li').addClass('on');
	
	$('#community ul.tabs a').click(function(){
		return switchCommunityTabs($(this));
	});
	
	
	
	
	// skin relativity box
	$('legend:contains("Link operations")').each(function(){
		$(this).parents('fieldset').addClass('relativity_box');
		$(this).replaceWith('<span>'+ $(this).text() +'</span>');
	})
	
});




var openReorder = function(link){

	
	if($(link).next('#reorderform').height() < 1){
		$('#reorderform').css('display','block');
		$('#reorderform').animate({
			height: '200px'
		},500);
	}else{
		$('#reorderform').animate({
			height: '0px'
		},500,function(){$('#reorderform').css('display','none')});
	}
	return false;
}





var switchCommunityTabs = function(tab){
	var tabid = $(tab).attr('id') + '_bloc';
	
		$('#community ul.tabs li').removeClass('on');
		$(tab).parent('li').addClass('on');
		$('.tabs_community').css('display','none');
		$('#'+tabid).css('display','block');
		return false;
}


var getTwitterStatus = function(statusWrapper){
	$.ajax({
	  type: "GET",
	  url: "/portlet/twitter/twitter-status.php",
	  dataType: "json",
	  success: function(msg){
			$(msg['status']).each(function(){
				var patt1 = new RegExp(/(http:\/\/[a-zA-Z0-9\.\/]*)\b/);
				ret  = this['text'].replace(patt1, "<a href='$1'>$1</a>");
				$('#'+statusWrapper).append('<li>'+ret+'</li>');
			});
	  }
	});
}

