// 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');
				// 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()

var setNavMargins = function setNavMargins () {
	var n = {	linksWidth: 0, elemsTotal:0, remainingSpace:0, margin:0, remainder:0, overage:0, numElems: 0	};
	$('#navbar li').each( function () {
		n.linksWidth += parseInt($(this).width());
		//log(parseInt($(this).width()));
	});
	n.numElems = $('#navbar li').length-1;
	n.elemsTotal = n.linksWidth;
	n.remainingSpace = $('#navbar').width() - n.elemsTotal;
	n.margin = n.remainingSpace / n.numElems;
	//n.remainder = n.margin % 1;
	$('#navbar li').css({
		marginRight: parseInt(n.margin)
	});
	
	// if the remainder is .75 or greater, set +3
	// if the remainder is between .5 and .74, set +2
	n.remainder = n.margin % 1;
	if ( n.remainder >= .75 ) {
		n.overage = 2;
	} else if (n.remainder >= .5) {
		n.overage = 1;
	}
	$('#navbar li:first').css({
		marginRight: parseInt(n.margin)+n.overage
	});
	$('#navbar li:last').prev().css({
		marginRight: parseInt(n.margin)+n.overage
	});
	$('#navbar li:last').css({
		marginRight: 0
	});
	//log(n);
} //end function setNavMargins

function cssSpriteHover (btn_id, delta_y) {
	var btn = $("#"+btn_id);
	// get current pageName & DOM object for IE and non-IE

	if (BrowserDetect.browser !== "Explorer"){ 	// Bind using standard CSS property, "background-position"
		var bgPosFull = btn.css('background-position');
		
		// For cases "0% 0%" or "0px 0px" : regex removal of "%" and "A-z" >> leaves numbers only
		var cleanBgPos = bgPosFull.replace(/[\%A-z]/g,"");
		
		var bgPosArray = cleanBgPos.split(" ");
		var orig_x = bgPosArray[0];
		var orig_y = bgPosArray[1];

		btn.unbind("hover");
		btn.hover(
			function () {
				$(this).css({backgroundPosition: orig_x+"px "+delta_y+"px"});
			},
			function () {
				$(this).css({backgroundPosition: orig_x +"px "+orig_y+"px"});
			}
		);
		
		if (typeof pageName !== "undefined") {
		}
	} else { // Bind hover using IE "background-position-x" and "-y" 
		var orig_x = btn.css("background-position-x");
		var orig_y = btn.css("background-position-y");
		var delta_y = delta_y+"px";
		//alert(btn+"\norig_yx"+orig_x+"\norig_y"+orig_y+"\ndelta_y"+delta_y); // Debug alert
		
		btn.unbind("hover");
		btn.hover(
			function () {
				//$(this).css({backgroundPosition: orig_x+"px "+delta_y+"px"});
				$(this).css({"background-position-y": delta_y});
			},
			function () {
				//$(this).css({backgroundPosition: orig_x +"px "+orig_y+"px"});
				$(this).css({"background-position-y": orig_y});
			}
		);
		// Make current page button sticky
/*
		if ( typeof pageName !== "undefined" && btn.attr("id") == currentBtn.attr("id") ) {
			currentBtn.unbind().addClass("navBtnClicked");
			currentBtn.css({"background-position-x": orig_x, "background-position-y": delta_y, "cursor" : "default"});
		}
*/
	} //end browser detect if/else
	
} // end function cssSpriteHover()

$(function() {	
	setNavMargins();
	$('#leftArrow, #rightArrow').click( function () {
		window.location = $(this).find('.linkedRecord').val();
	});
	if ( $('#leftArrow').length > 0) {
		cssSpriteHover ('leftArrow', -602);
		cssSpriteHover ('rightArrow', -602);
	}
}); //end jquery function

