/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
var PBNcarousel=null, PBNwidth;
function PBNcarousel_initCallback(carousel) {
    jQuery('.PBNcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(this.id.substr(10,1)));
        return false;
    });
	PBNcarousel = carousel
/*	try {
	carousel.scroll(1);
	} catch(e){};*/
	if (jQuery.browser.msie) {		// kludge!
		PBNwidth = (parseInt($('.jcarousel-item-1').css('width'))+30) * carousel.size()
	}
};
function PBN_change(inst, LIobj, index, action)
{
	var Img;
	for (var i=1; i<=PBNcarousel.options.size; i++) {
		if (i==index)
			$('#PBNsection'+i).addClass( 'active' )
		else
			$('#PBNsection'+i).removeClass( 'active' )
	}
}

// Ride the carousel...
var PBNinited=false;
jQuery(document).ready( PBNinit );
function PBNinit() {
	if (PBNinited || $('#PBNwrap').length==0) return;
	PBNinited=true;
    jQuery("#PBNcarousel").jcarousel({
        scroll: 1,
        initCallback: PBNcarousel_initCallback,
		easing: (typeof jQuery.easing.easeOutExpo=='function')?'easeOutExpo':null,
		animation:(typeof jQuery.easing.easeOutExpo=='function')?750:'normal',
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
		visible:1,
		itemVisibleInCallback: {onBeforeAnimation: PBN_change, onAfterAnimation: function(){ PBNLeft = $('.jcarousel-skin-PBN .jcarousel-list-horizontal').css('left') }}
    });
	jQuery(document).bind('click', function(ev) {
										ev=ev||Event;
										var src=ev.srcElement||ev.target;
										if (window.EVEclose && ! (src.id && src.id.substr(0,3)=='EVE') ) EVEclose(2);
										if (! (src.className.indexOf('jcarousel')!=-1 || (src.id && src.id=='PBNspacer')) ) PBNclose(2); 
										return true; });
	
	//$('#PBNwrap').css('visibility','');
	//$(document.body).bind('click', function(e){$('#PBNwrap').slideUp('fast')});
	$('#PBN').attr('href','javascript:;');
	$('#PBN').click( function(e) {
			var Loc=$('#PBN img');
			if (!$('#PBNwrap').is(':hidden')) {
				PBNclose(2);
				return;
			}
			$(document).keydown(PBNescape)
			if (Loc && Loc.length) {
				var bPandC = document.location.pathname.match(/\/multilin\//i)?1:0;
				$('#PBNwrap').css({'top': gYP(Loc[0]) + Loc[0].offsetHeight + (bPandC?-1:3),'left': gXP(Loc[0])- (bPandC?2:5), 'visibility':'visible'})
			}
			//$('#PBNwrap').css('visibility','')
			//if($('#PBNwrap').is(':hidden')) $(document.body).bind('click', function(e){$('#PBNwrap').slideUp('fast')});
			if (document.location.pathname.match(/\/multilin/i))
			$('#PBNwrap').show();	//$.browser.msie?'':'fast');
			else
			$('#PBNwrap').slideDown('normal'); 
			if (!PBNcarousel.first) 
				PBNcarousel.scroll(1)
			if(PBNwidth){ $('.jcarousel-skin-PBN .jcarousel-list-horizontal').css({'width': PBNwidth + 'px', left: PBNLeft}); }

			return false;
	} );
	
	//$('#PBN').bind('mouseout', PBNclose);
	$('#PBN').bind('mouseover', PBNnoclose);
	//$('#PBNwrap').bind('mouseout', PBNclose);
	$('#PBNwrap').bind('mouseover', PBNnoclose);
			
	if (!document.domain.match(/gedigitalenergy\.com/i))
		$('A').each( function(i) { if (this.href.indexOf('javascript')==-1 && this.href.hash=='' &&this.href.indexOf(document.domain)!=-1&& this.href.toLowerCase().indexOf('.com/digitalenergy') == -1) this.href = this.href.toLowerCase().replace(".com/powerquality",".com/digitalenergy/powerquality")} );
	//setTimeout("$('#PBNwrap').hide()",2000);
	$('#PBNwrap').hide()
	
}
var PBNtimer=null;
function PBNclose(e)
{
	if (typeof(e)=='number'&&e==2) {
			if (!$('#PBNwrap').is(':hidden'))
		$('#PBNwrap').hide();
		return;
	} 
	else
	if (typeof(e)!='number') {
		PBNtimer = setTimeout("PBNclose(1)", 1000);
		return
	}
	if (!$('#PBNwrap').is(':hidden')) {
			if (document.location.pathname.match(/\/multilin/i))
			$('#PBNwrap').hide();	//$.browser.msie?null:'fast');
			else
			$('#PBNwrap').slideUp('normal');
	}
	PBNtimer=null;
}
function PBNnoclose()
{
	if (PBNtimer) {clearTimeout(PBNtimer); PBNtimer=null;}
}
function PBNescape(event)
{
	if ((event.which||event.keyCode) == 27) {
		PBNclose(2)
		$(document).unbind(event)
		event.preventDefault()
	} 
}

function gXP(iE){
	xPos=eval(iE).offsetLeft;
	tempEl=eval(iE).offsetParent;
	while (tempEl!=null && tempEl.id!='p_'){
		xPos+=tempEl.offsetLeft;
		tempEl=tempEl.offsetParent;
	}
	return xPos;
}
function gYP(iE){
	yPos=eval(iE).offsetTop;
	tempEl=eval(iE).offsetParent;
	while (tempEl!=null ){
		yPos+=tempEl.offsetTop;
		tempEl=tempEl.offsetParent;
	}
	return yPos;
}
