var speed = 5000;
var speedChange = 1250;
var current_image = 1;
var SlideInterval = 1;
var type;
var load_image = 0;
var nxpic;
var speed_width = 50;
var searchString = '';
var albumID;
var maxPics = 20; 
var title;
var collectionTitle;
var collectionID = '';
var albumTitle;
var pageNum = 0;
var totalPics = 0;
var pagePics = 0;

$(document).ready(function(){   
    $(document).bind("contextmenu",function(e){   
        return false;   
    });   
}); 

function photoHome() {
	searchString = '';
	albumID = '';
	albumTitle = '';
	pageNum = 0;
	collectionTitle = '';
	collectionID = '';
	$('#Search').val('Search');
	$('#tdPhotos').hide();
	$('#tdSearch').hide();
	$('#trCollections').show();
	$('#error_message').hide();
	$('#trAlbums').hide();
	$('#trAlbum').hide();
	$('#tdAlbum_Name').hide();
	$('#tdCollection_Name').hide();	
	document.title = "RedbirdCentral.com - Photos";
}

function clearSearch() {
	searchString = '';
	$('#View_All').hide();
	$('#Search').val('Search');
	$('#tdSearch').hide();
	$('#error_message').hide();

	if(collectionID == '') {
		if(albumID == '%') {
			$('#trCollections').show();
			$('#trAlbum').hide();		
		}
		else {
			get_photos(albumID,albumTitle,'');
		}	
	}
	else {
		$('#trAlbums').show();
		$('#trAlbum').hide();	
	}
}

function preview(path,id) {	
	var browser=navigator.appName;	
	current_image = id;
	type = 'N';
	picNum = parseInt(current_image) + (pageNum*maxPics); 	
	
	$('#slideshow_time').html('');
	$('#footer').show(); 
	$('#footer2').hide(); 
	xpath =  path.replace('Thumb','Image');
	$('#Image_Preview').attr("src",xpath);
	$('#source').html($("#Thumb" + current_image).attr('title'));
 	$('#progress').html("<div align='center'> " + picNum + " of " +  $('#spnTotalImages').html() +  "</div>");
	
	if(picNum<= 1) {
		$('#previous').hide();
	}
	else {
		$('#previous').show();
	}
	
	if(picNum >= $('#spnTotalImages').html()) {
		$('#next').hide();
	}
	else {
		$('#next').show();
	}

	$('#prev').center().show();
	
	
	var browser=navigator.appName;
	if(browser == 'Microsoft Internet Explorer') {
		$("body").removeClass("background_image").addClass('darkenBackground');
	}
	
	if(path != '') {
		initImage();
	}
}

function previous() {	
	type = "P";
	current_image--;
	if(current_image < 1) {
		if(picNum > 2) {
			pageNum--;			
			current_image = maxPics+1;
			get_photos(albumID,albumTitle,collectionID);
			return;	
		}
		else {
			$('#hrefPrevious').href = ''; 
			return;
		}
	}
	picNum = parseInt(current_image) + (pageNum*maxPics);
	
	$('#progress').html("<div align='center'> " + picNum + " of " +  $('#spnTotalImages').html() +  "</div>");	
	id = "Thumb" + current_image;
	elementID = document.getElementById(id);
	nextPath = elementID.src;
	document['Image_Preview'].src = nextPath.replace('Thumb','Image');
	$('#source').html($("#Thumb" + current_image).attr('title'));
	initImage();	
	
	if(picNum <= 1) {
		$('#previous').hide();
	}	
	else {
		$('#previous').show();		
	}
	
	if(picNum >= $('#spnTotalImages').html()) {
		$('#next').hide();
	}	
	else {
		$('#next').show();		
	}	
}

