bs_calee_page_needs_refresh = false;


$jq(document).ready(function($jq) {
	body_width = $jq('#main-wrapper').width()-20;
	body_height = $jq('#main-wrapper').height();
	$jq('a.ajax_iframe').click(function(){
		bs_calee_page_needs_refresh = true;
		var url = $jq(this).attr('href');
		url = url.replace(/^.*#/, '');
		$jq.history.load(url);
		$jq('#main-wrapper').empty();
		$jq('<iframe />', {
			name: 'myFrame',
			    id:   'myFrame',
			    src: url,
			    width: body_width,
			    height: body_height
			    }).appendTo('#main-wrapper');
		$jq('html, body').animate({scrollTop:0}, 0);
		return false;
	    })

	    // $jq(".sf-menu"sample-menu-4").css("visibility", "visible");
	    // $jq(".ul.menu1_liste").find('ul').css("display", "none");
	    // $jq('.sf-menu').find('*').corner();

	    // initialise plugins


	    // $jq("ul.sf-menu").superfish({
	    // 	pathClass:  'current'
	    // 	    });






	    $jq.history.init(function(url) {
		    if(url == "" && bs_calee_page_needs_refresh) {
			location.reload()
			    } else {
			// restore the state from hash
		    }

		});


	if ( ! ($.browser.msie &&  parseInt($.browser.version, 10) < 8 ) ) {

	    menuYloc = $jq('#preface-top-inner').offset();

	    $jq(window).scroll(function(event){
		    try {

			var menuOffset =    -(menuYloc.top -  $jq(this).scrollTop());// + "px";
		    }
		    catch(err) {
			return true;
		    }
		    jq_preface_top = $jq('#preface-top');
		    if(menuOffset > 0) {
			if (!jq_preface_top.is('.scrolling')) {
			    jq_preface_top.css('display','none');
			    jq_preface_top.addClass("scrolling");

			    jq_preface_top.delay(500).fadeIn('slow', function() {
				    //				    				    alert('done');
				    // Animation complete.
				});
			}
		    }
		    else{
			jq_preface_top.removeClass("scrolling");
		    }

		});
	}

	//   num_items =  $(".menu-block-1 a.active-trail").length;
	//   template = "<span style=\"display:inline\"> &gt </span>"
	//   $jq('.menu-block-1 a.active-trail').each(function(index) {
	// 	    if (index != num_items-2)
	// 		$(this).after(template);
	// });

	//alert(document.location.host);


	/* Drupal does not allow relative links in menu: here a small hack */
	$jq('#header-group-wrapper').find('a').each(function(index) {
		new_link = $(this).attr('href');
		new_link = new_link.replace('www.bugseng.com',document.location.host);
		$(this).attr('href',new_link);
		if (new_link == 'http://www.nolink.org') {
		    $(this).removeAttr('href');
		    $(this).addClass("no-link");

		}
	    });

	$jq(document).find('span').each(function(index) {
		anchor_id  = $(this).attr('anchor_id');
		if  (anchor_id != null) {
		    $(this).children().each(function(index) {
			    new_link = $(this).attr('href');
			    new_link = new_link+'#'+anchor_id;
			    $(this).attr('href',new_link);
			})
			}
	    });



	function fixmenu() {
	    main_position = $jq("#main").position();
	    $jq('#preface-top').css('margin-left',main_position.left-15);
	}
	$jq(window).resize(fixmenu);
	fixmenu();

	function scrollWin(event){
	    newdata = event.data.areference.substring(1);
	    reference = $jq('a[name$='+ newdata + ']');
	    $jq('html,body').animate({
		    scrollTop: reference.offset().top -100
			}, 500);
	    return false;
	}


	/* Case: loaded url has an anchor */
	var url = window.location;
	var anchor = url.hash; //anchor with the # character
	var anchor2 = url.hash.substring(1); //anchor without the # character
	if (anchor2 != '') {
	    //     alert(anchor2)
	    reference = $jq('a[name$='+ anchor2 + ']');
	    $jq('html,body').animate({
		    scrollTop: reference.offset().top -100
			}, 700);

	}

	// Deal with anchors position
	$jq(document).find('a').each(function(index) {
		new_link = $jq(this).attr('href');
		if  (new_link != null) {
		    if (new_link.charAt(0) == '#') {
			$jq(this).click({areference:new_link},scrollWin);
		    }
		}
	    }
	    );
	// CLOSES ONLOAD
    })


