/*
(c) Andre Pahl 2011
*/

/* 	-----------------------------------------------------------------------------
	VARS
	----------------------------------------------------------------------------- */

cookieInitialized=0
SPEISEN_SECTIONS=new Array()
SPEISEN_AUTOMATIC=1
SPEISEN_POSITION=0
SPEISEN_ISFADING=0

IS_STARTPAGE=0

ACTIVE_OVERLAY_LINK=""	
DELAYTIMER_resize=null
userAgent = navigator.userAgent.toLowerCase()

isIE = (navigator.userAgent.match(/msie/i));
isIE7 = (navigator.userAgent.match(/msie 7/i));
isFF = (navigator.userAgent.match(/firefox/i));
isSafari=(navigator.userAgent.match(/safari/i));
isChrome=(navigator.userAgent.match(/chrome/i));

emailValidate =/^[A-Za-z0-9üÜúÚáäÄåÅóÓøØéÉíÍßæ\.\-\_]*?[A-Za-z0-9üÜúÚáäÄåÅóÓøØéÉíÍßæ\.\-\_]+@[A-Za-z0-9üÜúÚáäÄåÅóÓøØéÉíÍßæ\.\-\_]+[\w.-]*?\.[A-Za-z0-9üÜúÚäÄáåÅóÓøØéÉíÍßæ]{2,5}$/;

SLIDESHOW_isInitialized=0
SLIDESHOW_slideshow_active=0
SLIDESHOW_doShuffle=0
SLIDESHOW_currentPosition=0
SLIDESHOW_imageCounter=0
SLIDESHOW_slideshow_active=0
SLIDESHOW_timer=0
SLIDESHOW_timerID=0
SLIDESHOW_slideshow_interval=5000
SLIDESHOW_fadeouttime=2000
SLIDESHOW_fadeintime=2500
SLIDESHOW_all_images=new Array()

GALLERY_lastloaded=new Array()
SLIDESHOW_lastloaded=new Array()





/* 	-----------------------------------------------------------------------------
	INITIALIZE
	----------------------------------------------------------------------------- */

$(function() {
	
	// language
	iniCookie()
	
	
	/* 	-----------------------------------------------------------------------------
	SCROLLBAR
	----------------------------------------------------------------------------- */
	
	if (iOS==0) {
		$('.scroll-pane').each(
			function() {
				
				$(this).jScrollPane({showArrows:0, autoReinitialise:true});
			
				var api = $(this).data('jsp');
				var throttleTimeout;
				overlayID = api.getContentPane();
				overlayAPI = api
				
				$(window).bind(
					'resize',
					function() {
						if (!throttleTimeout) {
							throttleTimeout = setTimeout(
								function() {
									api.reinitialise();
									throttleTimeout = null;
								},
								50
							);
						}
					}
				);
			}
		);
	}
	
	

	/* 	-----------------------------------------------------------------------------
	RESIZE CONTENT
	----------------------------------------------------------------------------- */
	
	$(window).resize(function(){
	  RESIZE_NOW();
	});
	RESIZE_NOW()
	
	
	
	/* 	-----------------------------------------------------------------------------
	VOILA.
	----------------------------------------------------------------------------- */
	openExternal()
	

});




/* 	-----------------------------------------------------------------------------
	RESERVATION
	----------------------------------------------------------------------------- */