function next() {
	type = "N";
	current_image++;
	picNum = parseInt(current_image) + (pageNum*maxPics);
	
	if(picNum > $('#spnTotalImages').html()) { 
		stopSlideshow();
		return;
	}
	
	if(current_image > maxPics) {	
		pageNum++;			
		current_image = 0;
		get_photos(albumID,albumTitle,collectionID);
		return;			
	}	
	
	$('#progress').html("<div align='center'> " + picNum + " of " +  $('#spnTotalImages').html() +  "</div>");
	id = "Thumb" + current_image;
	elementID = document.getElementById(id);
	nextPath = elementID.src;
	$('#Image_Preview').attr("src",nextPath.replace('Thumb','Image'));	
	$('#source').html($("#Thumb" + current_image).attr('title'));
	initImage();
	
	if(picNum >= $('#spnTotalImages').html()) { 
		$('#next').hide();
	}	
	else {
		$('#next').show();		
	}	
	
	if(picNum <= 1) {
		$('#previous').hide();
	}	
	else {
		$('#previous').show();		
	}
}

function next_page() {
	pageNum++;
	type = "N";
	current_image = 0;
	get_photos(albumID,albumTitle,collectionID);	
}


function previous_page() {
	pageNum--;
	type = "P";
	get_photos(albumID,albumTitle,collectionID);
	current_image = maxPics+1;	
}

function searchAlbum() {	
	if($('#Search').value != '') {
		$('#tdSearch').show();	
		$('#View_All').show();
	}
	else {
		$('#View_All').hide();
	}
	pageNum = 0;
	get_photos(albumID,'',collectionID);	
}

function slideshow() {
	var browser=navigator.appName;
	if(browser == 'Microsoft Internet Explorer') {
		document.body.className = 'darkenBackground';
	}
	
	current_image = 1;
	picNum = parseInt(current_image) + (pageNum*maxPics);
	seconds_remaining = Math.round(((speed/1000)* ($('#spnTotalImages').html() - picNum)) + (($('#spnTotalImages').html() - picNum)*.250)) ;
	minutes_remaining = Math.floor(seconds_remaining/60);
	seconds = seconds_remaining - (minutes_remaining * 60);
	if(seconds < 10) {
		seconds = "0" + seconds;
	}
	if(seconds_remaining > 0) {
		$('#slideshow_time').html("-" + minutes_remaining + ":" + seconds);
	}
	else {
		$('#slideshow_time').html('');
	}
	path = $('#Thumb1').attr("src");
	document['Image_Preview'].src = path.replace('Thumb','Image');	
	$('#source').html($('#Thumb1').attr("alt"));
	$('#prev').center().show();

	$('#footer2').show()
	$('#footer').hide(); 
	SlideInterval=setInterval("nextSlide()",speed);
	$('#progress').html("<div align='center'> " + 1 + " of " +  $('#spnTotalImages').html() +  "</div>");	
	
	if(pageNum != 0) {
		$('#Image_Preview').attr("src","");
		pageNum = 0;
		type = "S";
		get_photos(albumID,'',collectionID);
		return;
	}
}


function faster() {
	if(speed_width >= 100) {
		return;
	}
	speedChange = speedChange - 167;
	speed = speed - speedChange;
	if (speed < 1000) {
		speed = 1000
	}
	speed_width = speed_width + 10;
	if(speed_width > 100) {
		speed_width = 100;
	}
	clearInterval (SlideInterval);
	SlideInterval=setInterval("nextSlide()",speed);	
	$('#speed_bar').width(speed_width);
	
	picNum = parseInt(current_image) + (pageNum*maxPics);
	seconds_remaining = Math.round(((speed/1000)* ($('#spnTotalImages').html() - picNum)) + (($('#spnTotalImages').html() - picNum)*.250)) ;
	minutes_remaining = Math.floor(seconds_remaining/60);
	seconds = seconds_remaining - (minutes_remaining * 60);
	if(seconds < 10) {
		seconds = "0" + seconds;
	}
	if(seconds_remaining > 0) {
		$('#slideshow_time').html("-" + minutes_remaining + ":" + seconds);
	}
	else {
		$('#slideshow_time').html('');
	}
}

