
function _get_method(o_,fct_) {
	var arg = ( _get_method.arguments.length > 2 ) ? _get_method.arguments[2] : null;
	return (function() { o_[fct_]( arg ) });
}

function jump_to( url ) {
	window.location.href = url;
}

var $Cookie = {
	set: function( name, value, o ) {
		var expires = ( typeof o.expires != "undefined" ) ? o.expires : null;
		var path = ( typeof o.path != "undefined" ) ? o.path : "/";
		var domain = ( typeof o.domain != "undefined" ) ? o.domain : window.location.hostname;
		var secure = ( typeof o.secure != "undefined" ) ? o.secure : false;
		document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
	}, 
	get: function (name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) {
				return this._get_val(j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) {
				break;
			}
		}
		return 0;
	}, 
	_get_val: function (offset) {
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
		endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	}
};
var $Video = {
	download: function( h ) {
		window.open( "/download.php?id=" + h, "dl_" + h, "menubar=no,status=no,scrollbars=no,menubar=no,width=400,height=400" );
	}
};

Element.addMethods( { 
	setPosition: function( element, _x, _y ) {
		if ( _x == 'left' ) {
			_x = document.viewport.getScrollOffsets().left;
		} else if ( _x == 'right' ) {
			_x = document.viewport.getWidth() - element.getWidth() + document.viewport.getScrollOffsets().left - 25;
		} else if ( _x == 'center' ) {
			_x = (document.viewport.getWidth() - element.getWidth()) / 2 + document.viewport.getScrollOffsets().left;
		} else {
			_x = document.viewport.getScrollOffsets().left + parseInt( _x );
		}
		if ( _y == 'top' ) {
			_y = document.viewport.getScrollOffsets().top;
		} else if ( _y == 'bottom' ) {
			_y = document.viewport.getHeight() - element.getHeight() + document.viewport.getScrollOffsets().top;
		} else if ( _y == 'center' ) {
			_y = (( document.viewport.getHeight() - element.getHeight() ) / 2 ) + document.viewport.getScrollOffsets().top;
		} else {
			_y = document.viewport.getScrollOffsets().top + parseInt( _y );
		}
		element.setStyle( {
			top: _y + 'px', 
			left: _x + 'px'
		} );
	}, 
	replaceClassName: function( element, class_remove, class_add ) {
		element.removeClassName( class_remove );
		element.addClassName( class_add );
	}
} );

var fade_background = Class.create({
	initialize: function( name, opacity, color ) {
		this.name = name;
		this.opacity = opacity;
		this.color = color;
		this.create( );
	}, 
	create: function() {
		if ( ! $( this.name ) ) {
			var f = document.createElement('div');
			f.id = this.name;
			f.style.position = 'absolute';
			f.style.backgroundColor = this.color;
			f.style.zIndex = 200;
			document.body.insertBefore( f, document.body.firstChild ) ;
			this.size();
			this.hide();
			$( this.name ).setOpacity( this.opacity );
			this.place();
			this.bplace = this.place.bindAsEventListener( this );
			Event.observe( window, 'scroll', this.bplace );
			Event.observe( window, 'resize', this.bplace );
		}
	}, 
	size: function() {
		$( this.name ).setStyle( {
			width: document.viewport.getDimensions().width + 'px', 
			height: document.viewport.getDimensions().height + 'px'
		} );
	}, 
	show: function() {
		$( this.name ).show();
	},
	hide: function() {
		$( this.name ).hide();
	}, 
	place: function() {
		this.size();
		$( this.name ).setPosition( 'center', 'center' );
	}, 
	remove: function() {
		Event.stopObserving( window, 'scroll', this.bplace );
		Event.stopObserving( window, 'resize', this.bplace );
		$( this.name ).remove();
	}
});

var $Dhtml = {
	show: function( name ) {
		this.create( name );
		this.place( name );
		this.background.show();
		
		if ( !Prototype.Browser.IE ) {
			document.body.style.overflow = 'hidden';
		}
	},
	hide: function ( name ) {
		Event.stopObserving( window, 'scroll', this.bplace );
		Event.stopObserving( window, 'resize', this.bplace );
		this.background.remove();
		this.remove( name );
		if ( !Prototype.Browser.IE ) {
			document.body.style.overflow = 'auto';
		}
	}, 
	create: function( name ) {
		if ( $( name ) ) {
			this.background = new fade_background( name + "_bg", 0.7, "#000" );
			$( name ).setStyle( { 
				position: 'absolute', 
				zIndex: 201,
				display: 'block'
			} );
			this.bplace = this.place.bindAsEventListener( this );
			Event.observe( window, 'scroll', this.bplace );
			Event.observe( window, 'resize', this.bplace );
		}
	}, 
	place: function( name ) {
		$( name ).setPosition( 'center', 'center' );
	}, 
	remove: function( name ) {
		$( name ).setStyle( { display: 'none' } );
	}
};