function sendReservation() {
	
	var RES_date=		$('#RES_date').val()
	var RES_time=		$('#RES_time').val()
	var RES_guestNbr=	$('#RES_guestNbr').val()
	var RES_name=		$('#RES_name').val()
	var RES_from=		$('#RES_from').val()
	var RES_phone=		$('#RES_phone').val()
	var RES_street=		$('#RES_street').val()
	var RES_city=		$('#RES_city').val()
	var RES_text=		$('#RES_text').val()

	
	// check form
	if (RES_date=="") {
		if (LANGUAGE==1) {
			$('#RES_error').html('Bitte geben Sie das gewünschte Datum an.<br />')
		} else {
			$('#RES_error').html('Please fill in a date.<br />')
		}
		return
	}
	
	
	if (RES_time=="") {
		if (LANGUAGE==1) {
			$('#RES_error').html('Bitte geben Sie die gewünschte Uhrzeit an.<br />')
		} else {
			$('#RES_error').html('Please fill in a time.<br />')
		}
		return
	}
	
	
	if (RES_guestNbr=="") {
		if (LANGUAGE==1) {
			$('#RES_error').html('Bitte geben Sie die Anzahl der Gäste an.<br />')
		} else {
			$('#RES_error').html('Please fill in the total number of guests.<br />')
		}
		return
	}
	
	if (RES_name=="") {
		if (LANGUAGE==1) {
			$('#RES_error').html('Bitte geben Sie ihren Namen ein.<br />')
		} else {
			$('#RES_error').html('Please fill in your name.<br />')
		}
		return
	}
	
	if (!(RES_from).match(emailValidate)) {
		if (LANGUAGE==1) {
			$('#RES_error').html('Bitte geben Sie eine gültige E-Mailadresse ein.<br />')
		} else {
			$('#RES_error').html('Please fill in a valid email address.<br />')
		}
		return
	}
	
	if (RES_phone=="") {
		if (LANGUAGE==1) {
			$('#RES_error').html('Bitte geben Sie eine Telefonnummer ein.<br />')
		} else {
			$('#RES_error').html('Please fill in your phone number.<br />')
		}
		return
	}
	
	
	
	
	$('#RES_error').html('')
	$('#RES_sendlink').css('display', 'none')
	
	if (LANGUAGE==1) {
		$('#RES_sending').html('<b>Ihre Reservierung wird versendet.</b><br />')
	} else {
		$('#RES_sending').html('<b>Sending.</b><br />')
	}
	
	
	var RES_date=		$('#RES_date').val()
	var RES_time=		$('#RES_time').val()
	var RES_guestNbr=	$('#RES_guestNbr').val()
	var RES_name=		$('#RES_name').val()
	var RES_from=		$('#RES_from').val()
	var RES_phone=		$('#RES_phone').val()
	var RES_street=		$('#RES_street').val()
	var RES_city=		$('#RES_city').val()
	var RES_text=		$('#RES_text').val()
	
	RES_date=		encodeURIComponent(RES_date)
	RES_time=		encodeURIComponent(RES_time)
	RES_guestNbr=	encodeURIComponent(RES_guestNbr)
	RES_name=		encodeURIComponent(RES_name)
	RES_from=		encodeURIComponent(RES_from)
	RES_phone=		encodeURIComponent(RES_phone)
	RES_street=		encodeURIComponent(RES_street)
	RES_city=		encodeURIComponent(RES_city)
	RES_text=		encodeURIComponent(RES_text)
	
	
	$.ajax({
		url: URL_ROOT+'_INCL/sendBooking.php?date='+RES_date+'&time='+RES_time+'&guestNbr='+RES_guestNbr+'&name='+RES_name+'&from='+RES_from+'&phone='+RES_phone+'&street='+RES_street+'&city='+RES_city+'&text='+RES_text+'&language='+LANGUAGE+'&r='+Math.random(),
		
		cache: false,
		success: function(result){
			
			$('#RES_thetext').css('display', 'none')
			$('#RES_thanks').css('display', 'block')
		
			if (LANGUAGE==1) {
				$('#RES_thanks').html('<b>Vielen Dank für Ihre Reservierungsanfrage.</b><br/>Wir setzen uns so bald wie möglich mit Ihnen in Verbindung.')
			} else {
				$('#RES_thanks').html('<b>Thank you for your request for a reservation.</b><br/>We will contact you as soon as possible.')
			}
			
		}
	});	
}




/* 	-----------------------------------------------------------------------------
	LANGUAGE COOKIE
	----------------------------------------------------------------------------- */

function changeLanguage(l) {
	if (cookieInitialized) {
		jQuery.cookie('margaux_language', l, { expires: 7, path: '/' })
		window.location.reload()
	}
}


// -----------------------------------------------------------------------------------------------------------------------
// RESIZE  
// -----------------------------------------------------------------------------------------------------------------------


function RESIZE_NOW() {
	
	var w=$(window).width()
	var h=$(window).height()
	
	var menuH=$("#MENU").height()
	var minH=314+20+menuH+30+20
	var mainH=Math.max(minH,Math.min(668, h-40))
	var mainY=Math.max(20, Math.round((h-mainH)/2.5))
	var mainX=Math.round((w-1030)/2)
	
	if (iOS) {
	
		if (IS_STARTPAGE==0) {
			mainY=40
		}
			
		$('#MAIN').css({'left':mainX, 'top':mainY, 'height':mainH})
		
		if (IS_STARTPAGE==0) {
			
			var th=$('#TEXT').height()
			var mh=314+20+20+th
			
			if (SPEISEN_SECTIONS.length>0) {
				// SPEISEN has extra links underneath -> area longer
				mh+=32
				
			} 
			
			mh=Math.max(mh, 314+20+menuH+260)
			
			var wh=$(window).height()-80
			mh=Math.max(mh, wh)
			
			$('#MAIN').css({'height':mh})
			
		}
		
	} else {
		
		$('#MAIN').css({'left':mainX, 'top':mainY, 'height':mainH})
		
		if (SPEISEN_SECTIONS.length>0) {
			// SPEISEN has extra links underneath -> make shorter
			$('#TEXT').css({'height':mainH-314-20-20-32})
			
		} else {
			$('#TEXT').css({'height':mainH-314-20-20})
		}
	}
	
}



// -----------------------------------------------------------------------------------------------------------------------
// BOOKING
// -----------------------------------------------------------------------------------------------------------------------


function booking() {
	
	var w=parseInt(screen.width)
	var h=parseInt(screen.height)
	var pw=700
	var ph=460
	var x=(w-pw)/2
	var y=(h-ph)/2
	
	POPPER(pw,ph,x,y)	
}
	

function POPPER(w,h,x,y) {
	
	newwindow=window.open('booking.php','Restaurant_Margaux_Berlin','location=no,status=no,toolbar=no,menubar=no,height='+h+',width='+w+',top='+y+',left='+x)
	if (window.focus) {newwindow.focus()}
	return false

	
}


// -----------------------------------------------------------------------------------------------------------------------
// START PAGE SLIDESHOW
// -----------------------------------------------------------------------------------------------------------------------

