$( document ).ready( function() {

	var mainmenu = new ProuserMenu();
	mainmenu.init();

	var sidemenu = new ProuserSidemenu();
	sidemenu.init();

	var gallery = new ProuserGallery();
	gallery.init();

} );

function ProuserMenu() {

	this.init = function() {
		var that = this;
		$( '#menu a' ).bind( 'mouseenter', function() { that.hover( this ); } );
		$( '#menu a' ).bind( 'mouseleave', function() { that.unhover( this ); } );
		$( '#menu a img' ).each( function() { that.preload( this ) } );
	}

	this.preload = function( oElement ) {
		$( '<img>' ).attr( 'src', this.makeHoverSrc( oElement ) );
	}

	this.hover = function( oElement ) {
		var oImageElement = this.getImageElement( oElement );
		$( oImageElement ).attr( 'src', this.makeHoverSrc( oImageElement ) );		
	}

	this.unhover = function( oElement ) {
		var oImageElement = this.getImageElement( oElement );
		$( oImageElement ).attr( 'src', this.makeUnhoverSrc( oImageElement ) );
	}

	this.makeHoverSrc = function( oElement ) {
		var sSrc = this.getSrcString( oElement );
		if ( sSrc.substring( sSrc.length-9, sSrc.length ) == 'hover.jpg' ) {
			sSrc;
		} else {
			return sSrc.substring( 0, sSrc.length-4 ) + '-hover.jpg';
		}
	}

	this.makeUnhoverSrc = function( oElement ) {
		var sSrc = this.getSrcString( oElement );
		if ( sSrc.substring( sSrc.length-9, sSrc.length ) == 'hover.jpg' ) {
			return sSrc.substring( 0, sSrc.length-10 ) + '.jpg';
		} else {
			return sSrc;
		}
	}

	this.getImageElement = function( oElement ) {
		return $( oElement ).children().get(0);
	}

	this.getSrcString = function( oImageElement ) {
		return $( oImageElement ).attr( 'src' );
	}

}

function ProuserSidemenu() {

	this.currentLevel = null;
	this.maxLevel = null;

	this.sidemenuitems = null;

	this.init = function() {
		var that = this;
		this.sidemenuitems = $( '.sidemenuitem' );
		this.currentLevel = this.sidemenuitems.length;
		this.maxLevel = this.sidemenuitems.length;
		this.sidemenuitems.hide();
		$( '.sidemenuitem:last' ).show();
		this.hideNextButton();
		$( '#backnav .back').bind( 'click', function() {
			that.back( this );
			return false;
		} );
		$( '#nextnav .next').bind( 'click', function() {
			that.next( this );
			return false;
		} );
		$( '#categories .rootlink' ).bind( 'click', function() {
			that.currentLevel = 2;
			that.back();
		});
	}

	this.back = function() {
		var that = this;
		var i = 1;
		if ( this.currentLevel > 1 ) {
			this.currentLevel -= 1;
			this.sidemenuitems.hide();
			this.sidemenuitems.each( function() {
				if ( i == that.currentLevel ) {
					$( this ).show();
				}
				i++;
			} );
			this.changeBackButton();
			if ( this.currentLevel == 1 ) {
				this.hideBackButton();
			}
			this.showNextButton();
		}
	}

	this.next = function() {
		var that = this;
		var i = 1;
		if ( this.currentLevel < this.maxLevel ) {
			this.currentLevel += 1;
			this.sidemenuitems.hide();
			this.sidemenuitems.each( function() {
				if ( i == that.currentLevel ) {
					$( this ).show();
				}
				i++;
			} );
			if ( this.currentLevel == this.maxLevel ) {
				this.hideNextButton();
			}
			this.showBackButton();
		}
	}

	this.changeBackButton = function() {
		var sName = "";
		if ( typeof this.sidemenuitems[this.currentLevel-3] == 'undefined' ) {
			sName = sBacknavDefault;
		} else {
			sName = $( this.sidemenuitems[this.currentLevel-3] ).find('a.active').html();
		}
		$( '#backnav a.text' ).html(sName);
	}

	this.hideBackButton = function() {
		$( '#backnav' ).hide();
	}

	this.showBackButton = function() {
		$( '#backnav' ).show();
	}

	this.hideNextButton = function() {
		$( '#nextnav' ).hide();
	}

	this.showNextButton = function() {
		//$( '#nextnav' ).show();
	}

}

function ProuserGallery() {

	this.modalWindow = modalWindow;

	this.init = function() {
		var that = this;
		$( '.gallery a' ).bind( 'click', function() {
			that.view( this );
		} );
		// Preload close button for IE6
		$( '<img>' ).attr( 'src', '/fileadmin/templates/images/modalwindow-close.gif' );
	}

	this.view = function( oElement ) {
		this.modalWindow.windowId = 'myModal';
		this.modalWindow.width = 480;
		this.modalWindow.height = 445;
		this.modalWindow.content = $( oElement ).next().html();
		this.modalWindow.open();
	};

}

var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	open:function()
	{
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		$(".modal-overlay").click(function(){modalWindow.close();});
	}
};

