// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};
// function of loading image
// params: (int) index of image in array, (int) length of images array

function preload(arrayOfImages, titlesArray) {
	var max = $(arrayOfImages).length,
			currImgNum = 1;
	// at least 1 image exist
	if(max>0)	{
		// create the td element
		var td = $('<td id="portfolio"></td>');
		// append to div#wrapper and put the div.clear after
		$(td).appendTo($('#wrapper')).after($('div.clear'));
		// load the first image
		LoadImage(0,max);
	}
	
	function LoadImage(index,max) {
		//log('src = ' + arrayOfImages[index]);
		// if current index is lower then max element (max-1)
		if(index<max)
			{
				//console.log("Loading "+currImgNum+" of "+max);
				currImgNum++;
				// create the LI, add loading class
				var list = $('<td id="portfolio_'+index+'" class="portfolioImg"></td>').attr('class','loading');
				if ( currImgNum % 2 == 0) {list.attr('class','clearLeft');}
				// append to td
				$('tr#portfolioTr').append(list);	
				// current LI
				var curr = $("tr#portfolioTr td#portfolio_"+index);
				// new image object
        var img = new Image();					
				// image onload
        $(img).load(function () {
            $(this).css('display','none'); // since .hide() failed in safari
            $(curr).removeClass('loading').addClass('portfolioImg').append(this);
						if (typeof titlesArray[index] !== "undefined" && titlesArray.length > 0) {
							$(curr).append("<br /><span class='imgTitle'>"+titlesArray[index]+"</span>");
						}
            $(this).fadeIn('fast',function(){
							// once the current loaded, trigger the next image
							LoadImage(index+1,max);
						});
        }).error(function () {
					// on error remove current
					$(curr).remove();
					// trigger the next image
					LoadImage(index+1,max);
        }).attr('src', arrayOfImages[index]);										   
			}
	}
} // end function preload()

function setNavWidth () {
	var width = 0, padding=7;
	$('ul.p7TBMimg li').each(function () {
		width += $(this).width();
		if($(this).hasClass('navSpacer')) { width += padding; }
	});
	//console.log(width);
	$('ul.p7TBMimg').width(width);
} // end function setNavWidth()

function setOffCameraImgHeight () {
	var maxHeight = $(window).height() - $("#header").height();
	$(".offCameraRecordImg").css({maxHeight:maxHeight})
} // end function setNavWidth()


$(function() {
	setNavWidth();
	if ($(".offCameraRecordImg").length > 0 ) {
		setOffCameraImgHeight();
	}
});