var $Favourites = {
	initialized: false, 
	initialize: function() {
		if ( this.initialized == false ) {
			var	cookies = new Array( 'video', 'photo', 'story', 'bonus' );
			this.content = {};
			for ( var i = 0; i < cookies.length; i ++ ) {
				this.content[ cookies[ i ] ] = new Array();
				var fromCookie = new String( $Cookie.get( 'fav-' + cookies[ i ] ) );
				if ( fromCookie.length > 0 ) {
					this.content[ cookies[ i ] ] = fromCookie.split( '/' );
				}
			}
			this.initialized = true;
		}
	}, 
	update: function( type ) {
		this.content[ type ] = this.content[ type ].without( 0 );
		var date_exp = new Date();
		date_exp.setTime( date_exp.getTime() + ( 24 * 3600 * 1000 ) );
		$Cookie.set( 'fav-' + type, this.content[ type ].join( '/' ), { expires: date_exp } );
	}, 
	clean_id: function( id ) {
		var cid = new String( id );
		if ( cid.lastIndexOf( '_' ) > -1 ) {
			cid = cid.substring( cid.lastIndexOf( '_' ) + 1 );
		}
		return cid;
	}, 
	add: function( type, id ) {
		this.initialize();
		id = this.clean_id( id );
		this.content[ type ][ this.content[ type ].length ] = id;
		this.content[ type ] = this.content[ type ].uniq();
		this.update( type );
		
		new Ajax.Request( "/favourite/add/" + type + "/" + id + "/", {
			method: 'get'
		} );
	}, 
	get: function( type ) {
		return this.content[ type ];
	}, 
	encode: function( type ) {
		this.initialize();
		var items = this.content[ type ].without( 0 );
		return { "items": items };
	}, 
	find: function( type, id ) {
		this.initialize();
		id = this.clean_id( id );
		var res = this.content[ type ].find( function( s ) {
			return s == id;
		} );
		return ( typeof res != "undefined" );
	}, 
	remove: function( type, id ) {
		this.initialize();
		id = this.clean_id( id );
		this.content[ type ] = this.content[ type ].without( id, 0 );
		this.update( type );
		new Ajax.Request( "/favourite/del/" + type + "/" + id + "/", {
			method: 'get'
		} );
	}, 
	
	hide_target: function( id ) {
		id = '_' + this.clean_id( id );
		if ( $( id ) ) {
			$( id ).hide();
		}
	}
};

var $StatusFavourites = {
	initialize: function( type, element ) {
		this.show( type, element );
		element.stopObserving();
		element.observe( "click", this.update.bindAsEventListener( this, type, element ) );
	}, 
	update: function( event ) {
		element = event.element();
		type = $A(arguments)[1];
		if ( $Favourites.find( type, element.id ) == true ) {
			$Favourites.remove( type, element.id );
			element.replaceClassName( "rem-favourite", "add-favourite" );
		} else {
			$Favourites.add( type, element.id );
			element.replaceClassName( "add-favourite", "rem-favourite" );
		}
	}, 
	show: function( type, element ) {
		if ( $Favourites.find( type, element.id ) == true) {
			element.addClassName( "rem-favourite" );
		} else {
			element.addClassName( "add-favourite" );
		}
	}
};


var gallery = Class.create( {
	initialize: function( hash ) {
		this.hash = hash;
		this.name = "photo_detail";
		this.close_button = "close-gallery";
		this.margin = 20;
		this.background = new fade_background( "photo_detail_bg", 0.9, "#000" );
		this.create();
		this.show();
	}, 
	create: function() {
		
		var d_photo_detail = document.createElement('iframe');
		d_photo_detail.setAttribute( "id", this.name );
		document.body.insertBefore( d_photo_detail, document.body.firstChild ) ;
		
		$( this.name ).setStyle( {
			position: 'absolute', 
			zIndex: 201, 
			width: ( document.viewport.getDimensions().width - this.margin * 2 ) + 'px', 
			height: ( document.viewport.getDimensions().height - this.margin * 2 ) + 'px'
		} );
		$( this.name ).allowTransparency = true;
		$( this.name ).frameBorder = 0;
		$( this.name ).frameBorder = 'no';
		$( this.name ).scrolling = 'no';
		$( this.name ).src = '/photo-viewer/' + this.hash + '/';
		
		var d_photo_close = document.createElement('div');
		d_photo_close.setAttribute( "id", this.close_button );
		document.body.insertBefore( d_photo_close, document.body.firstChild ) ;
		$( this.close_button ).setStyle( {
			position: 'absolute', 
			zIndex: 202
		} );
		$( this.close_button ).update( 'Fermer' );
		
		this.bplace = this.place.bindAsEventListener( this );
		this.bhide = this.hide.bindAsEventListener( this );
		
		$( this.close_button ).observe( 'click', this.bhide );
		
		Event.observe( window, 'scroll', this.bplace );
		Event.observe( window, 'resize', this.bplace );
	}, 
	show: function() {
		this.place();
		this.background.show();
		$( this.name ).show();
		if ( !Prototype.Browser.IE ) {
			document.body.style.overflow = 'hidden';
		}
	}, 
	hide: function() {
		Event.stopObserving( window, 'scroll', this.bplace );
		Event.stopObserving( window, 'resize', this.bplace );
		$( this.close_button ).stopObserving( 'click', this.bhide );
		this.background.remove();
		this.remove();
	}, 
	place: function() {
		$( this.name ).style.width = ( document.viewport.getDimensions().width - this.margin * 2 ) + 'px';
		$( this.name ).style.height = ( document.viewport.getDimensions().height - this.margin * 2 ) + 'px';
		$( this.name ).setPosition( 'center', 'center' );
		$( this.close_button ).setPosition(  'right', 'top' );
	}, 
	remove: function() {
		$( this.name ).src = '/blank.html';
		$( this.name ).remove();
		$( this.close_button ).remove();
		if ( !Prototype.Browser.IE ) {
			document.body.style.overflow = 'auto';
		}
	}
} );
