// JavaScript Document

// Mini CMS scripts

//	Function to set the focus on an element supplied
//	by the JS call e.g. <a href="javascript:doFocus('name');">Link</a>
//	would set the focus on an element with the id attribute "name".
function doFocus(el) {
    var x = document.getElementById(el).focus();
}

function doFocusByClass(theClass) {
    var allPageTags = document.getElementsByTagName("*");
    
    for (i = 0; i < allPageTags.length; i++) {
        if (allPageTags[i].className == theClass) {
            allPageTags[i].focus();
            break;
        }
    }
}

function showElement(id) {
    document.getElementById(id).style.display = "block";
}

function hideElement(id) {
    document.getElementById(id).style.display = "none";
}

function replacePaginationHtml(ajaxUrl, repeaterId, index, direction) {
    // Check to see if the browser is MSIE v6 and then we want to break
    //	if(jQuery.browser.msie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"])
    //	    return true;
    jQuery.get(
        ajaxUrl,
		{},
		function(data) {

		    var new_slider = jQuery("#" + repeaterId, data);
		    var cur_slider = jQuery("#" + repeaterId);

		    // Header pagination
		    var header_id = "#" + repeaterId + "header";
		    var header = jQuery(header_id, cur_slider);

		    if (header) {
		        var html = jQuery(header_id, new_slider);
		        header.replaceWith(html);
		    }

		    // Footer pagination
		    var footer_id = "#" + repeaterId + "footer";
		    var footer = jQuery(footer_id, cur_slider);

		    if (footer) {
		        var html = jQuery(footer_id, new_slider);
		        footer.replaceWith(html);
		    }

		    var holder = jQuery(".ajax_pagination_slider", cur_slider)
		    var cur_slide = jQuery(".ajax_pagination_slide", cur_slider);
		    var new_slide = jQuery(".ajax_pagination_slide", new_slider);

		    var width = holder.width();

		    if (direction > 0)
		        width = -width;

		    new_slide.css({
		        position: "absolute",
		        top: 0,
		        left: -width,
		        width: Math.abs(width)
		    });
		    
		    holder.animate(
				{ left: width },
				1000,
				null,
				function() {
				    holder.css("left", 0);

				    new_slide.css({
				        position: "static",
				        top: 0,
				        left: 0
				    });

				    cur_slide.remove();
				}
			);

		    holder.append(new_slide);

		    var height =  new_slide.height();

		    holder.css("height", height);
		},
		"html"
    );
}
//
//	Positioning and displaying of the translate and share toolboxes
//	jQuery required
//	03.06.09 BA
//
$(document).ready(function() {
	if ($('.pageTools').length) { // implies *not* zero
    	
  		$('#bookmarklistinchunk').hide();
		
		var nav = $("#nav_local");
		var leftnavT = nav.offset();					// retrieve offset location of the left nav
		var menuwidth = $("#nav_local").width();	// retrieve the width of the left nav
		var lnavPageTools = $("#nav_local .pageTools");
		var offS = lnavPageTools.offset();				// retrieve the offset location of the pagetools area
		var rcol = $(".chunktoolbar").position();		// retrieve the position of the toolbar menu (chunk)
		
// LEFT NAV Version
	$("#nav_local .shareThis").click(function() {
        $("#bookmarklistintools").css({ "left": (menuwidth*.7) + "px", "top": (offS.top - leftnavT.top) + "px" }); // place the toolbox at 70% width of menu
        $("#googletranslateintools").hide();	// hide the translation tools
		$("#bookmarklistintools").show();		// reveal the share tools
        return false;
    });
    $("#nav_local .translateThis").click(function() {
        $("#googletranslateintools").css({ "left": (menuwidth*.7) + "px", "top": (offS.top - leftnavT.top) + "px" });	// place the toolbox at 70% width of menu
        $("#bookmarklistintools").hide();		// hide the share tools
		$("#googletranslateintools").show();	// reveal the translate tools
        return false;
    });
// Right Col version
	$("#sub .shareThis").click(function() {

        $("#bookmarklistinchunk").css({ "left": (rcol.left-225) + "px", "top": (rcol.top+10) + "px" });
        $("#googletranslateinchunk").hide();	// hide the translation tools
		$("#bookmarklistinchunk").show();		// reveal the share tools
		var bHeight = $("#bookmarklistinchunk ul").height(); // retrieve height of bookmark list content
			$("#bookmarklistinchunk").css({"height":bHeight+"px"});
        return false;
    });
    $('#sub .translateThis').click(function() {
        $("#googletranslateinchunk").css({ "left": (rcol.left-225) + "px", "top": (rcol.top+10) + "px" });
        $("#bookmarklistinchunk").hide();		// hide the share tools
		$("#googletranslateinchunk").show();	// reveal the translate tools
        return false;
    });
	
	// Close handling 27.06.2011 sagba
	$('.bookmarklistclose').click(function(){
		$("#bookmarklistintools").hide();
		return false;
	});
	$('.googletranslateclose').click(function(){
		$("#googletranslateintools").hide();
		return false;
	});
	
    } else {
   // 	
  	}
	
	// Google translate tool check
	var domain = window.location.toString().split("//")[1].split(".")[1];
	
	if(domain=='googleusercontent'){
	$('#content').prepend('<div class="translated"><p>This page has been <a href="http://www.google.com/intl/en/help/faq_translation.html">automatically translated using Google</a>. Language usage and accuracy may differ to our original page. Please refer to the original page for accurate text.</p></div>');
	var cssObj = {
		'border':'1px dashed #1b6000',
		'background':'#ebffe3',
		'margin':'0.5em',
		'padding':'.3em .3em 0 .3em',
		'color':'#1b6000'
		}
	$('.translated').css(cssObj);
	$('.translated > p > a').css('color','#1b6000');
	}
});
