/* Author: 
	Elvedin Terzic
*/

var photoArray = new Array();
photoArray.push("img/live/1");
photoArray.push("img/live/2");
photoArray.push("img/live/3");
photoArray.push("img/live/4");
photoArray.push("img/live/5");

for (i=1; i<41; i++){
	if(i<10){
		photoArray.push("img/all/photo0"+i);
	} else {
		photoArray.push("img/all/photo"+i);
	}
}

var currentPage = 1;
var lastPage = Math.round(photoArray.length/4);

function nextPage(){
	if(currentPage < lastPage){
		bufferPage(currentPage + 1, flipPage);
		currentPage++;
	}
}

function previousPage(){
	if(currentPage > 1){
		bufferPage(currentPage, flipPage);
		currentPage--;
	}
}

function bufferPage(page, callback){
	if (typeof page == 'undefined') {
		page = currentPage;
	}
	pageindex = page - 1;
	pageindex = 4*pageindex+1;
	$('.content #visual .subsection#photo .nextpage img.first').attr('src', photoArray[pageindex]+"s.jpg");
	$('.content #visual .subsection#photo .nextpage img.second').attr('src', photoArray[pageindex+1]+"s.jpg");
	$('.content #visual .subsection#photo .nextpage img.third').attr('src', photoArray[pageindex+2]+"s.jpg");
	$('.content #visual .subsection#photo .nextpage img.fourth').attr('src', photoArray[pageindex+3]+"s.jpg");
	callback();
}

function flipPage(){
	$('.content #visual .subsection#photo .currentpage').hide(0,
		function(){
			$('.content #visual .subsection#photo .nextpage').show(500);
			$('.content #visual .subsection#photo .currentpage').addClass('previouspage').removeClass('currentpage');
			$('.content #visual .subsection#photo .nextpage').addClass('currentpage').removeClass('nextpage');
			$('.content #visual .subsection#photo .previouspage').addClass('nextpage').removeClass('previouspage');
		}
	);
}

function randomizeFlickering(){
	flickerLight($('#light1'), Math.floor(Math.random()*3));
	flickerLight($('#light2'), Math.floor(Math.random()*3));
	flickerLight($('#light3'), Math.floor(Math.random()*3));
	flickerLight($('#light4'), Math.floor(Math.random()*3));
}

function flickerLight(burn, type){
	burn.delay((Math.floor(Math.random()*10))*1000);
//flickerLight($('#lightout'));
	if(type == 0){
		burn.show(1).delay(50).hide(1).delay(500).show(1).delay(50).hide(1).delay(50).show(1).delay(50).hide(1).delay(50).show(1).delay(50).hide(1);
	} else if(type == 1){
		burn.show(1).delay(50).hide(1).delay(500).show(1).delay(50).hide(1);
	} else if(type == 2){
		burn.show(1).delay(50).hide(1);
	}
}

