$(document).ready(
	function() {
		initRollovers();
		initImgRotator();
		initMenuNav();
	}
)

function initRollovers() {
	$('.rollover').mouseover(
		function() { mouseOver( $(this) ) }
	 ).mouseout (
		function() { mouseOut( $(this) ) }
	 )
}


function mouseOver(obj) {
	var _this = $(obj);
	var src = _this.attr('src');
	var ext = src.match(/.[\w]+$/);
	var newSrc = src.replace(ext, '_on' + ext);
	_this.attr('src', newSrc);
}



function mouseOut(obj) {
	var _this = $(obj);
	var src = _this.attr('src');
	var ext = src.match(/.[\w]+$/);
	var newSrc = src.replace('_on' + ext, ext);
	_this.attr('src', newSrc);
}


function initMenuNav() {
	// quick ref
	var qMenuNav = $('#menu-nav img');
	
	// if menu navigation exists, attach events
	if( qMenuNav.size() > 0 ) {
	
		// for each item, attach mouse events, add index value to id, and then add a click event
		qMenuNav.each(
			
			function (index) {

				// quick reference to the navigation image
				var _this = $(this);

				// make the first item active
				if (index == 0 ) {
					mouseOver( _this );
					_this.addClass('active');
					$( $('#menu').find('img').get(0) ).addClass('active');
					$('#menu').html('<img src="images/' + _this.attr('menu') + '" />');

				}
				
				_this.mouseover(
					function() { 
						var _this = $(this);
						if( _this.attr('src').indexOf('_on') == -1) {
							mouseOver( $(this) ) 
						}
					}
					
				).mouseout(
					function() { 
						var _this = $(this);
						if( $(this).attr('className') != 'active' ) {
							mouseOut( $(this) );
						}
					}
				).attr(
					// id = index; the order of items is related to the other of menu images
					// if item #1 is thali, then the first image in #menu should be that image
					{id: index} 
				).click(
					function() { 
						// ...assuming the order of list items corresponds to the order of 
						// menu images in #menu, then the id of this image represents the one
						// that needs to be shown
						//$('#menu .active').removeClass('active');
						var qFormer = $('#menu-nav .active');
						qFormer.removeClass('active');
						mouseOut(qFormer);
						
						// insert corresponding menu img
						$( $('#menu').find('img').get(this.id) ).addClass('active');
						var imgSrc = $(this).attr('menu');
						$('#menu').html('<img src="images/' + imgSrc + '" />');
						// highlite this button
						$(this).addClass('active');
						return false;
					}
				)
			}			
			
			
		)
	}
}

function initImgRotator() {
	// get all the anchors in the thumbs UL
	var qThumbs = $('#thumbs a');
	
	if( qThumbs.size() > 0 ) {

		// store references to these elements in window so subsequent searches aren't necessary
		window.qDisplay = $('#display span');
		window.qCaption = $('#caption');
		
		// photo series starts at 1
		window.activeIndex = 1;

		// if the display panel and thumbs aren't located, dont attempt to attach events
		if( qThumbs && qDisplay) {
		
			// first anchor in series
			var qFirst = qThumbs.get(0);
			
			// last anchor in series
			var qLast = $(qThumbs).get( qThumbs.size()-1 );
			
			// class as last to remove right margin
			$(qLast).addClass('last');
			
			// apply orange border 
			$(qFirst).addClass('active');
			
			qThumbs.find('img').click(
				function() {
					// if an element was previously active, restore its default state
					$('.active', '#thumbs').removeClass('active');
					
					// determine the index of this thumbnail, assuming the naming convention: thumb_{n}.jpg
					var src = $(this).attr('src');
					var index = src.substring( 
						src.indexOf('thumb_')+ 6, 
						src.indexOf('.jpg')
					);
					var caption = $(this).attr('alt');
					
					// apply active class to containing anchor
					$(this).parent().addClass('active');
					
					// dump the contents of the display panel and replace it with the new image
					window.qDisplay.html('<img src="images/photo_' + index + '.jpg">');
					window.qCaption.html(caption);
					return false;
				}
			)

			qThumbs.focus(
				function() {
					$(this).blur();
				}
			)
		}
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_nbGroup(event, grpName) { //v6.0
var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])?args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) { img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr) for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
