///////////Play with these
var timeOut=7000;
var morphSpeed=1000;
var myWidths=["576", "640", "720", "800", "912", "1024", "1152", "1280", "1440", "1600"];
var pageFactor=".95"; //percent of the page the slideshow should take up vert/hort
var imageDimensions=eval("3/4"); //dimensions of the image 4x3 = 3/4 (height/width eg 800x600)
var handAdjustHeight=.95; //in a percentage
var handAdjustWidth=1; //as a decimal percent (eg .01-1)
var autoPlay=false; //auto-rotate;
///////////Not with these
imgClass="slide-img";
var currentPhoto=0;
var totalPhotos=0;
var myPhotos;
var myPhotoDots;
var myImages;
var myInterval;
var myAlbums;
myWidths.sort(sortNumber)
var myDimensions=returnSize();
var adjustedSize=findAdjustedSize(myDimensions);
var abc;



preparePage=function(){
	//set the body size
	loadBody(adjustedSize);
	abc=new picasaUser(username);
	pAlbum(abc,aid,authkey);
	myImages=abc.photos[aid];
	processImages();
}


function loadBody(customSize){
	adjustedSize=1440;
	//redraw the body box according to the page size
	/*$("#bodydiv").animate({
		width:customSize*handAdjustWidth,
		height:eval(customSize*imageDimensions*handAdjustHeight),
		marginLeft:'-'+eval(customSize*handAdjustWidth/2)
		}, 250);*/
	//create the slideshow box images
	document.getElementById("slideshow").innerHTML='<img id="imageNext" class="photo '+imgClass+'"><img id="imageCurrent" class="photo '+imgClass+'" >';
	
	
}

function processImages(){
	//load images in album
	
	//myImages.sort(function() {return 0.5 - Math.random()})
	//creates thumbnails
	processThumbs(myImages);	
	//place the first image in the lightbox
	$("#imageCurrent").attr({ 
		
	  src: myImages[Math.floor(Math.random()*myImages.length)]['src']+ '?imgmax='+adjustedSize
  	});
  	$("#imageCurrent").fadeIn(750);
  	if(autoPlay){
		startInterval();
	}
	 
}
function processThumbs(myImages){
	var myString="";
	for(thumb in myImages){
		myString+="<a href='javascript:setP("+thumb+")' class='portfolio-thumb'><img src='"+myImages[thumb]['src']+"?imgmax=72'></a>";
	}
	//add thumbnail html to #thumbnails
	document.getElementById("thumbnails").innerHTML=myString;

}
function startInterval(){
	clearInterval(myInterval);
	myInterval=setInterval("nextP()",timeOut);
}

function nextP(){
	nextPhoto=currentPhoto+1;
	if(nextPhoto==myImages.length){
		nextPhoto=0;
	}
	loadImage();
	
}

function lastP(){
	nextPhoto=currentPhoto-1;
	if(nextPhoto<0){
		nextPhoto=myImages.length-1;
	}
	loadImage();
}

function setP(n){
	nextPhoto=n;
	loadImage();
}
function loadImage(){
	myImage=new Image();
	clearInterval(myInterval);
	myImage.onload=function(){
		if(autoPlay){
			startInterval();
		}else{
			clearInterval(myInterval);
		}
		$("#imageCurrent").show();
		$("#imageNext").attr({ 
			src: myImages[nextPhoto]['src']+ '?imgmax='+adjustedSize
		});
		$("#imageCurrent").fadeOut(1000, function(){
			$("#imageNext").attr({ 
				src: myImages[nextPhoto]['src']+ '?imgmax='+adjustedSize
			});
			$("#imageNext").show(0, function(){
				$("#imageCurrent").attr({ 
					src: myImages[nextPhoto]['src']+ '?imgmax='+adjustedSize
				});
				
			});
		});
		
		currentPhoto=nextPhoto;
		
		
	}
	myImage.src=myImages[nextPhoto]['src']+ '?imgmax='+adjustedSize;
}



function sortNumber(a,b)
{
	return b-a;
}
function returnSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  myDimensions=[myWidth,myHeight];
  return(myDimensions);
}
function findAdjustedSize(tempDims){
	for(i=0; i<myWidths.length; i++){
		tempWidth=myWidths[i];
		tempHeight=Math.floor(tempWidth*imageDimensions);
		if(tempWidth <=Math.floor(tempDims[0]*pageFactor) && tempHeight <= Math.floor(tempDims[1]*pageFactor))
		{
			return myWidths[i];
		}
	}
	return myWidths[myWidths.length-1];
}
function loadjsfile(filename, filetype) {
    if (filetype == "js") { //if filename is a external JavaScript file
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype == "css") { //if filename is an external CSS file
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined") {
        document.getElementsByTagName("head")[0].appendChild(fileref)
        return true;
    }
}
function testForObject(Id, Tag)
{
  var o = document.getElementById(Id);
  if (o)
  {
    if (Tag)
    {
      if (o.tagName.toLowerCase() == Tag.toLowerCase())
      {
        return o;
      }
    }
    else
    {
      return o;
    }
  }
  return null;
}
$(document).ready(function() {
	
	if($.browser.opera){
		imgClass="opera-img";
	}
	if(testForObject("slideshow")){preparePage();}
	
	
});
