/** links fixer **/
function parseUrl(url)
{
	return {
		l : url.split( "/" ),
		head : function(s) {
			var res = "";
			for( var j = 3; j < this.l.length; j ++ ) {
				if( this.l[ j ] == s ) continue;
				res += "/" + this.l[ j ];
			}
			return res;
		},
		noExt : function() {
			var res = "";
			for( var j = 3; j < this.l.length; j ++ ) {
				res += "/" + this.l[j];
			}
			var dot = res.lastIndexOf( "." );
			if( dot != -1 ) {
				var b = res.substring( 0, dot );
				res = b;
			}
			return res;
		}
	};
}

function translateEvent(e)
{
	if (!e) var e = window.event;
	var hr;
	var targ;
	
	if( e.currentTarget )
		targ = e.currentTarget;
	else if( e.srcElement ) {
		targ = e.srcElement;
		
		while( targ.getAttribute( "medialink" ) != "yes" ) {
			targ = targ.parentNode;
		}
	}
		
	if( !targ ) return null;
	
	if( (targ.nodeType == 3) || (targ.nodeType == 4) )
		targ = targ.parentNode;
	
	if( targ.href !== undefined )
		hr = targ.href;
	else if( targ.getAttribute )
		hr = targ.getAttribute( "href" );
	
	return hr;
}

function markEvent(e)
{
	if( e.preventDefault !== undefined )
		e.preventDefault();
	else if( e.returnValue !== undefined )
		e.returnValue = false;
}

var links = document.getElementsByTagName( "A" );
var pvideo = /\/video\/*/;
var pimage = /\/images\/*/;
var pgallery = /\/[0-9]*,gallery.html/;
var sizeGrades = new Array(
	{ a : 500, b : 400 },
	{ a : 600, b : 500 },
	{ a : 700, b : 600 },
	{ a : 800, b : 700 },
	{ a : 900, b : 800 },
	{ a : 1100, b : 1000 },
	{ a : 0xffffff, b : 1100 }
);
var galleryView = new GalleryViewer( "galleryView", "fog", "gv", { width: 162, height: 162, innerWidth: 130, innerHeight: 130, horzBorderWidth: 5, vertBorderWidth: 5 }, sizeGrades );
var selfLink = "";
var selfPre = "";

if( document.location.pathname ) {
	var dot = document.location.pathname.lastIndexOf( "." );
	if( dot > 1 ) {
		selfLink = new String( document.location.pathname ).substring( 0, dot );
	} else {
		selfLink = new String( document.location.pathname );
	}
}

if( document.location ) {
	selfPre = document.location.protocol + "//" + document.location.hostname;
}

if( selfLink != "" ) {
	for( var j = 0; j < links.length; j ++ ) {
		var h;
		if( links[j].href !== undefined ) {
			h = links[j].href;
		} else if( links[j].getAttribute ) {
			h = links[j].getAttribute( "href" );
		}
		
		var check = new String(h);
		
		if( check.length > selfPre.length ) {
			if( check.substring( 0, selfPre.length ) != selfPre )
				continue;
		}
	
		if( pvideo.test( h ) ) {
			links[j].setAttribute( "medialink", "yes" );
			setEventHandler( links[j], "click", function(e) {
				var hr = translateEvent( e );
				if( hr === false ) return true;
				onVideoOpenSimple( parseUrl( hr ).head( "view.html" ) );				
				markEvent( e );
				return false;
			} );
		} else if( pimage.test( h ) ) {
			links[j].setAttribute( "medialink", "yes" );
			setEventHandler( links[j], "click", function(e) {
				var hr = translateEvent( e );
				if( hr === false ) return true;
				if( galleryView.showPicture( selfLink + "(foto:%w:%h:" + parseUrl( hr ).head( "_full.jpg" ) + ").jo" ) ) return true;
				markEvent( e );	
				return false;
			} );
		} else if( pgallery.test( h ) ) {
			links[j].setAttribute( "medialink", "yes" );
			setEventHandler( links[j], "click", function(e) {
				var hr = translateEvent( e );
				var galleryLink = parseUrl( hr ).noExt();
				if( galleryView.showGallery( galleryLink + "(gallery).jo", galleryLink + "(gallery:%i:%w:%h).jo" ) ) return true;
				markEvent( e );	
				return false;
			} );
		}
	}
}

