// JavaScript Document

function updateWindow(message) {
	$('div#updateMessage').text(message);
	$('div#updateWindow').slideDown(400,function(){
		slide = setTimeout(function(){ $('div#updateWindow').slideUp(400); },3000);											 
	})		
	.click(function(){
		$(this).slideUp(300);
		clearTimeout(slide);
	});	
}

function setPlayer(src){
  $('#rightCol embed').remove();
  $('<embed>').attr({ src:'/flash/dinerplayer.swf?auto=false&vid='+src, quality:'high', allowScriptAccess:'sameDomain', allowFullScreen:'false', type:'application/x-shockwave-flash', pluginspace:'http://www.adobe.com/go/getflashplayer' }).css({ width:'310px', height:'214px' }).prependTo('#rightCol');
   
}

function getMovies(){
  $.get('/includes/_getMovies.php',
		{},
		function(data){
		  $.each(data,function(x,obj){
		    if(!(x%6)){
			  $('.working').removeClass('working');
			  $('<div>').addClass('working').appendTo('#slides');	
			  $('<span>').addClass('jFlowControl').text(x).appendTo('#controller');
			}
				$('<img>').attr('src','/flash/video/thumbs/'+obj.thumb).addClass('vidThumb').appendTo('.working').click(function(){
					$('.vidThumb.current').removeClass('current');
					setPlayer('video/'+obj.path);
					$(this).addClass('current');
				});
		  });
		  $('#controller').jFlow({ 
					slides: '#slides',
					width: '310px',
					height: '119px'
				});
		  $('.vidThumb:eq(0)').click();
		},
  'json');
}
function changePic(images,current){ // Changes the viewer image and resets the rotation timer
  $('#viewerImg').fadeOut(300,function(){
		rotateTimer();
		$(this).attr('src','/images/homePgImgs/'+images[current]).fadeIn();
		$('#viewerControls').stop(false,true);
  });
}
function rotateTimer(){  // Sets the image rotation countdown
  $('#arrowRight').stop();
  $('#arrowRight').animate({ opacity: 1.0 },8000,function(){
		$(this).click();
  });
}
function createImgViewer(){	// Gets all images from the 'homePgImgs' folder and creates a slideshow from them.
  $.get('/includes/_getHomePgImgs.php',
		{},
		function(data){
		  var images = Array();
		  for(x = 0; x < data.length; x++){
		    images.push(data[x]);
		  }
		  images.reverse();
		  // Creates image viewer and popup controls
		  $('<img>').attr({ src:'/images/homePgImgs/'+images[0], id: 'viewerImg' }).appendTo('#imgBox');			
		  $('<div>').attr({ id: 'viewerControls' }).appendTo('#imgBox');
		  $('<img>').attr({ src:'/images/arrowR.png', id:'arrowRight'}).addClass('arrows').appendTo('#viewerControls');
		  $('<img>').attr({ src:'/images/arrowL.png', id:'arrowLeft'}).addClass('arrows').appendTo('#viewerControls');
		  		  
		  $('#imgBox').hover(function(){
				$('#viewerControls').stop(false,true).animate({
					bottom: '0px'						
				}); 
			  }, function(){
				$('#viewerControls').stop(false,true).animate({
					bottom:'-66px'						
				}, 1200, 'easeOutBounce');  
			});
		  
		  
		  // Preloads images and attaches 'click' events to the 'next' and 'previous' arrows in the popup controls
		  var current = 0;
		  var length = images.length;
		  $('<img>').attr('src','/images/homePgImgs/'+images[current+1]);
		  $('<img>').attr('src','/images/homePgImgs/'+images[length-1]);
		  $('#arrowRight').unbind().click(function(){
			current = (current+1 < length) ? current+1:0;
			changePic(images,current);
			$('<img>').attr('src','/images/homePgImgs/'+images[current+1]);
		  });
		  $('#arrowLeft').unbind().click(function(){
			current = (current-1 >= 0) ? current-1:length-1;
			changePic(images,current);
			$('<img>').attr('src','/images/homePgImgs/'+images[current-1]);
		  });
		  
		  // Starts the image switch timer after the viewer is loaded
		  rotateTimer();
		},
  'json');
}

function getSpecials(){ // Retrieves specials from the database and displays them on the page
  $('<p>').attr({ id: 'breakfast'}).addClass('meals').appendTo('#specials');
  $('<p>').attr({ id: 'lunch'}).addClass('meals').appendTo('#specials');
  $('<p>').attr({ id: 'dinner'}).addClass('meals').appendTo('#specials');
  $.get('/includes/_getSpecials.php',
		{},
		function(data){
		  for(x = 0; x < data.length; x++){
		    var meal = data[x].meal;
			$('#'+meal).text(data[x].name);
		  }
		},
  'json');			
}