function show_previous() {
	
	if (SPEISEN_ISFADING==1) {
		return
	}
	
	SPEISEN_AUTOMATIC=0
	
	var temp=SPEISEN_POSITION
	SPEISEN_POSITION--
	if (SPEISEN_POSITION<0) {
		SPEISEN_POSITION=SPEISEN_SECTIONS.length-1
	}
	SPEISEN_ISFADING=1
	
	$('#SPEISE_TEXT'+temp).fadeOut(600, function() { $('#SPEISE_TEXT'+SPEISEN_POSITION).fadeIn(1000, function() { SPEISEN_ISFADING=0 }) } )
			
	SLIDESHOW_imageCounter++
	SLIDESHOW_currentPosition=SPEISEN_SECTIONS[SPEISEN_POSITION] 
	SLIDESHOW_timer=0
	SLIDESHOW_fetchImage()
	
}


function show_next() {
	
	if (SPEISEN_ISFADING==1) {
		return
	}
	
	SPEISEN_AUTOMATIC=0
	
	var temp=SPEISEN_POSITION
	SPEISEN_POSITION++
	if (SPEISEN_POSITION>(SPEISEN_SECTIONS.length-1)) {
		SPEISEN_POSITION=0
	}
	SPEISEN_ISFADING=1
	
	$('#SPEISE_TEXT'+temp).fadeOut(600, function() { $('#SPEISE_TEXT'+SPEISEN_POSITION).fadeIn(1000, function() { SPEISEN_ISFADING=0 }) } )
			
	SLIDESHOW_imageCounter++
	SLIDESHOW_currentPosition=SPEISEN_SECTIONS[SPEISEN_POSITION] 
	SLIDESHOW_timer=0
	SLIDESHOW_fetchImage()
	
}




function SLIDESHOW_loadVars(sl_arr) {
	
	SLIDESHOW_all_images=sl_arr.split("[**]")
	
	if (SLIDESHOW_doShuffle) {
		var tmp, rand;
		for (var i =0; i<SLIDESHOW_all_images.length; i++){
			rand = Math.floor(Math.random() * SLIDESHOW_all_images.length);
			tmp = SLIDESHOW_all_images[i]; 
			SLIDESHOW_all_images[i] = SLIDESHOW_all_images[rand]; 
			SLIDESHOW_all_images[rand] =tmp;
		
		}
	}
		
}


function SLIDESHOW_start(sl_arr) {
	// start slideshow
	
	SLIDESHOW_loadVars(sl_arr)
	SLIDESHOW_timer=0
	SLIDESHOW_currentPosition=0
	
	if (SLIDESHOW_all_images.length>0) {
		SLIDESHOW_slideshow_active=1
		SLIDESHOW_fetchImage(0)
	}
}



function SLIDESHOW_fetchImage() {
	// preloads the next image in the slideshow
	
	if (!window.SLIDESHOW_all_images) {
		return
	}
	
	$('#SLIDESHOW_container').append('<div class="SLIDESHOW_photodiv"><img id="SLIDESHOW_img'+SLIDESHOW_imageCounter+'" class="SLIDESHOW_pic" onload="SLIDESHOW_showpic()" src="'+URL_ROOT+SLIDESHOW_all_images[SLIDESHOW_currentPosition]+'"></div>');
	
	
}
 
// SPEISEN_SECTIONS

function SLIDESHOW_showpic() {
	// displays the next slide pic, fades the old one out
	// initializes also preloading of the next images as soon as image is faded out
	// does that only if >1 images in slideshow
	
	if (!window.SLIDESHOW_all_images) {
		return
	}
	
	var currentTime=new Date()
	var ms = currentTime.getTime()
	var tempImg=$('#SLIDESHOW_img'+SLIDESHOW_imageCounter)
	
	if (ms>SLIDESHOW_timer && SLIDESHOW_slideshow_active==1) {
		
		if (in_array(SLIDESHOW_imageCounter,SLIDESHOW_lastloaded)) {
			return // double triggered onload
		}
		
		// prevent double triggering of onload event of image
		SLIDESHOW_lastloaded.push(SLIDESHOW_imageCounter)
		if (SLIDESHOW_lastloaded.length>2) {
			SLIDESHOW_lastloaded=SLIDESHOW_lastloaded.slice(1)
		}
		
		SLIDESHOW_timer=ms+SLIDESHOW_slideshow_interval
		
		
		/* ------------------------------------------------------------------------------------
		SPEISEN TEXTS yyy
		------------------------------------------------------------------------------------ */
		if (SPEISEN_SECTIONS.length>0 && SPEISEN_AUTOMATIC==1) {
			var temp=array_pos(SLIDESHOW_currentPosition-1, SPEISEN_SECTIONS)
			if (temp!=-1) {
				
				var a=temp-1
				if (a<0) {
					a=SPEISEN_SECTIONS.length-1
				}
				SPEISEN_POSITION=temp
				SPEISEN_ISFADING=1
				
				$('#SPEISE_TEXT'+a).fadeOut(600, function() { $('#SPEISE_TEXT'+temp).fadeIn(1000, function() { SPEISEN_ISFADING=0 }) } )
				/*
				$('.speise_text').css('display', 'none')
				$('#SPEISE_TEXT'+temp).css('display', 'block')
				*/
				
			}	
		}
		/* ------------------------------------------------------------------------------------
		END SPEISEN TEXTS
		------------------------------------------------------------------------------------ */
		
		if (isIE) {
			tempImg.fadeOut(0)
		}
		tempImg.fadeOut(1)
		tempImg.addClass('SLIDESHOW_pic_visible')
		tempImg.attr({'iW':tempImg.width(), 'iH':tempImg.height()})
		tempImg.fadeIn(SLIDESHOW_fadeintime, function() { SLIDESHOW_removeOldImages() } )
		
		if (SLIDESHOW_all_images.length>1) {
			SLIDESHOW_slideshow_active=1
			
			SLIDESHOW_imageCounter++
			
			if (SPEISEN_AUTOMATIC==0) {
				// cycle just on 1 speise
				
				var SP_start=SPEISEN_SECTIONS[SPEISEN_POSITION] 
				if (SPEISEN_POSITION==(SPEISEN_SECTIONS.length-1)) {
					var SP_end=SLIDESHOW_all_images.length
				} else {
					var SP_end=SPEISEN_SECTIONS[SPEISEN_POSITION+1]
				}
				
				SLIDESHOW_currentPosition++
				if (SLIDESHOW_currentPosition>=SP_end) {
					SLIDESHOW_currentPosition=SP_start
				}
				
			} else {
				
				SLIDESHOW_currentPosition++
				if (SLIDESHOW_currentPosition>=SLIDESHOW_all_images.length) {
					SLIDESHOW_currentPosition=0
				}
			}
			
						 
			SLIDESHOW_fetchImage()
			
		} else {
			SLIDESHOW_slideshow_active=0
			clearTimeout(SLIDESHOW_timerID)
		}
		
	
	} else {
		
		clearTimeout(SLIDESHOW_timerID)
		SLIDESHOW_timerID=setTimeout('SLIDESHOW_showpic()', 300) // not yet time for next slide
	}

}