function slower() {
	if(speed_width < 10) {
		return;
	}
	speed = speed + speedChange;
	speedChange = speedChange + 167;

	if(speed > 13000) {
		speed = 13000;
	}
	speed_width = speed_width - 10;
	if(speed_width < 10) {
		speed_width = 1;
	}
	clearInterval (SlideInterval); 
	SlideInterval=setInterval("nextSlide()",speed);
	$('#speed_bar').width(speed_width);
	
	picNum = parseInt(current_image) + (pageNum*maxPics);
	seconds_remaining = Math.round(((speed/1000)* ($('#spnTotalImages').html() - picNum)) + (($('#spnTotalImages').html() - picNum)*.250)) ;
	minutes_remaining = Math.floor(seconds_remaining/60);
	seconds = seconds_remaining - (minutes_remaining * 60);
	if(seconds < 10) {
		seconds = "0" + seconds;
	}
	if(seconds_remaining > 0) {
		$('#slideshow_time').html("-" + minutes_remaining + ":" + seconds);
	}
	else {
		$('#slideshow_time').html('');
	}
}

function nextSlide() {	
	var pic=new Image();
	pic.src= document['Image_Preview'].src;	
	if(pic.complete == false) {
		return;
	}	
	
	current_image++;	
	picNum = parseInt(current_image) + (pageNum*maxPics);
	seconds_remaining = Math.round(((speed/1000)* ($('#spnTotalImages').html() - picNum)) + (($('#spnTotalImages').html() - picNum)*.250)) ;
	minutes_remaining = Math.floor(seconds_remaining/60);
	seconds = seconds_remaining - (minutes_remaining * 60);
	if(seconds < 10) {
		seconds = "0" + seconds;
	}
	if(seconds_remaining > 0) {
		$('#slideshow_time').html("-" + minutes_remaining + ":" + seconds);
	}
	else {
		$('#slideshow_time').html('');
	}
	if(current_image <= $('#spnTotalImages').html()) {
		if(current_image > $('#PagePhotos').val()) {		
			if(picNum > $('#spnTotalImages').html()) { 
				stopSlideshow();
				return;
			}
			else {
				pageNum++;			
				current_image = 0;
				type = 'S';
				get_photos(albumID,albumTitle,collectionID);	
				return;			
			}
		}
		id = "Thumb" + current_image;
		elementID = document.getElementById(id);
		path = elementID.src;
		$('#Image_Preview').attr("src",path.replace('Thumb','Image'));
		$('#source').html($("#Thumb" + current_image).attr('title'));
		$('#progress').html("<div align='center'> " + picNum + " of " +  $('#spnTotalImages').html() +  "</div>");
		initImage();		
	}
	else
	{
		current_image= 1;
		stopSlideshow();
	}
}

function stopSlideshow() {
	clearInterval (SlideInterval);
	$("#prev").fadeOut(1000,restorebg()); 	
}

function album() {
	pageNum = 0;
	get_album_info();
	get_photos(albumID,'',collectionID);
	
	for(i=1;i<=maxPics;i++) {
		$("#Thumb" + i).atr("src","");				
		$("#tdThumb" + i).hide();		
	}		
}

function restorebg() {
	$("body").removeClass("darkenBackground").addClass("background_image");
}

//Load all photos on page into slideshow
function load_next() {
	var pic=new Image();
	pic.src=$('#Image_Preview').attr("src");	
	if(pic.complete == false) {
		window.setTimeout("load_next()",speedChange);
		return;
	}
	
	a = "Thumb" + parseInt(load_image);
	b = document.getElementById(a);
	
	width = Math.round(parseInt(load_image)/parseInt($('#PagePhotos').val())*100);
	if(width > 100) {
		width = 100;
	}
	$('#progress_bar').width(width + "%");
	
	if(document.getElementById(a)) {
		nextPath = b.src;
		document['Next_Image'].src = nextPath.replace('Thumb','Image');	
		nxpic=new Image();
		nxpic.src= document['Next_Image'].src;
		if(nxpic.complete) {	
			if(type == 'N') {
				load_image++;
			}
			else {
				load_image--; 
			}
			load_next();
		}
		else {
			window.setTimeout("load_next()",speedChange);
		}
	}
	else {
		return;
	}	
}



