$(document).ready( function(){tabbed_block_ajax_auto_attach();} )

function tabbed_block_ajax_auto_attach(){
  //init tabs
  $(".tabbed-block").each(function(){
    tabs(this)
    
  });
  $(".block-tabbed_block").each(function(){
    tabs_ajax(this)
    frag = tabbed_block_get_cookie(($(this).attr("id")));
    $(this).find(".tabs-nav").children("li").eq(frag.substr(-1)).trigger("click");
  });
}

function tabs_ajax(obj){
  //init ajax

  $(obj).children(".content").append("<a class='tabbed_block-ajax'>refresh</a>")
  
  $(obj).children(".content").children(".tabbed_block-ajax").click(function(){
    id = $(this).parent().parent().attr("id");
    $(this).parent().parent().addClass("tabbed_block-click");
    $(this).parent(".content").html("<i class='description'>Loading</i>");
    $.ajax({
      type: "POST",
      //base path is defined in tabbed_block.module
      url: base_path+"tabbed_block/menu",
      data: "id="+id,
      success: function (data) {
        data = Drupal.parseJson(data);
        $(".tabbed_block-click"+" .content").html( data.content );
        tabs_ajax(".tabbed_block-click");
        tabs(".tabbed_block-click .tabbed-block");
        frag = tabbed_block_get_cookie(($(".tabbed_block-click").attr("id")));
        $(".tabbed_block-click").find(".tabs-nav").children("li").eq(frag.substr(-1)).trigger("click");
        $(".tabbed_block-click").removeClass("tabbed_block-click");
      },
      error: function (){
        $(".tabbed_block-click"+" .content").html( "Sorry, there is an error occured." );
        tabs_ajax(".tabbed_block-click");
        tabs(".tabbed_block-click .tabbed-block");
        $(".tabbed_block-click").removeClass("tabbed_block-click");
      }
    });
  });
}

function tabs( id ){
	
	//add classes
	$(id).children("ul").addClass("tabs-nav");
	$(id).children("ul").children("li").eq(0).addClass("tabs-selected");
	
	//init clicks methods on tab-heads
	$(id).children("ul").children("li").each( function(){
		$(this).click(function(){

			//content
      click = $(this).children("a").attr("href");
      click = click.substr(click.search(/#/));
			$(this).parent().siblings("div").each(function(){
        $(this).addClass("tabs-hide");
      });
			$(this).parent().siblings(".tabs-container"+click).removeClass("tabs-hide");
			
			//head
			$(this).addClass("tabs-selected");
			$(this).siblings(".tabs-selected").removeClass("tabs-selected");
      
      //cookies
      tabbed_block_set_cookie( "block-tabbed_block-"+click.substr(9,1) , click );
			return false;
		});
	});
	
	//init tab-content
	$(id).children("div").addClass("tabs-container tabs-hide");
	$(id).children("div").eq(0).removeClass("tabs-hide");
}

function tabbed_block_set_cookie(c_name,value,expiredays)
{
  var exdate=new Date()
  exdate.setDate(exdate.getDate()+expiredays)
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function tabbed_block_get_cookie(c_name){
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}