function SLIDESHOW_removeOldImages() {
	// remove old divs, just leave 2 upper ones
	
	var temp=$('.SLIDESHOW_photodiv')
	
	if (temp.length>4) {
		for (var i=0; i<temp.length-4; i++) {
			$(temp[i]).remove()
		}
	}	
}


function SLIDESHOW_setState(act) {
	// sets the slideshow active/non-active
	// 0: paused / 1: go
	SLIDESHOW_slideshow_active=act
}

function SLIDESHOW_forget() {
	// deactivates the mood slideshow completely
	SLIDESHOW_slideshow_active=0
	SLIDESHOW_currentPosition=1
	SLIDESHOW_timer=0
	clearTimeout(SLIDESHOW_timerID)
}



// -----------------------------------------------------------------------------------------------------------------------
// UTILITIES
// -----------------------------------------------------------------------------------------------------------------------


function openExternal() {
	// open external links in new window. also updates for future ajax loaded links. only jquery 1.3+
	$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function(){
		var h=($(this).attr('href'))
		if (h.indexOf('margaux-berlin.de')==-1 && h.indexOf('localhost')==-1) {
			$(this).attr('target','_blank')
		}
	});
}


function mailMe(who,domain) {
	str = "mailto:" + who + "@" + domain;
	window.location = str;
}

function in_array(id, arr) {
	var found=0
	for (var i=0; i<arr.length; i++) {
		if (arr[i]==id) {
			found=1
			break;
		}
	}
	return found
}


function array_pos(id, arr) {
	var pos=-1
	
	for (var i=0; i<arr.length; i++) {
		if (arr[i]==id) {
			pos=i
			break;
		}
	}
	return pos
}


// -----------------------------------------------------------------------------------------------------------------------
// JQUERY PLUGINS
// -----------------------------------------------------------------------------------------------------------------------



/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

function iniCookie() {
	
	jQuery.cookie = function (key, value, options) {
	
		// key and value given, set cookie...
		if (arguments.length > 1 && (value === null || typeof value !== "object")) {
			options = jQuery.extend({}, options);
	
			if (value === null) {
				options.expires = -1;
			}
	
			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}
	
			return (document.cookie = [
				encodeURIComponent(key), '=',
				options.raw ? String(value) : encodeURIComponent(String(value)),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path ? '; path=' + options.path : '',
				options.domain ? '; domain=' + options.domain : '',
				options.secure ? '; secure' : ''
			].join(''));
		}
	
		// key and possibly options given, get cookie...
		options = value || {};
		var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
		return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
	};
	
	cookieInitialized=1

}


/*
 * jScrollPane - v2.0.0beta9 - 2011-02-04
 * http://jscrollpane.kelvinluck.com/
 *
 * Copyright (c) 2010 Kelvin Luck
 * Dual licensed under the MIT and GPL licenses.
 */
 