function initImage() {	
	$('#tblImage').hide();
	$('#divLoading').center().show();
		
	$('#Image_Preview').load(function() {
        if(load_image <= current_image) {		  
			load_image = current_image;		
			if(type == 'N') {
				load_image++;			
			}
			else {
				load_image--;
			}		 
			load_next();
		}	 
		
		$('#divLoading').hide();	
		$('#tblImage').show();  
		$('#Image_Preview').fadeIn();
		ImageWidth = $("#Image_Preview").width();
		ImageHeight = $("#Image_Preview").height();
		document.getElementById("areaPrevious").coords = "0,0," + Math.round((ImageWidth/2) - 1) + "," + ImageHeight;
		document.getElementById("areaNext").coords =  Math.round(ImageWidth/2) + "," + "0," + ImageWidth + "," + ImageHeight;
    });
	
}

function resetPageNum() {
	pageNum = 0;
}

function show_albums() {
	$('#trAlbum').hide();
	$('#trAlbums').show();
	$('#tdAlbum_Name').hide();	
}

function get_albums(id,title) { 
	$('#tdSearch').hide();
	searchString = '';
	$('#Search').value = 'Search';	
	$('#tdAlbum_Name').hide();
	$('#trAlbum').hide();
	albumID = '';
	pageNum = 0;
	
	$('#tdCollections').fadeTo('fast',.3);	 
	
	collectionTitle = title;
	collectionID = id;
	document.title = "RedbirdCentral.com - Photos - " + collectionTitle;
	$('#tdCollection_Name').show();	
	$('#tdPhotos').show();	
	$('#Collection_Name').html('<a href="javascript:show_albums()">' + title + '<a/>');
	
	$.ajax({
		url: 'code_photos-get_albums.php',	
		data: 'C_ID='+id,
		success: function(data) {	
			$('#tdAlbums').html(data); 
			$('#trCollections').hide();
			$('#tdCollections').fadeTo('fast',1.0);	
			$('#trAlbums').show();
		}
	});		
}
	

function get_photos(id,title,cid) { 	
	if(title != undefined && title != '') {
		$('#tdAlbum_Name').show();	
		$('#Album_Name').html(title);
		albumTitle = title;	
		collectionID = '';
	}
	
	$('#tdAlbums').fadeTo('fast',.3);	 
	$('#tdAlbum').fadeTo('fast',.3);
	
	document.title = "RedbirdCentral.com - Photos - " + collectionTitle + " - " + albumTitle;		
	
	albumID = id;
	if(id == undefined || id == '') {
		albumID = '%';
	}
	
	if($("#Search").val() != 'Search') {
		searchString = $("#Search").val();	
	}	
	
	$.ajax({
		url: 'code_photos-get_photos.php',	
		data: "Search="+searchString+"&A_ID="+albumID+"&C_ID="+cid+"&pageNum_Images="+pageNum+"&maxPics="+maxPics,
		success: function(data) {
			$('#trAlbum').show();
			$('#trCollections').hide();
			$('#trAlbums').hide();
			$('#tdAlbum').fadeTo('fast',1.0);
			$('#tdAlbums').fadeTo('fast',1.0);
			$('#tdAlbum').html(data);
			$('#Pages').val(pageNum);
			
			if($('#spnAlbumName').length) {
				$('#tdAlbum_Name').show();	
				$('#Album_Name').html($('#spnAlbumName').html());
				albumTitle = title;	
				collectionID = '';
				
				$('#tdPhotos').show();
				$('#tdCollection_Name').show();	
				$('#Collection_Name').html('<a href="javascript:get_albums(' + $('#spnCollectionID').html() + ',' + "'" + $('#spnCollectionName').html() + "'" +  ')">' + $('#spnCollectionName').html() + '<a/>');
				document.title = "RedbirdCentral.com - Photos - " + $('#spnCollectionName').html() + " - " + $('#spnAlbumName').html();	
			}
			
		}
	});		 
}