function hoverOut(){
	
}
$(document).ready(function(){
	$.ajaxSetup({ cache: false });

	$('.album div.band').hover(
		function(){
			$('.album #veil').stop().show();
			$('.album div.band').addClass('hovered');
			if($(this).attr('id') == "daniel1"){
				$('.album #header img').attr('src', 'img/visuals.png');
			} else if($(this).attr('id') == "daniel2"){
				$('.album #header img').attr('src', 'img/buy.png');
			} else if($(this).attr('id') == "graeme1"){
				$('.album #header img').attr('src', 'img/music.png');
			} else if($(this).attr('id') == "graeme2"){
				$('.album #header img').attr('src', 'img/news.png');
			} else if($(this).attr('id') == "matt1"){
				$('.album #header img').attr('src', 'img/band.png');
			} else if($(this).attr('id') == "matt2"){
				$('.album #header img').attr('src', 'img/comments.png');
			} /* else if($(this).attr('id') == "matt3"){
				$('.album #header').text('SECTION 7');
			}*/ else if($(this).attr('id') == "gandd3"){
				$('.album #header img').attr('src', 'img/shows.png');
			} 
		},
		function(){
			$('.album #veil').stop().hide();
			$('.album div.band').removeClass('hovered');
			$('.album #header img').attr('src', 'img/blank.png');
		}
	);
	$('.album .band').click(
		function(){
			//$('.album div.band').removeClass('clicked');
			//$(this).addClass('clicked');
			if($(this).attr('id') != "matt3"){
				$('.content').show();
				$('#contentunderlay').show();
			}
			if($(this).attr('id') == "daniel1"){
				$('.section').hide();
				$('.section#visual').show();
			} else if($(this).attr('id') == "daniel2"){
				$('.section').hide();
				$('.section#buy').show();
			} else if($(this).attr('id') == "graeme1"){
				$('.section').hide();
				$('.section#music').show();
			} else if($(this).attr('id') == "graeme2"){
				$('.section').hide();
				$('.section#news').show();
			} else if($(this).attr('id') == "matt1"){
				$('.section').hide();
				$('.section#band').show();
			} else if($(this).attr('id') == "matt2"){
				$('.section').hide();
				$.get('comments.txt', function(data) {
					$('div.section#comments .subsection .result').html(data);
				});
				$('.section#comments').show();
			} else if($(this).attr('id') == "matt3"){
				$('.section').hide();
				$('.yt_lightbox iframe').attr('src', 'http://www.youtube.com/embed/avYRmb1ilxM');
				$('#overlay').show();
				$('.yt_lightbox').show();
			} else if($(this).attr('id') == "gandd3"){
				$('.section').hide();
				$('.section#shows').show();
			}
		}
	);
	$('a#contact').click(
		function(){
			$('.social .contact').show();
			return false;
		}
	);
	$('.player a#hideplayer').click(
		function(){
			$('.player').hide();
			return false;
		}
	);
	$('.content #visual .subsection#video a').click(
		function(){
			$('.yt_lightbox iframe').attr('src', 'http://www.youtube.com/embed/'+$(this).attr('id'));
			$('#overlay').show();
			$('.yt_lightbox').show();
			return false;
		}
	);
	$('.content #visual .subsection#photo img').click(
		function(){
			src = $(this).attr('src');
			lastletter = src.indexOf(".jpg")-1;
			if(src.charAt(lastletter) == "s"){
				src = src.slice(0, lastletter) + src.slice(lastletter+1);
			}
			$('.photo_lightbox img').attr('src', src);
			$('#overlay').show();
			$('.photo_lightbox').show();
			return false;
		}
	);
	$('.yt_lightbox a#hidevideo').click(
		function(){
			$('.yt_lightbox').hide();
			$('#overlay').hide();
			return false;
		}
	);
	$('.photo_lightbox a#hidephoto').click(
		function(){
			$('.photo_lightbox').hide();
			$('#overlay').hide();
			return false;
		}
	);

	$('div.section#comments form input.submit').click(
		function(){
			$(this).attr('disabled', 'disabled');
			$.post("comment.php", { 
				comment: $('input#comment').val()
			}, 
			function(data) {
				$('div.section#comments form input#comment').val('');
				$('div.section#comments .subsection .result').html(data);
			}
			).error(function() { alert("There was an error processing your application."); });
			$(this).removeAttr('disabled');
			return false;
		}
	);
	$('input#comment').focus(
		function(){
			if($(this).val() == "Say what you will and hit Submit. -Recordbreaker"){
				$(this).val('');
			}
		}
	);
	$('input#comment').blur(
		function(){
			if($(this).val() == ""){
				$(this).val('Say what you will and hit Submit. -Recordbreaker');
			}
		}
	);
	$('.content .hide a').click(
		function(){
			$('.content').stop().hide();
			$('#contentunderlay').hide();
			return false;
		}
	);
	$('#footer .nav li a').click(
		function(){
			$('.album .band#'+$(this).attr('class')).click();
		}
	);
	$('#photo.subsection a#nextphoto').click(
		function(){
			nextPage();
			return false;
		}
	);
	$('#photo.subsection a#prevphoto').click(
		function(){
			previousPage();
			return false;
		}
	);

	setInterval("randomizeFlickering()", 15000);
});