(function(b,a,c){b.fn.jScrollPane=function(f){function d(D,N){var ay,P=this,X,aj,w,al,S,Y,z,r,az,aE,au,j,I,i,k,Z,T,ap,W,u,B,aq,ae,am,G,m,at,ax,y,av,aH,g,K,ai=true,O=true,aG=false,l=false,ao=D.clone(false,false).empty(),ab=b.fn.mwheelIntent?"mwheelIntent.jsp":"mousewheel.jsp";aH=D.css("paddingTop")+" "+D.css("paddingRight")+" "+D.css("paddingBottom")+" "+D.css("paddingLeft");g=(parseInt(D.css("paddingLeft"),10)||0)+(parseInt(D.css("paddingRight"),10)||0);function ar(aQ){var aO,aP,aK,aM,aL,aJ,aI,aN;ay=aQ;if(X===c){aI=D.scrollTop();aN=D.scrollLeft();D.css({overflow:"hidden",padding:0});aj=D.innerWidth()+g;w=D.innerHeight();D.width(aj);X=b('<div class="jspPane" />').css("padding",aH).append(D.children());al=b('<div class="jspContainer" />').css({width:aj+"px",height:w+"px"}).append(X).appendTo(D)}else{D.css("width","");aJ=D.innerWidth()+g!=aj||D.outerHeight()!=w;if(aJ){aj=D.innerWidth()+g;w=D.innerHeight();al.css({width:aj+"px",height:w+"px"})}if(!aJ&&K==S&&X.outerHeight()==Y){D.width(aj);return}K=S;X.css("width","");D.width(aj);al.find(">.jspVerticalBar,>.jspHorizontalBar").remove().end()}aO=X.clone(false,false).css("position","absolute");aP=b('<div style="width:1px; position: relative;" />').append(aO);b("body").append(aP);S=Math.max(X.outerWidth(),aO.outerWidth());aP.remove();Y=X.outerHeight();z=S/aj;r=Y/w;az=r>1;aE=z>1;if(!(aE||az)){D.removeClass("jspScrollable");X.css({top:0,width:al.width()-g});o();E();Q();x();ah()}else{D.addClass("jspScrollable");aK=ay.maintainPosition&&(I||Z);if(aK){aM=aC();aL=aA()}aF();A();F();if(aK){M(aM,false);L(aL,false)}J();af();an();if(ay.enableKeyboardNavigation){R()}if(ay.clickOnTrack){q()}C();if(ay.hijackInternalLinks){n()}}if(ay.autoReinitialise&&!av){av=setInterval(function(){ar(ay)},ay.autoReinitialiseDelay)}else{if(!ay.autoReinitialise&&av){clearInterval(av)}}aI&&D.scrollTop(0)&&L(aI,false);aN&&D.scrollLeft(0)&&M(aN,false);D.trigger("jsp-initialised",[aE||az])}function aF(){if(az){al.append(b('<div class="jspVerticalBar" />').append(b('<div class="jspCap jspCapTop" />'),b('<div class="jspTrack" />').append(b('<div class="jspDrag" />').append(b('<div class="jspDragTop" />'),b('<div class="jspDragBottom" />'))),b('<div class="jspCap jspCapBottom" />')));T=al.find(">.jspVerticalBar");ap=T.find(">.jspTrack");au=ap.find(">.jspDrag");if(ay.showArrows){aq=b('<a class="jspArrow jspArrowUp" />').bind("mousedown.jsp",aD(0,-1)).bind("click.jsp",aB);ae=b('<a class="jspArrow jspArrowDown" />').bind("mousedown.jsp",aD(0,1)).bind("click.jsp",aB);if(ay.arrowScrollOnHover){aq.bind("mouseover.jsp",aD(0,-1,aq));ae.bind("mouseover.jsp",aD(0,1,ae))}ak(ap,ay.verticalArrowPositions,aq,ae)}u=w;al.find(">.jspVerticalBar>.jspCap:visible,>.jspVerticalBar>.jspArrow").each(function(){u-=b(this).outerHeight()});au.hover(function(){au.addClass("jspHover")},function(){au.removeClass("jspHover")}).bind("mousedown.jsp",function(aI){b("html").bind("dragstart.jsp selectstart.jsp",aB);au.addClass("jspActive");var s=aI.pageY-au.position().top;b("html").bind("mousemove.jsp",function(aJ){U(aJ.pageY-s,false)}).bind("mouseup.jsp mouseleave.jsp",aw);return false});p()}}function p(){ap.height(u+"px");I=0;W=ay.verticalGutter+ap.outerWidth();X.width(aj-W-g);if(T.position().left===0){X.css("margin-left",W+"px")}}function A(){if(aE){al.append(b('<div class="jspHorizontalBar" />').append(b('<div class="jspCap jspCapLeft" />'),b('<div class="jspTrack" />').append(b('<div class="jspDrag" />').append(b('<div class="jspDragLeft" />'),b('<div class="jspDragRight" />'))),b('<div class="jspCap jspCapRight" />')));am=al.find(">.jspHorizontalBar");G=am.find(">.jspTrack");i=G.find(">.jspDrag");if(ay.showArrows){ax=b('<a class="jspArrow jspArrowLeft" />').bind("mousedown.jsp",aD(-1,0)).bind("click.jsp",aB);y=b('<a class="jspArrow jspArrowRight" />').bind("mousedown.jsp",aD(1,0)).bind("click.jsp",aB);
if(ay.arrowScrollOnHover){ax.bind("mouseover.jsp",aD(-1,0,ax));y.bind("mouseover.jsp",aD(1,0,y))}ak(G,ay.horizontalArrowPositions,ax,y)}i.hover(function(){i.addClass("jspHover")},function(){i.removeClass("jspHover")}).bind("mousedown.jsp",function(aI){b("html").bind("dragstart.jsp selectstart.jsp",aB);i.addClass("jspActive");var s=aI.pageX-i.position().left;b("html").bind("mousemove.jsp",function(aJ){V(aJ.pageX-s,false)}).bind("mouseup.jsp mouseleave.jsp",aw);return false});m=al.innerWidth();ag()}}function ag(){al.find(">.jspHorizontalBar>.jspCap:visible,>.jspHorizontalBar>.jspArrow").each(function(){m-=b(this).outerWidth()});G.width(m+"px");Z=0}function F(){if(aE&&az){var aI=G.outerHeight(),s=ap.outerWidth();u-=aI;b(am).find(">.jspCap:visible,>.jspArrow").each(function(){m+=b(this).outerWidth()});m-=s;w-=s;aj-=aI;G.parent().append(b('<div class="jspCorner" />').css("width",aI+"px"));p();ag()}if(aE){X.width((al.outerWidth()-g)+"px")}Y=X.outerHeight();r=Y/w;if(aE){at=Math.ceil(1/z*m);if(at>ay.horizontalDragMaxWidth){at=ay.horizontalDragMaxWidth}else{if(at<ay.horizontalDragMinWidth){at=ay.horizontalDragMinWidth}}i.width(at+"px");k=m-at;ad(Z)}if(az){B=Math.ceil(1/r*u);if(B>ay.verticalDragMaxHeight){B=ay.verticalDragMaxHeight}else{if(B<ay.verticalDragMinHeight){B=ay.verticalDragMinHeight}}au.height(B+"px");j=u-B;ac(I)}}function ak(aJ,aL,aI,s){var aN="before",aK="after",aM;if(aL=="os"){aL=/Mac/.test(navigator.platform)?"after":"split"}if(aL==aN){aK=aL}else{if(aL==aK){aN=aL;aM=aI;aI=s;s=aM}}aJ[aN](aI)[aK](s)}function aD(aI,s,aJ){return function(){H(aI,s,this,aJ);this.blur();return false}}function H(aL,aK,aO,aN){aO=b(aO).addClass("jspActive");var aM,aJ,aI=true,s=function(){if(aL!==0){P.scrollByX(aL*ay.arrowButtonSpeed)}if(aK!==0){P.scrollByY(aK*ay.arrowButtonSpeed)}aJ=setTimeout(s,aI?ay.initialDelay:ay.arrowRepeatFreq);aI=false};s();aM=aN?"mouseout.jsp":"mouseup.jsp";aN=aN||b("html");aN.bind(aM,function(){aO.removeClass("jspActive");aJ&&clearTimeout(aJ);aJ=null;aN.unbind(aM)})}function q(){x();if(az){ap.bind("mousedown.jsp",function(aN){if(aN.originalTarget===c||aN.originalTarget==aN.currentTarget){var aL=b(this),aO=aL.offset(),aM=aN.pageY-aO.top-I,aJ,aI=true,s=function(){var aR=aL.offset(),aS=aN.pageY-aR.top-B/2,aP=w*ay.scrollPagePercent,aQ=j*aP/(Y-w);if(aM<0){if(I-aQ>aS){P.scrollByY(-aP)}else{U(aS)}}else{if(aM>0){if(I+aQ<aS){P.scrollByY(aP)}else{U(aS)}}else{aK();return}}aJ=setTimeout(s,aI?ay.initialDelay:ay.trackClickRepeatFreq);aI=false},aK=function(){aJ&&clearTimeout(aJ);aJ=null;b(document).unbind("mouseup.jsp",aK)};s();b(document).bind("mouseup.jsp",aK);return false}})}if(aE){G.bind("mousedown.jsp",function(aN){if(aN.originalTarget===c||aN.originalTarget==aN.currentTarget){var aL=b(this),aO=aL.offset(),aM=aN.pageX-aO.left-Z,aJ,aI=true,s=function(){var aR=aL.offset(),aS=aN.pageX-aR.left-at/2,aP=aj*ay.scrollPagePercent,aQ=k*aP/(S-aj);if(aM<0){if(Z-aQ>aS){P.scrollByX(-aP)}else{V(aS)}}else{if(aM>0){if(Z+aQ<aS){P.scrollByX(aP)}else{V(aS)}}else{aK();return}}aJ=setTimeout(s,aI?ay.initialDelay:ay.trackClickRepeatFreq);aI=false},aK=function(){aJ&&clearTimeout(aJ);aJ=null;b(document).unbind("mouseup.jsp",aK)};s();b(document).bind("mouseup.jsp",aK);return false}})}}function x(){if(G){G.unbind("mousedown.jsp")}if(ap){ap.unbind("mousedown.jsp")}}function aw(){b("html").unbind("dragstart.jsp selectstart.jsp mousemove.jsp mouseup.jsp mouseleave.jsp");if(au){au.removeClass("jspActive")}if(i){i.removeClass("jspActive")}}function U(s,aI){if(!az){return}if(s<0){s=0}else{if(s>j){s=j}}if(aI===c){aI=ay.animateScroll}if(aI){P.animate(au,"top",s,ac)}else{au.css("top",s);ac(s)}}function ac(aI){if(aI===c){aI=au.position().top}al.scrollTop(0);I=aI;var aL=I===0,aJ=I==j,aK=aI/j,s=-aK*(Y-w);if(ai!=aL||aG!=aJ){ai=aL;aG=aJ;D.trigger("jsp-arrow-change",[ai,aG,O,l])}v(aL,aJ);X.css("top",s);D.trigger("jsp-scroll-y",[-s,aL,aJ]).trigger("scroll")}function V(aI,s){if(!aE){return}if(aI<0){aI=0}else{if(aI>k){aI=k}}if(s===c){s=ay.animateScroll}if(s){P.animate(i,"left",aI,ad)
}else{i.css("left",aI);ad(aI)}}function ad(aI){if(aI===c){aI=i.position().left}al.scrollTop(0);Z=aI;var aL=Z===0,aK=Z==k,aJ=aI/k,s=-aJ*(S-aj);if(O!=aL||l!=aK){O=aL;l=aK;D.trigger("jsp-arrow-change",[ai,aG,O,l])}t(aL,aK);X.css("left",s);D.trigger("jsp-scroll-x",[-s,aL,aK]).trigger("scroll")}function v(aI,s){if(ay.showArrows){aq[aI?"addClass":"removeClass"]("jspDisabled");ae[s?"addClass":"removeClass"]("jspDisabled")}}function t(aI,s){if(ay.showArrows){ax[aI?"addClass":"removeClass"]("jspDisabled");y[s?"addClass":"removeClass"]("jspDisabled")}}function L(s,aI){var aJ=s/(Y-w);U(aJ*j,aI)}function M(aI,s){var aJ=aI/(S-aj);V(aJ*k,s)}function aa(aU,aP,aJ){var aN,aK,aL,s=0,aT=0,aI,aO,aR,aQ,aS;try{aN=b(aU)}catch(aM){return}aK=aN.outerHeight();aL=aN.outerWidth();al.scrollTop(0);al.scrollLeft(0);while(!aN.is(".jspPane")){s+=aN.position().top;aT+=aN.position().left;aN=aN.offsetParent();if(/^body|html$/i.test(aN[0].nodeName)){return}}aI=aA();aO=aI+w;if(s<aI||aP){aQ=s-ay.verticalGutter}else{if(s+aK>aO){aQ=s-w+aK+ay.verticalGutter}}if(aQ){L(aQ,aJ)}viewportLeft=aC();aR=viewportLeft+aj;if(aT<viewportLeft||aP){aS=aT-ay.horizontalGutter}else{if(aT+aL>aR){aS=aT-aj+aL+ay.horizontalGutter}}if(aS){M(aS,aJ)}}function aC(){return -X.position().left}function aA(){return -X.position().top}function af(){al.unbind(ab).bind(ab,function(aL,aM,aK,aI){var aJ=Z,s=I;P.scrollBy(aK*ay.mouseWheelSpeed,-aI*ay.mouseWheelSpeed,false);return aJ==Z&&s==I})}function o(){al.unbind(ab)}function aB(){return false}function J(){X.find(":input,a").unbind("focus.jsp").bind("focus.jsp",function(s){aa(s.target,false)})}function E(){X.find(":input,a").unbind("focus.jsp")}function R(){var s,aI;X.focus(function(){D.focus()});D.attr("tabindex",0).unbind("keydown.jsp keypress.jsp").bind("keydown.jsp",function(aM){if(aM.target!==this){return}var aL=Z,aK=I;switch(aM.keyCode){case 40:case 38:case 34:case 32:case 33:case 39:case 37:s=aM.keyCode;aJ();break;case 35:L(Y-w);s=null;break;case 36:L(0);s=null;break}aI=aM.keyCode==s&&aL!=Z||aK!=I;return !aI}).bind("keypress.jsp",function(aK){if(aK.keyCode==s){aJ()}return !aI});if(ay.hideFocus){D.css("outline","none");if("hideFocus" in al[0]){D.attr("hideFocus",true)}}else{D.css("outline","");if("hideFocus" in al[0]){D.attr("hideFocus",false)}}function aJ(){var aL=Z,aK=I;switch(s){case 40:P.scrollByY(ay.keyboardSpeed,false);break;case 38:P.scrollByY(-ay.keyboardSpeed,false);break;case 34:case 32:P.scrollByY(w*ay.scrollPagePercent,false);break;case 33:P.scrollByY(-w*ay.scrollPagePercent,false);break;case 39:P.scrollByX(ay.keyboardSpeed,false);break;case 37:P.scrollByX(-ay.keyboardSpeed,false);break}aI=aL!=Z||aK!=I;return aI}}function Q(){D.attr("tabindex","-1").removeAttr("tabindex").unbind("keydown.jsp keypress.jsp")}function C(){if(location.hash&&location.hash.length>1){var aJ,aI;try{aJ=b(location.hash)}catch(s){return}if(aJ.length&&X.find(location.hash)){if(al.scrollTop()===0){aI=setInterval(function(){if(al.scrollTop()>0){aa(location.hash,true);b(document).scrollTop(al.position().top);clearInterval(aI)}},50)}else{aa(location.hash,true);b(document).scrollTop(al.position().top)}}}}function ah(){b("a.jspHijack").unbind("click.jsp-hijack").removeClass("jspHijack")}function n(){ah();b("a[href^=#]").addClass("jspHijack").bind("click.jsp-hijack",function(){var s=this.href.split("#"),aI;if(s.length>1){aI=s[1];if(aI.length>0&&X.find("#"+aI).length>0){aa("#"+aI,true);return false}}})}function an(){var aJ,aI,aL,aK,aM,s=false;al.unbind("touchstart.jsp touchmove.jsp touchend.jsp click.jsp-touchclick").bind("touchstart.jsp",function(aN){var aO=aN.originalEvent.touches[0];aJ=aC();aI=aA();aL=aO.pageX;aK=aO.pageY;aM=false;s=true}).bind("touchmove.jsp",function(aQ){if(!s){return}var aP=aQ.originalEvent.touches[0],aO=Z,aN=I;P.scrollTo(aJ+aL-aP.pageX,aI+aK-aP.pageY);aM=aM||Math.abs(aL-aP.pageX)>5||Math.abs(aK-aP.pageY)>5;return aO==Z&&aN==I}).bind("touchend.jsp",function(aN){s=false}).bind("click.jsp-touchclick",function(aN){if(aM){aM=false;return false}})}function h(){var s=aA(),aI=aC();
D.removeClass("jspScrollable").unbind(".jsp");D.replaceWith(ao.append(X.children()));ao.scrollTop(s);ao.scrollLeft(aI)}b.extend(P,{reinitialise:function(aI){aI=b.extend({},ay,aI);ar(aI)},scrollToElement:function(aJ,aI,s){aa(aJ,aI,s)},scrollTo:function(aJ,s,aI){M(aJ,aI);L(s,aI)},scrollToX:function(aI,s){M(aI,s)},scrollToY:function(s,aI){L(s,aI)},scrollToPercentX:function(aI,s){M(aI*(S-aj),s)},scrollToPercentY:function(aI,s){L(aI*(Y-w),s)},scrollBy:function(aI,s,aJ){P.scrollByX(aI,aJ);P.scrollByY(s,aJ)},scrollByX:function(s,aJ){var aI=aC()+s,aK=aI/(S-aj);V(aK*k,aJ)},scrollByY:function(s,aJ){var aI=aA()+s,aK=aI/(Y-w);U(aK*j,aJ)},positionDragX:function(s,aI){V(s,aI)},positionDragY:function(aI,s){V(aI,s)},animate:function(aI,aL,s,aK){var aJ={};aJ[aL]=s;aI.animate(aJ,{duration:ay.animateDuration,ease:ay.animateEase,queue:false,step:aK})},getContentPositionX:function(){return aC()},getContentPositionY:function(){return aA()},getContentWidth:function(){return S()},getContentHeight:function(){return Y()},getPercentScrolledX:function(){return aC()/(S-aj)},getPercentScrolledY:function(){return aA()/(Y-w)},getIsScrollableH:function(){return aE},getIsScrollableV:function(){return az},getContentPane:function(){return X},scrollToBottom:function(s){U(j,s)},hijackInternalLinks:function(){n()},destroy:function(){h()}});ar(N)}f=b.extend({},b.fn.jScrollPane.defaults,f);b.each(["mouseWheelSpeed","arrowButtonSpeed","trackClickSpeed","keyboardSpeed"],function(){f[this]=f[this]||f.speed});var e;this.each(function(){var g=b(this),h=g.data("jsp");if(h){h.reinitialise(f)}else{h=new d(g,f);g.data("jsp",h)}e=e?e.add(g):g});return e};b.fn.jScrollPane.defaults={showArrows:false,maintainPosition:true,clickOnTrack:true,autoReinitialise:false,autoReinitialiseDelay:500,verticalDragMinHeight:0,verticalDragMaxHeight:99999,horizontalDragMinWidth:0,horizontalDragMaxWidth:99999,animateScroll:false,animateDuration:300,animateEase:"linear",hijackInternalLinks:false,verticalGutter:4,horizontalGutter:4,mouseWheelSpeed:0,arrowButtonSpeed:0,arrowRepeatFreq:50,arrowScrollOnHover:false,trackClickSpeed:0,trackClickRepeatFreq:70,verticalArrowPositions:"split",horizontalArrowPositions:"split",enableKeyboardNavigation:true,hideFocus:false,keyboardSpeed:0,initialDelay:300,speed:30,scrollPagePercent:0.8}})(jQuery,this);



/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 * Thanks to: Seamus Leahy for adding deltaX and deltaY
 *
 * Version: 3.0.4
 * 
 * Requires: 1.2.2+
 */

(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener ) {
            for ( var i=types.length; i; ) {
                this.addEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = handler;
        }
    },
    
    teardown: function() {
        if ( this.removeEventListener ) {
            for ( var i=types.length; i; ) {
                this.removeEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = null;
        }
    }
};

$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});


function handler(event) {
    var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
    event = $.event.fix(orgEvent);
    event.type = "mousewheel";
    
    // Old school scrollwheel delta
    if ( event.wheelDelta ) { delta = event.wheelDelta/120; }
    if ( event.detail     ) { delta = -event.detail/3; }
    
    // New school multidimensional scroll (touchpads) deltas
    deltaY = delta;
    
    // Gecko
    if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
        deltaY = 0;
        deltaX = -1*delta;
    }
    
    // Webkit
    if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
    if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
    
    // Add event and delta to the front of the arguments
    args.unshift(event, delta, deltaX, deltaY);
    
    return $.event.handle.apply(this, args);
}

})(jQuery);