function showPage(page){
	page = page - 1; // allows for incoming argument to be 1 based rather than 0 based
	var count = $('#showsSidebar .show').size();
	var itemsPerPage = 3 // items to display per page
	var start = page * itemsPerPage; // starting item for the display range
	var finish = start + itemsPerPage; // terminating item for the display range
	var pages = Math.floor(count/itemsPerPage); // number of pages, uses to offset 1 based argument
	var sidebarNext = $('#sidebarNext');
	var sidebarPrev = $('#sidebarPrev');
	
	$('#showsSidebar .show:hidden, #showsSidebar .show:visible').hide().filter(function(index){
		return index >= start && index < finish;																										 
	}).fadeIn(800);
			
		
	switch(page){
		case pages-1:
			sidebarNext.css('visibility','hidden');
			sidebarPrev.css('visibility','visible');
			break;
		case 0:
			sidebarPrev.css('visibility','hidden');
			sidebarNext.css('visibility','visible');
			break;
		default:
			sidebarNext.add(sidebarPrev).css('visibility','visible');
			break;
	}

	sidebarNext.unbind().click(function(){
		showPage(page + 2); // uses +2 instead of +1 to overcome the 1 based argument
	});								 
	sidebarPrev.unbind().click(function(){
		showPage(page); // does not require -1 because of the 1 based argument
	});
	
	$('body').animate({ zindex: 1 }, 8000, function(){
		if(page < pages-1){
			showPage(page+2)
		} else {
			showPage(1);
		}
	});
}


function getBands(){
  $.get('/includes/_getBands.php',
		{ records: 100 },
		function(data){
		  for(x = 2; x < data.length; x++){
				if(x < 5){
				$('<div>').addClass('show').appendTo('#showsContent');
				$('<div>').addClass('showDate').html('<strong>'+data[x].day+'</strong>, '+data[x].date+' - '+data[x].time).appendTo('.show:last-child');
				$('<img>').addClass('showImg').attr({ src: '/images/bands/'+data[x].picture }).appendTo('.show:last-child');
				$('<h4>').addClass('showName').text(data[x].name).appendTo('.show:last-child');
				$('<p>').addClass('showDescription').text(data[x].description).appendTo('.show:last-child');
				$('<a>').addClass('showLink').attr({ href: data[x].website, target: '_blank'}).text('Visit Website').appendTo('.show:last-child');
					} else {
						$('<div>').addClass('show').appendTo('#showsSidebar');
						$('<div>').addClass('showDate').html('<strong>'+data[x].day+'</strong> - '+data[x].date).appendTo('.show:last-child');
						$('<h5>').addClass('showName').text(data[x].name).appendTo('.show:last-child');
					}
				}
				showPage(1);
		},
  'json');
}

function getAlbum(){
  $.get('/includes/_getAlbum.php',
		{},
		function(data){
		  for(x = 0; x < data.length; x++){
		    $('<a>').attr({ 'href':'/images/gallery/'+data[x].src, 'title':data[x].name, 'rel':'gal' }).addClass('fancy').appendTo('#galleryThumbs');
				$('<img>').attr('src','/images/gallery/thumbs/'+data[x].src).addClass('thumb').appendTo('.fancy:last-child');
		  }
		  $('a.fancy').fancybox({
		    'transitionIn'	:	'elastic',
		    'transitionOut'	:	'elastic',
		    'speedIn'		:	600, 
		    'speedOut'		:	200, 
		    'overlayShow'	:	true
      });
 		},
  'json');
}

// Admin scripts

function updateSpecials(){
  var values = $('#editSpecials').serialize();
  $.post('/includes/_updateSpecials.php',
		 values
  );
  return 'Updated Successfully';
}

function getBand(id){
  $.get('../includes/_getBand.php',
		{ id: id },
		function(data){
		  $('#addShow input[name="id"]').attr('value',data.id);
		  $('#addShow input[name="name"]').attr('value',data.name);
		  $('#addShow input[name="website"]').attr('value',data.link);
		  $('#addShow input[name="date"]').attr('value',data.date);
		  $('#addShow textarea').text(data.description);
		  $('#showHour option[value="'+data.hour+'"]').attr('selected','selected');
		  $('#showMinute option[value="'+data.minute+'"]').attr('selected','selected');
		  $('#showPeriod option[value="'+data.period+'"]').attr('selected','selected');
		},
  'json');
}

function addTimes(){
  for(x = 0; x < 12; x++){
	if(x == 0){
	  var hour = 12;
	} else if(x < 10){
	  var hour = '0'+x;
	} else {
	  var hour = x;	
	}
	$('<option>').attr({ value: hour }).text(hour).appendTo('#showHour');
  }  
  for(x = 0; x < 61; x++){
	var minute = x < 10 ? '0'+x:x;    
	$('<option>').attr({ value: minute}).text(minute).appendTo('#showMinute');
  }  
}

function deleteBand(e){
  var band = e.nextAll('.showName').text();
  var id = e.parent().attr('alt');
  var del = confirm('Are you sure you wish to delete: '+ band +'?');

  if(del){
    $.post('/admin/includes/_deleteBand.php',
		   { id:id },
		   function(data){
			 e.parent().fadeOut('slow');
			 $('#addShow').clear();
		   });
  }
}
