// JavaScript Document
$(document).ready( function() {
	oAHoverSwapImage.init();
	oImgRoundCorners.init();
	oDivRoundCorners.init('div.RoundCornersNeg', 'round-corner-div-12-4b6581-fff');
	oDivRoundCorners.init('div.RoundCornersPos', 'round-corner-div-12-ebedef-fff');
	fixFirstAndLastChild('#divContent');
	$(document).pngFix();
	$("a[rel^='prettyPhoto']").prettyPhoto();
	new ElementPaging('.T_projects div.ProjectPage', '.T_projects div.PageButtons a');
	$('div.ProjectPages').css('height', $('div.ProjectPages').css('min-height'));
} );

// HoverImageSource
oHoverImage = {
	 sHoverSuffix: '-hvr'
	,getSrc: function(sSrc, bHvr) {
		var sHvrSuf = oHoverImage.sHoverSuffix;
		var re = new RegExp('('+sHvrSuf+')?\.(gif|jpg|png)$');
		sSrc = sSrc.replace(re, bHvr ? sHvrSuf+'.$2' : '.$2');
		return sSrc;
	}
	,preload: function(sSrc) {
		jQuery('<img>').attr('src', oHoverImage.getSrc(sSrc, true));
	}
}
// HoverSwapImage class
oAHoverSwapImage = {
	 init: function() {
		$('.HoverSwapImage').each(
			function() {
				$(this).hover(
					function(eventObj) {
						$(this).find('img').attr('src', oHoverImage.getSrc($(this).find('img').attr('src'), eventObj.type == 'mouseenter'));
					}
				);
				oHoverImage.preload($(this).find('img').attr('src'));
				jQuery('<img>').attr('src', oHoverImage.getSrc($(this).find('img').attr('src'), true));
			}
		);
	}
}

// Round Image Borders
oImgRoundCorners = {
	 init: function() {
		$('img.RoundCorners').each( function() {
			var sMarginTop = $(this).css('margin-top');
			var sMarginBottom = $(this).css('margin-bottom');
			if($(this).parent('a').size() > 0) var jqInner = $(this).parent('a');
			else jqInner = $(this);
			$(this).css('margin', '0');
			jqInner.wrap('<span class="RoundCorners" style="margin-top: '+sMarginTop+'; margin-bottom: '+sMarginBottom+';" ></span>');
			jqInner.parent('span').append( '<img style="position: absolute; left: 0; top: 0;" src="../img/round-corner-img-12-fff-lt.png" alt="" /><img style="position: absolute; right: 0; top: 0;" class="RoundCorner" src="../img/round-corner-img-12-fff-rt.png" alt="" /><img style="position: absolute; left: 0; bottom: 0;" class="RoundCorner" src="../img/round-corner-img-12-fff-lb.png" alt="" /><img style="position: absolute; right: 0; bottom: 0;" class="RoundCorner" src="../img/round-corner-img-12-fff-rb.png" alt="" />' );
			var iWidth = $(this).width();
			if(iWidth > 0) jqInner.parent('span').css('width', iWidth);
		} );
	}
}
oDivRoundCorners = {
	 init: function( sJQSelector, sImgCornersPrefix) {
		$(sJQSelector).each( function() {
			$(this).append( '<img style="position: absolute; left: 0; top: 0;" src="../img/'+sImgCornersPrefix+'-lt.png" alt="" /><img style="position: absolute; right: 0; top: 0;" class="RoundCorner" src="../img/'+sImgCornersPrefix+'-rt.png" alt="" /><img style="position: absolute; left: 0; bottom: 0;" class="RoundCorner" src="../img/'+sImgCornersPrefix+'-lb.png" alt="" /><img style="position: absolute; right: 0; bottom: 0;" class="RoundCorner" src="../img/'+sImgCornersPrefix+'-rb.png" alt="" />' );
		} );
	}
}
function fixFirstAndLastChild(sJqSelector) {
	$(sJqSelector + ' :first-child').addClass('First');
	$(sJqSelector + ' :last-child').addClass('Last');
}

// ElementPaging
function ElementPaging(sPagesJq, sButtonsJq) {
	this.next = function() {
		if(this.iSelected < this.jqPages.length - 1) {
			this.iSelected++;
			this.showCurrent(true);
		}
	}
	this.prev = function() {
		if(this.iSelected > 0) {
			this.iSelected--;
			this.showCurrent(true);
		}
	}
	this.select = function(iSelection) {
		iSelection = iSelection % this.jqPages.length;
		if(iSelection >= 0 && iSelection < this.jqPages.length && this.iSelected != iSelection) {
			this.iSelected = iSelection;
			this.showCurrent(true);
		}
	}
	this.showCurrent = function(bTrans) {
		var nTransTime = bTrans ? this.nTransTime : 0;
		var iSelected = this.iSelected;
		var jqPageCont = this.jqPageCont;
		var iTotalPages = this.jqPages.length;
		this.jqPages.each( function(index) {
			if(iSelected == index) $(this).delay(nTransTime + 100).fadeIn(nTransTime); 
			else $(this).fadeOut(nTransTime);
			// The extra 100ms avoid being two visible elements at the same time
			// and showing the vertical scrollbar for an moment.
		});
		this.jqButtons.each( function(index) {
			if(iSelected == index % iTotalPages) $(this).addClass('Selected');
			else $(this).removeClass('Selected');
		} );
		// Hide both prev and next
		//if(this.jqPrev.css('display') == 'none') this.jqPrev.delay(nTransTime);
		//else this.jqPrev.fadeOut(nTransTime);
		//if(this.jqNext.css('display') == 'none') this.jqNext.delay(nTransTime);
		//else this.jqNext.fadeOut(nTransTime);
		// Show if needed
		//if(this.iSelected > 0) this.jqPrev.fadeIn(nTransTime);
		//if(this.iSelected < this.jqPages.length -1) this.jqNext.fadeIn(nTransTime);
	}
	this.nTransTime = 100;
	this.iSelected = 0;
	this.jqPages = $(sPagesJq);
	//this.jqPrev = $(sPrevJq);
	//this.jqNext = $(sNextJq);
	thisObject = this;
	//this.jqPrev.click( function() { thisObject.prev(); } );
	//this.jqNext.click( function() { thisObject.next(); } );
	this.jqButtons = $(sButtonsJq);
	if(this.jqPages.size() > 1) {
		this.jqButtons.each( function(index) {
			$(this).show();
			$(this).attr('href', 'javascript:void(0)');
			$(this).click( function() { thisObject.select(index); } );
		} );
		this.showCurrent(false);
	}
}


